24

I know, "no user-serviceable parts inside" ... but I'm curious:

In a Core Data sqlite3 DB, it seems I can get at the date within a ZDATE like so:

sqlite> select datetime(ZDATE,'unixepoch','31 years','localtime') from ZMYCLASS;
2003-12-11 19:00:00
2009-12-31 19:00:00
2009-01-24 19:00:00
2011-01-01 19:00:00
2009-10-03 20:00:00
...

Unix Epoch I get, but why 31 years?

1

1 Answer 1

28

Core Data stores dates relative to reference date, which is Jan 1st, 2001 (31 years after EPOCH as pointed out in the comments)

Here's some code to decode the dates from the table, in case it is useful to you.

NSNumber *time = [NSNumber numberWithDouble:(d - 3600)];
NSTimeInterval interval = [time doubleValue];    
NSDate *online = [NSDate dateWithTimeIntervalSinceReferenceDate:interval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss.SSS"];

NSLog(@"result: %@", [dateFormatter stringFromDate:online]);

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

Take a look at + dateWithTimeIntervalSinceReferenceDate:

4
  • 3
    Jan 1, 2001 is 31 years after the Unix Epoch. This sounds like an answer to me. May 22, 2012 at 18:52
  • 1
    Why did Apple go to the trouble of creating their own, custom epoch base? Y2k bug worries?
    – Ed J
    May 13, 2018 at 20:50
  • I have no authorative reference, so pure speculation but the epoch used by CFAbsoluteTime might be a reference to 2001 as the year when Mac OS X 10.0 was shipped. Mar 14, 2023 at 21:05
  • Thanks! that was very helpful, I was using a date field as a scalar type (TimeInterval) with a computed property that used timeIntervalSince1970, which caused my queries to return wrong results, using timeIntervalSinceReferenceDate fixed it.
    – Kumait
    Apr 8 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.