Skip to content

Commit

Permalink
epoch attribute should have a real default value
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 23, 2014
1 parent b78471e commit ea11ecb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/Mojo/ByteStream.pm
@@ -1,6 +1,6 @@
package Mojo::ByteStream;
use Mojo::Base -strict;
use overload '""' => sub { ${shift()} }, fallback => 1;
use overload '""' => sub { ${$_[0]} }, fallback => 1;

use Exporter 'import';
use Mojo::Collection;
Expand Down Expand Up @@ -52,7 +52,7 @@ sub split {

sub tap { shift->Mojo::Base::tap(@_) }

sub to_string { ${shift()} }
sub to_string { ${$_[0]} }

sub _delegate {
my ($self, $sub) = (shift, shift);
Expand Down
16 changes: 8 additions & 8 deletions lib/Mojo/Date.pm
Expand Up @@ -4,7 +4,7 @@ use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1;

use Time::Local 'timegm';

has 'epoch';
has epoch => sub {time};

my $RFC3339_RE = qr/
^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)(?:\.\d+)? # Date and time
Expand Down Expand Up @@ -48,26 +48,26 @@ sub parse {
}

# Invalid
else { return $self }
else { return $self->epoch(undef) }

# Prevent crash
my $epoch = eval { timegm($s, $m, $h, $day, $month, $year) };
return
defined $epoch && ($epoch += $offset) >= 0 ? $self->epoch($epoch) : $self;
my $epoch = eval { timegm $s, $m, $h, $day, $month, $year };
return $self->epoch(
(defined $epoch && ($epoch += $offset) >= 0) ? $epoch : undef);
}

sub to_datetime {

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

sub to_string {

# RFC 7231 (Sun, 06 Nov 1994 08:49:37 GMT)
my ($s, $m, $h, $mday, $month, $year, $wday) = gmtime(shift->epoch // time);
my ($s, $m, $h, $mday, $month, $year, $wday) = gmtime shift->epoch;
return sprintf '%s, %02d %s %04d %02d:%02d:%02d GMT', $DAYS[$wday], $mday,
$MONTHS[$month], $year + 1900, $h, $m, $s;
}
Expand Down Expand Up @@ -108,7 +108,7 @@ L<Mojo::Date> implements the following attributes.
my $epoch = $date->epoch;
$date = $date->epoch(784111777);
Epoch seconds.
Epoch seconds, defaults to the current time.
=head1 METHODS
Expand Down
7 changes: 7 additions & 0 deletions t/mojo/date.t
Expand Up @@ -68,6 +68,13 @@ is "$date", 'Sun, 06 Nov 1994 08:49:37 GMT', 'right format';
$date = Mojo::Date->new(1305280824);
is $date->to_string, 'Fri, 13 May 2011 10:00:24 GMT', 'right format';

# Current time roundtrips
my $before = time;
ok(Mojo::Date->new(Mojo::Date->new->to_string)->epoch >= $before,
'successful roundtrip');
ok(Mojo::Date->new(Mojo::Date->new->to_datetime)->epoch >= $before,
'successful roundtrip');

# Zero time checks
$date = Mojo::Date->new(0);
is $date->epoch, 0, 'right epoch value';
Expand Down

0 comments on commit ea11ecb

Please sign in to comment.