为什么 Calendar.date(from: DateComponents) 添加时间? [英] Why is Calendar.date(from: DateComponents) adding time?

查看:21
本文介绍了为什么 Calendar.date(from: DateComponents) 添加时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码创建了一个 DateComponents 实例:

I create an instance of DateComponents using the following code:

let dateComponents = DateComponents(
            calendar: .current,
            timeZone: Calendar.current.timeZone,
            era: nil,
            year: nil,
            month: nil,
            day: nil,
            hour: 9,
            minute: 0,
            second: 0,
            nanosecond: 0,
            weekday: 2,
            weekdayOrdinal: nil,
            quarter: nil,
            weekOfMonth: nil,
            weekOfYear: nil,
            yearForWeekOfYear: nil)

然后我打印 dateComponents 对象并获得以下(预期的)输出:

I then print the dateComponents object and get the following (expected) output:

calendar: gregorian (current) timeZone: Europe/London (current) 小时:9 分:0 秒:0 纳秒:0 工作日:2 isLeapMonth:false

calendar: gregorian (current) timeZone: Europe/London (current) hour: 9 minute: 0 second: 0 nanosecond: 0 weekday: 2 isLeapMonth: false

紧接着,我使用以下代码打印创建的日期:

Immediately following this, I print the date created using the following code:

print(Calendar.current.date(from: dateComponents)!)

令我非常沮丧和彻底不高兴的是,输出以下内容:

To my great dismay and thorough unhappiness, the following is outputted:

0001-01-01 09:01:15 +0000

0001-01-01 09:01:15 +0000

date(from: dateComponents) 函数似乎在从它们创建日期之前向 dateComponents 添加了超过一分钟的时间.

The date(from: dateComponents) function appears to have added just over a minute to the dateComponents before creating a date from them.

提前感谢您的帮助.

推荐答案

NSDate 对古代日期有一些奇怪且未记录的行为.变化似乎发生在 1895 年左右:

NSDate has some strange and undocumented behaviors for ancient dates. The change seems to have happened around 1895:

for year in 1890..<1900 {
    // January 1 of each year @ 9AM
    let dateComponents = DateComponents(
        calendar: .current,
        timeZone: Calendar.current.timeZone,
        year: year,
        month: 1,
        day: 1,
        hour: 9)

    if dateComponents.isValidDate {
        print(dateComponents.date!)
    }
}

我的日历是公历,时区是 EDT (UTC -0500).这是输出:

My calendar is Gregorian and timezone is EDT (UTC -0500). This is the output:

1890-01-01 14:17:32 +0000
1891-01-01 14:17:32 +0000
1892-01-01 14:17:32 +0000
1893-01-01 14:17:32 +0000
1894-01-01 14:17:32 +0000 // not correct
1895-01-01 14:00:00 +0000 // correct
1896-01-01 14:00:00 +0000
1897-01-01 14:00:00 +0000
1898-01-01 14:00:00 +0000
1899-01-01 14:00:00 +0000

因此,在 1895 年之前的几年里,Apple 以某种方式将我的时间增加了 17 分 32 秒.您得到了不同的偏移量,这可能是由于您的语言环境设置所致.

So for the years prior to 1895, Apple somehow added 17 minutes and 32 second to my time. You got a different offset, which is likely due your locale settings.

我找不到任何关于 1895 年公历的历史事件.这个问题提到英国开始切换到格林威治标准时间,格林威治天文台在 1890 年代开始调整不列颠群岛的日期/时间标准,因此可能导致了这种偏移.也许有人可以深入研究 Date/NSDate 的源代码并弄清楚?

I couldn't find anything historical event about the Gregorian calendar in 1895. This question mentions that Britain started to switch over to GMT and the Greenwich Observatory started adjusting date/time standards across the British Isles in the 1890s so that may have accounted for this offset. Perhaps someone can delve into the source code for Date / NSDate and figure it out?

如果您想使用 DateComponent 来存储重复的计划,请使用 nextDate(after:matching:matchingPolicy:) 来查找计划的下一次出现:>

If you want to use DateComponent to store a repeating schedule, use nextDate(after:matching:matchingPolicy:) to find the next occurance of your schedule:

let dateComponents = DateComponents(calendar: .current, timeZone: .current, hour: 9, weekday: 2)

// 9AM of the next Monday
let nextOccurance = Calendar.current.nextDate(after: Date(), matching: dateComponents, matchingPolicy: .nextTime)!

这篇关于为什么 Calendar.date(from: DateComponents) 添加时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆