diff util/uuid.lua @ 13983:837d47c24e3f 13.0

util.uuid: Update UUIDv7 to match RFC 9562 The data layout changed a bit since draft-peabody-dispatch-new-uuid-format-01 Main change is being based on milliseconds instead of seconds and slightly more entropy.
author Kim Alvefur <zash@zash.se>
date Fri, 10 Oct 2025 17:18:51 +0200
parents e6a5f196fc1f
children dab1a4002cb5
line wrap: on
line diff
--- a/util/uuid.lua	Mon Oct 06 01:22:40 2025 +0200
+++ b/util/uuid.lua	Fri Oct 10 17:18:51 2025 +0200
@@ -28,15 +28,11 @@
 
 local function generate_v7()
 	-- Sortable based on time and random
-	-- https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-01#section-4.4
-	local t = time.now();
-	local unixts = m_floor(t);
-	local unixts_a = m_floor(unixts / 16);
-	local unixts_b = m_floor(unixts % 16);
-	local subsec = t % 1;
-	local subsec_a = m_floor(subsec * 0x1000);
-	local subsec_b = m_floor(subsec * 0x1000000) % 0x1000;
-	return ("%08x-%x%03x-7%03x-%4s-%12s"):format(unixts_a, unixts_b, subsec_a, subsec_b, get_twobits() .. get_nibbles(3), get_nibbles(12));
+	-- https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-7
+	local unix_ts_ms = m_floor(time.now()*1000);
+	local unix_ts_ms_a = m_floor(unix_ts_ms / 0x10000);
+	local unix_ts_ms_b = unix_ts_ms % 0x10000;
+	return ("%08x-%4x-7%3s-%1s%3s-%12s"):format(unix_ts_ms_a, unix_ts_ms_b, get_nibbles(3), get_twobits(), get_nibbles(3), get_nibbles(12));
 end
 
 return {