timeIntervalSinceReferenceDate

timeIntervalSinceReferenceDate

我正在看一段代码,偶然发现timeIntervalBetween1970AndReferenceDatetimeIntervalSinceReferenceDate的用法。

代码返回以毫秒为单位的时间戳:

return (Date.timeIntervalBetween1970AndReferenceDate + date.timeIntervalSinceReferenceDate) * 1000


这不等于:

return date.timeIntervalSince1970 * 1000

最佳答案

确实,没有区别。 Date具有type属性

public static let timeIntervalBetween1970AndReferenceDate: TimeInterval
// The number of seconds from 1 January 1970 to the reference date, 1 January 2001


和实例属性

public var timeIntervalSinceReferenceDate: TimeInterval { get }
// The interval between the date value and 00:00:00 UTC on 1 January 2001.

public var timeIntervalSince1970: TimeInterval { get }
// The interval between the date value and 00:00:00 UTC on 1 January 1970.


这意味着对于任何date类型的值Date,标识

Date.timeIntervalBetween1970AndReferenceDate + date.timeIntervalSinceReferenceDate
     == date.timeIntervalSince1970


持有。实际上,正如我们在Date.swift#L111中所见,timeIntervalSince1970被实现为

public var timeIntervalSince1970: TimeInterval {
    return self.timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate
}

关于swift - 是timeIntervalBetween1970AndReferenceDate + timeIntervalSinceReferenceDate = timeIntervalSince1970,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51107324/

10-12 15:59