日历:获取日期成分,奇怪的情况 [英] Calendar: get date components, weird case
问题描述
我试图找出为什么这段代码如此奇怪。您可以在下面找到详细信息:
I'm trying to find out why this piece of code works so strange. You can find details below:
let nowDate = Date()
let threeDayBeforeNowDate_t1 = Date(timeIntervalSinceNow: -60 * 60 * 24 * 3)
let oneDayAfterNowDate = Date(timeIntervalSinceNow: 60 * 60 * 24 * 1)
let threeDayBeforeNowDate_t2 = Date(timeIntervalSinceNow: -60 * 60 * 24 * 3)
let threeDayBeforeNowDate = nowDate.addingTimeInterval(-60 * 60 * 24 * 3)
let diff_1 = threeDayBeforeNowDate_t1.timeIntervalSince(nowDate) - threeDayBeforeNowDate.timeIntervalSince(nowDate) // about 0.009357
let diff_2 = threeDayBeforeNowDate_t2.timeIntervalSince(nowDate) - threeDayBeforeNowDate.timeIntervalSince(nowDate) // about 0.010063
let diff_3 = threeDayBeforeNowDate_t2.timeIntervalSince(nowDate) - threeDayBeforeNowDate_t1.timeIntervalSince(nowDate) // about 0.000416
let calendar = Calendar.current
calendar.dateComponents(Set([Calendar.Component.day]), from: threeDayBeforeNowDate, to: oneDayAfterNowDate) // result = "day: 4 isLeapMonth: false"
calendar.dateComponents(Set([Calendar.Component.day]), from: threeDayBeforeNowDate_t1, to: oneDayAfterNowDate) // day: 4 isLeapMonth: false
calendar.dateComponents(Set([Calendar.Component.day]), from: threeDayBeforeNowDate_t2, to: oneDayAfterNowDate) // day: 3 isLeapMonth: false
我不明白,为什么我得到的结果如此不同,而日期(threeDayBeforeNow ...)相差不到一秒钟。
I don't understand, why I get so different results while dates (threeDayBeforeNow...) differ by less than a second.
推荐答案
正如Dávid已经解释过,问题在于,各个日期
是在不同点计算的时间上的差异,因此不是
恰好是4天。特别是,
threeDayBeforeNowDate_t2
和 oneDayAfterNowDate
之间的差小于 b $ b 4天,这就是为什么差额的 .day
部分是 3
。
As Dávid already explained, the problem is that the various dates
are computed at different points in time, therefore the difference is not
exactly 4 days. In particular, the difference between
threeDayBeforeNowDate_t2
and oneDayAfterNowDate
is less than
4 days, and that's why the .day
component of the difference is 3
.
下面是一个简单的示例,演示了这个问题(在操场上):
Here is a simplified example demonstrating the problem (in a Playground):
let nowDate = Date()
let date1 = Date(timeIntervalSinceNow: -60 * 60 * 24 * 4)
let date2 = nowDate.addingTimeInterval(-60 * 60 * 24 * 4)
let calendar = Calendar.current
calendar.dateComponents([.day, .hour, .minute, .second, .nanosecond], from: date1, to: nowDate)
// day: 3 hour: 23 minute: 59 second: 59 nanosecond: 994143066 isLeapMonth: false
calendar.dateComponents([.day, .hour, .minute, .second, .nanosecond], from: date2, to: nowDate)
// day: 4 hour: 0 minute: 0 second: 0 nanosecond: 0 isLeapMonth: false
date2
和 nowDate
r恰好是4天,但 date1
和 nowDate
相差很小,小于 4天(假设在此时间段内没有
的夏令时过渡)。
date2
and nowDate
differ by exactly 4 days, but date1
and nowDate
differ by a tiny be less than 4 days (assuming that there is no
daylight savings transition in this timespan).
这篇关于日历:获取日期成分,奇怪的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!