Swift中使用时区标识符配置时GMT值为何发生变化?
问题:时区标识符对应的GMT偏移为何随日期变化?
代码示例
let dateFormatter = ISO8601DateFormatter() let zurichTimeZone = TimeZone(identifier: "Europe/Zurich") dateFormatter.timeZone = zurichTimeZone // dateFormatter.timeZone = .init(abbreviation: "GMT+1:00") // prints timezone +1 print(dateFormatter.string(from: Date(timeIntervalSince1970: TimeInterval(100400)))) // prints timezone +2 print(dateFormatter.string(from: Date(timeIntervalSince1970: TimeInterval(1665957700))))
输出结果
- 第一个输出:
"1970-01-02T04:53:20+01:00" - 第二个输出:
"2022-10-17T00:01:40+02:00"
额外测试情况:
- 使用
Europe/Kalingrad作为时区标识符时,第一个输出偏移为+3,第二个为+2 - 使用
dateFormatter.timeZone = .init(abbreviation: "GMT+1:00")或Pacific/Guam时,偏移不会随日期变化
原因解析
- 带地区标识符的时区是「规则时区」:像
Europe/Zurich、Europe/Kalingrad这类时区,对应现实中该地区的实际时区规则,包含夏令时(DST)切换或历史时区政策调整。比如苏黎世1970年1月处于冬令时,执行GMT+1偏移;2022年10月处于夏令时,自动切换为GMT+2偏移。加里宁格勒则因当地时区政策的历史变动,导致不同年份的偏移存在差异。 - GMT缩写创建的是「固定偏移时区」:
init(abbreviation: "GMT+1:00")生成的时区不遵循任何夏令时或政策变化逻辑,始终保持固定的GMT+1偏移,因此所有日期的输出偏移一致。 - Pacific/Guam属于无夏令时时区:该地区全年采用同一GMT偏移(GMT+10),没有夏令时切换规则,所以不同日期的输出偏移不会变化。
内容的提问来源于stack exchange,提问作者Gipfeli
相关产品推荐
相关产品推荐

