views:

4719

answers:

4

I have a time in EST timezone, it is done using the NOW() function on the mysql server. Because my server is located in EST, the time stored is in EST. When I retrieve it from my app on the iPhone, I need to display it in the correct time zone of the user. How do I do that?

A: 

Use +[NSDate dateWithString:date] and pass a string in the following format YYYY-MM-DD HH:MM:SS ±HHMM to get a date object. You can then perform operations on the date, or format it for output using NSDateFormatter

rpetrich
+[NSDate dateWithString:date] doesn't seem to be available in the iPhone SDK.
Daniel Rinser
+14  A: 

NSDate doesn't include any time zone information. One way to convert NSDate objects from one timezone to another is:

// The date in your source timezone (eg. EST)
NSDate* sourceDate = [NSDate date];

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];

To get a list of available timezones on your system, use [NSTimeZone knownTimeZoneNames] or [NSTimeZone abbreviationDictionary].

I don't know in which format your MySQL date comes in, but assuming it is a string, you should use NSDateFormatter to parse the string and convert it to an NSDate obejct. For example:

NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate* sourceDate = [formatter dateFromString:@"2009-07-04 10:23:23"];

You have to set the formatter's date format according to the format of your mysql date string. If you need a reference for the format string syntax, see Unicode Date Format Patterns.

Hope this helps.

Daniel Rinser
of note: according to apple's docs, systemTimeZone is cached so you may want to clear it before accessing it. to do this, use [NSTimeZone resetSystemTimeZone].
taber
A: 

When I set my source timezone to "UTC" and destination to "EST", how come it subtracts the time and doesn't add. For example 5:00 UTC should translate to 9:00 EST. Can someone clarify?

+1  A: 

I think it depends on what you mean by EST - if you mean East Coast US, then in general, that is 5 hours behind UTC (but not accounting for daylight saving), which should give you 04:00 EST. Try to avoid using abbreviations where possible, as they are ambiguous, e.g. EST is the abbreviation for both America/Detroit and Australia/Sydney. Using NSTimeZone initWithName will give more precise results.

The Chronos Time Zone Repository provides a nicely readable XML timezone database that really helps in understanding how time zones work (it's all rather messy and changeable).

StephenT