Skip to content
Read a UUIDv7 timestamp on Python 3.12 and 3.13

Read a UUIDv7 timestamp on Python 3.12 and 3.13

November 23, 2026

An existing UUIDv7 stores a Unix timestamp in its leading 48 bits. Python 3.12 and 3.13 can parse the UUID and extract that integer even though these versions do not provide a UUIDv7 generator.

Run the example

Tested on Linux with Python 3.12.14 and Python 3.13.5. Use one Bash session and a text editor. Create a disposable working directory before saving the source:

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Save as demo_python_uuid7_extract_unix_milliseconds.py:

from datetime import datetime, timedelta, timezone
from uuid import UUID, RFC_4122

def unix_milliseconds(text):
    value = UUID(text)
    if value.variant != RFC_4122 or value.version != 7:
        raise ValueError("expected an RFC-variant UUIDv7")
    return value.int >> 80

sample = "017f22e2-79b0-7cc3-98c4-dc0c0c07398f"
milliseconds = unix_milliseconds(sample)
assert milliseconds == 1645557742000
instant = datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(milliseconds=milliseconds)
print("Unix milliseconds:", milliseconds)
print("UTC:", instant.isoformat(timespec="milliseconds"))
try:
    unix_milliseconds("67e55044-10b1-426f-9247-bb680e5fe0c8")
except ValueError as error:
    print("Rejected:", error)
else:
    raise AssertionError("UUIDv4 accepted")

Run it:

python3 -B demo_python_uuid7_extract_unix_milliseconds.py

Observed output:

Unix milliseconds: 1645557742000
UTC: 2022-02-22T19:22:22.000+00:00
Rejected: expected an RFC-variant UUIDv7

The sample is the RFC 9562 Appendix A.6 test vector. Shifting the 128-bit integer right by 80 leaves exactly its leading 48 timestamp bits. The checks reject another UUID version before interpreting those bits. Integer milliseconds avoid a floating-point timestamp conversion.

Limits and input policy

Do not use the older UUID .time interpretation for this task on Python 3.12 or 3.13. This reads an encoded timestamp; it does not prove when an event happened, validate the issuer, or generate a new UUID. UUID timestamps are not secrets. The extraction covers the full 48-bit field, but converting arbitrary values to Python datetime can exceed its supported calendar range.

Clean up

Remove the saved script and the empty working directory in the same Bash session:

rm -- demo_python_uuid7_extract_unix_milliseconds.py
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

If the directory is not empty, inspect its contents before removing anything else.

Reference

Official documentation

Acknowledgements

This post was created with assistance from AI.

Last updated on