Skip to content

Commit

Permalink
improved Mojo::Date to be able to handle higher precision times
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 23, 2014
1 parent ea11ecb commit 4ba2df0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

5.33 2014-08-23
- Improved Mojo::Date to be able to handle higher precision times.
- Improved Mojo::ByteStream performance.

5.32 2014-08-21
Expand Down
13 changes: 9 additions & 4 deletions lib/Mojo/Date.pm
Expand Up @@ -7,7 +7,7 @@ use Time::Local 'timegm';
has epoch => sub {time};

my $RFC3339_RE = qr/
^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)(?:\.\d+)? # Date and time
^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+(?:\.\d+)?) # Date and time
(?:Z|([+-])(\d+):(\d+))?$ # Offset
/xi;

Expand All @@ -22,7 +22,7 @@ sub parse {
my ($self, $date) = @_;

# epoch (784111777)
return $self->epoch($date) if $date =~ /^\d+$/;
return $self->epoch($date) if $date =~ /^\d+$|^\d+\.\d+$/;

# RFC 822/1123 (Sun, 06 Nov 1994 08:49:37 GMT)
my $offset = 0;
Expand Down Expand Up @@ -59,9 +59,10 @@ sub parse {
sub to_datetime {

# RFC 3339 (1994-11-06T08:49:37Z)
my ($s, $m, $h, $day, $month, $year) = gmtime shift->epoch;
return sprintf '%04d-%02d-%02dT%02d:%02d:%02dZ', $year + 1900, $month + 1,
my ($s, $m, $h, $day, $month, $year) = gmtime(my $epoch = shift->epoch);
my $str = sprintf '%04d-%02d-%02dT%02d:%02d:%02d', $year + 1900, $month + 1,
$day, $h, $m, $s;
return $epoch =~ /\.(\d+)$/ ? "$str.$1Z" : "${str}Z";
}

sub to_string {
Expand Down Expand Up @@ -130,6 +131,7 @@ Parse date.
# Epoch
say Mojo::Date->new('784111777')->epoch;
say Mojo::Date->new('784111777.21')->epoch;
# RFC 822/1123
say Mojo::Date->new('Sun, 06 Nov 1994 08:49:37 GMT')->epoch;
Expand All @@ -156,6 +158,9 @@ Render L<RFC 3339|http://tools.ietf.org/html/rfc3339> date and time.
# "1994-11-06T08:49:37Z"
Mojo::Date->new(784111777)->to_datetime;
# "1994-11-06T08:49:37.21Z"
Mojo::Date->new(784111777.21)->to_datetime;
=head2 to_string
my $str = $date->to_string;
Expand Down
8 changes: 4 additions & 4 deletions t/mojo/date.t
Expand Up @@ -15,7 +15,7 @@ is(Mojo::Date->new('2014-08-20T20:45:00')->epoch,
is(Mojo::Date->new(1408567500)->to_datetime,
'2014-08-20T20:45:00Z', 'right format');
is(Mojo::Date->new('2014-08-20T20:45:00.01')->epoch,
1408567500, 'right epoch value');
1408567500.01, 'right epoch value');
is(Mojo::Date->new('2014-08-20T20:45:00-00:46')->epoch,
1408570260, 'right epoch value');
is(Mojo::Date->new(1408570260)->to_datetime,
Expand All @@ -29,9 +29,9 @@ is(Mojo::Date->new(1408561140)->to_datetime,
is(Mojo::Date->new('1994-11-06T08:49:37Z')->epoch,
784111777, 'right epoch value');
is(Mojo::Date->new('1994-11-06t08:49:37.33z')->epoch,
784111777, 'right epoch value');
is(Mojo::Date->new(784111777)->to_datetime,
'1994-11-06T08:49:37Z', 'right format');
784111777.33, 'right epoch value');
is(Mojo::Date->new(784111777.33)->to_datetime,
'1994-11-06T08:49:37.33Z', 'right format');

# RFC 850/1036
is(Mojo::Date->new('Sunday, 06-Nov-94 08:49:37 GMT')->epoch,
Expand Down

0 comments on commit 4ba2df0

Please sign in to comment.