Skip to content

Instantly share code, notes, and snippets.

@NotWearingPants
Last active January 15, 2022 12:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NotWearingPants/d162aaf32aef0227bf6bbd37b7317633 to your computer and use it in GitHub Desktop.
Save NotWearingPants/d162aaf32aef0227bf6bbd37b7317633 to your computer and use it in GitHub Desktop.
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),
# )
@jleclanche
Copy link

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment