简体   繁体   English

在iOS中NSTimeZone的夏令时破碎了吗?

[英]In iOS is daylight saving in NSTimeZone broken?

I have two variables: 我有两个变量:

NSTimeZone *timeZoneMinus5 = [NSTimeZone timeZoneForSecondsFromGMT: -18000];
NSTimeZone *timeZoneEST = [NSTimeZone timeZoneWithName:@"EST"];

Tested on a device and on the simulator as well both return GMT-05:00 . 在设备和模拟器上测试,两者都返回GMT-05:00

As I'm writing this the time is 2:55 am (GMT) . 正如我写的那样,时间是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. 我在EST时区所以我的时间是10:55pm (GMT-05:00) ,观察2011年4月15日的夏令时。

However, [timeZoneEST isDaylightSavingTime] returns NO and [timeZoneEST daylightSavingTimeOffset] returns 0 . 但是, [timeZoneEST isDaylightSavingTime]返回NO[timeZoneEST daylightSavingTimeOffset]返回0

If I use timeZoneEST to get the current time as a string: 如果我使用timeZoneEST将当前时间作为字符串获取:

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) . 模拟器和设备都给了我9:55 PM (GMT-05:00)

In my understanding I should be getting 10:55 PM (GMT-05:00) . 根据我的理解,我应该在10:55 PM (GMT-05:00) Am I wrong or am I doing it wrong? 我错了还是我做错了?

You are defining your NSTimeZone to be EST, so it makes sense that [timeZoneEST isDaylightSavingTime] returns NO . 您将NSTimeZone定义为EST,因此[timeZoneEST isDaylightSavingTime]返回NO是有道理的。 Instead use [[NSTimeZone localTimeZone] daylightSavingTimeOffset]; 而是使用[[NSTimeZone localTimeZone] daylightSavingTimeOffset]; to get daylight saving time offset for the local time. 获得当地时间的夏令时偏移量。 It should return 3600 seconds. 它应该返回3600秒。

You are very confused. 你很困惑。 To start with, daylight savings time in the US Eastern time zone is -4, not -5. 首先,美国东部时区的夏令时是-4,而不是-5。 9:55 PM certainly is 5 hours before 2:55 am; 晚上9:55肯定是凌晨2:55前5小时; 10:55 PM would be 4 hours before. 下午10:55将是4个小时前。

Chances are that "EST" is giving you exactly that: Eastern Standard Time without regard for daylight savings time. 有可能“EST”正好给你:东部标准时间而不考虑夏令时。 "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. “EST5EDT”可能会为您提供您期望的时区,但(假设设备使用zoneinfo作为模拟器),“America / New_York”将是首选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM