iPhone 中的 NSTimeZone 是否会自动调整 DST
我有一个相当简单的问题。我有一组 UTC 日期。我想以针对 DST 调整的正确本地时间(或不,如果 DST 当前未生效)向用户表示这些内容。
因此,如果我执行以下操作,timeStamp 是否会正确设置为纽约当地时间,并在生效时考虑 DST
NSDate *date = initialise with a UTC time
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"America/New_York"];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *timeStamp = [formatter stringFromDate:date];
?还是我需要做其他事情?我是否检查 NSTimeZone.isDaylightSavingTime/NSTimeZone.daylightSavingTimeOffset 并相应地调整我的日期或类似的内容?
提前致谢?
特维布尔斯。
I have a fairly simple question. I have a set of dates in UTC. I want to represent these to the user in the correct local time adjusted (or not, if DST is not currently in effect) for DST.
So, if I do something such as the following, will timeStamp be set correctly to the local time in New York taking DST into account when in effect
NSDate *date = initialise with a UTC time
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"America/New_York"];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *timeStamp = [formatter stringFromDate:date];
Or so I need to do something else? Do I check NSTimeZone.isDaylightSavingTime/NSTimeZone.daylightSavingTimeOffset and adjust my date accordingly or anything like that?
Thanks in advance?
Twibbles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,如果您使用
[NSTimeZone timeZoneWithName]
创建时区,NSDateFormatter
将自动调整 DST。NSTimeZone
了解每个时区的 DST 规则,可以查看未来和过去,并对传递给stringForDate
的NSDate
执行正确的操作>。但是,如果您使用[NSTimeZone timeZoneForSecondsFromGMT]
创建NSTimeZone
,则不会进行 DST 调整。还应该注意的是,DST 规则可能会发生变化,这意味着NSTimeZone
不能保证始终对所有时区执行正确的操作。您不需要使用
NSTimeZone.isDaylightSavingTime
或NSTimeZone.daylightSavingTimeOffset
将NSDate
格式化为 DST 正确的本地时间。这两种方法都在当前时间运行。如果您想知道NSDate
是否属于时区的DST,您可以使用NSTimeZone.isDaylightSavingTimeForDate
方法,如果您想知道NSDate的DST偏移量是多少您可以使用 NSTimeZone.daylightSavingTimeOffsetForDate 方法。Yes,
NSDateFormatter
will automatically adjust for DST if you create the timeZone with[NSTimeZone timeZoneWithName]
.NSTimeZone
knows the DST rules for each time zone and can look into the future and the past and will do the right thing for theNSDate
passed tostringForDate
. However, if you create theNSTimeZone
with[NSTimeZone timeZoneForSecondsFromGMT]
DST adjustments are not made. It should also be noted that DST rules are subject to change, which meansNSTimeZone
is not guaranteed to always do the right thing for all time zones.You do not need to use
NSTimeZone.isDaylightSavingTime
orNSTimeZone.daylightSavingTimeOffset
to format anNSDate
as DST correct local time. Both of those methods operate on the current time. If you want to know if anNSDate
falls within DST in a timezone you can use theNSTimeZone.isDaylightSavingTimeForDate
method and if you want to know what the DST offset is for an NSDate you can use theNSTimeZone.daylightSavingTimeOffsetForDate
method.