Reputation: 4319
Aside from input and output of the functions, what is the difference between NSDate
's
func timeIntervalSinceDate(anotherDate: NSDate) -> NSTimeInterval
and NSCalendar
's
func components(unitFlags: NSCalendarUnit, fromDate startingDate: NSDate, toDate resultDate: NSDate, options opts: NSCalendarOptions) -> NSDateComponents
?
Which function is more precise? And which functions take into account daylight savings, leap seconds, etc.?
For example:
let today = NSDate()
let someDate = RandomFutureDate() // Assumes this gives random future date
let seconds = futureDate.timeIntervalSinceDate(today)
let anotherSeconds = NSCalendar.currentCalendar().components(.CalendarUnitSecond, fromDate: today, toDate: futureDate, options: nil).second
Which is more accurate/correct, seconds
or anotherSeconds
?
Note: seconds
may seem more accurate since it is a double, but does it take into account DST, leap seconds, etc.?
Upvotes: 2
Views: 798
Reputation: 52546
NSDate just counts consecutive seconds.
NSCalendar and NSDataComponents handle actual calendars. For example, you can use them to add "one month", and March 1st is increased by 31 days to April 1st, April 1st is increased by 30 days to May 1st, February 1st is increased sometimes by 28, sometimes by 29 days to March 1st. Adding a day isn't the same as adding 86400 seconds, because when daylight saving time changes, adding a day will add 23 hours or 25 hours.
NSDate also counts seconds in UTC. So you can't use NSDate to answer whether two dates are the same date - it depends on the time zone. Two dates four hours apart could be in different days (10pm and 2am) where I live, and on the same day (2am and 6am) where you live. NSCalendar takes care of time zone as well.
Upvotes: 1
Reputation: 131426
Its not that one is more accurate/precise. They do different things.
The timeIntervalSinceDate method gives you a raw calculation of the number of seconds difference between 2 dates, without any attempt to divide that difference into conventional units like minutes and seconds. I guess timeIntervalSinceDate is more precise because it gives you an answer in fractional seconds, with sub-millisecond precision.
Your second version uses a calendar object to calculate a whole number number of seconds between the 2 dates. That version can be used to figure out the number of years, months, weeks, days, etc. between 2 dates, and is very useful for "calendrical calculations" Obviously in your example you're only asking for the number of whole seconds difference.
As Martin R points out in his comment below, you can adjust your calendar code slightly and get very precise fractional seconds from an NSCalendar using CalendarUnitNanosecond.
NSDates are really just a thin wrapper around a count of the number of seconds since the OS X/iOS "epoch date". The timeIntervalSinceDate method is going to be lightning-fast, since it's just doing a simple floating point subtraction.
By comparison, code using NSCalendar and NSDateComponents can be quite complicated, and therefore slower to perform. Such calculations have to allow for different calendar systems, different numbers of days in each month, leap years, and lots of other edge cases.
Upvotes: 1