Mac Absolute Time

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 年起的秒數),所以轉換完全相同。