Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behind The Scenes: Core Data dates stored with 31 year offset?

Tags:

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?

like image 791
Joe D'Andrea Avatar asked May 22 '12 15:05

Joe D'Andrea


1 Answers

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:

like image 175
mprivat Avatar answered Oct 07 '22 16:10

mprivat