PRTime to datetime in Python

Hi guys, I am writing a script that retrieves information from places.sqlite (history) and realizes that it stores time in PRTime format. Is there a way available in python that could convert this time to date or should I do it myself?

+3
source share
2 answers

PRTime is the number of microseconds from January 1 to 1970 (see https://developer.mozilla.org/en/PRTime ), so just do this to get the UTC time

datetime.datetime(1970, 1, 1) + datetime.timedelta(microseconds=pr_time)

eg.

print datetime.datetime(1970, 1, 1) + datetime.timedelta(microseconds=time.time()*1000*1000)

output:

2010-03-25 13:30:02.243000
+3
source

there is no built-in method, but it looks pretty trivial:

>>> t = 1221842272303080
>>> t /= 1e6
>>> t
1221842272.30308
>>> import datetime
>>> datetime.datetime.fromtimestamp(t)
datetime.datetime(2008, 9, 19, 17, 37, 52, 303080)

As a result, local time, for UTC, you can do:

>>> import time
>>> time.gmtime(t)
time.struct_time(tm_year=2008, tm_mon=9, tm_mday=19, tm_hour=16, tm_min=37, tm_sec=52, tm_wday=4, tm_yday=263, tm_isdst=0)

py3k python 2.x

+1

Source: https://habr.com/ru/post/1738488/


All Articles