csgillespie / uuidv7

UUIDv7 in N languages

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UUIDv7 in N languages

UUIDv7 is a 128-bit unique identifier like its older siblings, such as the widely used UUIDv4. But unlike v4, UUIDv7 is time-sortable with 1 ms precision. By combining the timestamp and the random parts, UUIDv7 becomes an excellent choice for record identifiers in databases, including distributed ones.

This repo provides zero-dependency UUIDv7 implementations in various languages. If you spot a bug — please submit a pull request. PRs for other languages are also welcome!

Structure

UUIDv7 looks like this when represented as a string:

0190163d-8694-739b-aea5-966c26f8ad91
└─timestamp─┘ │└─┤ │└───rand_b─────┘
             ver │var
              rand_a

The 128-bit value consists of several parts:

  • timestamp (48 bits) is a Unix timestamp in milliseconds.
  • ver (4 bits) is a UUID version (7).
  • rand_a (12 bits) is randomly generated.
  • var* (2 bits) is equal to 10.
  • rand_b (62 bits) is randomly generated.

* In string representation, each symbol encodes 4 bits as a hex number, so the a in the example is 1010, where the first two bits are the fixed variant (10) and the next two are random. So the resulting hex number can be either 8 (1000), 9 (1001), a (1010) or b (1011).

See RFC 9652 for details.

Implementations

CC#C++ClojureCrystalDartEmacs LispElixirErlangGoJavaJavaScriptJuliaKotlinLuaNimPerlPHPPascalPowerShellPythonRRubyRustShellSQLSwiftTclVVimscriptZig

License

The Unlicense.

About

UUIDv7 in N languages

License:The Unlicense


Languages

Language:Pascal 5.9%Language:Swift 5.1%Language:C++ 4.5%Language:C# 4.5%Language:Java 4.4%Language:Rust 4.2%Language:Kotlin 4.2%Language:C 4.2%Language:Elixir 3.9%Language:Tcl 3.6%Language:Perl 3.5%Language:Emacs Lisp 3.4%Language:Dart 3.4%Language:TypeScript 3.2%Language:Shell 3.2%Language:JavaScript 3.2%Language:R 3.0%Language:Lua 2.8%Language:Nim 2.7%Language:PHP 2.7%Language:Clojure 2.6%Language:Python 2.6%Language:V 2.5%Language:Ruby 2.5%Language:Vim Script 2.4%Language:Zig 2.3%Language:Go 2.1%Language:Erlang 2.1%Language:Crystal 2.0%Language:PowerShell 1.8%Language:Julia 1.5%