aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/uuid.py
diff options
context:
space:
mode:
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>2025-03-31 14:32:54 +0200
committerGitHub <noreply@github.com>2025-03-31 14:32:54 +0200
commitba11f45dd969dfb039dfb47270de4f8c6a03d241 (patch)
treece94bd7282a6eff4ce5a1841983b1daeff97b7b8 /Lib/uuid.py
parentbab1398a47f6d0cfc1be70497f306874c749ef7c (diff)
downloadcpython-ba11f45dd969dfb039dfb47270de4f8c6a03d241.tar.gz
cpython-ba11f45dd969dfb039dfb47270de4f8c6a03d241.zip
gh-130843: expose 48-bit timestamp for UUIDv7 (#131838)
Diffstat (limited to 'Lib/uuid.py')
-rw-r--r--Lib/uuid.py19
1 files changed, 15 insertions, 4 deletions
diff --git a/Lib/uuid.py b/Lib/uuid.py
index 984981512d6..2c16c3f0f5a 100644
--- a/Lib/uuid.py
+++ b/Lib/uuid.py
@@ -134,9 +134,16 @@ class UUID:
fields a tuple of the six integer fields of the UUID,
which are also available as six individual attributes
- and two derived attributes. The time_* attributes are
- only relevant to version 1, while the others are only
- relevant to versions 1 and 6:
+ and two derived attributes. Those attributes are not
+ always relevant to all UUID versions:
+
+ The 'time_*' attributes are only relevant to version 1.
+
+ The 'clock_seq*' and 'node' attributes are only relevant
+ to versions 1 and 6.
+
+ The 'time' attribute is only relevant to versions 1, 6
+ and 7.
time_low the first 32 bits of the UUID
time_mid the next 16 bits of the UUID
@@ -145,7 +152,8 @@ class UUID:
clock_seq_low the next 8 bits of the UUID
node the last 48 bits of the UUID
- time the 60-bit timestamp
+ time the 60-bit timestamp for UUIDv1/v6,
+ or the 48-bit timestamp for UUIDv7
clock_seq the 14-bit sequence number
hex the UUID as a 32-character hexadecimal string
@@ -366,6 +374,9 @@ class UUID:
time_hi = self.int >> 96
time_lo = (self.int >> 64) & 0x0fff
return time_hi << 28 | (self.time_mid << 12) | time_lo
+ elif self.version == 7:
+ # unix_ts_ms (48) | ... (80)
+ return self.int >> 80
else:
# time_lo (32) | time_mid (16) | ver (4) | time_hi (12) | ... (64)
#