1

I am trying to pass a timestamp (yyyy-MM-dd HH:mm:ss) from mySQL (running on apache) to iOS. Currently, mysql is set to this timezone:

default-time-zone='+00:00'

When I pass this timestamp down to iOS, I use this code to convert the string to an NSDate:

-(NSDate *)formatDateWithString:(NSString *)dateString{
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
     NSDate* date = [dateFormatter dateFromString:dateString];
     NSLog(@"Formatted Timestamp: %@ - Original Timestamp: %@", date,dateString);
     return date;
}

This is the output for a mySQL timestamp of 2013-04-17 16:33:56:

Formatted Timestamp: 2013-04-17 23:33:56 +0000 - Original Timestamp: 2013-04-17 16:33:56

Why is iOS adding 7 hours? (FYI - I am located in San Francisco, Ca so I am sure it has something to do with my timezone being PDT. Just not sure why it is being converted that way when I don't specify it to be).

It is important to use the most "universal" timestamp possible in my app as I may have users all over the world and don't want to fuss with a lot of conversions. I want to be able to store the timestamps in mySQL, then just compare the differences between the stored server timestamp and the current server timestamp. I would prefer to not have to use a web request to get the "server time" everytime I need to do a comparison if possible.

UPDATE

By adding this line of code to my dateFormatter, it seems that the two times are now matching correctly:

[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

However, can anyone verify that it will always continue to match, regardless of the time of year? (i.e. Daylight Savings time, etc.)

JimmyJammed
  • 9,598
  • 19
  • 79
  • 146

1 Answers1

3

By default NSDateFormatter is set to localTimeZone. Since your dateString didn't had timezone information, it calculated the time wrt to localTimeZone. If you know that the timezone of your dateString is UTC then you need to set that to dateFormatter.

NSDate represents absolute time, the representation can vary wrt timeZones but it is a unique value and it will not be affected by daylight savings.

-(NSDate *)formatDateWithString:(NSString *)dateString{
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
     [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
     NSDate* date = [dateFormatter dateFromString:dateString];
     NSLog(@"Formatted Timestamp: %@ - Original Timestamp: %@", date,dateString);
     return date;
}
Anupdas
  • 10,211
  • 2
  • 35
  • 60
  • Yup that was the conclusion I came to shortly after posting. Can you verify that these times will always match correctly? (i.e. Won't change due to daylight savings, etc.) – JimmyJammed Apr 17 '13 at 17:00
  • @JamesHickman I'm not very sure about the daylight savings. But this have always worked for me. If you have control over the dateFormat you can include the timeZone info like +0700 or +0000 to your dateFormat for more flexibility. This post will be a good pointer http://stackoverflow.com/a/6866992/767730 – Anupdas Apr 17 '13 at 17:08
  • Ok, from what I gather it seems that UTC is not affected by any daylight savings time or other seasonal changes. Fortunately, I only need to show the difference in terms of time between any 2 dates in my app, so I don't need to display exact time for every timezone. – JimmyJammed Apr 17 '13 at 17:38
  • @JamesHickman - In an app I worked on, we used `-[NSDate dateWithTimeIntervalSince1970]` and had the API provide a number of seconds since January 1 1970, GMT. This removed any issues of needing to know which time zone the user is in, and our various clients display date/time info based on the user's current time zone or a specified default. – Aaron Brager Apr 17 '13 at 18:34
  • @AaronBrager - Yeah that is pretty much what I am doing now with the actual date / time stamp. I would have preferred to use time intervals as I thought it would be more consistent and easier to convert for timezones, but mysql doesn't have a built in way to store timestamps in a 'Timestamp' field as time intervals (at least that I know of). – JimmyJammed Apr 19 '13 at 00:44