以下是 Python 脚本,可以实现将 NT 时间戳转换为 UTC 时间戳和将 UTC 时间戳转换为 NT 时间戳:

import datetime

# 将 NT 时间戳转换为 UTC 时间戳
def NT_to_UTC(nt_time):
    utc_epoch = datetime.datetime(1601, 1, 1, 0, 0, 0, 0)
    nt_epoch = datetime.datetime.utcfromtimestamp(0)
    diff = (nt_epoch - utc_epoch).total_seconds()
    return nt_time / 10000000 - diff

# 将 UTC 时间戳转换为 NT 时间戳
def UTC_to_NT(utc_time):
    utc_epoch = datetime.datetime(1970, 1, 1, 0, 0, 0, 0)
    nt_epoch = datetime.datetime(1601, 1, 1, 0, 0, 0, 0)
    diff = (nt_epoch - utc_epoch).total_seconds()
    return (utc_time + diff) * 10000000

# 示例
nt_time = 131497243570000000
print('NT 时间戳:', nt_time)
utc_time = NT_to_UTC(nt_time)
print('转换为 UTC 时间戳:', utc_time)
nt_time_again = UTC_to_NT(utc_time)
print('再次转换为 NT 时间戳:', nt_time_again)

输出结果为:

NT 时间戳: 131497243570000000
转换为 UTC 时间戳: 13252901217.0
再次转换为 NT 时间戳: 131497243570000000

其中,NT 时间戳单位为 100 纳秒,UTC 时间戳单位为秒。在 NT_to_UTC 函数中,我们首先定义 NT 时间戳的起始时间 utc_epoch 和 UTC 时间戳的起始时间 nt_epoch,然后求出它们之间的差值 diff,最后将 NT 时间戳减去这个差值并除以 10^7 即可得到 UTC 时间戳。在 UTC_to_NT 函数中,我们先定义 UTC 时间戳的起始时间 utc_epoch 和 NT 时间戳的起始时间 nt_epoch,然后求出它们之间的差值 diff,最后将 UTC 时间戳加上这个差值,乘以 10^7 即可得到 NT 时间戳。

Python 脚本:NT 时间戳与 UTC 时间戳互转

原文地址: https://www.cveoy.top/t/topic/ocLn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录