Skip to content

Instantly share code, notes, and snippets.

@CarstenSchelp
Created November 19, 2019 15:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarstenSchelp/b6fb490e8cc65e3ec7d62b950d1f3ad8 to your computer and use it in GitHub Desktop.
Save CarstenSchelp/b6fb490e8cc65e3ec7d62b950d1f3ad8 to your computer and use it in GitHub Desktop.
Convert .net / C# time ticks to posix or python datetime
'''
Convert .net-style ticks to posix or python datetime.
.net ticks (like in C#) are 100 nanosecond counts
starting from january 1st in year 0001
This one is not very thorougly tested but worked for me when I needed it.
'''
import datetime
_epoch = datetime.date(1970, 1 , 1)
_dotnet_minvalue = datetime.date(1, 1, 1)
_offset = (_epoch - _dotnet_minvalue).total_seconds()
_ten_million = 10000000
def ticks_to_posix(dotnet_ticks):
seconds = dotnet_ticks / _ten_million;
posix = seconds - _offset
return posix
def ticks_to_datetime(dotnet_ticks):
posix = ticks_to_posix(dotnet_ticks)
return datetime.datetime.fromtimestamp(posix)
# demo
dotnet_ticks = 637074993332500000
print("dotnet_ticks: ", dotnet_ticks)
print("posix: ", ticks_to_posix(dotnet_ticks))
print("datetime: ", ticks_to_datetime(dotnet_ticks))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment