# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
package CrashTest::Helper::DateTime;
use Mojo::Base 'Mojolicious::Plugin';
use DateTime::Format::Pg;
sub register {
my ($self, $app, $conf) = @_;
$app->helper(date_from_db_utc => sub {
my $d = $self->_date_from_db_utc(@_);
return "" unless $d;
$d->set_time_zone('local')->strftime("%F %T");
});
$app->helper(date_with_tz_from_db_utc => sub {
my $d = $self->_date_from_db_utc(@_);
return "" unless $d;
$d->strftime("%FT%TZ");
});
$app->helper(human_date_from_epoch => sub {
my ($c, $ts) = @_;
my $d;
eval {
$d = DateTime->from_epoch(epoch => $ts);
};
$c->app->log->warn($@) if $@;
return "" unless $d;
$d->set_time_zone('local')->strftime("%F %T");
});
}
sub _date_from_db_utc {
my ($self, $c, $dbdate) = @_;
my $utcdate;
eval {
$utcdate = DateTime::Format::Pg->parse_datetime($dbdate)->set_time_zone('UTC');
};
$c->app->log->warn($@) if $@;
return $utcdate;
}
1;