I have two variables:
NSTimeZone *timeZoneMinus5 = [NSTimeZone timeZoneForSecondsFromGMT: -18000];
NSTimeZone *timeZoneEST = [NSTimeZone timeZoneWithName:@"EST"];
Te开发者_开发技巧sted on a device and on the simulator as well both return GMT-05:00
.
As I'm writing this the time is 2:55 am (GMT)
. I am in the EST timezone so my time is 10:55pm (GMT-05:00)
, observing Daylight Saving Time on April 15 2011.
However, [timeZoneEST isDaylightSavingTime]
returns NO
and [timeZoneEST daylightSavingTimeOffset]
returns 0
.
If I use timeZoneEST
to get the current time as a string:
NSDate *gmtTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"h:mm a (z)"];
[dateFormatter setTimeZone: timeZoneEST];
NSString *timeStr = [dateFormatter stringFromDate: gmtTime];
both the simulator and the device gives me 9:55 PM (GMT-05:00)
.
In my understanding I should be getting 10:55 PM (GMT-05:00)
. Am I wrong or am I doing it wrong?
You are very confused. To start with, daylight savings time in the US Eastern time zone is -4, not -5. 9:55 PM certainly is 5 hours before 2:55 am; 10:55 PM would be 4 hours before.
Chances are that "EST" is giving you exactly that: Eastern Standard Time without regard for daylight savings time. "EST5EDT" might give you the time zone you are expecting, although (assuming the device uses zoneinfo as the simulator does) "America/New_York" would be preferred.
You are defining your NSTimeZone
to be EST, so it makes sense that [timeZoneEST isDaylightSavingTime]
returns NO
. Instead use [[NSTimeZone localTimeZone] daylightSavingTimeOffset];
to get daylight saving time offset for the local time. It should return 3600
seconds.
精彩评论