Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nil NSDate when trying to get date from UTC string in zulu time

Tags:

Writing an iPhone app in Objective-C, I have a date in string form (in UTC format, with a Z on the end to denote zero UTC offset, or zulu time), which I need to parse into an NSDate object.

A bit of code:

NSDateFormatter* df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"]; NSString* str = @"2009-08-11T06:00:00.000Z"; NSDate* date = [df dateFromString:str];
Running this through the debugger, date ends up nil! I'm assuming it has something to do with my date format string.

How can I fix it to correctly parse the date string?

A thought would be to make the Z in the date format literal, a la setting the date format to yyyy-MM-dd'T'HH:mm:ss.SSS'Z'.

That would work, except when the Z is parsed as a literal, the date loses offset information, and so is ambiguous, and therefore interpreted to be local time.

For example, if the string to parse was 2009-08-11T06:00:00.000Z (6:00 zulu time) it would be interpreted as 6:00 local time, and an incorrect offset would then be applied. It would then be parsed as 2009-08-11T06:00:00.000-600 (12:00 zulu time) with the offset depending on the user's offset.

Thanks!
like image 406
Eric Freese Avatar asked Aug 11 '09 23:08

Eric Freese


People also ask

How do I get current timeZone in Objective C?

timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; //Create a date string in the local timezone dateFormatter. timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone]. secondsFromGMT]; NSString *localDateString = [dateFormatter stringFromDate:[NSDate date]]; NSLog(@"date = %@", localDateString);


2 Answers

I've had this problem also, I'm not sure if it's a API bug within Apple's code, or my lack of understanding, but I've worked around it by using hour offsets in my date strings.

If you change the code in your example to:

NSDateFormatter* df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"]; NSString* str = @"2009-08-11T06:00:00.000-0700";   // NOTE -0700 is the only change NSDate* date = [df dateFromString:str]; 

It will now parse the date string. Of course the -0700 hours is my offset, you'd have to change it to yours. Hope this helps.

like image 138
NWCoder Avatar answered Oct 10 '22 05:10

NWCoder


Most answers suggest you to treat 'Z' as a literal character. Do not do this!

The Z actually means that the date is offset by 0 to UTC (+0000).

This is according to the time zone format ISO8601:

ISO 8601 time zone format: A constant, specific offset from UTC, which always has the same format except UTC itself ("Z").

"-08:00"

"Z"

What you want to do is use the following format for your NSDateFormatter:

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];                                   // 2013-11-18T23:00:00.324Z                                   [formatter setTimeZone:[NSTimeZone localTimeZone]];                                   [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];                                   return formatter; 

By repeating the Z five times, you tell the formatter to use ISO8601 when parsing the string.

Bonus:

  • Use one to three Zs for RFC 822 GMT format.
  • Use four Zs for localized GMT format.

For more information check this document.

like image 38
Pedro Mancheno Avatar answered Oct 10 '22 04:10

Pedro Mancheno