Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bobvawter/5f119dcbec8a4e7e1667e068ef1bf00e to your computer and use it in GitHub Desktop.
Save bobvawter/5f119dcbec8a4e7e1667e068ef1bf00e to your computer and use it in GitHub Desktop.
Version 1 UUID to Timestamp in CockroachDB or PostgreSQL
WITH u (u) AS (VALUES ('4d1e53e2-a182-11ed-a4bf-3f161938a1c4'::UUID::BYTES))
SELECT to_timestamp(
(
(
get_byte(u, 0) << 24
| get_byte(u, 1) << 16
| get_byte(u, 2) << 8
| get_byte(u, 3) << 0
| get_byte(u, 4) << 40
| get_byte(u, 5) << 32
| (get_byte(u, 6) & 0xF) << 56
| get_byte(u, 7) << 48
)
- 0x01B21DD213814000
)::FLOAT8
/ 10000000.0
)
FROM u;
to_timestamp
---------------------------------
2023-01-31 16:14:13.000701+00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment