Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

timeIntervalSinceReferenceDate vs timeIntervalSince1970 vs timeIntervalSinceNow

Tags:

ios

swift

swift2

I am trying to study NSDate class in Swift 2.

I found that each NSDate object has three properties, which are:

  1. timeIntervalSinceReferenceDate
  2. timeIntervalSince1970
  3. 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?

like image 994
user2228656 Avatar asked Oct 25 '15 14:10

user2228656


2 Answers

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. 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).

timeIntervalSince1970 uses the epoch date that is the standard in UNIX: midnight on January 1, 1970 in GMT. 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.

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:

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. The init methods are named a little more clearly in Objective-C:

+ (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.

like image 77
Duncan C Avatar answered Oct 21 '22 19:10

Duncan C


timeIntervalSinceReferenceDate is the number of seconds since January, 1st, 2001: 12:00 am (mid night)

timeIntervalSince1970 is the number of seconds since January, 1st, 1970, 12:00 am (mid night)

timeIntervalSinceNow is the number of seconds since now

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 ...

let sNow = NSDate()
sNow.timeIntervalSinceReferenceDate
sNow.timeIntervalSinceNow
sNow.timeIntervalSince1970
like image 24
William Kinaan Avatar answered Oct 21 '22 17:10

William Kinaan