Skip to content

Commit

Permalink
[strptime] Parse out century if specified
Browse files Browse the repository at this point in the history
If we've been given a year that's got the century attached (i.e., the
year is > 1900), store the century.  Then use this information to defeat
the guessing behavior of timegm.
  • Loading branch information
perlpilot authored and atoomic committed Jan 16, 2020
1 parent e971110 commit d745098
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 13 additions & 7 deletions lib/Date/Parse.pm
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ sub {
my $dtstr = lc shift;
my $merid = 24;
my($year,$month,$day,$hh,$mm,$ss,$zone,$dst,$frac);
my($century,$year,$month,$day,$hh,$mm,$ss,$zone,$dst,$frac);
$zone = tz_offset(shift) if @_;
Expand Down Expand Up @@ -195,12 +195,15 @@ sub {
}
}
$year -= 1900 if defined $year && $year > 1900;
if (defined $year && $year > 1900) {
$century = int($year / 100);
$year -= 1900;
}
$zone += 3600 if defined $zone && $dst;
$ss += "0.$frac" if $frac;
return ($ss,$mm,$hh,$day,$month,$year,$zone);
return ($ss,$mm,$hh,$day,$month,$year,$zone,$century);
}
ESQ

Expand Down Expand Up @@ -233,7 +236,7 @@ sub str2time
return undef
unless @t;

my($ss,$mm,$hh,$day,$month,$year,$zone) = @t;
my($ss,$mm,$hh,$day,$month,$year,$zone, $century) = @t;
my @lt = localtime(time);

$hh ||= 0;
Expand All @@ -252,6 +255,9 @@ sub str2time
$year = ($month > $lt[4]) ? ($lt[5] - 1) : $lt[5]
unless(defined $year);

# we were given a 4 digit year, so let's keep using those
$year += 1900 if defined $century;

return undef
unless($month <= 11 && $day >= 1 && $day <= 31
&& $hh <= 23 && $mm <= 59 && $ss <= 59);
Expand Down Expand Up @@ -317,9 +323,9 @@ date string does not specify a timezone.
=item strptime(DATE [, ZONE])
C<strptime> takes the same arguments as str2time but returns an array of
values C<($ss,$mm,$hh,$day,$month,$year,$zone)>. Elements are only defined
if they could be extracted from the date string. The C<$zone> element is
the timezone offset in seconds from GMT. An empty array is returned upon
values C<($ss,$mm,$hh,$day,$month,$year,$zone,$century)>. Elements are only
defined if they could be extracted from the date string. The C<$zone> element
is the timezone offset in seconds from GMT. An empty array is returned upon
failure.
=back
Expand Down
2 changes: 1 addition & 1 deletion t/cpanrt.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ my $i = 1;
my @t = strptime($str);
my $t = join ":", map { defined($_) ? $_ : "-" } @t;
print "# $str => $t\n";
print "not " unless $t eq "-:35:22:30:10:108:3600";
print "not " unless $t eq "-:35:22:30:10:108:3600:20";
print "ok ", $i++, "\n";
}
}
Expand Down

0 comments on commit d745098

Please sign in to comment.