Skip to content

Instantly share code, notes, and snippets.

@renatocron
Created August 10, 2018 16:58
Show Gist options
  • Save renatocron/04c4f693c26153640ec203ec6209cac8 to your computer and use it in GitHub Desktop.
Save renatocron/04c4f693c26153640ec203ec6209cac8 to your computer and use it in GitHub Desktop.
create schema if not exists utils;
create table utils.replaceable_now ( the_time timestamp with time zone );
CREATE OR REPLACE FUNCTION replaceable_now() RETURNS timestamp with time zone AS $AA$
SELECT coalesce((select the_time from utils.replaceable_now limit 1), now());
$AA$ LANGUAGE SQL STABLE;
/*
-- run in prod:
drop table utils.replaceable_now;
CREATE OR REPLACE FUNCTION replaceable_now() RETURNS timestamp with time zone AS $AA$
SELECT now()
$AA$ LANGUAGE SQL STABLE;
*/
sub set_relative_time {
my ( $date, %conf ) = @_;
my $schema = SFC->model( exists $conf{model} ? $conf{model} : 'DB' );
$schema->storage->dbh_do(
sub {
shift;
my $cn = shift;
my $res = $cn->do(qq{update utils.replaceable_now set the_time = '$date'::timestamp with time zone });
if ( $res eq '0E0' ) {
$cn->do(qq{insert into utils.replaceable_now ( the_time) values ( '$date'::timestamp with time zone )});
}
}
);
}
sub epoch_to_datetime {
my $epoch = shift;
return DateTime->from_epoch( epoch => $epoch )->datetime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment