I'm having trouble understanding the way NSDate handles (and requires) time zones.
I have integrated a large sqlite3 database with my iPhone app. I store my dates in the standard SQL format "2009-03-24 18:44:13". SQL doesn't seem to care about time zones and that's fine with me.
When I load the textual representation of a date/time into an NSDate object, I use a line of code like this:
Code:
date = [NSDate dateWithString:[NSString stringWithFormat:@"%@ -0600", [NSString stringWithUTF8String:(char *)sqlite3_column_text(hydrate_statement, 1)]]];
This creates an NSDate object represented as "2009-03-24 18:44:13 -0600". Then when I write my dates back to SQL as text, I simply trim off the " -0600".
The obvious problem is, the "-0600" offset works for my location but probably not my user's. And worse, when I load a bunch of dates one at a time into a UIDatePicker in my app, they can be off by an hour depending on whether the system thinks Daylight Savings Time is in effect on that particular date. To explain this another way, last month I had to hard code "-0700" instead of "-0600".
I've tried using the following line of code to effectively turn off time zone support:
Code:
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
That seems to work for dates I load from my database but any dates I create via [NSDate date] or edit via UIDatePicker get screwed up by 6 hours.
Can anyone give me some advice?
Please note that I don't want to store my dates as doubles. I want them to be human-readable when browsing the database in something like the Terminal app. I just want all times to be interpreted as "the local time in the user's time zone" across all records.
Do I need to determine the time zone offset for each particular date before I create an NSDate from loaded text? If so that seems like a major pain but I suspect [NSTimeZone secondsFromGMTForDate] might be designed for this problem.
Thanks in advance,
Todd