lib/CrashTest/Helper/DateTime.pm
author Vincent Tondellier <tonton+hg@team1664.org>
Sun, 18 Dec 2016 21:11:27 +0100
changeset 122 8692800ec9ba
parent 104 b75005d8b002
permissions -rw-r--r--
Use Dzil PkgVersion Add a newline after each package to make Dist::Zilla happy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     1
# This program is free software: you can redistribute it and/or modify
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     2
# it under the terms of the GNU General Public License as published by
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     3
# the Free Software Foundation, either version 3 of the License, or
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     4
# (at your option) any later version.
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     5
#
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     6
# This program is distributed in the hope that it will be useful,
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     9
# GNU General Public License for more details.
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    10
#
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    11
# You should have received a copy of the GNU General Public License
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    12
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    13
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    14
package CrashTest::Helper::DateTime;
122
8692800ec9ba Use Dzil PkgVersion
Vincent Tondellier <tonton+hg@team1664.org>
parents: 104
diff changeset
    15
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    16
use Mojo::Base 'Mojolicious::Plugin';
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    17
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    18
use DateTime::Format::Pg;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    19
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    20
sub register {
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    21
    my ($self, $app, $conf) = @_;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    22
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    23
    $app->helper(date_from_db_utc => sub {
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    24
        my $d = $self->_date_from_db_utc(@_);
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    25
        return "" unless $d;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    26
        $d->set_time_zone('local')->strftime("%F %T");
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    27
    });
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    28
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    29
    $app->helper(date_with_tz_from_db_utc => sub {
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    30
        my $d = $self->_date_from_db_utc(@_);
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    31
        return "" unless $d;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    32
        $d->strftime("%FT%TZ");
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    33
    });
104
b75005d8b002 Display dates in human format
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    34
b75005d8b002 Display dates in human format
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    35
    $app->helper(human_date_from_epoch => sub {
b75005d8b002 Display dates in human format
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    36
        my ($c, $ts) = @_;
b75005d8b002 Display dates in human format
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    37
        my $d;
b75005d8b002 Display dates in human format
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    38
        eval {
b75005d8b002 Display dates in human format
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    39
            $d = DateTime->from_epoch(epoch => $ts);
b75005d8b002 Display dates in human format
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    40
        };
b75005d8b002 Display dates in human format
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    41
        $c->app->log->warn($@) if $@;
b75005d8b002 Display dates in human format
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    42
        return "" unless $d;
b75005d8b002 Display dates in human format
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    43
        $d->set_time_zone('local')->strftime("%F %T");
b75005d8b002 Display dates in human format
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    44
    });
b75005d8b002 Display dates in human format
Vincent Tondellier <tonton+hg@team1664.org>
parents: 78
diff changeset
    45
78
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    46
}
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    47
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    48
sub _date_from_db_utc {
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    49
    my ($self, $c, $dbdate) = @_;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    50
    my $utcdate;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    51
    eval {
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    52
        $utcdate = DateTime::Format::Pg->parse_datetime($dbdate)->set_time_zone('UTC');
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    53
    };
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    54
    $c->app->log->warn($@) if $@;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    55
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    56
    return $utcdate;
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    57
}
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    58
0ebef32c34af Refactor everything
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    59
1;