gpt4 book ai didi

objective-c - 如何创建将处于 DST 模式的 NSDate

转载 作者:行者123 更新时间:2023-11-28 12:41:43 25 4
gpt4 key购买 nike

我想知道如何创建将处于 DST 模式的 NSDate。

我正在使用方法nextDaylightSavingTimeTransition

这是来自文档:

This property contains the date of the next (after the current instant) daylight saving time transition for the receiver. Depending on the time zone of the receiver, the value of this property may represent a change of the time zone's offset from GMT. Returns nil if the time zone of the receiver does not currently observe daylight saving time.

然后我验证它是否处于 DST 模式:

我正在使用方法:isDaylightSavingTimeForDate:

这是来自文档:

YES if the receiver uses daylight saving time at aDate, otherwise NO.

代码:

NSDate *nextDstTransition = [[NSTimeZone localTimeZone] nextDaylightSavingTimeTransition];


BOOL isDstOnForOldDate = [[NSTimeZone localTimeZone] isDaylightSavingTimeForDate:nextDstTransition];

很遗憾,我的 nextDstTransition 不在夏令时。我的假设与时区问题有关。

您能否分享有关在 DST 模式下创建 NSDate 的选项。

最佳答案

如果您仔细阅读文档:nextDaylightSavingTimeTransition:返回 DST 在给定日期之后更改的下一次时间,如果您处于 DST 从不更改(始终打开或始终关闭)的时区,则返回 nil。如果您的日期是夏令时,那么您就可以了,否则您需要知道它何时更改。

NSDate* date = ...
if (! [[NSTimeZone localTimeZone] isDaylightSavingTimeForDate:date]) {
NSDate* change = [[NSTimeZone localTimeZone] nextDaylightSavingTimeTransitionAfterDate:date];
if (change != nil) {
date = change;
} else {
// Tough, there is no DST in this time zone.
}
}

关于objective-c - 如何创建将处于 DST 模式的 NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39250516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com