您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

从python中的UUID v1中提取时间

从python中的UUID v1中提取时间

在/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

解决time.time()的方程,您将获得

time.time()-like quantity = ((timestamp - 0x01b21dd213814000L)*100/1e9)

因此使用:

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)

这给出了与生成的UUID关联的日期时间uuid.uuid1

python 2022/1/1 18:32:51 有217人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶