This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/lib/Pear/LocalLoop/Plugin/Datetime.pm

102 lines
2.5 KiB
Perl
Raw Permalink Normal View History

2017-08-14 11:45:54 +00:00
package Pear::LocalLoop::Plugin::Datetime;
use Mojo::Base 'Mojolicious::Plugin';
use DateTime::Format::Strptime;
sub register {
2021-03-20 12:09:50 +00:00
my ( $plugin, $app, $conf ) = @_;
2017-08-14 11:45:54 +00:00
2021-03-20 12:09:50 +00:00
$app->helper(
human_datetime_parser => sub {
return DateTime::Format::Strptime->new( pattern => '%x %X' );
}
);
2017-09-28 15:39:03 +00:00
2021-03-20 12:09:50 +00:00
$app->helper(
format_human_datetime => sub {
my ( $c, $datetime_obj ) = @_;
return $c->human_datetime_parser->format_datetime( $datetime_obj, );
}
2017-09-28 15:39:03 +00:00
);
2021-03-20 12:09:50 +00:00
$app->helper(
iso_datetime_parser => sub {
return DateTime::Format::Strptime->new(
pattern => '%Y-%m-%dT%H:%M:%S.%3N%z' );
}
);
2021-03-20 12:09:50 +00:00
$app->helper(
iso_date_parser => sub {
return DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' );
}
);
2021-03-20 12:09:50 +00:00
$app->helper(
iso_month_parser => sub {
return DateTime::Format::Strptime->new( pattern => '%Y-%m' );
}
);
2021-03-20 12:09:50 +00:00
$app->helper(
parse_iso_date => sub {
my ( $c, $date_string ) = @_;
return $c->iso_date_parser->parse_datetime( $date_string, );
}
);
2021-03-20 12:09:50 +00:00
$app->helper(
format_iso_date => sub {
my ( $c, $datetime_obj ) = @_;
return $c->iso_date_parser->format_datetime( $datetime_obj, );
}
);
2021-03-20 12:09:50 +00:00
$app->helper(
parse_iso_month => sub {
my ( $c, $date_string ) = @_;
return $c->iso_month_parser->parse_datetime( $date_string, );
}
);
2021-03-20 12:09:50 +00:00
$app->helper(
format_iso_month => sub {
my ( $c, $datetime_obj ) = @_;
return $c->iso_month_parser->format_datetime( $datetime_obj, );
}
);
2021-03-20 12:09:50 +00:00
$app->helper(
parse_iso_datetime => sub {
my ( $c, $date_string ) = @_;
return $c->iso_datetime_parser->parse_datetime( $date_string, );
}
);
2021-03-20 12:09:50 +00:00
$app->helper(
format_iso_datetime => sub {
my ( $c, $datetime_obj ) = @_;
return unless defined $datetime_obj;
return $c->iso_datetime_parser->format_datetime( $datetime_obj, );
}
);
2021-03-20 12:09:50 +00:00
$app->helper(
db_datetime_parser => sub {
return shift->schema->storage->datetime_parser;
}
);
2021-03-20 12:09:50 +00:00
$app->helper(
format_db_datetime => sub {
my ( $c, $datetime_obj ) = @_;
$datetime_obj->set_time_zone('UTC');
return $c->db_datetime_parser->format_datetime( $datetime_obj, );
}
);
2017-08-14 11:45:54 +00:00
2021-03-20 15:02:00 +00:00
return 1;
2017-08-14 11:45:54 +00:00
}
1;