Quick question: Is there a pythonic (whether in the standard libraries or not) way to convert unix 32-bit epoch time to windows 64-bit epoch time and back again?
Asked
Active
Viewed 8,105 times
2 Answers
11
You can convert a POSIX timestamp to a datetime
with
>>> tstamp = 1325178061 # right about now
>>> from datetime import datetime
>>> datetime.fromtimestamp(tstamp)
datetime.datetime(2011, 12, 29, 18, 1, 1)
The fromtimestamp
named constructor accepts POSIX timestamps on all platforms (!).
Conversion to a Windows timestamp would be a matter of subtracting the Windows epoch, which Wikipedia says is January 1, 1601, and converting the resulting timedelta
to a number of seconds:
>>> W_EPOCH = datetime(1601, 1, 1)
>>> (datetime.fromtimestamp(tstamp) - W_EPOCH).total_seconds()
12969655261.0
Now you've got a float
that you convert to int
and store as a 64-bit quantity in whichever way you like.

Adam Johnson
- 471
- 1
- 5
- 11

Fred Foo
- 355,277
- 75
- 744
- 836
-
It just gets funny if we have to convert to something other than the Win32 FILETIME format (but I agree it's almost certain that's what the OP wants), because MS was quite.. inventive with timeformats. [See here](http://blogs.msdn.com/b/oldnewthing/archive/2003/09/05/54806.aspx) for a list. Just hope you never have to work with OLE times ugh – Voo Dec 29 '11 at 17:44
-
this is good, but the one thing about this is I'm on 2.6.5, and there is no total_seconds call.... – user1040625 Dec 29 '11 at 17:48
-
Also, Voo, I am indeed using FILETIME format...just to confirm, I would need to convert these seconds to 10^-7 seconds, right? – user1040625 Dec 29 '11 at 18:03
-
@user - yes, those are 100 nanosecond "increments" since Jan 1, 1601. Which is also epoch time zero for COBOL. Maybe where MS got the idea I suppose, from the MS COBOL initiative. – jim mcnamara Dec 29 '11 at 18:37
2
To convert from a Windows EPOCH timestamp to a datetime object (but not the other way around); here's a solution I came up with:
from datetime import datetime, timezone
def convert_from(windows_timestamp: int) -> datetime:
unix_epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)
windows_epoch = datetime(1601, 1, 1, tzinfo=timezone.utc)
epoch_delta = unix_epoch - windows_epoch
windows_timestamp_in_seconds = windows_timestamp / 10_000_000
unix_timestamp = windows_timestamp_in_seconds - epoch_delta.total_seconds()
return datetime.utcfromtimestamp(unix_timestamp)
This allows you to pass in the Windows timestamp as is and it will spit out a valid Python datetime object.
NOTE: This is Python 3 specific.

Jitsusama
- 617
- 7
- 20