You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python脚本中转换iPhone数据库中的十六进制Mac绝对时间戳?

iPhone数据库十六进制时间戳转换方法

核心原理

iPhone数据库里的这类时间戳属于Mac绝对时间,以纳秒为单位计数,起始基准是UTC时间2001年1月1日00:00:00。你提供的十六进制字符串是64位大端字节序的无符号整数,直接对应这个时间基准后的纳秒总数。

转换步骤

  • 清理十六进制字符串:去掉空格,保留纯十六进制字符
  • 转十进制整数:将清理后的十六进制字符串转换为十进制数,得到总纳秒数
  • 转秒数:把纳秒数除以10^9,转换为以秒为单位的时间差
  • 计算目标时间:以2001年1月1日UTC为起点,加上转换后的秒数,得到最终UTC时间

Python代码实现

import datetime

def convert_iphone_hex_timestamp(hex_str):
    # 移除十六进制字符串中的空格
    cleaned_hex = hex_str.replace(" ", "")
    # 转换为十进制整数(大端字节序直接用int转换即可)
    total_nanoseconds = int(cleaned_hex, 16)
    # 纳秒转秒
    total_seconds = total_nanoseconds / 10**9
    # Mac时间基准点:2001-01-01 00:00:00 UTC
    mac_epoch = datetime.datetime(2001, 1, 1, tzinfo=datetime.timezone.utc)
    # 计算最终UTC时间
    target_time = mac_epoch + datetime.timedelta(seconds=total_seconds)
    return target_time

# 测试示例
sample_hex = "41 C4 C2 FA 5E 8C E8 64"
result = convert_iphone_hex_timestamp(sample_hex)
print(result.strftime("%Y-%m-%d %H:%M:%S %Z"))
# 输出:2023-01-29 00:26:37 UTC

验证说明

示例中的十六进制41 C4 C2 FA 5E 8C E8 64转换为十进制后是696644797000000000纳秒,除以10^9得到696644797秒。从2001年1月1日0点加上该秒数,正好匹配你给出的2023年1月29日00:26:37 UTC。

内容的提问来源于stack exchange,提问作者Crtaylor26

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 03:08:14