Skip to content

Commit 075e7f7

Browse files
authored
UUIDv7 Draft 3 will simplify node generation (#21)
1 parent 2324d2d commit 075e7f7

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

src/uuid6/__init__.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def time(self) -> int:
6666
| (self.time_hi_version & 0x0FFF)
6767
)
6868
if self.version == 7:
69-
return self.unixts * 10 ** 9 + _subsec_decode(self.subsec)
69+
return self.unixts * 10**9 + _subsec_decode(self.subsec)
7070
return super().time
7171

7272
@property
@@ -75,11 +75,11 @@ def unixts(self) -> int:
7575

7676

7777
def _subsec_decode(value: int) -> int:
78-
return -(-value * 10 ** 9 // 2 ** 30)
78+
return -(-value * 10**9 // 2**30)
7979

8080

8181
def _subsec_encode(value: int) -> int:
82-
return value * 2 ** 30 // 10 ** 9
82+
return value * 2**30 // 10**9
8383

8484

8585
_last_v6_timestamp = None
@@ -127,14 +127,12 @@ def uuid7() -> UUID:
127127
if _last_v7_timestamp is not None and nanoseconds <= _last_v7_timestamp:
128128
nanoseconds = _last_v7_timestamp + 1
129129
_last_v7_timestamp = nanoseconds
130-
timestamp_s, timestamp_ns = divmod(nanoseconds, 10 ** 9)
130+
timestamp_s, timestamp_ns = divmod(nanoseconds, 10**9)
131131
subsec = _subsec_encode(timestamp_ns)
132132
subsec_a = subsec >> 18
133133
subsec_b = (subsec >> 6) & 0x0FFF
134134
subsec_c = subsec & 0x3F
135-
rand = 0
136-
while rand == 0:
137-
rand = secrets.randbits(56)
135+
rand = secrets.randbits(56)
138136
uuid_int = (timestamp_s & 0x0FFFFFFFFF) << 92
139137
uuid_int |= subsec_a << 80
140138
uuid_int |= subsec_b << 64

test/test_uuid6.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import unittest
2+
from datetime import datetime
23
from time import time_ns
34
from unittest.mock import patch
45
from uuid import uuid1
56

67
from uuid6 import DraftUUID, uuid6, uuid7
78

8-
YEAR_IN_NS = 3600 * 24 * 36525 * 10 ** 7
9+
YEAR_IN_NS = 3600 * 24 * 36525 * 10**7
910

1011

1112
class DraftUUIDTests(unittest.TestCase):
@@ -100,10 +101,24 @@ def test_uuid7_far_in_future(self):
100101
def test_time(self):
101102
uuid_1 = uuid1()
102103
uuid_6 = uuid6()
103-
self.assertAlmostEqual(uuid_6.time / 10 ** 7, uuid_1.time / 10 ** 7, 3)
104+
self.assertAlmostEqual(uuid_6.time / 10**7, uuid_1.time / 10**7, 3)
104105
cur_time = time_ns()
105106
uuid_7 = uuid7()
106-
self.assertAlmostEqual(uuid_7.time / 10 ** 9, cur_time / 10 ** 9, 3)
107+
self.assertAlmostEqual(uuid_7.time / 10**9, cur_time / 10**9, 3)
108+
109+
def test_time_zero(self):
110+
uuid_6 = DraftUUID(hex="00000000-0000-6000-8000-000000000000")
111+
self.assertEqual(uuid_6.time, 0)
112+
uuid_7 = DraftUUID(hex="00000000-0000-7000-8000-000000000000")
113+
self.assertEqual(uuid_7.time, 0)
114+
115+
def test_time_max(self):
116+
uuid_6 = DraftUUID(hex="ffffffff-ffff-6fff-bfff-ffffffffffff")
117+
self.assertEqual(uuid_6.time, 1152921504606846975)
118+
uuid_7 = DraftUUID(hex="ffffffff-ffff-7fff-bfff-ffffffffffff")
119+
self.assertEqual(uuid_7.time, 68719476736000000000)
120+
dt = datetime.utcfromtimestamp(uuid_7.time / 10**9)
121+
self.assertEqual(dt, datetime(4147, 8, 20, 7, 32, 16))
107122

108123
def test_uuid7_from_hex(self):
109124
uuid_7 = DraftUUID(hex="061d0edc-bea0-75cc-9892-f6295fd7d295")

0 commit comments

Comments
 (0)