最近在實驗室幫師兄做一些項目,遇到各種沒見過的時間戳,在此總結一下。
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(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None) + datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=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
