Skip to content

Instantly share code, notes, and snippets.

@gamesbook
Last active July 2, 2024 08:31
Show Gist options
  • Save gamesbook/03d030b7b79370fb6b2a67163a8ac3b5 to your computer and use it in GitHub Desktop.
Save gamesbook/03d030b7b79370fb6b2a67163a8ac3b5 to your computer and use it in GitHub Desktop.
Convert C# tick dates into a Python / readable date
# -*- coding: utf-8 -*-
"""
Purpose: Convert .NET ticks to formatted ISO8601 time
Author: D Hohls < [email protected]>
"""
from __future__ import print_function
import datetime
import sys
def convert_dotnet_tick(ticks):
"""Convert .NET ticks to formatted ISO8601 time
Args:
ticks: integer
i.e 100 nanosecond increments since 1/1/1 AD"""
_date = datetime.datetime(1, 1, 1) + \
datetime.timedelta(microseconds=ticks // 10)
if _date.year < 1900: # strftime() requires year >= 1900
_date = _date.replace(year=_date.year + 1900)
return _date.strftime("%Y-%m-%dT%H:%M:%S.%fZ")[:-3]
if __name__ == "__main__":
try:
print(convert_dotnet_tick(int(sys.argv[1])))
except:
print("Missing or invalid argument; use, e.g.:"
" python ticks.py 636245666750411542")
print("with result: %s " % convert_dotnet_tick( 636245666750411542))
@aidevlabuser
Copy link

Thanks!

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