28

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.

cdecker
  • 4,515
  • 8
  • 46
  • 75

4 Answers4

39

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.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Looks like you solved my problem. In the meantime I had to go back and create my own constructor since it was generating only the current timestamps, and I needed random past timestamps. Thanks for your quick answer :-) – cdecker Sep 26 '10 at 14:08
  • 1
    interesting, uuid epoch refers to the Julian calendar.. (facepalm) – ezdazuzena May 22 '14 at 08:21
  • 3
    @ezdazuzena Actually, Gregorian (see [RFC 4122, section 4.1.4](https://tools.ietf.org/html/rfc4122#section-4.1.4) about Gregorian reform to the Christian calendar). This is totally arbitrary, of course, but so is the UNIX epoch. I challenge you to pick a better one, explain why your decision is not just as arbitrary, *and* demonstrate a need to change the existing standard (and the slew of dependent and indirectly dependent ones) to follow yours instead. – zxq9 May 10 '15 at 04:00
19

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)
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
12

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()
Joseph Lust
  • 19,340
  • 7
  • 85
  • 83
  • I tried calling `print(time_uuid.TimeUUID(bytes=uuid.uuid1().bytes).get_datetime())` constantly but I saw the time only changes every 2 secs. It doesn't seem to calculate precisely. – Ruben Alves Jan 19 '21 at 12:57
  • But I see that uuid1().time has different values each time it is called, which is what I expected. Given that I only wanted to know which one was created first, comparing them by `uuid1().time` did the trick for me. – Ruben Alves Jan 19 '21 at 13:07
  • 3
    I would not recommend adding a dependency for what amounts to one line of code. – Ninjakannon Jan 26 '22 at 23:45
3

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)
jchysk
  • 1,538
  • 1
  • 15
  • 27
iamthedrake
  • 2,239
  • 1
  • 16
  • 13