Skip to content

Commit

Permalink
Add snowflake_time() SQL function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ammon Smith committed Jun 22, 2019
1 parent 8fea90a commit 56a0d1a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions id_to_snowflake.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Helper function to convert Discord IDs into snowflakes purely within Postgres
-- Equivalent in Python:
-- datetime.utcfromtimestamp(((id >> 22) + DISCORD_EPOCH) / 1000)
-- where DISCORD_EPOCH is 1420070400000

CREATE FUNCTION snowflake_time(id bigint)
RETURNS TIMESTAMP WITH TIME ZONE
LANGUAGE SQL
AS 'SELECT to_timestamp(((id >> 22) + 1420070400000) / 1000) AS timestamp'
RETURNS NULL ON NULL INPUT
IMMUTABLE;

0 comments on commit 56a0d1a

Please sign in to comment.