【问题标题】:Swift configuring timezone with identifier changes GMT value使用标识符快速配置时区更改 GMT 值
【发布时间】:2022-11-01 22:54:22
【问题描述】:

我有以下操场代码:

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”.为什么 GMT 值会发生变化?如果使用“欧洲/加里宁格勒”作为标识符,我会得到+3从第一和+2从第二张印刷品开始。当我设置时区时,问题不会出现“dateFormatter.timeZone = .init(缩写:“GMT+1:00”)”.我在使用时也找不到这种行为“太平洋/关岛”.

【问题讨论】:

    标签: swift timezone


    【解决方案1】:

    命名的TimeZones 考虑了夏令时 (DST),而由 GMT 偏移创建的则不考虑。

    苏黎世冬令时为 GMT+1,夏令时为 GMT+2。

    您创建的 2 个日期位于时钟变化的不同侧,因此 1 个使用夏令时,而另一个使用冬令时。这就是使用 2 个不同时区的 1 小时时差的来源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 2011-10-26
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 2012-05-02
      • 1970-01-01
      相关资源
      最近更新 更多