【发布时间】:2016-12-17 03:31:48
【问题描述】:
我需要将文件时间转换为日期时间。我正在使用此代码filetime.py,来自here,如该线程Datetime to filetime (Python) 中所述。
在代码中
EPOCH_AS_FILETIME = 116444736000000000 # January 1, 1970 as MS file time
HUNDREDS_OF_NANOSECONDS = 10000000
def filetime_to_dt(ft):
"""Converts a Microsoft filetime number to a Python datetime. The new datetime object is time zone-naive but is equivalent to tzinfo=utc.
>>> filetime_to_dt(116444736000000000)
datetime.datetime(1970, 1, 1, 0, 0)
"""
# Get seconds and remainder in terms of Unix epoch
(s, ns100) = divmod(ft - EPOCH_AS_FILETIME, HUNDREDS_OF_NANOSECONDS)
# Convert to datetime object
dt = datetime.utcfromtimestamp(s)
# Add remainder in as microseconds. Python 3.2 requires an integer
dt = dt.replace(microsecond=(ns100 // 10))
return dt
datetime.utcfromtimestamp 在 Windows 系统上不取负值,因此我无法转换 1970 年 1 月 1 日之前的文件时间。但我可以使用完全相同的代码在 Mac 上转换 1970 年之前的日期(原因 here)。 Windows有什么解决方法吗?
【问题讨论】:
-
所以你的意思是你想在正常日期时间转换一些纪元时间对吗?