确定东海岸目前是否通过iPhone SDK使用EST或EDT

时间:2010-07-01 00:06:26

标签: iphone cocoa

如何使用iPhone SDK确定东海岸目前是否正在使用EST或EDT?

我知道NSTimeZone类,我尝试了以下但是它不起作用,结果是“东海岸不在DT上”,它目前是。这让我相信isDayLightSavingTime只是检查它是否传递了ST或DT值而不是确定EST当前是否应该是EDT

NSTimeZone *easternTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
if ([easternTimeZone isDaylightSavingTime]) {
    NSLog(@"East coast is NOT on DT");
} else {
    NSLog(@"East coast is on DT");      
}

更新:

最终目标是我需要能够计算东部时区的正确当前时间,同时考虑他们目前是否正在观察夏令时。

更新2:

有趣的结果,当我改变时

NSTimeZone *easternTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];

NSTimeZone *easternTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EDT"];

它仍然返回“东海岸不在DT”

2 个答案:

答案 0 :(得分:9)

改为使用US/Eastern

NSTimeZone *easternTimeZone = [NSTimeZone timeZoneWithName:@"US/Eastern"];
if ([easternTimeZone isDaylightSavingTime]) {
    NSLog(@"East coast is NOT on DT");
} else {
    NSLog(@"East coast is on DT");      
}

请注意,您需要使用-[NSTimeZone timeZoneWithName:],而不是-[NSTimeZone timeZoneWithAbbreviation:]

答案 1 :(得分:2)

您确定这不仅仅是您日志记录中的错误吗?

您正在检查[easternTimeZone isDaylightSavingTime],如果确实是您正在记录@"East coast is NOT on DT"。似乎逻辑是正确的,因为这是你得到的结果,你只是以错误的方式记录它......