Skip to content

Commit

Permalink
added more Mojo::Date tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 5, 2011
1 parent 60cda9d commit 422e851
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,6 +1,6 @@
This file documents the revision history for Perl extension Mojolicious.

2.0 2011-10-05 00:00:00
2.0 2011-10-06 00:00:00
- Code name "Leaf Fluttering In Wind", this is a major release.
- Increased Perl version requirement to 5.10.1.
- Improved message parser performance slightly.
Expand Down
82 changes: 21 additions & 61 deletions lib/Mojo/Date.pm
Expand Up @@ -15,17 +15,10 @@ my @MONTHS = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
my %MONTHS;
{
my $i = 0;
for my $month (@MONTHS) {
$MONTHS{$month} = $i;
$i++;
}
$MONTHS{$_} = $i++ for @MONTHS;
}

sub new {
my $self = shift->SUPER::new();
$self->parse(@_);
return $self;
}
sub new { shift->SUPER::new->parse(@_) }

# "I suggest you leave immediately.
# Or what? You'll release the dogs or the bees?
Expand All @@ -35,76 +28,43 @@ sub parse {
my ($self, $date) = @_;
return $self unless defined $date;

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

# Remove spaces, weekdays and timezone
$date =~ s/^\s+//;
my $re = join '|', @DAYS;
$date =~ s/^(?:$re)[a-z]*,?\s*//i;
$date =~ s/GMT\s*$//i;
$date =~ s/\s+$//;

# RFC 822/1123 - Sun, 06 Nov 1994 08:49:37 GMT
my ($day, $month, $year, $hour, $minute, $second);
if ($date =~ /^(\d+)\s+(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)$/) {
$day = $1;
$month = $MONTHS{$2};
$year = $3;
$hour = $4;
$minute = $5;
$second = $6;
# RFC 822/1123 (Sun, 06 Nov 1994 08:49:37 GMT)
my ($day, $month, $year, $h, $m, $s);
if ($date =~ /^\w+\,\s+(\d+)\s+(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+GMT$/) {
($day, $month, $year, $h, $m, $s) = ($1, $MONTHS{$2}, $3, $4, $5, $6);
}

# RFC 850/1036 - Sunday, 06-Nov-94 08:49:37 GMT
elsif ($date =~ /^(\d+)-(\w+)-(\d+)\s+(\d+):(\d+):(\d+)$/) {
$day = $1;
$month = $MONTHS{$2};
$year = $3;
$hour = $4;
$minute = $5;
$second = $6;
# RFC 850/1036 (Sunday, 06-Nov-94 08:49:37 GMT)
elsif ($date =~ /^\w+\,\s+(\d+)-(\w+)-(\d+)\s+(\d+):(\d+):(\d+)\s+GMT$/) {
($day, $month, $year, $h, $m, $s) = ($1, $MONTHS{$2}, $3, $4, $5, $6);
}

# ANSI C asctime() - Sun Nov 6 08:49:37 1994
elsif ($date =~ /^(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)$/) {
$month = $MONTHS{$1};
$day = $2;
$hour = $3;
$minute = $4;
$second = $5;
$year = $6;
# ANSI C asctime() (Sun Nov 6 08:49:37 1994)
elsif ($date =~ /^\w+\s+(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)$/) {
($month, $day, $h, $m, $s, $year) = ($MONTHS{$1}, $2, $3, $4, $5, $6);
}

# Invalid format
# Invalid
else { return $self }

# Prevent crash
my $epoch;
eval {
$epoch =
Time::Local::timegm($second, $minute, $hour, $day, $month, $year);
};
return $self if $@ || $epoch < 0;
$self->epoch($epoch);
$epoch = eval { Time::Local::timegm($s, $m, $h, $day, $month, $year) };
$self->epoch($epoch) if !$@ && $epoch >= 0;

return $self;
}

sub to_string {
my $self = shift;

# RFC 822/1123
my ($second, $minute, $hour, $mday, $month, $year, $wday) =
gmtime($self->epoch // time);
return sprintf(
"%s, %02d %s %04d %02d:%02d:%02d GMT",
$DAYS[$wday], $mday, $MONTHS[$month], $year + 1900,
$hour, $minute, $second
);
# RFC 2616 (Sun, 06 Nov 1994 08:49:37 GMT)
my ($s, $m, $h, $mday, $month, $year, $wday) = gmtime($self->epoch // time);
return sprintf "%s, %02d %s %04d %02d:%02d:%02d GMT", $DAYS[$wday], $mday,
$MONTHS[$month], $year + 1900, $h, $m, $s;
}

1;
Expand Down
26 changes: 20 additions & 6 deletions t/mojo/date.t
@@ -1,32 +1,46 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use Test::More tests => 10;
use Test::More tests => 17;

# "Can't we have one meeting that doesn't end with digging up a corpse?"
use_ok 'Mojo::Date';

# RFC 822/1123
my $date = Mojo::Date->new('Sun, 06 Nov 1994 08:49:37 GMT');
is $date->epoch, 784111777, 'right epoch value';
$date = Mojo::Date->new('Fri, 13 May 2011 10:00:24 GMT');
is $date->epoch, 1305280824, 'right epoch value';

# RFC 850/1036
is $date->parse('Sunday, 06-Nov-94 08:49:37 GMT')->epoch,
is Mojo::Date->new('Sunday, 06-Nov-94 08:49:37 GMT')->epoch,
784111777, 'right epoch value';
is Mojo::Date->new('Friday, 13-May-11 10:00:24 GMT')->epoch,
1305280824, 'right epoch value';

# ANSI C asctime()
is $date->parse('Sun Nov 6 08:49:37 1994')->epoch,
is Mojo::Date->new('Sun Nov 6 08:49:37 1994')->epoch,
784111777, 'right epoch value';
is Mojo::Date->new('Fri May 13 10:00:24 2011')->epoch,
1305280824, 'right epoch value';

# Invalid string
is Mojo::Date->new('123 abc')->epoch, undef, 'no epoch value';
is Mojo::Date->new('abc')->epoch, undef, 'no epoch value';
is Mojo::Date->new('Xxx, 00 Xxx 0000 00:00:00 XXX')->epoch, undef,
'no epoch value';

# to_string
$date->parse(784111777);
$date = Mojo::Date->new(784111777);
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';

# Zero time checks
$date->parse(0);
$date = Mojo::Date->new(0);
is $date->epoch, 0, 'right epoch value';
is "$date", 'Thu, 01 Jan 1970 00:00:00 GMT', 'right format';
is $date->parse('Thu, 01 Jan 1970 00:00:00 GMT')->epoch,
is Mojo::Date->new('Thu, 01 Jan 1970 00:00:00 GMT')->epoch,
0, 'right epoch value';

# Negative epoch value
Expand Down

0 comments on commit 422e851

Please sign in to comment.