Skip to content

Commit fba4f04

Browse files
authored
Add optional node parameter to uuid6() (#109)
1 parent 38d46e3 commit fba4f04

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ If your use case requires greater granularity than UUID version 7 can provide, y
5252

5353
Generate a UUID version 6 object from a UUID version 1 object.
5454

55-
### uuid6.uuid6(*clock_seq=None*)
55+
### uuid6.uuid6(*node=None*, *clock_seq=None*)
5656

57-
Generate a UUID from a random number, sequence number, and the current time. If *clock_seq* is given, it is used as the sequence number; otherwise a random 14-bit sequence number is chosen.
57+
Generate a UUID from a host ID, sequence number, and the current time. If *node* is not given, a random 48-bit number is chosen. If *clock_seq* is given, it is used as the sequence number; otherwise a random 14-bit sequence number is chosen.
5858

5959
### uuid6.uuid7()
6060

src/uuid6/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ def uuid1_to_uuid6(uuid1: uuid.UUID) -> UUID:
9292
_last_v8_timestamp = None
9393

9494

95-
def uuid6(clock_seq: Optional[int] = None) -> UUID:
95+
def uuid6(node: Optional[int] = None, clock_seq: Optional[int] = None) -> UUID:
9696
r"""UUID version 6 is a field-compatible version of UUIDv1, reordered for
9797
improved DB locality. It is expected that UUIDv6 will primarily be
9898
used in contexts where there are existing v1 UUIDs. Systems that do
9999
not involve legacy UUIDv1 SHOULD consider using UUIDv7 instead.
100100
101+
If 'node' is not given, a random 48-bit number is chosen.
102+
101103
If 'clock_seq' is given, it is used as the sequence number;
102104
otherwise a random 14-bit sequence number is chosen."""
103105

@@ -112,12 +114,14 @@ def uuid6(clock_seq: Optional[int] = None) -> UUID:
112114
_last_v6_timestamp = timestamp
113115
if clock_seq is None:
114116
clock_seq = secrets.randbits(14) # instead of stable storage
117+
if node is None:
118+
node = secrets.randbits(48)
115119
time_high_and_time_mid = (timestamp >> 12) & 0xFFFFFFFFFFFF
116120
time_low_and_version = timestamp & 0x0FFF
117121
uuid_int = time_high_and_time_mid << 80
118122
uuid_int |= time_low_and_version << 64
119123
uuid_int |= (clock_seq & 0x3FFF) << 48
120-
uuid_int |= secrets.randbits(48)
124+
uuid_int |= node & 0xFFFFFFFFFFFF
121125
return UUID(int=uuid_int, version=6)
122126

123127

test/test_vectors.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ def test_uuid6_from_int(self):
4444
self.assertEqual(str(uuid_6), "1ec9414c-232a-6b00-b3c8-9e6bdeced846")
4545

4646
@patch("uuid6._last_v6_timestamp", 1)
47-
@patch("secrets.randbits", return_value=0x9E6BDECED846)
4847
@patch("time.time_ns", return_value=0x16D6320C3D4DCC00)
49-
def test_uuid6_hex_from_time(self, mocktime, mockrand):
50-
uuid_6 = uuid6(0x3C8 | int("11", 2) << 12)
48+
def test_uuid6_hex_from_time(self, mocktime):
49+
uuid_6 = uuid6(node=0x9E6BDECED846, clock_seq=0x3C8 | int("11", 2) << 12)
5150
self.assertEqual(str(uuid_6), "1ec9414c-232a-6b00-b3c8-9e6bdeced846")
5251

5352
def test_uuid6_time_from_hex(self):

0 commit comments

Comments
 (0)