从Python中的UUID v1中提取时间

发布于 2024-09-24 14:41:42 字数 110 浏览 2 评论 0原文

我的程序中随机生成了一些 UUID,但我希望能够提取生成的 UUID 的时间戳以用于测试目的。我注意到使用 fields 访问器我可以获取时间戳的各个部分,但我不知道如何组合它们。

I have some UUIDs that are being generated in my program at random, but I want to be able to extract the timestamp of the generated UUID for testing purposes. I noticed that using the fields accessor I can get the various parts of the timestamp but I have no idea on how to combine them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

睫毛溺水了 2024-10-01 14:41:42

查看 /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。

Looking inside /usr/lib/python2.6/uuid.py you'll see

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

solving the equations for time.time(), you'll get

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

So use:

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)

This gives the datetime associated with a UUID generated by uuid.uuid1.

稀香 2024-10-01 14:41:42

您可以使用直接定义的简单公式:

时间戳是一个 60 位值。对于 UUID 版本 1,这是
以协调世界时 (UTC) 表示,计数为 100-
自 1582 年 10 月 15 日 00:00:00.00(日期
公历对基督教历法的改革)。

>>> 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)

You could use a simple formula that follows directly from the definition:

The timestamp is a 60-bit value. For UUID version 1, this is
represented by Coordinated Universal Time (UTC) as a count of 100-
nanosecond intervals since 00:00:00.00, 15 October 1582 (the date of
Gregorian reform to the Christian calendar).

>>> 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)
故人如初 2024-10-01 14:41:42

或者只使用 TimeUUID 库,这样你就知道你没有弄错数学

示例

import uuid
import time_uuid

my_uuid = uuid.UUID('{12345678-1234-5678-1234-567812345678}')
ts = time_uuid.TimeUUID(bytes=my_uuid.bytes).get_timestamp()

Or just use the TimeUUID library, so that you know you didn't get the math wrong

Example

import uuid
import time_uuid

my_uuid = uuid.UUID('{12345678-1234-5678-1234-567812345678}')
ts = time_uuid.TimeUUID(bytes=my_uuid.bytes).get_timestamp()
梦行七里 2024-10-01 14:41:42

由于我安装了 Cassandra 并且将其与 Cassandra 一起使用,因此我能够使用 cassandra.util 中的 datetime_from_uuid1

>>> import uuid
>>> from cassandra.util import datetime_from_uuid1
>>> foo = uuid.uuid1()
>>> dt_foo = datetime_from_uuid1(foo)
>>> dt_foo
datetime.datetime(2016, 07, 26, 8, 2, 12, 104560)

Since I have Cassandra installed and I am using this with Cassandra I was able to use the datetime_from_uuid1 from cassandra.util

>>> import uuid
>>> from cassandra.util import datetime_from_uuid1
>>> foo = uuid.uuid1()
>>> dt_foo = datetime_from_uuid1(foo)
>>> dt_foo
datetime.datetime(2016, 07, 26, 8, 2, 12, 104560)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文