在 iOS 中,NSTimeZone 中的夏令时是否被破坏?
我有两个变量:
NSTimeZone *timeZoneMinus5 = [NSTimeZone timeZoneForSecondsFromGMT: -18000];
NSTimeZone *timeZoneEST = [NSTimeZone timeZoneWithName:@"EST"];
在设备和模拟器上测试,并且都返回 GMT-05:00
。
当我写这篇文章时,时间是凌晨 2:55 (GMT)
。我位于 EST 时区,因此我的时间是 10:55pm (GMT-05:00)
,2011 年 4 月 15 日实行夏令时。
但是,[timeZoneEST isDaylightSavingTime]
返回 NO
且 [timeZoneEST daylightSavingTimeOffset]
返回 0
。
如果我使用 timeZoneEST
获取当前时间作为字符串:
NSDate *gmtTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"h:mm a (z)"];
[dateFormatter setTimeZone: timeZoneEST];
NSString *timeStr = [dateFormatter stringFromDate: gmtTime];
模拟器和设备都会给我 9:55 PM (GMT-05:00)
。
根据我的理解,我应该得到10:55 PM (GMT-05:00)
。是我做错了还是我做错了?
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
.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你很困惑。首先,美国东部时区的夏令时是-4,而不是-5。 9:55 PM 当然比凌晨 2:55 早 5 个小时;晚上 10:55 比 4 小时早。
“EST”很可能为您提供准确的信息:东部标准时间,而不考虑夏令时。 “EST5EDT”可能会为您提供所需的时区,尽管(假设设备像模拟器一样使用区域信息)“America/New_York”将是首选。
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.
您将
NSTimeZone
定义为 EST,因此[timeZoneEST isDaylightSavingTime]
返回NO
是有意义的。而是使用[[NSTimeZone localTimeZone] daylightSavingTimeOffset];
获取当地时间的夏令时偏移。它应该返回3600
秒。You are defining your
NSTimeZone
to be EST, so it makes sense that[timeZoneEST isDaylightSavingTime]
returnsNO
. Instead use[[NSTimeZone localTimeZone] daylightSavingTimeOffset];
to get daylight saving time offset for the local time. It should return3600
seconds.