Browse files

* Add time zone in datetime functions

* Add timestamp as parameter for datetime functions
  • Loading branch information...
1 parent c7d9f28 commit 71141b639eeabb5f0be7b991416aaf99f8f5012f @rshadow rshadow committed Aug 15, 2013
Showing with 44 additions and 20 deletions.
  1. +7 −0 debian/changelog
  2. +22 −15 lib/Mojolicious/Plugin/Human.pm
  3. +15 −5 t/010_human.t
View
7 debian/changelog
@@ -1,3 +1,10 @@
+libmojolicious-plugin-human-perl (0.7-1) nowtaxi; urgency=low
+
+ * Add time zone in datetime functions
+ * Add timestamp as parameter for datetime functions
+
+ -- Roman V. Nikolaev <rshadow@rambler.ru> Thu, 15 Aug 2013 16:26:21 +0400
+
libmojolicious-plugin-human-perl (0.6-1) nowtaxi; urgency=low
* Fix datetime parse
View
37 lib/Mojolicious/Plugin/Human.pm
@@ -11,7 +11,7 @@ use Carp;
use DateTime;
use DateTime::Format::DateParse;
-our $VERSION = '0.6';
+our $VERSION = '0.7';
=encoding utf-8
@@ -131,7 +131,7 @@ my $REGEXP_DIGIT = qr{^(-?\d+)(\d{3})};
sub clean_phone($$$);
sub human_phone($$$);
-sub date_parse($);
+sub date_parse($;$);
sub register {
@@ -145,44 +145,45 @@ sub register {
$conf->{datetime} //= '%F %H:%M';
$conf->{time} //= '%H:%M:%S';
$conf->{date} //= '%F';
+ $conf->{tz} //= 'local';
$conf->{phone_country} //= 7;
$conf->{phone_region} //= 495;
# Datetime
$app->helper(str2time => sub {
- my ($self, $str) = @_;
- my $datetime = date_parse( $str );
+ my ($self, $str, $tz) = @_;
+ my $datetime = date_parse( $str, $tz // $conf->{tz} );
return $str unless $datetime;
return $datetime->epoch;
});
$app->helper(strftime => sub {
- my ($self, $format, $str) = @_;
+ my ($self, $format, $str, $tz) = @_;
return unless defined $str;
- my $datetime = date_parse( $str );
+ my $datetime = date_parse( $str, $tz // $conf->{tz} );
return $str unless $datetime;
return $datetime->strftime( $format );
});
$app->helper(human_datetime => sub {
- my ($self, $str) = @_;
- my $datetime = date_parse( $str );
+ my ($self, $str, $tz) = @_;
+ my $datetime = date_parse( $str, $tz // $conf->{tz} );
return $str unless $datetime;
return $datetime->strftime($conf->{datetime});
});
$app->helper(human_time => sub {
- my ($self, $str) = @_;
- my $datetime = date_parse( $str );
+ my ($self, $str, $tz) = @_;
+ my $datetime = date_parse( $str, $tz // $conf->{tz} );
return $str unless $datetime;
return $datetime->strftime($conf->{time});
});
$app->helper(human_date => sub {
- my ($self, $str) = @_;
- my $datetime = date_parse( $str );
+ my ($self, $str, $tz) = @_;
+ my $datetime = date_parse( $str, $tz // $conf->{tz} );
return $str unless $datetime;
return $datetime->strftime($conf->{date});
});
@@ -309,13 +310,19 @@ Get a string and return DateTime or undef.
=cut
-sub date_parse($) {
- my ($str) = @_;
+sub date_parse($;$) {
+ my ($str, $tz) = @_;
return unless $str;
+ $tz //= 'local';
+
my $dt = eval {
- DateTime::Format::DateParse->parse_datetime( $str );
+ if( $str =~ m{^\d+$} ) {
+ DateTime->from_epoch( epoch => $str, time_zone => $tz );
+ } else {
+ DateTime::Format::DateParse->parse_datetime( $str, $tz );
+ }
};
return if !$dt or $@;
View
20 t/010_human.t
@@ -6,7 +6,7 @@ use utf8;
use open qw(:std :utf8);
use lib qw(lib ../lib ../../lib);
-use Test::More tests => 30;
+use Test::More tests => 33;
use Encode qw(decode encode);
@@ -33,17 +33,27 @@ ok $t, 'Test Mojo created';
$t->app->routes->get("/test/human")->to( cb => sub {
my ($self) = @_;
- my $dt = DateTime->from_epoch( epoch => time, time_zone => 'local');
+ my $time = time;
+ my $dt = DateTime->from_epoch( epoch => $time, time_zone => 'local');
my $dstr = $dt->strftime('%F %T %z');
is $self->str2time( $dstr ), $dt->epoch, 'str2time';
is $self->strftime('%T', $dstr), $dt->strftime('%T'), 'strftime';
+
is $self->human_datetime( $dstr ), $dt->strftime('%F %H:%M'),
- 'human_datetime';
+ 'human_datetime from ISO';
+ is $self->human_datetime( $time ), $dt->strftime('%F %H:%M'),
+ 'human_datetime from timestamp';
+
is $self->human_time( $dstr ), $dt->strftime('%H:%M:%S'),
- 'human_time';
+ 'human_time from ISO';
+ is $self->human_time( $time ), $dt->strftime('%H:%M:%S'),
+ 'human_time from timestamp';
+
is $self->human_date( $dstr ), $dt->strftime('%F'),
- 'human_date';
+ 'human_date from ISO';
+ is $self->human_date( $time ), $dt->strftime('%F'),
+ 'human_date from timestamp';
is $self->human_money, undef, 'human_money undefined';
is $self->human_money(''), '', 'human_money empty';

0 comments on commit 71141b6

Please sign in to comment.