sukhvir
sukhvir

Reputation: 5575

Unable to convert widows epoch time to normal date time

I am trying to parse my Google Chrome Bookmarks file.

Entries in that file have a field called date_added with values like 13195047309383442

I am unable to convert this time to standard date time.

I have tried the following:

import datetime
datetime.fromtimestamp(13195047309383442).strftime('%Y-%m-%d %H:%M:%S')

Unfortunately, this results in an error: OSError: [Errno 22] Invalid argument

My initial reaction was to divide the time by 1000000 as I discovered that Windows stores the epoch time in microseconds. This resulted in following:

datetime.fromtimestamp(13195047309383442/1000000).strftime('%Y-%m-%d %H:%M:%S')

'2388-02-19 21:55:09'

As can be seen above the year field is incorrect.

Where am I going wrong and how do I fix this?

Upvotes: 1

Views: 465

Answers (3)

Chris H
Chris H

Reputation: 101

You mentioned you were trying to parse the Chrome bookmarks file but from what I have read the Chrome bookmark timestamps don't use the Unix 1970 date. It uses the 1601-01-1 date (which is why your date overshoots the Feb 19th 2019 date you might be expecting).

Check out this resource: http://fileformats.archiveteam.org/wiki/Chrome_bookmarks

And this converter tool, it actually has a Python snippet of how the conversion is done. https://www.epochconverter.com/webkit

Upvotes: 1

Stephen Rauch
Stephen Rauch

Reputation: 49812

To convert a windows timestamp to a Python datetime you need to use the Window epoch:

Code:

import datetime as dt
windows_epoch = dt.datetime(year=1601, month=1, day=1)

def datetime_from_window_ts(timestamp):
    return windows_epoch + dt.timedelta(seconds=timestamp/1e6)

Test Code:

print(datetime_from_window_ts(13195047309383442))

Results:

2019-02-19 10:55:09.383442

Upvotes: 1

lenik
lenik

Reputation: 23556

You're using wrong time origin. For Unix it's 1970/1/1, for Windows it's 1601/1/1 or something. You may try his code:

>>> microseconds = 13195047309383442
>>> seconds, microseconds = divmod(microseconds, 1000000)
>>> days, seconds = divmod(seconds, 86400)
>>> datetime.datetime(1601, 1, 1) + datetime.timedelta(days, seconds, microseconds)
datetime.datetime(2019, 2, 19, 10, 55, 9, 383442)

Upvotes: 1

Related Questions