blib/lib/HTTP/Date.pm

filter: Statementstmt 98.9% 90 / 91 Branchbran 89.7% 61 / 68 Conditioncond 97.3% 37 / 38 MC/DCmcdc 94.1% 16 / 17 Subroutinesub 100% 6 / 6 Podpod 100% 5 / 5 totaltotal 95.5% 215 / 225 CC 48 SCAR 31.0
CC
48
Cov
95%
parse_date
30.2
str2time
16.0
time2str
2.0
CRAP 22.2
↑ Summary
1 package HTTP::Date;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 our $VERSION = '6.08';
6  
7 5 require Exporter;
8 5 our @ISA = qw(Exporter);
9 5 our @EXPORT = qw(time2str str2time);
10 5 our @EXPORT_OK = qw(parse_date time2iso time2isoz);
11  
12 5 require Time::Local;
13  
14 5 our ( @DoW, @MoY, %MoY );
15 5 @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
16 5 @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
17 5 @MoY{@MoY} = ( 1 .. 12 );
18  
19 5 my %GMT_ZONE = ( GMT => 1, UTC => 1, UT => 1, Z => 1 );
20  
21 1 ▶sub time2str (;$) {
Subroutine time2str: called CC=2 SCAR=7.0
Pod documented
22 1 my $time = shift;
23 1 ▶ $time = time unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 0 1 1
24 1 my ( $sec, $min, $hour, $mday, $mon, $year, $wday ) = gmtime($time);
25 1 sprintf(
26 "%s, %02d %s %04d %02d:%02d:%02d GMT",
27 $DoW[$wday],
28 $mday, $MoY[$mon], $year + 1900,
29 $hour, $min, $sec
30 );
31 }
32  
33 142 ▶sub str2time ($;$) {
Subroutine str2time: called CC=16 SCAR=27.7
Pod documented
34 142 my $str = shift;
35 142 ▶ return undef unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 1 141 142
36  
37 # fast exit for strictly conforming string
38 141 ▶ if ( $str
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^[SMTWF][a-z][a-z], ([0-9][0-9]) ([JFMAJSOND][a-z][a-z]) ([0-9][0-9][0-9][0-9]) ([0-9][0-9]):([0-9][0-9]):([0-9][0-9]) GMT$/) 3 138 141
39 =~ /^[SMTWF][a-z][a-z], ([0-9][0-9]) ([JFMAJSOND][a-z][a-z]) ([0-9][0-9][0-9][0-9]) ([0-9][0-9]):([0-9][0-9]):([0-9][0-9]) GMT$/
40 ) {
41 3 return eval {
42 3 my $t = Time::Local::timegm( $6, $5, $4, $1, $MoY{$2} - 1, $3 );
43 3 ▶ $t < 0 ? undef : $t;
Branch: 1 branch
BranchTrueFalseTotal
$t < 0 ? : 1 2 3
44 };
45 }
46  
47 138 my @d = parse_date($str);
48 138 ▶ return undef unless @d;
Branch: 1 branch
BranchTrueFalseTotal
unless @d 12 126 138
49 126 $d[1]--; # month
50  
51 126 my $tz = pop(@d);
52 126 ▶ unless ( defined $tz ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tz) 63 63 126
53 63 ▶ unless ( defined( $tz = shift ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($tz = shift())) 20 43 63
54 20 return eval {
55 20 my $frac = $d[-1];
56 20 $frac -= ( $d[-1] = int($frac) );
57 20 my $t = Time::Local::timelocal( reverse @d ) + $frac;
58 15 ▶ $t < 0 ? undef : $t;
Branch: 1 branch
BranchTrueFalseTotal
$t < 0 ? : 1 14 15
59 };
60 }
61 }
62  
63 106 my $offset = 0;
64 106 ▶ if ( $GMT_ZONE{ uc $tz } ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($GMT_ZONE{uc $tz}) { } 73 33 106
elsif ($tz =~ /^([-+])?([0-9][0-9]?):?([0-9][0-9])?$/) { } 29 4 33
65  
66 # offset already zero
67 }
68 elsif ( $tz =~ /^([-+])?([0-9][0-9]?):?([0-9][0-9])?$/ ) {
69 29 $offset = 3600 * $2;
70 29 ▶ $offset += 60 * $3 if $3;
Branch: 1 branch
BranchTrueFalseTotal
if $3 29 0 29
71 29 ▶ $offset *= -1 if $1 && $1 eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $1 and $1 eq "-" 9 20 29
Condition100%3/3
$1 and $1 eq "-"
!ll&&!rl&&r
8129
MC/DC100%2/2
$1 and $1 eq "-"
$1 $1 eq "-"
Truth table100%3/3
A and B
A= $1B= $1 eq "-"
ABresult
0X0
100
111
72 }
73 else {
74 4 ▶ eval { require Time::Zone } || return undef;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Time::Zone } } or return undef 4 0 4
75 4 $offset = Time::Zone::tz_offset($tz);
76 4 ▶ return undef unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 1 3 4
77 }
78  
79 105 return eval {
80 105 my $frac = $d[-1];
81 105 $frac -= ( $d[-1] = int($frac) );
82 105 my $t = Time::Local::timegm( reverse @d ) + $frac;
83 105 ▶ $t < 0 ? undef : $t - $offset;
Branch: 1 branch
BranchTrueFalseTotal
$t < 0 ? : 1 104 105
84 };
85 }
86  
87 157 ▶sub parse_date ($) {
Subroutine parse_date: called CC=30 SCAR=34.1
Pod documented
88 157 local ($_) = shift;
89 157 ▶ return unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 157 157
90  
91 # Reject over-long input up front, before any regex runs, so hostile
92 # strings cannot drive the parsing regexes into pathological backtracking.
93 # Length is measured as given (leading/trailing whitespace included). This
94 # cap is a security limit, not a tunable: every format we accept is far
95 # shorter, and the parsing regexes still contain adjacent unbounded
96 # quantifiers, so raising it demands re-benchmarking against hostile input.
97 157 ▶ return if length($_) > 64;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ > 64 6 151 157
98  
99 # More lax parsing below
100 151 s/^\s+//; # kill leading space
101 151 s/^(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)[a-z]*,?\s*//i; # Useless weekday
102  
103 151 my ( $day, $mon, $yr, $hr, $min, $sec, $tz, $ampm );
104  
105 # Then we are able to check for most of the formats with this regexp
106 (
107 151 ▶ ( $day, $mon, $yr, $hr, $min, $sec, $tz )
Branch: 1 branch
BranchTrueFalseTotal
() = m[^ ([0-9][0-9]?) # day (?:\s+|[-/]) (\w+) # month (?:\s+|[-/]) ([0-9]+) # year (?: (?:\s+|:) # separator before clock ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds )? # optional clock \s* ([-+]?[0-9]{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone \s* (?:\(\w+\)|\w{3,})? # ASCII representation of timezone. \s*$ ]x or ($mon, $day, $hr, $min, $sec, $tz, $yr) = /^ (\w{1,3}) # month \s+ ([0-9][0-9]?) # day \s+ ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds \s+ (?:([A-Za-z]+)\s+)? # optional timezone ([0-9]+) # year \s*$ # allow trailing whitespace /x or ($mon, $day, $yr, $hr, $min, $sec) = /^ (\w{3}) # month \s+ ([0-9][0-9]?) # day \s+ (?: ([0-9][0-9][0-9][0-9]) | # year ([0-9]{1,2}):([0-9]{2}) # hour:min (?::([0-9][0-9]))? # optional seconds ) \s*$ /x or ($yr, $mon, $day, $hr, $min, $sec, $tz) = m[^ ([0-9]{4}) # year [-/]? ([0-9][0-9]?) # numerical month [-/]? ([0-9][0-9]?) # day (?: (?:\s+|[-:Tt]) # separator before clock ([0-9][0-9]?):?([0-9][0-9]) # hour:min (?::?([0-9][0-9](?:\.[0-9]*)?))? # optional seconds (and fractional) )? # optional clock \s* ([-+]?[0-9][0-9]?:?(?:[0-9][0-9])? |Z|z)? # timezone (Z is "zero meridian", i.e. GMT) \s*$ ]x or ($mon, $day, $yr, $hr, $min, $ampm) = /^ ([0-9]{2}) # numerical month - ([0-9]{2}) # day - ([0-9]{2,4}) # year \s+ ([0-9][0-9]?):([0-9][0-9])([APap][Mm]) # hour:min AM or PM \s*$ /x or return 139 12 151
Condition100%12/12
() = m[^ ([0-9][0-9]?) # day (?:\s+|[-/]) (\w+) # month (?:\s+|[-/]) ([0-9]+) # year (?: (?:\s+|:) # separator before clock ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds )? # optional clock \s* ([-+]?[0-9]{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone \s* (?:\(\w+\)|\w{3,})? # ASCII representation of timezone. \s*$ ]x or ($mon, $day, $hr, $min, $sec, $tz, $yr) = /^ (\w{1,3}) # month \s+ ([0-9][0-9]?) # day \s+ ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds \s+ (?:([A-Za-z]+)\s+)? # optional timezone ([0-9]+) # year \s*$ # allow trailing whitespace /x or ($mon, $day, $yr, $hr, $min, $sec) = /^ (\w{3}) # month \s+ ([0-9][0-9]?) # day \s+ (?: ([0-9][0-9][0-9][0-9]) | # year ([0-9]{1,2}):([0-9]{2}) # hour:min (?::([0-9][0-9]))? # optional seconds ) \s*$ /x or ($yr, $mon, $day, $hr, $min, $sec, $tz) = m[^ ([0-9]{4}) # year [-/]? ([0-9][0-9]?) # numerical month [-/]? ([0-9][0-9]?) # day (?: (?:\s+|[-:Tt]) # separator before clock ([0-9][0-9]?):?([0-9][0-9]) # hour:min (?::?([0-9][0-9](?:\.[0-9]*)?))? # optional seconds (and fractional) )? # optional clock \s* ([-+]?[0-9][0-9]?:?(?:[0-9][0-9])? |Z|z)? # timezone (Z is "zero meridian", i.e. GMT) \s*$ ]x or ($mon, $day, $yr, $hr, $min, $ampm) = /^ ([0-9]{2}) # numerical month - ([0-9]{2}) # day - ([0-9]{2,4}) # year \s+ ([0-9][0-9]?):([0-9][0-9])([APap][Mm]) # hour:min AM or PM \s*$ /x
l!l&&r!l&&!r
1221712
() = m[^ ([0-9][0-9]?) # day (?:\s+|[-/]) (\w+) # month (?:\s+|[-/]) ([0-9]+) # year (?: (?:\s+|:) # separator before clock ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds )? # optional clock \s* ([-+]?[0-9]{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone \s* (?:\(\w+\)|\w{3,})? # ASCII representation of timezone. \s*$ ]x or ($mon, $day, $hr, $min, $sec, $tz, $yr) = /^ (\w{1,3}) # month \s+ ([0-9][0-9]?) # day \s+ ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds \s+ (?:([A-Za-z]+)\s+)? # optional timezone ([0-9]+) # year \s*$ # allow trailing whitespace /x or ($mon, $day, $yr, $hr, $min, $sec) = /^ (\w{3}) # month \s+ ([0-9][0-9]?) # day \s+ (?: ([0-9][0-9][0-9][0-9]) | # year ([0-9]{1,2}):([0-9]{2}) # hour:min (?::([0-9][0-9]))? # optional seconds ) \s*$ /x or ($yr, $mon, $day, $hr, $min, $sec, $tz) = m[^ ([0-9]{4}) # year [-/]? ([0-9][0-9]?) # numerical month [-/]? ([0-9][0-9]?) # day (?: (?:\s+|[-:Tt]) # separator before clock ([0-9][0-9]?):?([0-9][0-9]) # hour:min (?::?([0-9][0-9](?:\.[0-9]*)?))? # optional seconds (and fractional) )? # optional clock \s* ([-+]?[0-9][0-9]?:?(?:[0-9][0-9])? |Z|z)? # timezone (Z is "zero meridian", i.e. GMT) \s*$ ]x
l!l&&r!l&&!r
824029
() = m[^ ([0-9][0-9]?) # day (?:\s+|[-/]) (\w+) # month (?:\s+|[-/]) ([0-9]+) # year (?: (?:\s+|:) # separator before clock ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds )? # optional clock \s* ([-+]?[0-9]{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone \s* (?:\(\w+\)|\w{3,})? # ASCII representation of timezone. \s*$ ]x or ($mon, $day, $hr, $min, $sec, $tz, $yr) = /^ (\w{1,3}) # month \s+ ([0-9][0-9]?) # day \s+ ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds \s+ (?:([A-Za-z]+)\s+)? # optional timezone ([0-9]+) # year \s*$ # allow trailing whitespace /x or ($mon, $day, $yr, $hr, $min, $sec) = /^ (\w{3}) # month \s+ ([0-9][0-9]?) # day \s+ (?: ([0-9][0-9][0-9][0-9]) | # year ([0-9]{1,2}):([0-9]{2}) # hour:min (?::([0-9][0-9]))? # optional seconds ) \s*$ /x
l!l&&r!l&&!r
77569
() = m[^ ([0-9][0-9]?) # day (?:\s+|[-/]) (\w+) # month (?:\s+|[-/]) ([0-9]+) # year (?: (?:\s+|:) # separator before clock ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds )? # optional clock \s* ([-+]?[0-9]{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone \s* (?:\(\w+\)|\w{3,})? # ASCII representation of timezone. \s*$ ]x or ($mon, $day, $hr, $min, $sec, $tz, $yr) = /^ (\w{1,3}) # month \s+ ([0-9][0-9]?) # day \s+ ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds \s+ (?:([A-Za-z]+)\s+)? # optional timezone ([0-9]+) # year \s*$ # allow trailing whitespace /x
l!l&&r!l&&!r
71674
MC/DC100%5/5
() = m[^ ([0-9][0-9]?) # day (?:\s+|[-/]) (\w+) # month (?:\s+|[-/]) ([0-9]+) # year (?: (?:\s+|:) # separator before clock ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds )? # optional clock \s* ([-+]?[0-9]{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone \s* (?:\(\w+\)|\w{3,})? # ASCII representation of timezone. \s*$ ]x or ($mon, $day, $hr, $min, $sec, $tz, $yr) = /^ (\w{1,3}) # month \s+ ([0-9][0-9]?) # day \s+ ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds \s+ (?:([A-Za-z]+)\s+)? # optional timezone ([0-9]+) # year \s*$ # allow trailing whitespace /x or ($mon, $day, $yr, $hr, $min, $sec) = /^ (\w{3}) # month \s+ ([0-9][0-9]?) # day \s+ (?: ([0-9][0-9][0-9][0-9]) | # year ([0-9]{1,2}):([0-9]{2}) # hour:min (?::([0-9][0-9]))? # optional seconds ) \s*$ /x or ($yr, $mon, $day, $hr, $min, $sec, $tz) = m[^ ([0-9]{4}) # year [-/]? ([0-9][0-9]?) # numerical month [-/]? ([0-9][0-9]?) # day (?: (?:\s+|[-:Tt]) # separator before clock ([0-9][0-9]?):?([0-9][0-9]) # hour:min (?::?([0-9][0-9](?:\.[0-9]*)?))? # optional seconds (and fractional) )? # optional clock \s* ([-+]?[0-9][0-9]?:?(?:[0-9][0-9])? |Z|z)? # timezone (Z is "zero meridian", i.e. GMT) \s*$ ]x or ($mon, $day, $yr, $hr, $min, $ampm) = /^ ([0-9]{2}) # numerical month - ([0-9]{2}) # day - ([0-9]{2,4}) # year \s+ ([0-9][0-9]?):([0-9][0-9])([APap][Mm]) # hour:min AM or PM \s*$ /x
() = m[^ ([0-9][0-9]?) # day (?:\s+|[-/]) (\w+) # month (?:\s+|[-/]) ([0-9]+) # year (?: (?:\s+|:) # separator before clock ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds )? # optional clock \s* ([-+]?[0-9]{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone \s* (?:\(\w+\)|\w{3,})? # ASCII representation of timezone. \s*$ ]x ($mon, $day, $hr, $min, $sec, $tz, $yr) = /^ (\w{1,3}) # month \s+ ([0-9][0-9]?) # day \s+ ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds \s+ (?:([A-Za-z]+)\s+)? # optional timezone ([0-9]+) # year \s*$ # allow trailing whitespace /x ($mon, $day, $yr, $hr, $min, $sec) = /^ (\w{3}) # month \s+ ([0-9][0-9]?) # day \s+ (?: ([0-9][0-9][0-9][0-9]) | # year ([0-9]{1,2}):([0-9]{2}) # hour:min (?::([0-9][0-9]))? # optional seconds ) \s*$ /x ($yr, $mon, $day, $hr, $min, $sec, $tz) = m[^ ([0-9]{4}) # year [-/]? ([0-9][0-9]?) # numerical month [-/]? ([0-9][0-9]?) # day (?: (?:\s+|[-:Tt]) # separator before clock ([0-9][0-9]?):?([0-9][0-9]) # hour:min (?::?([0-9][0-9](?:\.[0-9]*)?))? # optional seconds (and fractional) )? # optional clock \s* ([-+]?[0-9][0-9]?:?(?:[0-9][0-9])? |Z|z)? # timezone (Z is "zero meridian", i.e. GMT) \s*$ ]x ($mon, $day, $yr, $hr, $min, $ampm) = /^ ([0-9]{2}) # numerical month - ([0-9]{2}) # day - ([0-9]{2,4}) # year \s+ ([0-9][0-9]?):([0-9][0-9])([APap][Mm]) # hour:min AM or PM \s*$ /x
Truth table100%6/6
A or B or C or D or E
A= () = m[^ ([0-9][0-9]?) # day (?:\s+|[-/]) (\w+) # month (?:\s+|[-/]) ([0-9]+) # year (?: (?:\s+|:) # separator before clock ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds )? # optional clock \s* ([-+]?[0-9]{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone \s* (?:\(\w+\)|\w{3,})? # ASCII representation of timezone. \s*$ ]xB= ($mon, $day, $hr, $min, $sec, $tz, $yr) = /^ (\w{1,3}) # month \s+ ([0-9][0-9]?) # day \s+ ([0-9][0-9]?):([0-9][0-9]) # hour:min (?::([0-9][0-9]))? # optional seconds \s+ (?:([A-Za-z]+)\s+)? # optional timezone ([0-9]+) # year \s*$ # allow trailing whitespace /xC= ($mon, $day, $yr, $hr, $min, $sec) = /^ (\w{3}) # month \s+ ([0-9][0-9]?) # day \s+ (?: ([0-9][0-9][0-9][0-9]) | # year ([0-9]{1,2}):([0-9]{2}) # hour:min (?::([0-9][0-9]))? # optional seconds ) \s*$ /xD= ($yr, $mon, $day, $hr, $min, $sec, $tz) = m[^ ([0-9]{4}) # year [-/]? ([0-9][0-9]?) # numerical month [-/]? ([0-9][0-9]?) # day (?: (?:\s+|[-:Tt]) # separator before clock ([0-9][0-9]?):?([0-9][0-9]) # hour:min (?::?([0-9][0-9](?:\.[0-9]*)?))? # optional seconds (and fractional) )? # optional clock \s* ([-+]?[0-9][0-9]?:?(?:[0-9][0-9])? |Z|z)? # timezone (Z is "zero meridian", i.e. GMT) \s*$ ]xE= ($mon, $day, $yr, $hr, $min, $ampm) = /^ ([0-9]{2}) # numerical month - ([0-9]{2}) # day - ([0-9]{2,4}) # year \s+ ([0-9][0-9]?):([0-9][0-9])([APap][Mm]) # hour:min AM or PM \s*$ /x
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
108 = /^
109 ([0-9][0-9]?) # day
110 (?:\s+|[-\/])
111 (\w+) # month
112 (?:\s+|[-\/])
113 ([0-9]+) # year
114 (?:
115 (?:\s+|:) # separator before clock
116 ([0-9][0-9]?):([0-9][0-9]) # hour:min
117 (?::([0-9][0-9]))? # optional seconds
118 )? # optional clock
119 \s*
120 ([-+]?[0-9]{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone
121 \s*
122 (?:\(\w+\)|\w{3,})? # ASCII representation of timezone.
123 \s*$
124 /x
125 )
126  
127 ||
128  
129 # Try the ctime and asctime format
130 (
131 ( $mon, $day, $hr, $min, $sec, $tz, $yr )
132 = /^
133 (\w{1,3}) # month
134 \s+
135 ([0-9][0-9]?) # day
136 \s+
137 ([0-9][0-9]?):([0-9][0-9]) # hour:min
138 (?::([0-9][0-9]))? # optional seconds
139 \s+
140 (?:([A-Za-z]+)\s+)? # optional timezone
141 ([0-9]+) # year
142 \s*$ # allow trailing whitespace
143 /x
144 )
145  
146 ||
147  
148 # Then the Unix 'ls -l' date format
149 (
150 ( $mon, $day, $yr, $hr, $min, $sec )
151 = /^
152 (\w{3}) # month
153 \s+
154 ([0-9][0-9]?) # day
155 \s+
156 (?:
157 ([0-9][0-9][0-9][0-9]) | # year
158 ([0-9]{1,2}):([0-9]{2}) # hour:min
159 (?::([0-9][0-9]))? # optional seconds
160 )
161 \s*$
162 /x
163 )
164  
165 ||
166  
167 # ISO 8601 format '1996-02-29 12:00:00 -0100' and variants
168 (
169 ( $yr, $mon, $day, $hr, $min, $sec, $tz )
170 = /^
171 ([0-9]{4}) # year
172 [-\/]?
173 ([0-9][0-9]?) # numerical month
174 [-\/]?
175 ([0-9][0-9]?) # day
176 (?:
177 (?:\s+|[-:Tt]) # separator before clock
178 ([0-9][0-9]?):?([0-9][0-9]) # hour:min
179 (?::?([0-9][0-9](?:\.[0-9]*)?))? # optional seconds (and fractional)
180 )? # optional clock
181 \s*
182 ([-+]?[0-9][0-9]?:?(?:[0-9][0-9])?
183 |Z|z)? # timezone (Z is "zero meridian", i.e. GMT)
184 \s*$
185 /x
186 )
187  
188 ||
189  
190 # Windows 'dir': '11-12-96 03:52PM' and four-digit year variant
191 (
192 ( $mon, $day, $yr, $hr, $min, $ampm )
193 = /^
194 ([0-9]{2}) # numerical month
195 -
196 ([0-9]{2}) # day
197 -
198 ([0-9]{2,4}) # year
199 \s+
200 ([0-9][0-9]?):([0-9][0-9])([APap][Mm]) # hour:min AM or PM
201 \s*$
202 /x
203 )
204  
205 || return; # unrecognized format
206  
207 # Translate month name to number
208 $mon
209 = $MoY{$mon}
210 139 ▶ || $MoY{"\u\L$mon"}
Condition94%16/17
$HTTP::Date::MoY{$mon} || $HTTP::Date::MoY{ucfirst lc $mon} || $mon =~ /^[0-9][0-9]?$/ && $mon >= 1 && $mon <= 12 && int $mon || (return)
l!l
1354
$HTTP::Date::MoY{$mon} or $HTTP::Date::MoY{ucfirst lc $mon} or $mon =~ /^[0-9][0-9]?$/ and $mon >= 1 and $mon <= 12 and int $mon
l!l&&r!l&&!r
77584
$HTTP::Date::MoY{$mon} or $HTTP::Date::MoY{ucfirst lc $mon}
l!l&&r!l&&!r
334462
$mon =~ /^[0-9][0-9]?$/ and $mon >= 1 and $mon <= 12 and int $mon
!ll&&!rl&&r
4058
$mon =~ /^[0-9][0-9]?$/ and $mon >= 1 and $mon <= 12
!ll&&!rl&&r
2258
$mon =~ /^[0-9][0-9]?$/ and $mon >= 1
!ll&&!rl&&r
1160
MC/DC83%5/6
$HTTP::Date::MoY{$mon} || $HTTP::Date::MoY{ucfirst lc $mon} || $mon =~ /^[0-9][0-9]?$/ && $mon >= 1 && $mon <= 12 && int $mon || (return)
$HTTP::Date::MoY{$mon} $HTTP::Date::MoY{ucfirst lc $mon} $mon =~ /^[0-9][0-9]?$/ $mon >= 1 $mon <= 12 int $mon
Truth table85%6/7
A or B or C and D and E and F
A= $HTTP::Date::MoY{$mon}B= $HTTP::Date::MoY{ucfirst lc $mon}C= $mon =~ /^[0-9][0-9]?$/D= $mon >= 1E= $mon <= 12F= int $mon
ABCDEFresult
1XXXXX1
01XXXX1
0011111
000XXX0
0010XX0
00110X0
0011100
211 || ( $mon =~ /^[0-9][0-9]?$/ && $mon >= 1 && $mon <= 12 && int($mon) )
212 || return;
213  
214 # If the year is missing, we assume first date before the current,
215 # because of the formats we support such dates are mostly present
216 # on "ls -l" listings.
217 135 ▶ unless ( defined $yr ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $yr) { } 134 1 135
if (do { length $yr < 3 }) 23 111 134
218 1 my $cur_mon;
219 1 ( $cur_mon, $yr ) = (localtime)[ 4, 5 ];
220 1 $yr += 1900;
221 1 $cur_mon++;
222 1 ▶ $yr-- if $mon > $cur_mon;
Branch: 1 branch
BranchTrueFalseTotal
if $mon > $cur_mon 0 1 1
223 }
224 0 elsif ( length($yr) < 3 ) {
225  
226 # Find "obvious" year
227 23 my $cur_yr = (localtime)[5] + 1900;
228 23 my $m = $cur_yr % 100;
229 23 my $tmp = $yr;
230 23 $yr += $cur_yr - $m;
231 23 $m -= $tmp;
232 23 ▶ $yr += ( $m > 0 ) ? 100 : -100
Branch: 2 branches
BranchTrueFalseTotal
if abs $m > 50 21 2 23
$m > 0 ? : 0 21 21
233 if abs($m) > 50;
234 }
235  
236 # Make sure clock elements are defined
237 135 ▶ $hr = 0 unless defined($hr);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hr 34 101 135
238 135 ▶ $min = 0 unless defined($min);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min 34 101 135
239 135 ▶ $sec = 0 unless defined($sec);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sec 65 70 135
240  
241 # Compensate for AM/PM
242 135 ▶ if ($ampm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ampm) 17 118 135
243 17 $ampm = uc $ampm;
244 17 ▶ $hr = 0 if $hr == 12 && $ampm eq 'AM';
Branch: 1 branch
BranchTrueFalseTotal
if $hr == 12 and $ampm eq "AM" 10 7 17
Condition100%3/3
$hr == 12 and $ampm eq "AM"
!ll&&!rl&&r
4310
MC/DC100%2/2
$hr == 12 and $ampm eq "AM"
$hr == 12 $ampm eq "AM"
Truth table100%3/3
A and B
A= $hr == 12B= $ampm eq "AM"
ABresult
0X0
100
111
245 17 ▶ $hr += 12 if $ampm eq 'PM' && $hr != 12;
Branch: 1 branch
BranchTrueFalseTotal
if $ampm eq "PM" and $hr != 12 2 15 17
Condition100%3/3
$ampm eq "PM" and $hr != 12
!ll&&!rl&&r
1232
MC/DC100%2/2
$ampm eq "PM" and $hr != 12
$ampm eq "PM" $hr != 12
Truth table100%3/3
A and B
A= $ampm eq "PM"B= $hr != 12
ABresult
0X0
100
111
246 }
247  
248 135 ▶ return ( $yr, $mon, $day, $hr, $min, $sec, $tz )
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 131 4 135
249 if wantarray;
250  
251 4 ▶ if ( defined $tz ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tz) { } 2 2 4
252 2 ▶ $tz = "Z" if $tz =~ /^(GMT|UTC?|[-+]?0+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $tz =~ /^(GMT|UTC?|[-+]?0+)$/ 2 0 2
253 }
254 else {
255 2 $tz = "";
256 }
257 4 return sprintf(
258 "%04d-%02d-%02d %02d:%02d:%02d%s",
259 $yr, $mon, $day, $hr, $min, $sec, $tz
260 );
261 }
262  
263 14 ▶sub time2iso (;$) {
Subroutine time2iso: called CC=2 SCAR=6.9
Pod documented
264 14 my $time = shift;
265 14 ▶ $time = time unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 1 13 14
266 14 my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime($time);
267 14 sprintf(
268 "%04d-%02d-%02d %02d:%02d:%02d",
269 $year + 1900, $mon + 1, $mday, $hour, $min, $sec
270 );
271 }
272  
273 2 ▶sub time2isoz (;$) {
Subroutine time2isoz: called CC=2 SCAR=6.9
Pod documented
274 2 my $time = shift;
275 2 ▶ $time = time unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 1 1 2
276 2 my ( $sec, $min, $hour, $mday, $mon, $year ) = gmtime($time);
277 2 sprintf(
278 "%04d-%02d-%02d %02d:%02d:%02dZ",
279 $year + 1900, $mon + 1, $mday, $hour, $min, $sec
280 );
281 }
282  
283 5 1;
284  
285 # ABSTRACT: HTTP::Date - date conversion routines
286 #
287  
288 __END__
↑ Summary