简体   繁体   English

iOS 10时区格式问题,日期创建为timeIntervalSince1970

[英]iOS 10 timezone format trouble with dates created as timeIntervalSince1970

Before I file a radar I would like to doublecheck if I am not missing something (and learn from this). 在我提交雷达之前,如果我没有遗漏某些东西(并从中学习),我想进行双重检查。 Here is my test code from a Swift 3 playground (code behaves the same in my app). 这是我在Swift 3游乐场的测试代码(代码在我的应用程序中表现相同)。 I am currently working in the timezone "Europe/Lisbon" which in February is the same as GMT. 我目前在“欧洲/里斯本”时区工作,2月份与格林尼治标准时间相同。

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

Somehow the formatted time is one hour off except when I set the Timezone as 不知何故格式化的时间是一小时关闭,除非我将时区设置为

TimeZone(secondsFromGMT: TimeZone.current.secondsFromGMT())

Is this expected behavior? 这是预期的行为吗? Thanks! 谢谢!

The results are in fact correct, because in the year 1970 the "Europe/Lisbon" timezone was "UTC+1": 结果实际上是正确的,因为在1970年“欧洲/里斯本”时区是“UTC + 1”:

let tz = TimeZone(identifier: "Europe/Lisbon")!

print(tz.secondsFromGMT())
// Output: 0

print(tz.secondsFromGMT(for: Date(timeIntervalSince1970: 0)))
// Output: 3600

so that midnight on "Jan 1, 1970 GMT" was one o'clock in the morning in Lisbon. 因此,格林威治标准时间1970年1月1日午夜时分在里斯本一点钟。

You can lookup the timezones and daylight saving time periods for past and future years at https://www.timeanddate.com/time/zone/portugal/lisbon . 您可以在https://www.timeanddate.com/time/zone/portugal/lisbon上查找过去和未来几年的时区和夏令时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM