Olga Dalton
Olga Dalton

Reputation: 849

Get timezone name from NSDate

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?

Upvotes: 5

Views: 6738

Answers (1)

Tommy
Tommy

Reputation: 100622

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.

Upvotes: 7

Related Questions