Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change RFC3339_RE to accept offset like +08:00,+0800 or +08/+8 #1167

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions lib/Mojo/Date.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ use Time::Local 'timegm';

has epoch => sub {time};

my $RFC3339_RE = qr/
^(\d+)-(\d+)-(\d+)\D+(\d+):(\d+):(\d+(?:\.\d+)?) # Date and time
(?:Z|([+-])(\d+):(\d+))?$ # Offset
my $ISO8601_RE = qr/
^(\d{4}(?!\d{2}\b)) # Years
(?:(-?)(?:(\d\d)(?:\2(\d\d))? # Calendar dates
|W(\d\d)-?([1-7])? # Week dates
|(\d{3})) # Ordinal dates
(?:(?:T|\s+)(\d\d)(?:(:?)(\d\d))?(?:\9(\d\d(?:[\.,]\d+)?)?)? # Time
(?:Z|([\+-])(\d{1,2}):?(\d{2})?)?)?)?$ # Offset
/xi;

my @DAYS = qw(Sun Mon Tue Wed Thu Fri Sat);
Expand All @@ -32,10 +36,27 @@ sub parse {
($day, $month, $year, $h, $m, $s) = ($1, $MONTHS{$2}, $3, $4, $5, $6);
}

# RFC 3339 (1994-11-06T08:49:37Z)
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;
# ISO 8601 (2000-01-02 03:04:05.678+0900)
# Groups: 2000,-,01,02,undef,undef,undef,03,:,04,05.678,+,09,00
elsif ($date =~ $ISO8601_RE) {
($year, $h, $m, $s) = ($1, $8//0, $10//0, $11//0);
if ( $day = $7 or my $week = $5 ) {
$month = 0;
unless($day) {
my $days = 0;
$days += _is_leap_year($_)?366:365 for (1970..($year - 1));
my $wday_offset = ($days + 3) % 7; # first day of 1970 is 4(Thu)
$day = ($week - 1) * 7 + ($6 // 1) - $wday_offset;
}
my $n = _is_leap_year($year)? 29 : 28;
my @m = (31,$n,31,30,31,30,31,31,30,31,30,31);
for (@m) { last if $day <= $_; $day -= $_; $month++ }
}
else {
($month, $day) = (($3 // 1) - 1, $4 // 1);
}
$offset = (($13 * 3600) + (($14 // 0) * 60)) * ($12 eq '+' ? -1 : 1) if $12;
$s =~ s/,/./g; # must be at last else $n will be replaced
}

# ANSI C asctime() (Sun Nov 6 08:49:37 1994)
Expand Down Expand Up @@ -68,6 +89,14 @@ sub to_string {
$MONTHS[$month], $year + 1900, $h, $m, $s;
}

sub _is_leap_year {
return 0 if $_[0] % 4;
return 1 if $_[0] % 100;
return 0 if $_[0] % 400;

return 1;
}

1;

=encoding utf8
Expand Down Expand Up @@ -144,6 +173,16 @@ Parse date.
say Mojo::Date->new('1994-11-06T08:49:37+01:00')->epoch;
say Mojo::Date->new('1994-11-06T08:49:37-01:00')->epoch;

# ISO 8601
# Support almost all format described in ISO 8601
# For partial date time like '1994-11 01:01' will fallback to
# '1994-11-01 01::01::00'. However, for digital only date
# like '2014' or '20141102' will be treated as epoch instead
# of ISO 8601 date, e.g. '2014' => 1970-01-01T00:33:34
say Mojo::Date->new('19941106T084937+0100')->epoch;
say Mojo::Date->new('1994-1106T08:49:37+0630')->epoch;
say Mojo::Date->new('2017-355T08:49:37+0630')->epoch;

=head2 to_datetime

my $str = $date->to_datetime;
Expand Down
30 changes: 30 additions & 0 deletions t/mojo/date.t
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,36 @@ is(Mojo::Date->new('1994-11-06t08:49:37.33z')->epoch,
is(Mojo::Date->new(784111777.33)->to_datetime,
'1994-11-06T08:49:37.33Z', 'right format');

# ISO 8601
is(Mojo::Date->new('20140820T204500')->epoch,
1408567500, 'date time format yyyymmddTHHMMSS');
is(Mojo::Date->new('20140820T204500+00')->epoch,
1408567500, 'date time format yyyymmddTHHMMSS+00');
is(Mojo::Date->new('20140820T214500+01')->epoch,
1408567500, 'date time format yyyymmddTHHMMSS+HH');
is(Mojo::Date->new('20140820T221500+130')->epoch,
1408567500, 'date time format yyyymmddTHHMMSS+HMM');
is(Mojo::Date->new('2014-08-20 22:15:00.05+0130')->epoch,
1408567500.05, 'date time format yyyy-mm-ddTHH:MM:SS.NN+HHMM');
is(Mojo::Date->new('2014-08-20T19:15:00,06-0130')->epoch,
1408567500.06, 'date time format yyyy-mm-ddTHH:MM:SS,NN-HHMM');
is(Mojo::Date->new('2014-232T20:45:00')->epoch,
1408567500, 'date time format yyyy-DDDTHH:MM:SS');
is(Mojo::Date->new('2014-W34-3T20:45:00')->epoch,
1408567500, 'date time format yyyy-Www-DTHH:MM:SS');
is(Mojo::Date->new('2014W343T20:45:00')->epoch,
1408567500, 'date time format yyyyWwwDTHH:MM:SS');
is(Mojo::Date->new('2014-01')->epoch,
1388534400, 'date format yyyy-mm');
is(Mojo::Date->new('2014-01-01')->epoch,
1388534400, 'date format yyyy-mm-dd');
is(Mojo::Date->new('2014-01-01T00')->epoch,
1388534400, 'date time format yyyy-mm-ddTHH');
is(Mojo::Date->new('2014-01-01T00:00')->epoch,
1388534400, 'date time format yyyy-mm-ddTHH:MM');
is(Mojo::Date->new('2014-01-01T00:00+00')->epoch,
1388534400, 'date time format yyyy-mm-ddTHH:MM+HH');

# Special cases
is(Mojo::Date->new('Sun , 06-Nov-1994 08:49:37 UTC')->epoch,
784111777, 'right epoch value');
Expand Down