Skip to content

Commit

Permalink
allow setting an explicit timestamp when generating a uuidv7
Browse files Browse the repository at this point in the history
  • Loading branch information
umeldt committed Dec 8, 2024
1 parent d3ea71e commit ac10ca6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/uuid.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ select uuid7();
-- 018ff383-94fd-70fa-8da6-339180b8e15d
```

The timestamp used for creating the UUID can be specified explicitly by passing the number of seconds since 1 January 1970.

```sql
select uuid7(0);
-- 00000000-0000-7558-a13b-e7014913d8ad
```

```sql
select uuid7(1700000000);
-- 018bcfe5-6800-7b8a-8a96-e315717d31bb
```

### uuid7_timestamp_ms

```text
Expand Down
10 changes: 8 additions & 2 deletions src/uuid/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*
* uuid4() - generate a version 4 UUID as a string
* uuid7() - generate a version 7 UUID as a string
* uuid7(X) - generate a version 7 UUID as a string using X seconds since the unix epoch as the timestamp
* uuid_str(X) - convert a UUID X into a well-formed UUID string
* uuid_blob(X) - convert a UUID X into a 16-byte blob
* uuid7_timestamp_ms(X) - extract unix timestamp in miliseconds
Expand Down Expand Up @@ -190,7 +191,12 @@ static void uuid_v7_generate(sqlite3_context* context, int argc, sqlite3_value**
(void)argv;

struct timespec ts;
timespec_get(&ts, TIME_UTC);
if (argc == 1 && sqlite3_value_type(argv[0])==SQLITE_INTEGER) {
sqlite3_int64 seconds = sqlite3_value_int64(argv[0]);
ts.tv_sec = seconds;
} else {
timespec_get(&ts, TIME_UTC);
}
unsigned long long timestampMs = ts.tv_sec * 1000ULL + ts.tv_nsec / 1000000;

sqlite3_randomness(16, aBlob);
Expand Down Expand Up @@ -266,7 +272,7 @@ int uuid_init(sqlite3* db) {
sqlite3_create_function(db, "uuid4", 0, flags, 0, uuid_v4_generate, 0, 0);
sqlite3_create_function(db, "gen_random_uuid", 0, flags, 0, uuid_v4_generate, 0, 0);
#ifndef SQLEAN_OMIT_UUID7
sqlite3_create_function(db, "uuid7", 0, flags, 0, uuid_v7_generate, 0, 0);
sqlite3_create_function(db, "uuid7", -1, flags, 0, uuid_v7_generate, 0, 0);
sqlite3_create_function(db, "uuid7_timestamp_ms", 1, det_flags, 0, uuid_v7_extract_timestamp_ms,
0, 0);
#endif
Expand Down
1 change: 1 addition & 0 deletions test/uuid.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ select '3_07', uuid_blob(null) is null;

-- uuid7
select '4_01', uuid7() like '________-____-7___-____-____________';
select '4_02', uuid7(0) like '00000000-0000-7___-____-____________';

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

0 comments on commit ac10ca6

Please sign in to comment.