Last active
January 15, 2022 12:46
-
-
Save NotWearingPants/d162aaf32aef0227bf6bbd37b7317633 to your computer and use it in GitHub Desktop.
Python code to convert from/to Windows FILETIME (https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
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 | |
# NOTE: for some reason Python limits the year to 4 digits, so the maximum supported value for FILETIME is 0x24c85a5ed1c03fff | |
# calculated by: hex(convert_to_file_time(datetime.datetime.max) + (FILE_TIME_MICROSECOND - 1)) | |
# for bigger values Python will throw `OverflowError: date value out of range` | |
# To use with construct: | |
# import construct | |
# WindowsFileTime = construct.ExprAdapter(construct.Int64ul, | |
# lambda obj, context: convert_from_file_time(obj), | |
# lambda obj, context: convert_to_file_time(obj), | |
# ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello if you're arriving from Google. Note that I published a (public domain / CC0) python package for the same functionality as
winfiletime
.https://github.com/jleclanche/winfiletime
https://pypi.org/project/winfiletime/