在我提交雷达之前,我想仔细检查我是否遗漏了什么(并从中学习)。这是我来自 Swift 3 Playground 的测试代码(代码在我的应用程序中表现相同)。我目前在“欧洲/里斯本”时区工作,二月份与格林威治标准时间相同。
import UIKit
let date = Date(timeIntervalSince1970: 0)
var formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
print(TimeZone.current.abbreviation()!) // GMT
formatter.timeZone = TimeZone.current
print(formatter.string(from: date)) // 01:00
formatter.timeZone = TimeZone(secondsFromGMT: TimeZone.current.secondsFromGMT())
print(formatter.string(from: date)) // 00:00
formatter.timeZone = TimeZone(identifier: "Europe/London")
print(formatter.string(from: date)) // 01:00
formatter.timeZone = TimeZone(identifier: "America/Los_Angeles")
print(formatter.string(from: date)) // 16:00
除了我将时区设置为
TimeZone(secondsFromGMT: TimeZone.current.secondsFromGMT())
这是预期的行为吗?谢谢!
最佳答案
结果实际上是正确的,因为在 1970 年,“欧洲/里斯本”时区是“UTC+1”:
let tz = TimeZone(identifier: "Europe/Lisbon")!
print(tz.secondsFromGMT())
// Output: 0
print(tz.secondsFromGMT(for: Date(timeIntervalSince1970: 0)))
// Output: 3600
所以“格林威治标准时间 1970 年 1 月 1 日”的午夜是凌晨一点 在里斯本。
您可以查询时区和夏令时时间段 在过去和 future 几年 https://www.timeanddate.com/time/zone/portugal/lisbon .
关于iOS 10 时区格式问题,日期创建为 timeIntervalSince1970,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42327055/