Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 03-24-2009, 08:04 PM   #1 (permalink)
Scenario
Registered Member
 
Join Date: Jul 2008
Posts: 13
Scenario is on a distinguished road
Default SQLite and NSDate time zones

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

Last edited by Scenario; 03-24-2009 at 08:23 PM.
Scenario is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 175,631
Threads: 94,089
Posts: 402,801
Top Poster: BrianSlick (7,990)
Welcome to our newest member, hanhaursh7
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:02 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.