iOS 10 个时区格式问题,日期创建为 timeIntervalSince1970
iOS 10 timezone format trouble with dates created as timeIntervalSince1970
在我提交雷达之前,我想仔细检查我是否遗漏了什么(并从中学习)。这是我在 Swift 3 playground 中的测试代码(代码在我的应用程序中的行为相同)。我目前在时区 "Europe/Lisbon" 工作,二月份与格林威治标准时间相同。
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 年 "Europe/Lisbon" 时区是 "UTC+1":
let tz = TimeZone(identifier: "Europe/Lisbon")!
print(tz.secondsFromGMT())
// Output: 0
print(tz.secondsFromGMT(for: Date(timeIntervalSince1970: 0)))
// Output: 3600
所以 "Jan 1, 1970 GMT" 的午夜是凌晨一点
在里斯本。
您可以查询时区和夏令时
过去和未来几年在 https://www.timeanddate.com/time/zone/portugal/lisbon.
在我提交雷达之前,我想仔细检查我是否遗漏了什么(并从中学习)。这是我在 Swift 3 playground 中的测试代码(代码在我的应用程序中的行为相同)。我目前在时区 "Europe/Lisbon" 工作,二月份与格林威治标准时间相同。
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 年 "Europe/Lisbon" 时区是 "UTC+1":
let tz = TimeZone(identifier: "Europe/Lisbon")!
print(tz.secondsFromGMT())
// Output: 0
print(tz.secondsFromGMT(for: Date(timeIntervalSince1970: 0)))
// Output: 3600
所以 "Jan 1, 1970 GMT" 的午夜是凌晨一点 在里斯本。
您可以查询时区和夏令时 过去和未来几年在 https://www.timeanddate.com/time/zone/portugal/lisbon.