Skip to content

Commit 38d46e3

Browse files
authored
Optimize UUID conversion (#108)
1 parent f4f56de commit 38d46e3

1 file changed

Lines changed: 3 additions & 8 deletions

File tree

src/uuid6/__init__.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,9 @@ def uuid1_to_uuid6(uuid1: uuid.UUID) -> UUID:
8282
r"""Generate a UUID version 6 object from a UUID version 1 object."""
8383
if uuid1.version != 1:
8484
raise ValueError("given UUID's version number must be 1")
85-
timestamp = uuid1.time
86-
time_high_and_time_mid = timestamp >> 12
87-
time_low_and_version = timestamp & 0x0FFF
88-
uuid_int = time_high_and_time_mid << 80
89-
uuid_int |= time_low_and_version << 64
90-
uuid_int |= uuid1.clock_seq << 48
91-
uuid_int |= uuid1.node
92-
return UUID(int=uuid_int, version=6, is_safe=uuid1.is_safe)
85+
h = uuid1.hex
86+
h = h[13:16] + h[8:12] + h[0:5] + "6" + h[5:8] + h[16:]
87+
return UUID(hex=h, is_safe=uuid1.is_safe)
9388

9489

9590
_last_v6_timestamp = None

0 commit comments

Comments
 (0)