您没有指定时区(通过设置components 或calendar 的timeZone 属性)。因此系统使用您的本地时区将components 转换为日期。
操场系统使用您的本地时区将newDate 转换为字符串“Jan 1, 1 at 7:00 AM”。 (操场系统有显示Date 对象的特例代码。特例代码使用您当地的时区。)
print 函数使用 UTC 时区将newDate 转换为字符串“0001-01-01 11:56:02 +0000”。 (print 函数使用CustomStringConvertible 协议。Date 的CustomStringConvertible 实现使用UTC 时区。)
我推断您的本地时区是美国/东部时间,也称为America/New_York:
import Foundation
var calendar = Calendar(identifier: .gregorian)
let components = NSDateComponents()
components.hour = 7
components.minute = 0
for timeZoneIdentifier in TimeZone.knownTimeZoneIdentifiers {
calendar.timeZone = TimeZone(identifier: timeZoneIdentifier)!
let date = calendar.date(from: components as DateComponents)!
let dateString = "\(date)"
if dateString == "0001-01-01 11:56:02 +0000" {
print("\(timeZoneIdentifier) \(date)")
}
}
// Only one line of output: America/New_York 0001-01-01 11:56:02 +0000
那么为什么会有奇怪的 UTC 分钟和秒数? Because at noon on November 18, 1883, the US and Canada railway companies began using a new, standard time system,这是我们今天仍在使用的时间系统。观察:
import Foundation
let formatter = ISO8601DateFormatter()
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = TimeZone(identifier: "America/New_York")!
let components = NSDateComponents()
components.year = 1883
components.month = 11
components.day = 18
components.hour = 11
print(formatter.string(from: calendar.date(from: components as DateComponents)!))
// prints 1883-11-18T15:56:02Z
components.hour = 12
print(formatter.string(from: calendar.date(from: components as DateComponents)!))
// prints 1883-11-18T17:00:00Z
中午之前,与UTC时间的差是4:56:02。
在标准铁路时间出现之前,我们通常根据当地的正午来定义当地时间(太阳在天空中最高且阴影正好指向北方或南方的时刻,或者如果太阳直接在头顶上则完全消失)。
如果我们查看 the tz Time Zone Database 中 America/New_York 的定义,我们会发现:
# From Paul Eggert (2014-09-06):
# Monthly Notices of the Royal Astronomical Society 44, 4 (1884-02-08), 208
# says that New York City Hall time was 3 minutes 58.4 seconds fast of
# Eastern time (i.e., -4:56:01.6) just before the 1883 switch. Round to the
# nearest second.
再往下一点:
Zone America/New_York -4:56:02 - LMT 1883 Nov 18 12:03:58
这解释了我们在上面看到的 1883 年 11 月 18 日中午之前的差异。