Skip to content

Commit

Permalink
added basic support for RFC 3339
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 20, 2014
1 parent 3b20f39 commit 6c249f1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,5 +1,7 @@

5.32 2014-08-20
5.32 2014-08-21
- Added to_datetime method to Mojo::Date.
- Improved Mojo::Date to support RFC 3339.

5.31 2014-08-19
- Improved Mojolicious::Static to allow custom content types.
Expand Down
52 changes: 46 additions & 6 deletions lib/Mojo/Date.pm
Expand Up @@ -6,6 +6,13 @@ use Time::Local 'timegm';

has 'epoch';

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

my @DAYS = qw(Sun Mon Tue Wed Thu Fri Sat);
my @MONTHS = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my %MONTHS;
Expand All @@ -16,15 +23,23 @@ sub new { @_ > 1 ? shift->SUPER::new->parse(@_) : shift->SUPER::new }
sub parse {
my ($self, $date) = @_;

# RFC 822/1123 (Sun, 06 Nov 1994 08:49:37 GMT)
my $offset = 0;
my ($day, $month, $year, $h, $m, $s);

# RFC 822/1123 (Sun, 06 Nov 1994 08:49:37 GMT)
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);
}

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

# RFC 3339
elsif ($date =~ $RFC3339_RE) {
($year, $month, $day, $h, $m, $s) = ($1, $2 - 1, $3, $4, $5, $6);
$offset = (($8 * 3600) + ($9 * 60)) * ($7 eq '-' ? -1 : 1) if $7;
}

# 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);
Expand All @@ -40,14 +55,22 @@ sub parse {

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

sub to_datetime {

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

sub to_string {
my $self = shift;

# RFC 7231 (Sun, 06 Nov 1994 08:49:37 GMT)
my ($s, $m, $h, $mday, $month, $year, $wday) = gmtime($self->epoch // time);
my ($s, $m, $h, $mday, $month, $year, $wday) = gmtime(shift->epoch // time);
return sprintf '%s, %02d %s %04d %02d:%02d:%02d GMT', $DAYS[$wday], $mday,
$MONTHS[$month], $year + 1900, $h, $m, $s;
}
Expand Down Expand Up @@ -75,12 +98,14 @@ Mojo::Date - HTTP date
=head1 DESCRIPTION
L<Mojo::Date> implements HTTP date and time functions based on
L<RFC 7230|http://tools.ietf.org/html/rfc7230> and
L<RFC 7231|http://tools.ietf.org/html/rfc7231>.
L<RFC 7230|http://tools.ietf.org/html/rfc7230>,
L<RFC 7231|http://tools.ietf.org/html/rfc7231> and
L<RFC 3339|http://tools.ietf.org/html/rfc3339>.
Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
1994-11-06T08:49:37Z ; RFC 3339
=head1 ATTRIBUTES
Expand Down Expand Up @@ -123,12 +148,27 @@ Parse date.
# Ansi C asctime()
say Mojo::Date->new('Sun Nov 6 08:49:37 1994')->epoch;
# RFC 3339
say Mojo::Date->new('1994-11-06T08:49:37Z')->epoch;
=head2 to_datetime
my $str = $date->to_datetime;
Render L<RFC 3339|http://tools.ietf.org/html/rfc3339> date and time.
# "1994-11-06T08:49:37Z"
Mojo::Date->new(784111777)->to_datetime;
=head2 to_string
my $str = $date->to_string;
Render date suitable for HTTP messages.
# "Sun, 06 Nov 1994 08:49:37 GMT"
Mojo::Date->new(784111777)->to_string;
=head1 OPERATORS
L<Mojo::Date> overloads the following operators.
Expand Down
26 changes: 26 additions & 0 deletions t/mojo/date.t
Expand Up @@ -9,6 +9,30 @@ 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 3339
is(Mojo::Date->new('2014-08-20T20:45:00')->epoch,
1408567500, 'right epoch value');
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');
is(Mojo::Date->new('2014-08-20T20:45:00-00:46')->epoch,
1408564740, 'right epoch value');
is(Mojo::Date->new(1408564740)->to_datetime,
'2014-08-20T19:59:00Z', 'right format');
is(Mojo::Date->new('2014-08-20t20:45:00-01:46')->epoch,
1408561140, 'right epoch value');
is(Mojo::Date->new('2014-08-20t20:45:00+01:46')->epoch,
1408573860, 'right epoch value');
is(Mojo::Date->new(1408573860)->to_datetime,
'2014-08-20T22:31:00Z', 'right format');
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');

# RFC 850/1036
is(Mojo::Date->new('Sunday, 06-Nov-94 08:49:37 GMT')->epoch,
784111777, 'right epoch value');
Expand All @@ -35,6 +59,8 @@ is(Mojo::Date->new('Sun Nov 6 08:49:37 1994 GARBAGE')->epoch,
undef, 'no epoch value');
is(Mojo::Date->new('Fri, 75 May 2011 99:99:99 GMT')->epoch,
undef, 'no epoch value');
is(Mojo::Date->new('0000-00-00T00:00:00+01:00')->epoch,
undef, 'no epoch value');

# to_string
$date = Mojo::Date->new(784111777);
Expand Down

0 comments on commit 6c249f1

Please sign in to comment.