Last active
June 4, 2018 14:07
-
-
Save codingoutloud/6800434 to your computer and use it in GitHub Desktop.
How many ticks since the first moment of 01-01-0001, the first day of the epoch used in Python (and .NET) date time libraries? This python program snippet calculates this. A Tick is defined (matching .NET's DateTime.UtcNow.Ticks property) as 1 / 10,000,000 of a Second. This conversion might be useful if you need interoperability across .NET lang…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bill Wilder (@codingoutloud), 02-Oct-2013 | |
# Original: https://gist.github.com/codingoutloud/6800434 | |
import datetime | |
def ticks_since_epoch(start_time_override = None): | |
""" | |
Calculates number of Ticks since Jan 1, 0001 epoch. Uses current time unless another time is supplied. | |
Mimics behavior of System.DateTime.UtcNow.Ticks from.NET with 10 million Ticks per second. | |
http://msdn.microsoft.com/en-us/library/system.datetime.ticks.aspx | |
@return: Number of Ticks since Jan 1, 0001 epoch (earliest date supported by Python datetime feature) | |
""" | |
if (start_time_override is None): | |
start_time = datetime.datetime.utcnow() | |
else: | |
start_time = start_time_override | |
ticks_per_ms = 10000 | |
ms_per_second = 1000 | |
ticks_per_second = ticks_per_ms * ms_per_second | |
span = start_time - datetime.datetime(1, 1, 1) | |
ticks = int(span.total_seconds() * ticks_per_second) | |
return ticks | |
print ticks_since_epoch() | |
print ticks_since_epoch(datetime.datetime(2013, 10, 24, 10, 0, 30)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment