开发者

PRTime to datetime in Python

开发者 https://www.devze.com 2022-12-24 07:15 出处:网络
I am writing a script that is retrieving in开发者_如何学编程formation from file places.sqlite (history) and realized that it stores the time in the PRTime format. Is there a method available in Python

I am writing a script that is retrieving in开发者_如何学编程formation from file places.sqlite (history) and realized that it stores the time in the PRTime format. Is there a method available in Python which could convert this date time or do I have to make it myself?


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

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

For example,

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

Output:

2010-03-25 13:30:02.243000


There isn't any built-in method, but it does seem quite trivial to implement:

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

The result is local time. For UTC you could 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)

That's py3k output, and it might differ slightly in Python 2.x.

0

精彩评论

暂无评论...
验证码 换一张
取 消