python 将 1970 年之前的日期的文件时间转换为日期时间
python convert filetime to datetime for dates before 1970
我需要将文件时间转换为日期时间。我正在使用此代码 filetime.py, from here as mentioned in this thread 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 有任何解决方法吗?
According to docs,你必须使用:
dt = datetime.fromtimestamp(s, datetime.timezone.utc)
而不是:
dt = datetime.utcfromtimestamp(s)
通过向参考日期添加 timedelta
,您可以使用任何您喜欢的日期公式。 timedelta
可正可负。
def filetime_to_dt(ft):
us = (ft - EPOCH_AS_FILETIME) // 10
return datetime(1970, 1, 1) + timedelta(microseconds = us)
首先您需要将其转换为可识别的文件时间格式,如下所示:
>>> dwLowDateTime = 0x0F7297A80
>>> dwHighDateTime = 0x1C3F10F << 32
>>> ft = (dwLowDateTime & 0xFFFFFFFF) | dwHighDateTime
127210265370000000
然后使用这个脚本,它将文件时间转换为日期时间,反之亦然https://gist.github.com/Mostafa-Hamdy-Elgiar/9714475f1b3bc224ea063af81566d873
>>> filetime_to_dt(ft)
2004-02-12 02:28:57
在时间范围之间使用 timedelta
s 到 add/subtract/divide:
FILE_TIME_EPOCH = datetime.datetime(1601, 1, 1)
FILE_TIME_MICROSECOND = 10 # FILETIME counts 100 nanoseconds intervals = 0.1 microseconds, so 10 of those are 1 microsecond
def convert_from_file_time(file_time):
microseconds_since_file_time_epoch = file_time // FILE_TIME_MICROSECOND
return FILE_TIME_EPOCH + datetime.timedelta(microseconds=microseconds_since_file_time_epoch)
另一面是:
def convert_to_file_time(date_time):
microseconds_since_file_time_epoch = (date_time - FILE_TIME_EPOCH) // datetime.timedelta(microseconds=1)
return microseconds_since_file_time_epoch * FILE_TIME_MICROSECOND
我需要将文件时间转换为日期时间。我正在使用此代码 filetime.py, from here as mentioned in this thread 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 有任何解决方法吗?
According to docs,你必须使用:
dt = datetime.fromtimestamp(s, datetime.timezone.utc)
而不是:
dt = datetime.utcfromtimestamp(s)
通过向参考日期添加 timedelta
,您可以使用任何您喜欢的日期公式。 timedelta
可正可负。
def filetime_to_dt(ft):
us = (ft - EPOCH_AS_FILETIME) // 10
return datetime(1970, 1, 1) + timedelta(microseconds = us)
首先您需要将其转换为可识别的文件时间格式,如下所示:
>>> dwLowDateTime = 0x0F7297A80
>>> dwHighDateTime = 0x1C3F10F << 32
>>> ft = (dwLowDateTime & 0xFFFFFFFF) | dwHighDateTime
127210265370000000
然后使用这个脚本,它将文件时间转换为日期时间,反之亦然https://gist.github.com/Mostafa-Hamdy-Elgiar/9714475f1b3bc224ea063af81566d873
>>> filetime_to_dt(ft)
2004-02-12 02:28:57
在时间范围之间使用 timedelta
s 到 add/subtract/divide:
FILE_TIME_EPOCH = datetime.datetime(1601, 1, 1)
FILE_TIME_MICROSECOND = 10 # FILETIME counts 100 nanoseconds intervals = 0.1 microseconds, so 10 of those are 1 microsecond
def convert_from_file_time(file_time):
microseconds_since_file_time_epoch = file_time // FILE_TIME_MICROSECOND
return FILE_TIME_EPOCH + datetime.timedelta(microseconds=microseconds_since_file_time_epoch)
另一面是:
def convert_to_file_time(date_time):
microseconds_since_file_time_epoch = (date_time - FILE_TIME_EPOCH) // datetime.timedelta(microseconds=1)
return microseconds_since_file_time_epoch * FILE_TIME_MICROSECOND