lib/CrashTest/Helper/DateTime.pm
author Vincent Tondellier <tonton+hg@team1664.org>
Sat, 29 Oct 2016 13:20:07 +0200
changeset 114 1f67942c76c9
parent 104 b75005d8b002
child 122 8692800ec9ba
permissions -rw-r--r--
Fixes for Mojolicious 7

# 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;