pg_uuidv7-compatible UUID version 7 generation
Project description
edwh-uuid7
A Python library that implements UUIDv7 generation and parsing.
It supports conversion to and from datetime values, ensuring full sorting compatibility with UUIDs produced by PostgreSQL’s pg_uuidv7 extension, PostgreSQL 18’s built-in uuid7, and Python 3.14’s uuid.uuid7.
Unlike the standard uuid.uuid7, this library also allows generating UUIDs for specific timestamps.
Overview
UUIDv7 is a time-sortable UUID format composed of a timestamp and random bits. Our implementation follows the UUIDv7 specification, which provides flexibility in ensuring monotonicity. We utilize microseconds for sub-millisecond precision within the timestamp; this acts as a stateless monotonic counter for UUIDs generated within the same millisecond and leaves more room for entropy compared to using nanoseconds.
UUIDv7 Layout (128 bits)
| Bits | Size (bits) | Field | Description |
|---|---|---|---|
| 0–47 | 48 | timestamp (ms) | Unix timestamp in milliseconds (sortable) |
| 48–51 | 4 | version | UUID version (0b0111 for v7) |
| 52–71 | 20 | sub-ms (μs) | Sub-millisecond fraction (0–999,999) |
| 72–127 | 56 | random | Random bits (includes UUID variant information) |
This design makes UUIDv7 globally sortable by creation time (1 μs resolution) while retaining enough entropy for distributed uniqueness.
Key Features of edwh-uuid7:
- Compatible with
pg_uuidv7’suuid_generate_v7(),uuid_v7_to_timestamptz(),uuid_timestamptz_to_v7()functions. - Sorting compatible with Python 3.14's
uuid.uuid7() - Provides conversion between
UUIDv7anddatetime. - Supports timezone-aware and naive datetime conversions.
Why this package?
The widely-used uuid7 Python package uses
a different timestamp scheme, which is not compatible with sorting behavior as implemented in the PostgreSQL
pg_uuidv7 extension. This package ensures compatibility, ensuring that UUIDv7s generated will sort correctly alongside
those produced by pg_uuidv7.
Comparison with Python 3.14's uuid7 functionality
Key differences between this library and Python 3.14's built-in uuid7:
- Timestamp Implementation: This library works with μs (microseconds), while Python 3.14+ uses a counter mechanism
- Conversion Utilities: This library provides uuid7 ↔ datetime conversion, which isn't available in the standard library implementation (at the time of writing)
- PostgreSQL Compatibility: Both implementations are sorting compatible with the
pg_uuidv7PostgreSQL extension
Installation
pip install edwh-uuid7
Usage
Here are examples of how to use edwh-uuid7:
Generating a UUIDv7
from edwh_uuid7 import uuid7 # or `uuid_generate_v7`
uuid = uuid7()
print(f"Generated UUIDv7: {uuid}")
Converting a UUIDv7 to a datetime
from edwh_uuid7 import uuid7, uuid7_to_datetime # or uuid_generate_v7, uuid_v7_to_timestamptz
uuid = uuid7() # UUID('0196905b-4434-74ef-aa27-442be7069beb')
datetime_value = uuid7_to_datetime(uuid) # datetime.datetime(2025, 5, 2, 9, 37, 2, 516000, tzinfo=datetime.timezone.utc)
print(f"Corresponding datetime: {datetime_value}") # 2025-05-02 09:37:02.516000+00:00
High-precision mode
This library provides enhanced timestamp precision capabilities:
- Default Precision: By default, uuid7 supports millisecond precision
- Enhanced Recovery: When working with UUIDs created by this library (
uuid7()oruuid7(timestamp_ns=...)), you can enablehigh_precision=Trueto recover datetime values with microsecond accuracy
Example:
now = datetime.fromtimestamp(time.time()) # datetime.datetime(2025, 5, 9, 17, 53, 50, 514106)
uuid = datetime_to_uuid7(now) # UUID('0196b5c0-c0b2-719e-802c-9964c4992a66')
uuid7_to_datetime(uuid) # datetime(2025, 5, 9, 15, 53, 50, 514000, tzinfo=datetime.timezone.utc)
uuid7_to_datetime(uuid, high_precision=True) # datetime(2025, 5, 9, 15, 53, 50, 514106, tzinfo=datetime.timezone.utc)
Converting a datetime to a UUIDv7
from edwh_uuid7.core import datetime_to_uuid7 # or uuid_timestamptz_to_v7
from datetime import datetime, timezone
dt = datetime(2025, 5, 2, 9, 37, 2, 516000, tzinfo=timezone.utc)
uuid_from_dt = datetime_to_uuid7(dt) # UUID('0196905b-4434-7000-8012-620314c8b88b')
print(f"UUIDv7 from datetime: {uuid_from_dt}") # 0196905b-4434-7000-8012-620314c8b88b
Extracting a timezone-aware datetime
from edwh_uuid7 import uuid7, uuid7_to_datetime # or uuid_generate_v7, uuid_v7_to_timestamptz
from zoneinfo import ZoneInfo
uuid = uuid7() # UUID('0196905f-0339-769a-9f0b-8fedc645a688')
datetime_value = uuid7_to_datetime(uuid, tz=ZoneInfo("Europe/Amsterdam")) # datetime.datetime(2025, 5, 2, 11, 41, 8, 25000, tzinfo=zoneinfo.ZoneInfo(key='Europe/Amsterdam'))
print(f"Generated UUID: {uuid}") # 0196905f-0339-769a-9f0b-8fedc645a688
print(f"Timezone-aware datetime: {datetime_value}") # 2025-05-02 11:41:08.025000+02:00
Caveats
While microseconds are used to ensure correct sorting, accuracy is limited (truncated) when converting back to
datetime, consistent with the behavior of the pg_uuidv7 function uuid_timestamptz_to_v7.
For example:
Input: 2025-05-02 08:46:48.307329+00:00
UUID conversion: 0196902d-45f3-7505-8038-7795a5ec9a1b (example)
Converted Back: 2025-05-02 08:46:48.307000+00:00
This truncation is by design (in favor of entropy) and aligns with the PostgreSQL pg_uuidv7 implementation.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file edwh_uuid7-0.2.3.tar.gz.
File metadata
- Download URL: edwh_uuid7-0.2.3.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3354ba9be573aaacfdf82f94245df41caebfecf1db9492a9cbe77b6f6e0e100
|
|
| MD5 |
83cbc2e5846d7ca0ad710c4bbfbc96df
|
|
| BLAKE2b-256 |
a95385eb9f73e804eb9bec08d8092e9f5570945a5a6c9223630dbd41a59270fe
|
File details
Details for the file edwh_uuid7-0.2.3-py3-none-any.whl.
File metadata
- Download URL: edwh_uuid7-0.2.3-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a2c270438af7109ba415211d190c22505fa25620cba58836b3ba3f19ef21747
|
|
| MD5 |
65b4257760b40f6d9ec2b8f6f60061d1
|
|
| BLAKE2b-256 |
fdd3798515d597e495985f251c57ee9b550188f896f1bb5ef71838522f753842
|