Skip to content

Commit

Permalink
Add support for Windows four-digit year format
Browse files Browse the repository at this point in the history
  • Loading branch information
kwrightgsg authored and oalders committed Jul 6, 2023
1 parent 2320580 commit 20de3bf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Change history for HTTP-Date
- Use copyright year rather than range (GH#18) (Olaf Alders)
- Replace "Test" with "Test::More" (GH#20) (James Raspass)
- Remove the executable bit from the test (GH#21) (James Raspass)
- Add support for Windows four-digit year format (Grant Street Group)

6.05 2019-11-19 03:02:32Z
- Bump minimum version of Time::Local to 1.28 (GH#17) (Olaf Alders)
Expand Down
7 changes: 4 additions & 3 deletions lib/HTTP/Date.pm
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ sub parse_date ($) {

||

# Windows 'dir' 11-12-96 03:52PM
# Windows 'dir': '11-12-96 03:52PM' and four-digit year variant
(
( $mon, $day, $yr, $hr, $min, $ampm )
= /^
(\d{2}) # numerical month
-
(\d{2}) # day
-
(\d{2}) # year
(\d{2,4}) # year
\s+
(\d\d?):(\d\d)([APap][Mm]) # hour:min AM or PM
\s*$
Expand Down Expand Up @@ -365,7 +365,8 @@ The function is able to parse the following formats:
"Feb 3 1994" -- Unix 'ls -l' format
"Feb 3 17:03" -- Unix 'ls -l' format
"11-15-96 03:52PM" -- Windows 'dir' format
"11-15-96 03:52PM" -- Windows 'dir' format
"11-15-1996 03:52PM" -- Windows 'dir' format with four-digit year
The parser ignores leading and trailing whitespace. It also allow the
seconds to be missing and the month to be numerical in most formats.
Expand Down
12 changes: 11 additions & 1 deletion t/date.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use strict;
use warnings;

use Test::More tests => 135;
use Test::More tests => 141;
use HTTP::Date;

# test str2time for supported dates. Test cases with 2 digit year
Expand Down Expand Up @@ -35,6 +35,7 @@ my (@tests) = (
'Feb 3 1994', # Unix 'ls -l' format

"02-03-94 12:00AM", # Windows 'dir' format
"02-03-1994 12:00AM", # Windows 'dir' format with four-digit year

# ISO 8601 formats
'1994-02-03 00:00:00 +0000',
Expand Down Expand Up @@ -157,6 +158,15 @@ is( $t, "1996-11-12 13:05:00" );
$t = time2iso( str2time("11-12-96 12:05PM") );
is( $t, "1996-11-12 12:05:00" );

$t = time2iso( str2time("11-12-01 12:00PM") );
is( $t, "2001-11-12 12:00:00" );

$t = time2iso( str2time("11-12-1996 12:00AM") );
is( $t, "1996-11-12 00:00:00" );

$t = time2iso( str2time("11-12-2022 12:00AM") );
is( $t, "2022-11-12 00:00:00" );

$t = str2time("2000-01-01 00:00:01.234");
note "FRAC $t = ", time2iso($t);
cmp_ok( abs( ( $t - int($t) ) - 0.234 ), '<', 0.000001 );
Expand Down

0 comments on commit 20de3bf

Please sign in to comment.