夏時(shí)令(Daylight Saving Time:DST),又稱“日光節(jié)約時(shí)制”和“夏令時(shí)間”,是一種為節(jié)約能源而人為規(guī)定地方時(shí)間的制度,在這一制度實(shí)行期間所采用的統(tǒng)一時(shí)間稱為“夏令時(shí)間”。一般在天亮早的夏季人為將時(shí)間調(diào)快一小時(shí),可以使人早起早睡,減少照明量,以充分利用光照資源,從而節(jié)約照明用電。各個(gè)采納夏時(shí)制的國(guó)家具體規(guī)定不同。目前全世界有近110個(gè)國(guó)家每年要實(shí)行夏令時(shí)。
公司有個(gè)郵件項(xiàng)目,其中Exchange日歷需要大量的日期時(shí)間的計(jì)算。前兩天客戶反饋一個(gè)問題:循環(huán)日歷修改單日后,生成的exception日期未生效,也就是說修改后的會(huì)議時(shí)間沒有改動(dòng)。正常來說這是個(gè)主流程功能,不太可能出現(xiàn)這樣的問題,我們本地各種日志分析和造日歷會(huì)議想要復(fù)現(xiàn)這個(gè)問題,徒勞無功。后來在查另一個(gè)完全不相干的問題時(shí),查到了用戶時(shí)區(qū)被改動(dòng)導(dǎo)致日期錯(cuò)誤問題,靈感乍現(xiàn),連忙去試試之前用戶反饋的日歷周期在不同時(shí)區(qū)上的日歷表現(xiàn)。果然,這次看到了不同。
先看我在控制臺(tái)輸出的兩個(gè)日期:
//美國(guó)時(shí)區(qū)
(lldb) po excStartTime
2022-02-18 07:15:00 +0000
(lldb) po self.startTime
2021-10-29 06:15:00 +0000
(lldb) po [excStartTime beginningOfDay]
2022-02-18 05:00:00 +0000
(lldb) po [self.startTime beginningOfDay]
2021-10-29 04:00:00 +0000
//中國(guó)時(shí)區(qū)
(lldb) po excStartTime
2022-02-18 07:15:00 +0000
(lldb) po self.startTime
2021-10-29 06:15:00 +0000
(lldb) po [excStartTime beginningOfDay]
2022-02-17 16:00:00 +0000
(lldb) po [self.startTime beginningOfDay]
2021-10-28 16:00:00 +0000
//獲取一天的開始時(shí)間
- (NSDate *)beginningOfDay {
NSCalendar *calendar = [NSCalendar currentCalendar];
// Get the weekday component of the current date
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
fromDate:self];
return [calendar dateFromComponents:components];
}
看到這個(gè)數(shù)據(jù)2022-02-18 05:00:00 +0000 2021-10-29 04:00:00 +0000我們實(shí)在無法理解,同時(shí)區(qū)條件下,兩個(gè)日期的一天開始時(shí)間竟然不同?而且中國(guó)可以,美國(guó)不行?這不可能吧!時(shí)區(qū)固定了,開始時(shí)間還能變?我一度以為計(jì)算錯(cuò)了,換了各種方法和時(shí)區(qū),結(jié)果都是一樣的!內(nèi)心無比狂躁啊,只能搜索,為什么兩個(gè)日期計(jì)算出來會(huì)相差一個(gè)小時(shí)。驀然的,一個(gè)夏令時(shí)調(diào)整時(shí)間相差1小時(shí)的新聞出現(xiàn)在我的眼前,然后看剛才的日期2021-10-29,這正好是在夏令時(shí)期間,而我的時(shí)區(qū)是在美國(guó),是一個(gè)實(shí)行夏令時(shí)的國(guó)家。一切得到了解釋,日歷計(jì)算日開始時(shí)間自動(dòng)給我算上了夏令時(shí),而我拿一個(gè)2021-10-29有夏令時(shí)的日開始時(shí)間,去參與計(jì)算一個(gè)2022-02-18無夏令時(shí)日期,自然不可能算對(duì)。
代碼驗(yàn)證下
NSTimeInterval daylightSavingInterval = [[NSTimeZone systemTimeZone] daylightSavingTimeOffsetForDate:[calendar dateFromComponents:components]];
(lldb) po daylightSavingInterval
3600
果然是1小時(shí)。
知道了原因就好辦了。既然一天的時(shí)間開始變?cè)缌艘恍r(shí),想要消除這個(gè)誤差,那么在夏令時(shí)期間補(bǔ)上即可,再拿補(bǔ)上1小時(shí)后的時(shí)間去參與后續(xù)計(jì)算即可。可以用daylightSavingTimeOffsetForDate方法獲取到夏令時(shí)差,這個(gè)方法會(huì)根據(jù)該時(shí)區(qū)國(guó)家是否有夏令時(shí)而變動(dòng),0或者3600,因此可以直接兼容無夏令時(shí)的情況。其他方法可參考NSTimeZone類的方法。
//獲取一天的開始時(shí)間,同時(shí)消除夏令時(shí)時(shí)差
- (NSDate *)benginningOfDayWithDayLinght {
NSCalendar *calendar = [NSCalendar currentCalendar];
// Get the weekday component of the current date
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
fromDate:self];
//計(jì)算一天開始時(shí)間要補(bǔ)上美國(guó)等夏令時(shí)間差1小時(shí) (美國(guó)夏令時(shí)結(jié)束日是2021-11-07)去掉時(shí)差在計(jì)算,否則每天的開始時(shí)間不一致會(huì)導(dǎo)致exception計(jì)算錯(cuò)誤。
//如果需要支持夏令時(shí),通過isDaylightSavingTimeForDate或者daylightSavingTimeOffsetForDate判斷
NSTimeInterval daylightSavingInterval = [[NSTimeZone systemTimeZone] daylightSavingTimeOffsetForDate:[calendar dateFromComponents:components]];
NSTimeInterval sinceTime = [calendar dateFromComponents:components].timeIntervalSince1970;
NSDate *resultDate = [NSDate dateWithTimeIntervalSince1970:sinceTime+daylightSavingInterval];
return resultDate;
}
后言:夏令時(shí),雖然達(dá)到了節(jié)約能源的目的,但隨著都市化夜生活的增多,作用越來越小了。而且還增加了人心理和生理負(fù)擔(dān)以及各種場(chǎng)合系統(tǒng)的計(jì)算負(fù)擔(dān)。功與過,實(shí)難論斷。