Determine if the east coast is currently using EST or EDT via iPhone SDK
How can I determine if the east coast is currently using EST or EDT using the iPhone SDK?
I am aware of the NSTimeZone class and I attempted the following but it did not work, the result is "East coast is NOT on DT" which it currently is. This leads me to believe that isDayLightSavingTime simply checks w开发者_JAVA百科hether its being passed an ST or DT value rather than determining if EST should currently be EDT
NSTimeZone *easternTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
if ([easternTimeZone isDaylightSavingTime]) {
NSLog(@"East coast is NOT on DT");
} else {
NSLog(@"East coast is on DT");
}
UPDATE:
The end goal is I need to be able to calculate the correct current time in the eastern timezone, taking into account whether they are currently observing daylight savings time.
UPDATE 2:
Interesting result, when I change
NSTimeZone *easternTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
to
NSTimeZone *easternTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EDT"];
it still returns "East Coast is NOT on DT"
Use US/Eastern
instead.
NSTimeZone *easternTimeZone = [NSTimeZone timeZoneWithName:@"US/Eastern"];
if ([easternTimeZone isDaylightSavingTime]) {
NSLog(@"East coast is NOT on DT");
} else {
NSLog(@"East coast is on DT");
}
Note that you need to use -[NSTimeZone timeZoneWithName:]
, instead of -[NSTimeZone timeZoneWithAbbreviation:]
Are you sure this isn't just a bug in your logging?
You're checking for [easternTimeZone isDaylightSavingTime]
and if it's true you're logging @"East coast is NOT on DT"
. It appears that the logic is correct, as this is the result you're getting, you're just logging it the wrong way...
精彩评论