Skip to content

Commit

Permalink
feat: functions to convert unix epoch to fil epoch (#252)
Browse files Browse the repository at this point in the history
adds `unix_to_height` and `height_and_unix` to translate a unix epoch in seconds to a fil epoch. Makes searching the messages tables more fun.


License: MIT
Signed-off-by: Oli Evans <[email protected]>
  • Loading branch information
olizilla authored Nov 24, 2020
1 parent 8e4825e commit 2f13ca1
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions storage/migrations/21_height_functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package migrations

import (
"github.com/go-pg/migrations/v8"
)

// Schema version 21 adds helper functions to convert from unix epoch to fil epoch

func init() {
up := batch(`
CREATE OR REPLACE FUNCTION public.unix_to_height(unix_epoch bigint) RETURNS bigint AS $$
SELECT ((unix_epoch - 1598306400) / 30)::bigint;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION public.height_to_unix(fil_epoch bigint) RETURNS bigint AS $$
SELECT ((fil_epoch * 30) + 1598306400)::bigint;
$$ LANGUAGE SQL;
`)

down := batch(`
DROP FUNCTION IF EXISTS public.unix_to_height;
DROP FUNCTION IF EXISTS public.height_to_unix;
`)

migrations.MustRegisterTx(up, down)
}

0 comments on commit 2f13ca1

Please sign in to comment.