Skip to content

Instantly share code, notes, and snippets.

@code2319
Created February 19, 2023 08:51
Show Gist options
  • Save code2319/82dd862dc2f26afef70658665d54ce82 to your computer and use it in GitHub Desktop.
Save code2319/82dd862dc2f26afef70658665d54ce82 to your computer and use it in GitHub Desktop.
"""
18-digit LDAP/Win32 FILETIME timestamp to human-readable date and
"""
import datetime
def mstimestamp_to_datetime(mstimestamp: int) -> datetime.datetime:
magic_number = 11_644_473_600
shift = 10_000_000
timestamp = (mstimestamp / shift) - magic_number
return datetime.datetime.fromtimestamp(timestamp)
def datetime_to_mstimestamp(dt: datetime.datetime) -> int:
timestamp = int(dt.timestamp())
magic_number = 116_444_736_000_000_000
shift = 10_000_000
return (timestamp * shift) + magic_number
print(datetime.datetime.now())
print(datetime_to_mstimestamp(datetime.datetime.now()))
print(mstimestamp_to_datetime(datetime_to_mstimestamp(datetime.datetime.now())))
"""
2023-02-19 11:49:35.034931
133212701750000000
2023-02-19 11:49:35
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment