简体   繁体   English

将Long Int时间戳转换为日期/时间

[英]Convert Long Int Timestamp to Date/Time

I use connect the sqlite3 of chrome 'History' file for analysis. 我使用连接chrome'History'文件的sqlite3进行分析。 But there are data for last_visit_time, which is timestamp, but I don't know how to change it to real time. 但是存在last_visit_time的数据,它是时间戳,但是我不知道如何将其更改为实时数据。 I test like this: 我这样测试:

2012-04-18 23:22:11.084300 (utctime)

2012-04-19 01:22:11.084300 (myPC time)

12,979,264,931,952,304 (the time stamp in the data)

I printed the utctime and clicked a website, almost at the same time. 我几乎同时打印了utctime并单击了一个网站。 So I get the statistic like above. 所以我得到了上面的统计数据。 12,979,264,931,952,304 is the long int, so normal way to convert is not possible. 12,979,264,931,952,304是long int,因此无法使用正常的转换方法。

How can I convert the timestamp to a date? 如何将时间戳转换为日期?

The timestamp it stores is the number of microseconds since midnight UTC on January 1st, 1601. To convert this to the current time, you can add the number of microseconds you have to the epoch date, with help from this answer , like this: 它存储的时间戳是自1月1日,1601将此转换为当前的时间,你可以添加你需要的划时代日期微秒的数量,以帮助午夜UTC微秒数这个答案 ,就像这样:

>>> import datetime
>>> epoch_start = datetime.datetime(1601, 1, 1)
>>> delta = datetime.timedelta(microseconds=12979264931952304)
>>> epoch_start + delta
datetime.datetime(2012, 4, 18, 23, 22, 11, 952304)

To convert to your local timezone, you can use this method (note that I am currently UTC - 4, while it looks like you are UTC + 2): 要转换为您的本地时区,可以使用以下方法 (请注意,我当前是UTC-4,而看起来您是UTC + 2):

>>> from dateutil import tz
>>> from_zone = tz.tzutc()
>>> to_zone = tz.tzlocal()
>>> utc_time = (epoch_start + delta).replace(tzinfo=from_zone)
>>> utc_time.astimezone(to_zone)
datetime.datetime(2012, 4, 18, 19, 22, 11, 952304, tzinfo=tzlocal())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM