NSPredicate:NSLog of predicate using NSDate seems wrong

NSPredicate: NSLog of predicate using NSDate seems wrong

我的错误在哪里?

以下代码行:

NSPredicate* someTestPredicate = [NSPredicate predicateWithFormat:@"departureTime >= %@", [NSDate date]];
    NSLog(@"Some Test Predicate: %@", someTestPredicate);

结果:

2019-04-02 20:19:55.671894+0200 [9402:337625] Some Test Predicate: departureTime >= CAST(575921995.671820, "NSDate")

而这个时间戳指的是: 04/01/1988 @ 6:19pm (UTC)

那为什么年份这么不对呢?我做错了什么?

您的数学是正确的,但 NSDate 使用的内部参考日期是 2001-01-01,而不是 1970-01-01,正如在许多其他地方看到的 UNIX 时间戳。这也是 NSDate 有 timeIntervalSinceReferenceDate and timeIntervalSince1970.

的原因

NSDate 的纪元与 Unix 纪元不同。来自 the docs:

Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).

因此,从 2001/01/01 开始的 575921995.671820 秒是 2019-04-02 18:19:55 UTC。