cde*_*ker 18 python uuid timestamp
我有一些UUID在我的程序中随机生成,但我希望能够提取生成的UUID的时间戳以用于测试目的.我注意到使用fields访问器我可以得到时间戳的各个部分,但我不知道如何组合它们.
unu*_*tbu 28
查看/usr/lib/python2.6/uuid.py里面你会看到
def uuid1(node=None, clock_seq=None):
...
nanoseconds = int(time.time() * 1e9)
# 0x01b21dd213814000 is the number of 100-ns intervals between the
# UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
timestamp = int(nanoseconds/100) + 0x01b21dd213814000L
Run Code Online (Sandbox Code Playgroud)
解决time.time()的方程,你会得到
time.time()-like quantity = ((timestamp - 0x01b21dd213814000L)*100/1e9)
Run Code Online (Sandbox Code Playgroud)
所以使用:
In [3]: import uuid
In [4]: u = uuid.uuid1()
In [58]: datetime.datetime.fromtimestamp((u.time - 0x01b21dd213814000L)*100/1e9)
Out[58]: datetime.datetime(2010, 9, 25, 17, 43, 6, 298623)
Run Code Online (Sandbox Code Playgroud)
这给出了与生成的UUID相关联的日期时间uuid.uuid1.
您可以使用直接来自定义的简单公式:
时间戳是60位值.对于UUID版本1,这由协调世界时(UTC)表示为自1582年10月15日00:00:00.00(格里高利改革至基督教历法的日期)以100纳秒间隔的计数.
>>> from uuid import uuid1
>>> from datetime import datetime, timedelta
>>> datetime(1582, 10, 15) + timedelta(microseconds=uuid1().time//10)
datetime.datetime(2015, 11, 13, 6, 59, 12, 109560)
Run Code Online (Sandbox Code Playgroud)
例
import uuid
import time_uuid
my_uuid = uuid.UUID('{12345678-1234-5678-1234-567812345678}')
ts = time_uuid.TimeUUID(bytes=my_uuid.bytes).get_timestamp()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12996 次 |
| 最近记录: |