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?

Joe D'Andrea
  • 5,141
  • 6
  • 49
  • 67
  • You can convert the decimal found in the sqlite to a real date time using http://www.epochconverter.com/coredata – J3RM Oct 27 '16 at 16:54

1 Answers1

25

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:

mprivat
  • 21,582
  • 4
  • 54
  • 64
  • 3
    Jan 1, 2001 is 31 years after the Unix Epoch. This sounds like an answer to me. – paulmelnikow May 22 '12 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 '18 at 20:50
  • I have no authorative reference, so pure speculation but the epoch used by [CFAbsoluteTime](https://developer.apple.com/documentation/corefoundation/cfabsolutetime) _might_ be a reference to 2001 as the year when [Mac OS X 10.0](https://en.wikipedia.org/wiki/Mac_OS_X_10.0) was shipped. – Stefan Schmidt Mar 14 '23 at 21:05