从Python中的UUID v1中提取时间
我的程序中随机生成了一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看 /usr/lib/python2.6/uuid.py 内部,您将看到
求解 time.time() 的方程,您将得到
所以使用:
这给出了与 uuid 生成的 UUID 关联的日期时间。 uuid1。
Looking inside /usr/lib/python2.6/uuid.py you'll see
solving the equations for time.time(), you'll get
So use:
This gives the datetime associated with a UUID generated by
uuid.uuid1
.您可以使用直接定义的简单公式:
You could use a simple formula that follows directly from the definition:
或者只使用 TimeUUID 库,这样你就知道你没有弄错数学
示例
Or just use the TimeUUID library, so that you know you didn't get the math wrong
Example
由于我安装了 Cassandra 并且将其与 Cassandra 一起使用,因此我能够使用 cassandra.util 中的 datetime_from_uuid1
Since I have Cassandra installed and I am using this with Cassandra I was able to use the datetime_from_uuid1 from cassandra.util