Skip to content

Commit 2e02f6c

Browse files
committed
uuid: set nsec part of timestamp to 0 for uuid7(x)
1 parent ac10ca6 commit 2e02f6c

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/uuid/extension.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
*
1515
* uuid4() - generate a version 4 UUID as a string
1616
* uuid7() - generate a version 7 UUID as a string
17-
* uuid7(X) - generate a version 7 UUID as a string using X seconds since the unix epoch as the timestamp
17+
* uuid7(X) - generate a version 7 UUID as a string
18+
* with a unix timestamp of X seconds.
1819
* uuid_str(X) - convert a UUID X into a well-formed UUID string
1920
* uuid_blob(X) - convert a UUID X into a 16-byte blob
2021
* uuid7_timestamp_ms(X) - extract unix timestamp in miliseconds
@@ -191,11 +192,12 @@ static void uuid_v7_generate(sqlite3_context* context, int argc, sqlite3_value**
191192
(void)argv;
192193

193194
struct timespec ts;
194-
if (argc == 1 && sqlite3_value_type(argv[0])==SQLITE_INTEGER) {
195-
sqlite3_int64 seconds = sqlite3_value_int64(argv[0]);
196-
ts.tv_sec = seconds;
195+
if (argc == 1 && sqlite3_value_type(argv[0]) == SQLITE_INTEGER) {
196+
sqlite3_int64 seconds = sqlite3_value_int64(argv[0]);
197+
ts.tv_sec = seconds;
198+
ts.tv_nsec = 0;
197199
} else {
198-
timespec_get(&ts, TIME_UTC);
200+
timespec_get(&ts, TIME_UTC);
199201
}
200202
unsigned long long timestampMs = ts.tv_sec * 1000ULL + ts.tv_nsec / 1000000;
201203

@@ -272,7 +274,8 @@ int uuid_init(sqlite3* db) {
272274
sqlite3_create_function(db, "uuid4", 0, flags, 0, uuid_v4_generate, 0, 0);
273275
sqlite3_create_function(db, "gen_random_uuid", 0, flags, 0, uuid_v4_generate, 0, 0);
274276
#ifndef SQLEAN_OMIT_UUID7
275-
sqlite3_create_function(db, "uuid7", -1, flags, 0, uuid_v7_generate, 0, 0);
277+
sqlite3_create_function(db, "uuid7", 0, flags, 0, uuid_v7_generate, 0, 0);
278+
sqlite3_create_function(db, "uuid7", 1, flags, 0, uuid_v7_generate, 0, 0);
276279
sqlite3_create_function(db, "uuid7_timestamp_ms", 1, det_flags, 0, uuid_v7_extract_timestamp_ms,
277280
0, 0);
278281
#endif

test/uuid.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ select '3_07', uuid_blob(null) is null;
3131
-- uuid7
3232
select '4_01', uuid7() like '________-____-7___-____-____________';
3333
select '4_02', uuid7(0) like '00000000-0000-7___-____-____________';
34+
select '4_03', uuid7(1733668387) like '0193a6b0-38b8-7___-____-____________';
3435

3536
-- uuid7_timestamp_ms
3637
select '5_01', uuid7_timestamp_ms('018ff38a-a5c9-712d-bc80-0550b3ad41a2') = 1717777901001;

0 commit comments

Comments
 (0)