簡體   English   中英

iOS 10時區格式問題,日期創建為timeIntervalSince1970

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

在我提交雷達之前,如果我沒有遺漏某些東西(並從中學習),我想進行雙重檢查。 這是我在Swift 3游樂場的測試代碼(代碼在我的應用程序中表現相同)。 我目前在“歐洲/里斯本”時區工作,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

不知何故格式化的時間是一小時關閉,除非我將時區設置為

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日午夜時分在里斯本一點鍾。

您可以在https://www.timeanddate.com/time/zone/portugal/lisbon上查找過去和未來幾年的時區和夏令時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM