Mac Absolute Time 转 Unix 时间戳转换器
把 Mac Absolute Time 转换为 Unix 时间。精确公式与大数支持。输出:秒、毫秒、ISO 8601 与可读格式。
自 2001-01-01 UTC 以来的秒数
当前时间 — Mac 绝对时间 (CFAbsoluteTime)—
Unix 时间戳(秒)—
什么是 Mac Absolute Time?
Apple 的 Core Foundation 将时间度量为 CFAbsoluteTime:自参考日期 2001 年 1 月 1 日 00:00:00 UTC 起的双精度秒数。这个「Mac absolute time」遍布 macOS 和 iOS,包括 plist 文件、统一日志系统和许多 Cocoa API。
- 纪元:2001-01-01 00:00:00 UTC(Cocoa/Core Foundation 的参考日期)
- 类型:浮点秒,因此小数部分携带亚秒精度
- 当前值约为 7.4 × 10^8(小于等效的 Unix 时间戳)
- 常见于:CFAbsoluteTimeGetCurrent()、NSDate timeIntervalSinceReferenceDate、plist 的 Date 值
如何将 Mac Absolute Time 转换为 Unix 时间
Unix 纪元(1970)与 Apple 纪元(2001)之间相差 978,307,200 秒。加上这个偏移即可把 Apple 秒转换为 Unix 秒。
- Unix seconds = MacAbsoluteTime + 978307200
- 示例:721692800 → 1700000000 → 2023-11-14 22:13:20 UTC
- 反向:MacAbsoluteTime = Unix seconds − 978307200
- Swift: Date(timeIntervalSinceReferenceDate: t).timeIntervalSince1970
相关的 Apple 格式
Core Data 以相同的 CFAbsoluteTime 值持久化日期,因此 Core Data 时间戳转换器使用相同的算法。对看起来像 Unix 时间戳的值要小心——若忘记偏移,基于 2001 的值会显得早了约 31 年。
- Core Data SQLite 将 ZDATE 列存为自 2001 年起的秒
- 接近 7×10^8 的裸值几乎肯定是 Apple 时间,而非 Unix 时间
- 某些 APNs 和日志值也使用相同的参考日期
相关时间戳格式转换器
其他 Apple 平台时间类型
CFAbsoluteTime 是 Apple 的标准基准,但相关类型出现在各个 Apple 框架中:
- NSDate 具有相同的底层 timeIntervalSinceReferenceDate(自 2001 年起的秒)
- NSTimeInterval 是双精度秒值的 typedef(不总是 Apple 纪元——取决于上下文)
- plist 的 <date> 元素以 ISO 8601 字符串写入,而非原始 CFAbsoluteTime——持久化前请先转换
- iOS 统一日志时间戳和 Core Telephony 事件通常使用 CFAbsoluteTime
- macOS 的 log show 输出默认打印本地时间;原始事件时间戳仍为 UTC
FAQ
- 什么是 CFAbsoluteTime 纪元?
- CFAbsoluteTime 计数自 2001 年 1 月 1 日 00:00:00 UTC 以来的秒数,这是 Core Foundation/Cocoa 的参照日期。
- 如何把 Mac 绝对时间转换为 Unix 时间?
- 给 Mac 绝对时间加上 978,307,200 秒,即可得到以秒为单位的 Unix 时间戳。
- Mac 绝对时间和 Core Data 时间戳一样吗?
- 是的。Core Data 把日期存为 CFAbsoluteTime(自 2001 年起的秒数),所以转换完全相同。