5
votes

Any way to get timezone abbreviation(e.g UTC, EET) from NSDate? I'm getting NSDate from string, e.g 2012-08-26 02:54:50 +0200 and need to show timezone of this date.

Currently doing that:

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat: @"yyyy-MM-dd HH:mm:ssZZZ"];
NSDate *isoDate = [formatter dateFromString: isoTime.value];
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMTForDate: isoDate];
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT: offset];

But I'm getting GMT+03:00 for timeZone abbreviation, but it should be EET. Any way to get it?

1
This may not be possible, because the same offset may correspond to multiple time zone names. For example, COT (Colombia time) and EST (US Eastern Standard Time) are both GMT-5. - Sergey Kalinichenko
As Tommy says, you can't get what isn't there. There's no timezone data in an NSDate. - Hot Licks
(Though you can, of course, separately extract the timezone from the date string you're parsing.) - Hot Licks

1 Answers

7
votes

An NSDate is time zone independent; it doesn't inherently have a time zone. You therefore can't get a time zone from it.

secondsFromGMTForDate: returns the offset of that time zone (the default one, in your case) from GMT at the specified date. It's returning information about the NSTimeZone which may depend on the date (eg, if your time zone honours daylight savings), not about the date.