Skip to content

Instantly share code, notes, and snippets.

@gamesbook
Last active February 3, 2024 13:20
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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 < dhohls@csir.co.za>
"""
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