简体   繁体   English

timeIntervalSinceReferenceDate vs timeIntervalSince1970 vs timeIntervalSinceNow

[英]timeIntervalSinceReferenceDate vs timeIntervalSince1970 vs timeIntervalSinceNow

I am trying to study NSDate class in Swift 2. 我正在尝试在Swift 2中学习NSDate类。

I found that each NSDate object has three properties, which are: 我发现每个NSDate对象都有三个属性,分别是:

  1. timeIntervalSinceReferenceDate timeIntervalSinceReferenceDate
  2. timeIntervalSince1970 timeIntervalSince1970
  3. timeIntervalSinceNow timeIntervalSinceNow

A little bit confusing for me. 对我来说有点困惑。 I couldn't understand the interpretation of them though I know they represents the seconds. 我无法理解他们的解释,虽然我知道他们代表秒。

I did this code: 我做了这个代码:

let whatISThis = NSDate(timeIntervalSinceReferenceDate: NSTimeInterval(60))

Okay I got a date, but what is that? 好的,我有约会,但那是什么? what is the difference between those three properties? 这三个属性有什么区别?

timeIntervalSinceReferenceDate is the number of seconds since January, 1st, 2001: 12:00 am (mid night) timeIntervalSinceReferenceDate是自2001年1月1日以来的秒数:凌晨12:00(深夜)

timeIntervalSince1970 is the number of seconds since January, 1st, 1970, 12:00 am (mid night) timeIntervalSince1970是1970年1月1日上午12:00(中午)以来的秒数

timeIntervalSinceNow is the number of seconds since now timeIntervalSinceNow是从现在开始的秒数

I will list these examples: 我将列出这些例子:

let s0 = NSDate(timeIntervalSinceReferenceDate: NSTimeInterval(0)) // it means give me the time that happens after January,1st, 2001, 12:00 am by zero seconds
print("\(s0)") //2001-01-01 00:00:00

let s60 = NSDate(timeIntervalSinceReferenceDate: NSTimeInterval(60)) //it means give me the time that happens after January, 1st, 2001, 12:00 am by **60 seconds**
print("\(s60)") //2001-01-01 00:01:00

let s2 = NSDate(timeIntervalSince1970: NSTimeInterval(0)) // it means give me the time that happens after January, 1st, 1970 12:00 am by **zero** seconds
print("\(s2)") //1970-01-01 00:00:00

let s3 = NSDate() // it means the current time
print("\(s3)")//2015-10-25 14:12:40

let s4 = NSDate(timeIntervalSinceNow: NSTimeInterval(60)) //it means one minute (60 seconds) after the current time
print("\(s4)") //2015-10-25 14:13:40

let s5 = NSDate(timeIntervalSinceNow: NSTimeInterval(-60)) // it means one minute (60 seconds) before the current time
print("\(s5)") //2015-10-25 14:11:40

let sd = NSDate(timeIntervalSinceReferenceDate: NSTimeInterval(60)) // it means one minute after the reference time (January, 1st, 1970: 12:00 am)
print("\(sd)") //2001-01-01 00:01:00

And of course if you have a NSDate objects, you can take all these properties simply ... 当然,如果你有一个NSDate对象,你可以简单地采取所有这些属性......

let sNow = NSDate()
sNow.timeIntervalSinceReferenceDate
sNow.timeIntervalSinceNow
sNow.timeIntervalSince1970

William explained the difference between the initializers well. 威廉解释了初始化者之间的区别。 (Voted) (投票)

NSDate also has properties that let you ask a date to give you back time intervals. NSDate还具有一些属性,可让您询问日期以回溯时间间隔。 I'll talk about those in a minute. 我会在一分钟内谈论这些。

First a little background: The timeIntervalSince1970 and timeIntervalSinceReferenceDate methods use a different "epoch date", or date that is considered the "zero date" (the date with a numeric value of zero). 首先是一个小背景: timeIntervalSince1970timeIntervalSinceReferenceDate方法使用不同的“纪元日期”,或者被视为“零日期”的日期(数值为零的日期)。

timeIntervalSince1970 uses the epoch date that is the standard in UNIX: midnight on January 1, 1970 in GMT. timeIntervalSince1970使用UNIX中标准的纪元日期:1970年1月1日格林尼治标准时间午夜。 This is also the standard epoch date for dates on the internet. 这也是互联网上日期的标准纪元日期。

timeIntervalSinceReferenceDate uses Mac OS/iOS epoch date: Midnight on January 1, 2001. You can easily calculate a constant offset between these 2 reference dates and convert between them using addition/subtraction. timeIntervalSinceReferenceDate使用Mac OS / iOS纪元日期:2001年1月1日午夜。您可以轻松计算这两个参考日期之间的常量偏移量,并使用加/减来在它们之间进行转换。

In addition to the init methods you've been talking about, NSDate has properties that give you a value that is the number of seconds since a given reference date: 除了你一直在讨论的init方法之外,NSDate还有一些属性可以提供一个值,该值是给定引用日期以来的秒数:

var timeIntervalSinceReferenceDate: NSTimeInterval
var timeIntervalSince1970: NSTimeInterval

It's confusing because the names of the properties and the names of the init methods appear identical in Swift. 这很令人困惑,因为属性的名称和init方法的名称在Swift中看起来是相同的。 The init methods are named a little more clearly in Objective-C: 在Objective-C中更清楚地命名了init方法:

+ (instancetype _Nonnull)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)seconds

+ (instancetype _Nonnull)dateWithTimeIntervalSince1970:(NSTimeInterval)seconds

Usually I find Swift's method naming to be cleaner than Objective-C's, but init methods can be an exception. 通常我发现Swift的方法命名比Objective-C更清晰,但init方法可能是个例外。

You can find time intervals between date objects with 您可以在日期对象之间找到时间间隔
- timeIntervalSinceDate:

Those three properties are shortcuts to find the same thing with other convenience date object. 这三个属性是与其他便利日期对象找到相同内容的快捷方式。

  1. timeIntervalSince1970 — January 1, 1970 at 12:00 am GMT. timeIntervalSince1970 - 1970年1月1日格林尼治标准时间上午12:00。
  2. timeIntervalSinceReferenceDate — January 1, 2001, at 12:00 am GMT. timeIntervalSinceReferenceDate - 2001年1月1日,格林尼治标准时间上午12:00。
  3. timeIntervalSinceNow — current date and time. timeIntervalSinceNow - 当前日期和时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM