Last active
September 23, 2019 09:13
-
-
Save microo8/f1e54f56022d1a5aaeb3 to your computer and use it in GitHub Desktop.
PostgreSQL - getting mac address and timestamp from uuid type generated from uuid_generate_v1() in uuid-ossp extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION uuid_macaddr(id uuid) | |
RETURNS macaddr AS | |
$BODY$ | |
select substring(id::text, 25, 12)::macaddr | |
$BODY$ | |
LANGUAGE sql IMMUTABLE STRICT | |
COST 100; | |
CREATE OR REPLACE FUNCTION uuid_timestamp(id uuid) | |
RETURNS timestamp with time zone AS | |
$BODY$ | |
select TIMESTAMP WITH TIME ZONE 'epoch' + | |
(((('x' || lpad(split_part(id::text, '-', 1), 16, '0'))::bit(64)::bigint) + | |
(('x' || lpad(split_part(id::text, '-', 2), 16, '0'))::bit(64)::bigint << 32) + | |
((('x' || lpad(split_part(id::text, '-', 3), 16, '0'))::bit(64)::bigint&4095) << 48) - 122192928000000000) / 10000000 ) * INTERVAL '1 second'; | |
$BODY$ | |
LANGUAGE sql IMMUTABLE STRICT | |
COST 100; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment