不同时间戳总结

最近在实验室帮师兄做一些项目,遇到各种没见过的时间戳,在此总结一下。

 

Unix时间

关于这个时间,大家一般都比较了解,记录了从1970年1月1日0时0分0秒开始到现在的总秒数。

这篇文章中有关于Unix时间和各种常用时间的关系,在这里分享一下。

 

Window NT时间

The 18-digit Active Directory timestamps, also named 'Windows NT time format' and 'Win32 FILETIME or SYSTEMTIME'. These are used in Microsoft Active Directory for pwdLastSet, accountExpires, LastLogon, LastLogonTimestamp and LastPwdSet. The timestamp is the number of 100-nanoseconds intervals (1 nanosecond = one billionth of a second) since Jan 1, 1601 UTC.

 

也就是说,Windows NT时间表示从1602年1月1日UTC时间开始的100纳秒数。引自网站,这个网站还提供了在线转换Windows NT时间到人类可读时间的功能。并提供了Windows cmd 和 Power shell转换Windows NT时间的方法,以下(假设当前Windows NT时间为131194973730000000):

command line:

w32tm.exe /ntte 131194973730000000 

power shell:

(Get-Date 1/1/1601).AddDays(131194973730000000/864000000000)

这个时间大量用在windows NT操作系统中,我就是在获取注册表项的修改时间是注意到的。

Chrome时间:

准确的说是chrome history time format,被用在chrome记录浏览历史的sqlite文件中,用来记录浏览时间。这个时间是否被用在chrome的其他地方,我目前还不知道,等有时间再深入探讨。它的定义是这样的:

the number of microseconds since January 1, 1601 UTC

与Windows NT时间的起始时间相同,但时间单位不同。

Firefox时间:

同样是Firefox history time format,是Firefox历史记录文件(文件格式同样为sqlite)所使用的时间。定义为:

the number of microseconds since January 1, 1970 UTC

与Chrome时间的单位相同,都为微秒级,但起始时间却与Unix时间相同。

Python时间处理模块:

最后介绍用python处理以上不同时间戳所用到的模块。

 

关于Unix时间,首推的当然是time这个模块,在这篇文章有详细介绍,我就不详细展开了。

 

处理其他时间戳就要用到datetime这个模块了。它的基本用法是这样的:

date_string = datetime.datetime(yearmonthdayhour=0minute=0second=0microsecond=0tzinfo=None) +  datetime.timedelta(days=0seconds=0microseconds=0milliseconds=0minutes=0hours=0weeks=0)

比如计算Windows NT时间、Chrome时间、Firefox时间可以这样(假设Windows NT时间为131194973730000000,chrome时间为13106382734598133,Firefox时间为1461852498963000):

1 import datetime
2 
3 win_nt =  datetime.datetime( 1601, 1, 1 ) + datetime.timedelta( microseconds=131194973730000000//10 )
4 chrome = datetime.datetime( 1601, 1, 1 ) + datetime.timedelta( microseconds=13106382734598133 )
5 firefox = datetime.datetime( 1970, 1, 1 ) + datetime.timedelta( microseconds=1461852498963000 )
6 print("Window NT time : " + str(win_nt)) 7 print("Chrome time : " + str(chrome)) 8 print("Firefox time : " + str(firefox))

 

结果为:

Window NT time : 2016-09-28 00:49:33
Chrome time : 2016-04-29 05:52:14.598133
Firefox time : 2016-04-28 14:08:18.963000

 

posted on 2016-09-28 09:52  YoungForest  阅读(3002)  评论(0编辑  收藏  举报