如何创建将处于 DST 模式的 NSDate
How to create NSDate that will be in DST mode
我想知道如何创建将处于 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
的选项。
经过调查和寻找最优雅的解决方案后,我明白可以这样做。
//This is date object and from beginning we could make prediction that is is in DST Mode.
NSDate *workDateInDST = [[NSTimeZone localTimeZone] nextDaylightSavingTimeTransition];
//Hear we could validate if it is DST or not
if (![[NSTimeZone localTimeZone] isDaylightSavingTimeForDate:workDateInDST]) {
workDateInDST = [NSDate date];
}
如果您仔细阅读文档:nextDaylightSavingTimeTransition: returns 下一次 DST 在给定日期之后更改,或者如果您处于 DST 从不更改的时区(始终打开,或始终离开)。如果您的日期是夏令时,那么您就可以了,否则您需要知道它何时更改。
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.
}
}
我想知道如何创建将处于 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
的选项。
经过调查和寻找最优雅的解决方案后,我明白可以这样做。
//This is date object and from beginning we could make prediction that is is in DST Mode.
NSDate *workDateInDST = [[NSTimeZone localTimeZone] nextDaylightSavingTimeTransition];
//Hear we could validate if it is DST or not
if (![[NSTimeZone localTimeZone] isDaylightSavingTimeForDate:workDateInDST]) {
workDateInDST = [NSDate date];
}
如果您仔细阅读文档:nextDaylightSavingTimeTransition: returns 下一次 DST 在给定日期之后更改,或者如果您处于 DST 从不更改的时区(始终打开,或始终离开)。如果您的日期是夏令时,那么您就可以了,否则您需要知道它何时更改。
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.
}
}