In [1]:
import datetime def analyze(s: str): s = s.replace("-", "") print("Hex form:", s) n = int.from_bytes(bytes.fromhex(s), "big") version = (n >> (9 * 8 + 4)) & 0x0F print("Version:", version) variant = (n >> (7 * 8 + 6)) & 0x2 print("Variant:", variant) millis = (n >> (10 * 8)) print("Timestamp:", millis // 1000) dt = datetime.datetime.fromtimestamp(millis / 1000) print("Datetime:", dt.isoformat()) random_a = (n >> (8 * 8)) & 0xFFF print("Random A:", random_a, hex(random_a).zfill(3)) random_b = n & 0x3fffffffffffffff print("Random B:", random_b, hex(random_b).zfill(16)) def test(desc, uuid): print(desc) analyze(uuid) print() test("UUID v7 Example", "018c69b4-0be1-7530-8693-266ca9016c48") test("Leo's UUID v7", "018c69e5-a66d-7000-836f-14a4ea4b106d") test("UUID v4 Example", "ca9a0f58-76d5-4f45-8c0e-a2d8b2f24234") test("RFC UUID v7 Example", "017F22E2-79B0-7CC3-98C4-DC0C0C07398F")
Out:
UUID v7 Example Hex form: 018c69b40be175308693266ca9016c48 Version: 7 Variant: 2 Timestamp: 1702580456 Datetime: 2023-12-14T22:00:56.417000 Random A: 1328 0x530 Random B: 473764633937800264 0x693266ca9016c48 Leo's UUID v7 Hex form: 018c69e5a66d7000836f14a4ea4b106d Version: 7 Variant: 2 Timestamp: 1702583707 Datetime: 2023-12-14T22:55:07.245000 Random A: 0 0x0 Random B: 247439203066646637 0x36f14a4ea4b106d UUID v4 Example Hex form: ca9a0f5876d54f458c0ea2d8b2f24234 Version: 4 Variant: 2 Timestamp: 222763031230 Datetime: 9029-01-29T22:47:10.165009 Random A: 3909 0xf45 Random B: 868810829727941172 0xc0ea2d8b2f24234 RFC UUID v7 Example Hex form: 017F22E279B07CC398C4DC0C0C07398F Version: 7 Variant: 2 Timestamp: 1645557742 Datetime: 2022-02-22T22:22:22 Random A: 3267 0xcc3 Random B: 1784793296645077391 0x18c4dc0c0c07398f