Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 519
Olga Dalton Avatar asked Aug 26 '12 01:08

Olga Dalton


1 Answers

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.

like image 115
Tommy Avatar answered Nov 06 '22 09:11

Tommy