幕后花絮:核心数据日期存储31年偏移量?

Joe*_*rea 21 datetime core-data unix-timestamp

我知道,"里面没有用户可维修的部件"......但我很好奇:

在Core Data sqlite3 DB中,似乎我可以在ZDATE中获取日期,如下所示:

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
...
Run Code Online (Sandbox Code Playgroud)

Unix Epoch我得到了,但为什么31年?

mpr*_*vat 21

核心数据存储相对于参考日期的日期,即2001年1月1日(EPOCH之后31年,如评论中所指出)

这里有一些代码来解码表中的日期,以防它对你有用.

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]);
Run Code Online (Sandbox Code Playgroud)

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

看一眼 + dateWithTimeIntervalSinceReferenceDate:

  • 2001年1月1日是Unix Epoch之后的31年.这听起来像是对我的回答. (3认同)