|
1 | | -See <https://buildkite.com/blog/goodbye-integers-hello-uuids> |
| 1 | +# [zig-uuidv7](https://github.com/coolaj86/zig-uuidv7) |
2 | 2 |
|
3 | | - |
| 3 | +Generate UUID v7 strings, like `019212d3-87f4-7d25-902e-b8d39fe07f08`. |
| 4 | + |
| 5 | +```sh |
| 6 | +uuidv7 > uuidv7.txt |
| 7 | +``` |
| 8 | + |
| 9 | +```text |
| 10 | +019212d3-87f4-7d25-902e-b8d39fe07f08 |
| 11 | +``` |
| 12 | + |
| 13 | +| 32-bit time | 16-bit time | 4 ver + 12 rnd | 2 var + 14 rnd | 16 rnd + 32 rnd | |
| 14 | +| :------------------------: | :---------: | :------------: | :------------: | :----------------------------------------: | |
| 15 | +| 01 92 12 d3 | 87 f4 | 7d 25 | 90 2e | b8 d3 9f e0 7f 08 | |
| 16 | + |
| 17 | +# Table of Contents |
| 18 | + |
| 19 | +- [UUIDv7 Spec](#uuidv7-spec) |
| 20 | + - [By the Characters](#by-the-characters) |
| 21 | + - [By the Bits](#by-the-bits) |
| 22 | +- [Build](#build) |
| 23 | +- [License](#license) |
| 24 | + |
| 25 | +# UUIDv7 Spec |
| 26 | + |
| 27 | +## By the Characters |
| 28 | + |
| 29 | +There are 36 characters total: 32 hex (`0123456789abcdef`) + 4 dashes (`-`) |
4 | 30 |
|
5 | 31 | ```text |
6 | | - 0 1 2 3 |
7 | | - 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
8 | | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
9 | | - | unix_ts_ms | |
10 | | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
11 | | - | unix_ts_ms | ver | rand_a | |
12 | | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
13 | | - |var| rand_b | |
14 | | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
15 | | - | rand_b | |
16 | | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
17 | | - ```` |
| 32 | + 8 time 4 time 1v + 3ra ½v + 3½rb 12 random b |
| 33 | +019212d3 - 87f4 - 7d25 - 902e - b8d39fe07f08 |
| 34 | +``` |
| 35 | + |
| 36 | +- 8ch hex time high |
| 37 | +- `-` |
| 38 | +- 4ch hex time low |
| 39 | +- `-` |
| 40 | +- 4ch hex version + "random a" |
| 41 | + - 1ch hex version: `7` |
| 42 | + - 3ch hex "random a" |
| 43 | +- `-` |
| 44 | +- 4ch hex variant + "random b" |
| 45 | + - 1ch hex version: `8`, `9`, `a`, `b` |
| 46 | + - 3ch hex "random b" |
| 47 | +- `-` |
| 48 | +- 12ch hex randam a |
| 49 | + - 4ch hex random a |
| 50 | + - 8ch hex random a |
| 51 | + |
| 52 | +## By the Bits |
| 53 | + |
| 54 | +```text |
| 55 | + 48 time 4ver, 12ra 2var, 14rb random b |
| 56 | +019212d3-87f4 - 7d25 - 902e - b8d39fe07f08 |
| 57 | +``` |
| 58 | + |
| 59 | +- 48 bits of timestamp |
| 60 | + - 32-bit high (minutes to years) |
| 61 | + - 16-bit low (seconds & milliseconds) |
| 62 | +- 16 bits of version + random |
| 63 | + - 4-bit version (`0b0111`) |
| 64 | + - 12-bit random |
| 65 | +- 64-bits variant + random |
| 66 | + - 2-bit variant (`0b10`) |
| 67 | + - 62-bit random |
| 68 | + |
| 69 | +# Build |
| 70 | + |
| 71 | +See </build.sh>. |
| 72 | + |
| 73 | +Builds with zig v0.13 and the v0.14 previews so far. |
| 74 | + |
| 75 | +```sh |
| 76 | +curl https://webi.sh/zig@0.13 | sh |
| 77 | +source ~/.config/envman/PATH.env |
| 78 | +``` |
| 79 | + |
| 80 | +```sh |
| 81 | +zig build-exe ./uuidv7.zig -O ReleaseSmall -femit-bin="uuidv7" |
| 82 | + |
| 83 | +for b_target in x86-linux-musl aarch64-macos-none x86_64-windows-gnu; do |
| 84 | + zig build-exe ./uuidv7.zig -O ReleaseSmall \ |
| 85 | + -target "${b_target}" -femit-bin="uuidv7-${b_target}" |
| 86 | +done |
| 87 | +``` |
| 88 | + |
| 89 | +# License |
| 90 | + |
| 91 | +Copyright 2024 AJ ONeal <aj@therootcompany.com> |
| 92 | + |
| 93 | +This Source Code Form is subject to the terms of the Mozilla Public \ |
| 94 | +License, v. 2.0. If a copy of the MPL was not distributed with this \ |
| 95 | +file, You can obtain one at https://mozilla.org/MPL/2.0/. |
0 commit comments