Flexible.pm

filter: Statementstmt 92.4% 318 / 344 Branchbran 73.8% 93 / 126 Conditioncond 47.0% 8 / 17 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 83.6% 41 / 49 Podpod 100% 2 / 2 totaltotal 84.8% 464 / 547 CC 71 SCAR 24.1
CC
71
Cov
86%
_parse_timezone
24.4
_do_math
20.0
_set_year
19.0
CRAP 11.2
↑ Summary Flexible/lang.pm »
1 package DateTime::Format::Flexible;
2 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 22 our $VERSION = '0.34';
6  
7 22 ▶use base 'DateTime::Format::Builder';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 22 ▶use DateTime::Format::Flexible::lang;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use DateTime::Infinite;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 22 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 22 my $DELIM = qr{(?:\\|\/|-|'|\.|\s)};
15 22 my $HMSDELIM = qr{(?:\.|:)};
16 22 my $YEAR = qr{(\d{1,4})};
17 22 my $MON = qr{([0-1]?\d)};
18 22 my $DAY = qr{([0-3]?\d)};
19 22 my $HOUR = qr{([0-2]?\d)};
20 22 my $HM = qr{([0-2]?\d)$HMSDELIM([0-5]?\d)};
21 22 my $HMS = qr{([0-2]?\d)$HMSDELIM([0-5]?\d)$HMSDELIM([0-5]?\d)};
22 22 my $HMSNS = qr{T?([0-2]?\d)$HMSDELIM([0-5]?\d)$HMSDELIM([0-5]?\d)$HMSDELIM(\d+)T?};
23 22 my $AMPM = qr{(a\.?m?|p\.?m?)\.?}i;
24  
25 22 my $MMDDYYYY = qr{(\d{1,2})${DELIM}(\d{1,2})${DELIM}(\d{1,4})};
26 22 my $YYYYMMDD = qr{(\d{4})${DELIM}${MON}${DELIM}${DAY}};
27 22 my $YYMMDD = qr{(\d\d)${DELIM}?([0-1]\d)${DELIM}?([0-3]\d)};
28 22 my $MMYY = qr{(\d{1,2})${DELIM}(\d{1,2})}; # YEAR must be > 31 unless MMYY
29 22 my $MMDD = qr{(\d{1,2})$DELIM(\d{1,2})};
30 22 my $XMMXDD = qr{X([0-1]?\d)X${DELIM}?([0-3]?\d)};
31 22 my $DDXMMX = qr{(\d{1,2})${DELIM}?X(\d{1,2})X};
32 22 my $DDXMMXYYYY = qr{(\d{1,2})${DELIM}X(\d{1,2})X$DELIM(\d{1,4})};
33 22 my $MMYYYY = qr{(\d{1,2})$DELIM(\d{4})};
34 22 my $XMMXYYYY = qr{X(\d{1,2})X${DELIM}(\d{4})};
35 22 my $XMMXDDYYYY = qr{X(\d{1,2})X${DELIM}?(\d{1,2})${DELIM}?(\d{1,4})};
36  
37 22 my $HMSMD = [ qw( hour minute second month day ) ];
38 22 my $HMSMDY = [ qw( hour minute second month day year ) ];
39 22 my $HMSYMD = [ qw( hour minute second year month day ) ];
40 22 my $HMSNSMDY = [ qw( hour minute second nanosecond month day year ) ];
41 22 my $HMSDM = [ qw( hour minute second day month ) ];
42 22 my $HMMDY = [ qw( hour minute month day year ) ];
43 22 my $HMMD = [ qw( hour minute month day ) ];
44 22 my $HMAPMMDD = [ qw( hour minute ampm month day ) ];
45 22 my $HMAPMMDDYYYY = [ qw( hour minute ampm month day year ) ];
46 22 my $DM = [ qw( day month ) ];
47 22 my $DMY = [ qw( day month year ) ];
48 22 my $DMHM = [ qw( day month hour minute ) ];
49 22 my $DMHMS = [ qw( day month hour minute second ) ];
50 22 my $DMHMSAP = [ qw( day month hour minute second ampm ) ];
51 22 my $DMYHM = [ qw( day month year hour minute ) ];
52 22 my $DMYHMS = [ qw( day month year hour minute second ) ];
53 22 my $DMYHMSNS = [ qw( day month year hour minute second nanosecond ) ];
54 22 my $DMYHMSAP = [ qw( day month year hour minute second ampm ) ];
55  
56 22 my $M = [ qw( month ) ];
57 22 my $MD = [ qw( month day ) ];
58 22 my $MY = [ qw( month year ) ];
59 22 my $MDY = [ qw( month day year ) ];
60 22 my $MDHMS = [ qw( month day hour minute second ) ];
61 22 my $MDHMSAP = [ qw( month day hour minute second ampm ) ];
62 22 my $MYHMS = [ qw( month year hour minute second ) ];
63 22 my $MYHMSAP = [ qw( month year hour minute second ampm ) ];
64 22 my $MDYHM = [ qw( month day year hour minute second ) ];
65 22 my $MDYHMS = [ qw( month day year hour minute second ) ];
66 22 my $MDYHMAP = [ qw( month day year hour minute ampm ) ];
67 22 my $MDYHMSAP = [ qw( month day year hour minute second ampm ) ];
68 22 my $MDHMSY = [ qw( month day hour minute second year ) ];
69 22 my $MDHMSNSY = [ qw( month day hour minute second nanosecond year ) ];
70  
71  
72 22 my $Y = [ qw( year ) ];
73 22 my $YM = [ qw( year month ) ];
74 22 my $YMD = [ qw( year month day ) ];
75 22 my $YMDH = [ qw( year month day hour ) ];
76 22 my $YHMS = [ qw( year hour minute second ) ];
77 22 my $YMDHM = [ qw( year month day hour minute ) ];
78 22 my $YMHMS = [ qw( year month hour minute second ) ];
79 22 my $YMDHAP = [ qw( year month day hour ampm ) ];
80 22 my $YMDHMS = [ qw( year month day hour minute second ) ];
81 22 my $YMDHMAP = [ qw( year month day hour minute ampm ) ];
82 22 my $YMHMSAP = [ qw( year month hour minute second ampm ) ];
83 22 my $YMDHMSAP = [ qw( year month day hour minute second ampm ) ];
84 22 my $YMDHMSNS = [ qw( year month day hour minute second nanosecond ) ];
85 22 my $YMDHMSNSAP = [ qw( year month day hour minute second nanosecond ampm ) ];
86  
87 22 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 22 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 22 ▶use DateTime::Format::Builder 0.74;
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 22 my $base_dt;
92 sub base
93 {
94 17657 ▶ my ( $self , $dt ) = @_;
Subroutine base: called CC=3 SCAR=11.0
Pod documented
95 17657 ▶ $base_dt = $dt if ( $dt );
Branch: 1 branch
BranchTrueFalseTotal
if $dt 7 17650 17657
96 17657 ▶ return $base_dt || DateTime->now;
Condition66%2/3
$base_dt || 'DateTime'->now
l!l&&r!l&&!r
627170300
MC/DC0%0/2
$base_dt || 'DateTime'->now
$base_dt 'DateTime'->now
Truth table66%2/3
A || B
A= $base_dtB= 'DateTime'->now
ABresult
1X1
011
000
97 }
98  
99 my $formats =
100 [
101 [ preprocess => \&_fix_alpha ] ,
102 { length => [18..22] , params => $YMDHMSAP , regex => qr{\A(\d{4})$DELIM(\d{2})$DELIM(\d{2})\s$HMS\s?$AMPM\z} , postprocess => \&_fix_ampm } ,
103 { length => [16..20] , params => $YMDHMAP , regex => qr{\A(\d{4})$DELIM(\d{2})$DELIM(\d{2})\s$HM\s?$AMPM\z} , postprocess => \&_fix_ampm } ,
104  
105 # 2011-06-16-17.43.30.000000
106 { length => [26] , params => $YMDHMSNS , regex => qr{\A(\d{4})$DELIM(\d{2})$DELIM(\d{2})${DELIM}$HMSNS\z} } ,
107  
108 ########################################################
109 ##### Month/Day/Year
110 # M/DD/Y, MM/D/Y, M/D/Y, MM/DD/Y, M/D/YY, M/DD/YY, MM/D/Y, MM/SS/YY,
111 # M/D/YYYY, M/DD/YYYY, MM/D/YYYY, MM/DD/YYYY
112  
113 { length => [5..10], params => $MDY, regex => qr{\A${MON}${DELIM}${DAY}${DELIM}${YEAR}\z}, postprocess => [ \&_fix_year, \&_fix_zero_month ] },
114 { length => [12..14], params => $MDY, regex => qr{\AX${MON}X${DELIM}n${DAY}n${DELIM}${YEAR}\z} },
115 { length => [11..19], params => $MDYHMS, regex => qr{\A${MON}${DELIM}${DAY}${DELIM}${YEAR}\s$HMS\z}, postprocess => [ \&_fix_year, \&_fix_zero_month ] },
116 { length => [11..20], params => $MDYHMAP, regex => qr{\A${MON}${DELIM}${DAY}${DELIM}${YEAR}\s$HM\s?$AMPM\z}, postprocess => [ \&_fix_ampm , \&_fix_year, \&_fix_zero_month ] } ,
117 { length => [14..22], params => $MDYHMSAP, regex => qr{\A${MON}${DELIM}${DAY}${DELIM}${YEAR}\s$HMS\s?$AMPM\z}, postprocess => [ \&_fix_ampm , \&_fix_year, \&_fix_zero_month ] } ,
118  
119 # 02/28/2014 14:30 (missing seconds)
120 { length => [14..16], params => $MDYHM, regex => qr{\A$MMDDYYYY\s$HM\z}, postprocess => [\&_set_default_seconds, \&_fix_zero_month] } ,
121  
122 ########################################################
123 ##### Year/Month/Day
124 # YYYY/M/D, YYYY/M/DD, YYYY/MM/D, YYYY/MM/DD
125 # YYYY/MM/DD HH:MM:SS
126 # YYYY-MM HH:MM:SS
127 { length => [6,7], params => $YM, regex => qr{\A(\d{4})$DELIM$MON\z} },
128 { length => [12..16], params => $YMHMS, regex => qr{\A(\d{4})$DELIM$MON\s$HMS\z} },
129 { length => [14..19], params => $YMHMSAP, regex => qr{\A(\d{4})$DELIM$MON\s$HMS\s?$AMPM\z} , postprocess => \&_fix_ampm },
130 { length => [8..10], params => $YMD, regex => qr{\A$YYYYMMDD\z} },
131 { length => [10..12], params => $YMDH, regex => qr{\A${YYYYMMDD}\s${HOUR}z} },
132 { length => [13..15], params => $YMDHAP, regex => qr{\A${YYYYMMDD}\s${HOUR}\s?${AMPM}\z} , postprocess => \&_fix_ampm },
133 { length => [11..16], params => $YMDHM, regex => qr{\A$YYYYMMDD\s$HM\z} },
134 { length => [14..19], params => $YMDHMAP, regex => qr{\A$YYYYMMDD\s$HM\s?$AMPM\z}, postprocess => \&_fix_ampm },
135 { length => [14..19], params => $YMDHMS, regex => qr{\A$YYYYMMDD\s$HMS\z} },
136 { length => [17..21], params => $YMDHMSAP, regex => qr{\A$YYYYMMDD\s$HMS\s?$AMPM\z}, postprocess => \&_fix_ampm },
137 # 950404 00:22:12 => 1995-04-04T00:22:12
138 { length => [15], params => $YMDHMS, regex => qr{\A$YYMMDD\s$HMS\z}, postprocess => \&_fix_year },
139 { length => [19], params => $YMDHMSNS, regex => qr{\A$YYMMDD\s$HMSNS\z}, postprocess => \&_fix_year },
140  
141  
142 ########################################################
143 ##### YYYY-MM-DDTHH:MM:SS
144 # this is what comes out of the database
145 { length => 19, params => $YMDHMS, regex => qr{\A(\d{4})$DELIM(\d{2})$DELIM(\d{2})T(\d{2}):(\d{2}):(\d{2})\z} },
146  
147 { length => 16, params => $YMDHMS, regex => qr{\A(\d{4})(\d{2})(\d{2})(\d{2}):(\d{2}):(\d{2})\z} },
148 { length => 13, params => $YMDHM , regex => qr{\A(\d{4})(\d{2})(\d{2})(\d{2}):(\d{2})\z} },
149 { length => 8 , params => $YMD , regex => qr{\A(\d{4})(\d{2})(\d{2})\z} },
150  
151 { length => 10 , params => $YMD , regex => qr{\AY(\d{2})Y$DELIM(\d{2})$DELIM(\d{2})\z} , postprocess => \&_fix_year } ,
152 # 96-06-1800:00:00
153 { length => 18 , params => $YMDHMS , regex => qr{\AY(\d{2})Y$DELIM(\d{2})$DELIM(\d{2})$HMS\z} , postprocess => \&_fix_year } ,
154 # 96-06-1800:00
155 { length => 15 , params => $YMDHM , regex => qr{\AY(\d{2})Y$DELIM(\d{2})$DELIM(\d{2})$HM\z} , postprocess => \&_fix_year } ,
156 # 9931201 at 05:30:25 pM GMT
157  
158 # 1993120105:30:25.05 am
159 { length => 22 , params => $YMDHMSNSAP ,
160 regex => qr{\A(\d{4})(\d{2})(\d{2})${HMSNS}\s${AMPM}\z} ,
161 postprocess => \&_fix_ampm },
162  
163 # 1993120105:30:25 am
164 { length => 19 , params => $YMDHMSAP ,
165 regex => qr{\A(\d{4})(\d{2})(\d{2})${HMS}\s${AMPM}\z} ,
166 postprocess => \&_fix_ampm },
167  
168 ########################################################
169 ##### Month/Year
170 ##### year must be 4 digits unless it is > 31
171 ##### or MMYY is true
172 # M/YYYY, MM/YYYY
173 { length => [6,7], params => $MY, regex => qr{\A$MMYYYY\z}, postprocess => \&_fix_zero_month },
174 { length => [3..5], params => $MY, regex => qr{\A$MMYY\z},
175 postprocess => [sub {
176 13 ▶ my %args = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
177 13 ▶ if ( exists $args{args} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"args"}) 3 10 13
178 {
179 3 my %original_args = @{$args{args}};
180 3 ▶ return 1 if ( $original_args{MMYY} );
Branch: 1 branch
BranchTrueFalseTotal
if $original_args{"MMYY"} 3 0 3
181 }
182 10 ▶ return 1 if ( $args{parsed}{year} > 31 );
Branch: 1 branch
BranchTrueFalseTotal
if $args{"parsed"}{"year"} > 31 1 9 10
183 9 return 0;
184 }, \&_fix_year] },
185 ########################################################
186 ##### Month/Day
187 # M/D, M/DD, MM/D, MM/DD
188 { length => [3..5], params => $MD, regex => qr{\A$MMDD\z},
189 9 ▶ postprocess => sub { my %args = @_; $args{parsed}{year} = __PACKAGE__->base->year } },
Subroutine __ANON__: called CC=1 SCAR=0.0
190  
191 { length => [9..14], params => $MDHMS, regex => qr{\A$MMDD\s$HMS\z},
192 0 ▶ postprocess => sub { my %args = @_; $args{parsed}{year} = __PACKAGE__->base->year } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
193 { length => [12..17], params => $MDHMSAP, regex => qr{\A$MMDD\s$HMS\s?$AMPM\z},
194 0 ▶ postprocess => [sub { my %args = @_; $args{parsed}{year} = __PACKAGE__->base->year },\&_fix_ampm] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
195  
196 ########################################################
197 ##### Dates with at in their name: 12-10-65 at 5:30:25
198 # the language plugins should wrap the time like this: T5:30:25T
199 # 2005-06-12 T3Tp (15)
200 { length => [16,17], params => $MDYHMS, regex => qr{\A${MON}${DAY}${YEAR}T${HMS}T\z}, postprocess => [ \&_fix_year, \&_fix_zero_month ] } ,
201 { length => [17,18,20], params => $MDYHMS, regex => qr{\A${MON}${DELIM}${DAY}${DELIM}${YEAR}\s?T${HMS}T\z}, postprocess => [ \&_fix_year, \&_fix_zero_month ] } ,
202 { length => [17..20], params => $DMYHM, regex => qr{\A${DAY}${DELIM}X${MON}X${DELIM}${YEAR}\sT${HM}T\z}, postprocess => [ \&_fix_year, \&_fix_zero_month ] } ,
203 { length => [20], params => $MDYHMS, regex => qr{\AX${MON}X${DELIM}${DAY}${DELIM}${YEAR}\s?T${HMS}T\z}, postprocess => \&_fix_year } ,
204 { length => [21], params => $MDYHMAP, regex => qr{\A${MON}${DELIM}${DAY}${DELIM}${YEAR}\s?T${HM}T\s${AMPM}\z}, postprocess => [ \&_fix_ampm, \&_fix_zero_month ] } ,
205 { length => [20,21], params => $YMDHMAP, regex => qr{\A${YYYYMMDD}\s?T${HM}T\s${AMPM}\z}, postprocess => \&_fix_ampm } ,
206 { length => [21,22], params => $YMDHMSAP, regex => qr{\A${YEAR}${MON}${DAY}\s?T${HMS}T\s${AMPM}\z}, postprocess => \&_fix_ampm } ,
207 { length => [15], params => $YMDHAP, regex => qr{\A${YEAR}${DELIM}${MON}${DELIM}${DAY}\s?T${HOUR}T\s?${AMPM}\z}, postprocess => \&_fix_ampm } ,
208 { length => [16..18], params => $YMDHM, regex => qr{\A${YEAR}${DELIM}${MON}${DELIM}${DAY}\s?T${HM}T\z}, postprocess => \&_fix_year } ,
209 { length => [21], params => $YMDHMS, regex => qr{\A${YEAR}${DELIM}${MON}${DELIM}${DAY}\s?T${HMS}T\z}, postprocess => \&_fix_year } ,
210 { length => [16], params => $MDYHMS, regex => qr{\A${MON}${DAY}(\d\d)\s?T${HMS}T\z}, postprocess => [ \&_fix_year, \&_fix_zero_month ] } ,
211 { length => [16], params => $YMDHAP, regex => qr{\A${YEAR}${DELIM}${MON}${DELIM}${DAY}\s?T${HOUR}T${AMPM}\z}, postprocess => \&_fix_ampm } ,
212  
213 { length => [15,16], params => $MDHMS, regex => qr{\A${MON}${DELIM}${DAY}\s?T${HMS}T\z},
214 4 ▶ postprocess => sub { my %args = @_; $args{parsed}{year} = __PACKAGE__->base->year } } ,
Subroutine __ANON__: called CC=1 SCAR=0.0
215 { length => [17,18], params => $MDHMS, regex => qr{\AX${MON}X${DELIM}${DAY}\s?T${HMS}T\z},
216 2 ▶ postprocess => sub { my %args = @_; $args{parsed}{year} = __PACKAGE__->base->year } } ,
Subroutine __ANON__: called CC=1 SCAR=0.0
217  
218 ########################################################
219 # YYYY HH:MM:SS
220 { length => [13], params => $YHMS, regex => qr{\A$YEAR\s$HMS\z} } ,
221  
222 ########################################################
223 # time first
224 # (5:30 12-10)
225 { length => [8..11], params => $HMMD, regex => qr{\A${HM}\s${MMDD}\z}, postprocess => \&_set_default_year },
226 # 5:30:25:05/1/1/65
227 # 12:30:25:05/10/10/65
228 { length => [17..20], params => $HMSNSMDY, regex => qr{\A${HMSNS}${DELIM}${MMDDYYYY}\z}, postprocess => \&_fix_year },
229 # 5:30:25 12101965
230 { length => [14..16], params => $HMSMDY, regex => qr{\A${HMS}${DELIM}${MON}${DAY}${YEAR}\z}, postprocess => \&_fix_year },
231 { length => [14..19], params => $HMSMDY, regex => qr{\A${HMS}${DELIM}${MMDDYYYY}\z}, postprocess => \&_fix_year },
232  
233 { length => [14..19], params => $HMSYMD, regex => qr{\A${HMS}${DELIM}${YYYYMMDD}\z}, postprocess => \&_fix_year },
234  
235 # 5:30 pm 121065 => 2065-12-01T17:30:00
236 { length => [14,18], params => $HMAPMMDDYYYY, regex => qr{\A${HM}\s${AMPM}\s${MON}${DAY}${YEAR}},postprocess => [\&_fix_ampm, \&_fix_year] },
237 { length => [16,19], params => $HMAPMMDDYYYY, regex => qr{\A${HM}\s${AMPM}\s${MMDDYYYY}},postprocess => [\&_fix_ampm, \&_fix_year] },
238  
239  
240 ########################################################
241 ##### Alpha months
242 # _fix_alpha changes month name to "XMX"
243 # 18-XMX, X1X-18, 08-XMX-99, XMY-08-1999, 1999-X1Y-08, 1999-X10X-08
244  
245 # DD-mon, D-mon, D-mon-YY, DD-mon-YY, D-mon-YYYY, DD-mon-YYYY, D-mon-Y, DD-mon-Y
246 { length => [5..7], params => $DM, regex => qr{\A${DDXMMX}\z},
247 6 ▶ postprocess => sub { my %args = @_; $args{parsed}{year} = __PACKAGE__->base->year } },
Subroutine __ANON__: called CC=1 SCAR=0.0
248 { length => [9..15], params => $DMHM, regex => qr{\A${DDXMMX}\s${HM}\z},
249 6 ▶ postprocess => sub { my %args = @_; $args{parsed}{year} = __PACKAGE__->base->year } },
Subroutine __ANON__: called CC=1 SCAR=0.0
250 { length => [9..18], params => $DMHMS, regex => qr{\A${DDXMMX}\s${HMS}\z},
251 2 ▶ postprocess => sub { my %args = @_; $args{parsed}{year} = __PACKAGE__->base->year } },
Subroutine __ANON__: called CC=1 SCAR=0.0
252 { length => [11..21], params => $DMHMSAP, regex => qr{\A${DDXMMX}\s${HMS}\s?$AMPM\z},
253 0 ▶ postprocess => [sub { my %args = @_; $args{parsed}{year} = __PACKAGE__->base->year }, \&_fix_ampm] } ,
Subroutine __ANON__: not called CC=1 SCAR=6.9
254  
255 { length => [7..12], params => $DMY, regex => qr{\A${DDXMMXYYYY}\z}, postprocess => \&_fix_year },
256 { length => [12..18], params => $DMYHM, regex => qr{\A${DDXMMXYYYY}\s${HM}\z}, postprocess => \&_fix_year },
257 { length => [12..21], params => $DMYHMS, regex => qr{\A${DDXMMXYYYY}\s${HMS}\z}, postprocess => \&_fix_year },
258 { length => [16..25], params => $DMYHMSNS, regex => qr{\A${DDXMMXYYYY}\s${HMSNS}\z}, postprocess => \&_fix_year },
259 { length => [14..24], params => $DMYHMSAP, regex => qr{\A${DDXMMXYYYY}\s${HMS}\s?$AMPM\z}, postprocess => [ \&_fix_year , \&_fix_ampm ] },
260 { length => [9..15] , params => $HMSMD, regex => qr{\A${HMS}${XMMXDD}\z}, postprocess => \&_set_default_year },
261  
262 # Fri Dec 2 22:56:03.500 GMT+0 1994
263 { length => [24], params => $MDHMSNSY, regex => qr{\A${XMMXDD}\s${HMSNS}\s${YEAR}\z}, },
264  
265 { length => [9..15] , params => $HMSDM, regex => qr{\A${HMS}${DELIM}?${DDXMMX}\z}, postprocess => \&_set_default_year },
266 { length => [11..17], params => $HMSMDY, regex => qr{\A${HMS}${XMMXDDYYYY}\z}, postprocess => \&_fix_year },
267 { length => [6..11], params => $HMMD, regex => qr{\A${HM}${XMMXDD}\z}, postprocess => \&_set_default_year },
268  
269 # mon
270 { length => [3,4], params => $M, regex => qr{\AX${MON}X\z},
271 12 ▶ postprocess => sub { my %args = @_;$args{parsed}{year} = __PACKAGE__->base->year;$args{parsed}{day} = 1; } },
Subroutine __ANON__: called CC=1 SCAR=0.0
272  
273 # mon-D , mon-DD, mon-YYYY, mon-D-Y, mon-DD-Y, mon-D-YY, mon-DD-YY
274 # mon-D-YYYY, mon-DD-YYYY
275 { length => [8,9], params => $MY, regex => qr{\A${XMMXYYYY}\z} },
276 { length => [14..18], params => $MYHMS, regex => qr{\A${XMMXYYYY}\s${HMS}\z} },
277 { length => [16..21], params => $MYHMSAP, regex => qr{\A${XMMXYYYY}\s${HMS}\s?$AMPM\z}, postprocess => \&_fix_ampm },
278  
279 { length => [5..7], params => $MD, regex => qr{\A$XMMXDD\z},
280 18 ▶ postprocess => sub { my %args = @_; _set_year( @_ ) } },
Subroutine __ANON__: called CC=1 SCAR=0.0
281 { length => [10..18], params => $MDHMS, regex => qr{\A$XMMXDD\s$HMS\z},
282 0 ▶ postprocess => sub { my %args = @_; $args{parsed}{year} = __PACKAGE__->base->year } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
283 { length => [12..21], params => $MDHMSAP, regex => qr{\A$XMMXDD\s$HMS\s?$AMPM\z} ,
284 0 ▶ postprocess => [sub { my %args = @_; $args{parsed}{year} = __PACKAGE__->base->year }, \&_fix_ampm] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
285  
286 { length => [7..12], params => $MDY, regex => qr{\A$XMMXDDYYYY\z}, postprocess => \&_fix_year },
287 { length => [12..21], params => $MDYHMS, regex => qr{\A$XMMXDDYYYY\s$HMS\z}, postprocess => \&_fix_year },
288 { length => [14..17], params => $MDYHM, regex => qr{\A$XMMXDDYYYY\s$HM\z},
289 2 ▶ postprocess => sub { my %args = @_; $args{parsed}{second} = 0; _fix_year(%args) } },
Subroutine __ANON__: called CC=1 SCAR=0.0
290 { length => [14..24], params => $MDYHMSAP, regex => qr{\A$XMMXDDYYYY\s$HMS\s?$AMPM\z}, postprocess => [ \&_fix_year , \&_fix_ampm ] },
291  
292 # YYYY-mon-D, YYYY-mon-DD, YYYY-mon
293 { length => [8,9], params => $YM, regex => qr{\A(\d{4})${DELIM}X(\d{1,2})X\z} },
294 { length => [13..18], params => $YMHMS, regex => qr{\A(\d{4})${DELIM}X(\d{1,2})X\s$HMS\z} },
295 { length => [15..21], params => $YMHMSAP, regex => qr{\A(\d{4})${DELIM}X(\d{1,2})X\s$HMS\s?$AMPM\z} , postprocess => \&_fix_ampm },
296 { length => [9..12], params => $YMD, regex => qr{\A(\d{4})${DELIM}X(\d{1,2})X$DELIM(\d{1,2})\z} },
297 { length => [15..21], params => $YMDHMS, regex => qr{\A(\d{4})${DELIM}X(\d{1,2})X$DELIM(\d{1,2})\s$HMS\z} },
298 { length => [18..24], params => $YMDHMSAP, regex => qr{\A(\d{4})${DELIM}X(\d{1,2})X$DELIM(\d{1,2})\s$HMS\s?$AMPM\z} , postprocess => \&_fix_ampm },
299 # month D, Y | month D, YY | month D, YYYY | month DD, Y | month DD, YY
300 # month DD, YYYY
301 { length => [9..13], params => $MDY, regex => qr{\AX(\d{1,2})X\s(\d{1,2}),\s(\d{1,4})\z} },
302 { length => [5..22], params => $MDYHMS, regex => qr{\AX(\d{1,2})X\s(\d{1,2}),\s(\d{1,4})\s$HMS\z} },
303 { length => [7..25], params => $MDYHMSAP, regex => qr{\AX(\d{1,2})X\s(\d{1,2}),\s(\d{1,4})\s$HMS\s?$AMPM\z} , postprocess => \&_fix_ampm },
304  
305 # D month, Y | D month, YY | D month, YYYY | DD month, Y | DD month, YY
306 # DD month, YYYY
307 # nDDn XMMX
308 { length => [8..13], params => $DMY, regex => qr{\A(\d{1,2})\sX(\d{1,2})X,?\s(\d{1,4})\z} },
309 { length => [13..21], params => $DMYHMS, regex => qr{\A(\d{1,2})\sX(\d{1,2})X,?\s(\d{1,4})\s$HMS\z} },
310 { length => [16..27], params => $DMYHMSAP, regex => qr{\A(\d{1,2})\sX(\d{1,2})X,?\s(\d{1,4})\s$HMS\s?$AMPM\z}, postprocess => \&_fix_ampm },
311 { length => [7..9], params => $DM, regex => qr{\An(\d{1,2})n\sX(\d{1,2})X\z}, postprocess => \&_set_default_year },
312  
313 # Dec 03 20:53:10 2009
314 { length => [16..21], params => $MDHMSY , regex => qr{\AX(\d{1,2})X\s(\d{1,2})\s$HMS\s(\d{4})\z} } ,
315 { length => [10..18], params => $HMMDY , regex => qr{\A$HM\sX${MON}X\s$DAY\s$YEAR\z} },
316 # 8:00 pm Dec 10th => 8:00pm X12X n10n
317 { length => [14..19] , params => $HMAPMMDD , regex => qr{\A$HM\s?$AMPM\sX${MON}X\sn${DAY}n\z} ,
318 2 ▶ postprocess => [sub { my %args = @_; $args{parsed}{year} = __PACKAGE__->base->year }, \&_fix_ampm] },
Subroutine __ANON__: called CC=1 SCAR=0.0
319 # 5:30 DeC 1
320 { length => [11], params => $HMMD, regex => qr{\A${HM}\sX${MON}X\s${DAY}\z}m, postprocess => \&_set_default_year },
321  
322 ########################################################
323 ##### Bare Numbers
324 # 20060518T051326, 20060518T0513, 20060518T05, 20060518, 200608
325 # 20060518 12:34:56
326 { length => [16..20], params => $YMDHMSAP, regex => qr{\A(\d{4})(\d{2})(\d{2})\s$HMS\s?$AMPM\z} , postprocess => \&_fix_ampm },
327 { length => [14..17], params => $YMDHMS, regex => qr{\A(\d{4})(\d{2})(\d{2})\s$HMS\z} },
328 # 19960618000000 => 1996-06-18T00:00:00
329 { length => 14, params => $YMDHMS, regex => qr{\A(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})\z} },
330 { length => 15, strptime => '%Y%m%dT%H%M%S' } ,
331 { length => 13, strptime => '%Y%m%dT%H%M' } ,
332 { length => 11, strptime => '%Y%m%dT%H' } ,
333 { length => 8, strptime => '%Y%m%d' } ,
334 { length => 6, strptime => '%Y%m' } ,
335 { length => 4, strptime => '%Y' } ,
336  
337 ########################################################
338 ##### bare times
339 # HH:MM:SS
340 { length => [5..10],
341 params => [ qw( hour minute second ) ] ,
342 regex => qr{\AT?${HMS}T?\z} ,
343 postprocess => sub {
344 10 ▶ my %args = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
345 10 $args{parsed}{year} = __PACKAGE__->base->year;
346 10 $args{parsed}{month} = __PACKAGE__->base->month;
347 10 $args{parsed}{day} = __PACKAGE__->base->day;
348 }
349 },
350 # HH:MM:SS AM
351 { length => [7..13],
352 params => [ qw( hour minute second ampm ) ] ,
353 regex => qr{\AT?${HMS}T?\s?$AMPM\z} ,
354 postprocess => [sub {
355 5 ▶ my %args = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
356 5 $args{parsed}{year} = __PACKAGE__->base->year;
357 5 $args{parsed}{month} = __PACKAGE__->base->month;
358 5 $args{parsed}{day} = __PACKAGE__->base->day;
359 }, \&_fix_ampm]
360 },
361  
362 # HH:MM
363 { length => [3..7],
364 params => [ qw( hour minute ) ] ,
365 regex => qr{\AT?${HM}T?\z} ,
366 postprocess => sub {
367 12 ▶ my %args = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
368 12 $args{parsed}{year} = __PACKAGE__->base->year;
369 12 $args{parsed}{month} = __PACKAGE__->base->month;
370 12 $args{parsed}{day} = __PACKAGE__->base->day;
371 }
372 },
373 # HH:MM am
374 { length => [5..10],
375 params => [ qw( hour minute ampm ) ] ,
376 regex => qr{\A$HM\s?$AMPM\z} ,
377 postprocess => [sub {
378 20 ▶ my %args = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
379 20 $args{parsed}{year} = __PACKAGE__->base->year;
380 20 $args{parsed}{month} = __PACKAGE__->base->month;
381 20 $args{parsed}{day} = __PACKAGE__->base->day;
382 }, \&_fix_ampm ]
383 } ,
384  
385 # HH am
386 { length => [2..5],
387 params => [ qw( hour ampm ) ] ,
388 regex => qr{\A$HOUR\s?$AMPM\z} ,
389 postprocess => [sub {
390 2 ▶ my %args = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
391 2 $args{parsed}{year} = __PACKAGE__->base->year;
392 2 $args{parsed}{month} = __PACKAGE__->base->month;
393 2 $args{parsed}{day} = __PACKAGE__->base->day;
394 }, \&_fix_ampm ]
395 } ,
396  
397 ########################################################
398 # Day of year
399 # 1999345 => 1999, 345th day of year
400 { length => [5,7], params => [ qw( year doy ) ] ,
401 regex => qr{\A$YEAR(?:$DELIM)?(\d{3})\z} ,
402 postprocess => [ \&_fix_year , \&_fix_day_of_year ] } ,
403 { length => [10..18], params => [ qw( year doy hour minute second ) ] ,
404 regex => qr{\A$YEAR(?:$DELIM)?(\d{3})\s$HMS\z} ,
405 postprocess => [ \&_fix_year , \&_fix_day_of_year ] } ,
406 { length => [12..21], params => [ qw( year doy hour minute second ampm ) ] ,
407 regex => qr{\A$YEAR(?:$DELIM)?(\d{3})\s$HMS\s?$AMPM\z} ,
408 postprocess => [ \&_fix_year , \&_fix_day_of_year , \&_fix_ampm ]} ,
409  
410 # this is the format for Websphere mq
411 # http://publib.boulder.ibm.com/infocenter/wmqv6/v6r0/index.jsp?topic=/com.ibm.mq.csqzak.doc/js01396.htm
412 # hundreths are not a valid parameter to DateTime->new, so we turn them into nanoseconds
413 { length => [16], params => $YMDHMSNS , regex => qr{\A(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})\z} ,
414 postprocess => sub {
415 1 ▶ my %args = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
416 1 my $t = sprintf( '%s0' , $args{parsed}{nanosecond} ) * 1_000_000;
417 1 $args{parsed}{nanosecond} = $t;
418 }
419 },
420  
421 {
422 params => [],
423 length => [8],
424 regex => qr{\Ainfinity\z},
425 constructor => sub {
426 6 ▶ return DateTime::Infinite::Future->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
427 },
428 },
429 {
430 params => [],
431 length => [9],
432 regex => qr{\A\-infinity\z},
433 constructor => sub {
434 0 ▶ return DateTime::Infinite::Past->new;
Subroutine __ANON__: not called CC=1 SCAR=6.9
435 },
436 },
437  
438 # nanoseconds. no length here, we do not know how many digits they will use for nanoseconds
439 { params => [ qw( year month day hour minute second nanosecond ) ] , regex => qr{\A$YYYYMMDD(?:\s|T)T?${HMS}${HMSDELIM}(\d+)T?\z} } ,
440  
441 # epochtime
442 {
443 params => [] , # we specifically set the params below
444 regex => qr{\A\d+\.?\d+?\z} ,
445 postprocess => sub {
446 2 ▶ my %args = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
447 2 my $dt = DateTime->from_epoch( epoch => $args{input} );
448 2 $args{parsed}{year} = $dt->year;
449 2 $args{parsed}{month} = $dt->month;
450 2 $args{parsed}{day} = $dt->day;
451 2 $args{parsed}{hour} = $dt->hour;
452 2 $args{parsed}{minute} = $dt->minute;
453 2 $args{parsed}{second} = $dt->second;
454 2 $args{parsed}{nanosecond} = $dt->nanosecond;
455 2 return 1;
456 }
457 },
458 22 ];
459  
460 22 DateTime::Format::Builder->create_class( parsers => { parse_datetime => $formats } );
461  
462 sub build
463 {
464 0 ▶ my $self = shift;
Subroutine build: not called CC=1 SCAR=6.9
Pod documented
465 0 return $self->parse_datetime( @_ );
466 }
467  
468 sub _fix_day_of_year
469 {
470 57 ▶ my %args = @_;
Subroutine _fix_day_of_year: called CC=1 SCAR=0.0
471  
472 57 my $doy = $args{parsed}{doy};
473 57 delete $args{parsed}{doy};
474  
475 my $dt = DateTime->from_day_of_year(
476 year => $args{parsed}{year} ,
477 57 day_of_year => $doy
478 );
479 57 $args{parsed}{month} = $dt->month;
480 57 $args{parsed}{day} = $dt->day;
481  
482 57 return 1;
483 }
484  
485 sub _fix_alpha
486 {
487 6313 ▶ my %args = @_;
Subroutine _fix_alpha: called CC=18 SCAR=29.1
488 6313 my ($date, $p) = @args{qw( input parsed )};
489 6313 ▶ my %extra_args = @{$args{args}} if exists $args{args};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"args"} 308 6005 6313
490  
491 6313 ▶ if ( exists $extra_args{strip} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $extra_args{"strip"}) 140 6173 6313
492 {
493 140 ▶ my @strips = ref( $extra_args{strip} ) eq 'ARRAY' ? @{$extra_args{strip}} : ($extra_args{strip});
Branch: 1 branch
BranchTrueFalseTotal
ref $extra_args{'strip'} eq 'ARRAY' ? : 54 86 140
494 140 foreach my $strip ( @strips )
495 {
496 193 ▶ if ( ref( $strip ) eq 'Regexp' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $strip eq 'Regexp') { } 192 1 193
497 {
498 192 $date =~ s{$strip}{}mx;
499 }
500 else
501 {
502 1 croak( "parameter strip requires a regular expression" );
503 }
504 }
505 }
506  
507 6312 ▶ if ( exists $extra_args{base} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $extra_args{"base"}) 2 6310 6312
508 {
509 2 __PACKAGE__->base( $extra_args{base} );
510 }
511  
512 6312 ( $date , $p ) = _parse_timezone( $date , $p , \%extra_args );
513  
514 6312 $date = _clean_whitespace( $date );
515  
516 my $lang = DateTime::Format::Flexible::lang->new(
517 lang => $extra_args{lang},
518 6312 base => __PACKAGE__->base,
519 );
520  
521 6312 my $stripped = $date;
522 6312 $stripped =~ s{$DELIM|$HMSDELIM}{}gm;
523  
524 6312 ▶ if ( $stripped =~ m{(\D)} )
Branch: 1 branch
BranchTrueFalseTotal
if ($stripped =~ /(\D)/) { } 3765 2547 6312
525 {
526 3765 ▶ printf( "# before lang: %s\n", $date ) if $ENV{DFF_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DFF_DEBUG"} 0 3765 3765
527 3765 ( $date , $p ) = $lang->_cleanup( $date , $p );
528 3765 ▶ printf( "# after lang: %s\n", $date ) if $ENV{DFF_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DFF_DEBUG"} 0 3765 3765
529 }
530 else
531 {
532 2547 ▶ printf( "# ignoring languages, no non numbers (%s)\n", $stripped ) if $ENV{DFF_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DFF_DEBUG"} 0 2547 2547
533 }
534  
535 6312 $date =~ s{($DELIM)+}{$1}mxg; # make multiple delimeters into one
536 # remove any leading delimeters unless it is -infinity
537 6312 ▶ $date =~ s{\A$DELIM+}{}mx if ( not $date eq '-infinity' );
Branch: 1 branch
BranchTrueFalseTotal
unless $date eq "-infinity" 6312 0 6312
538 6312 $date =~ s{$DELIM+\z}{}mx; # remove any trailing delimeters
539 6312 $date =~ s{\,+}{}gmx; # remove commas
540  
541 # if we have two digits at the beginning of our date that are greater than 31,
542 # we have a possible two digit year
543 6312 ▶ if ( my ( $possible_year , $remaining ) = $date =~ m{\A(\d\d)($DELIM.+)}mx )
Branch: 1 branch
BranchTrueFalseTotal
if (() = $date =~ /\A(\d\d)($DELIM.+)/mx) 1593 4719 6312
544 {
545 1593 ▶ if ( $possible_year > 31 )
Branch: 1 branch
BranchTrueFalseTotal
if ($possible_year > 31) 8 1585 1593
546 {
547 8 $date =~ s{\A(\d\d)}{Y$1Y}mx;
548 }
549 }
550  
551 # try and detect DD-MM-YYYY
552 6312 ▶ if ( $extra_args{european} )
Branch: 1 branch
BranchTrueFalseTotal
if ($extra_args{"european"}) 21 6291 6312
553 {
554 21 ▶ if ( my ( $m , $d , $y ) = $date =~ m{\A$MMDDYYYY}mx )
Branch: 1 branch
BranchTrueFalseTotal
if (() = $date =~ /\A$MMDDYYYY/mx) 11 10 21
555 {
556 11 $date =~ s{\A$MMDDYYYY}{$2-$1-$3}mx;
557 }
558 }
559  
560 6312 ▶ printf( "# date: (%s) length: (%s) timezone: [%s] \n" , $date , length( $date ) , $p->{time_zone}||q{none} ) if $ENV{DFF_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DFF_DEBUG"} 0 6312 6312
Condition0%0/2
$p->{'time_zone'} || 'none'
l!l
00
MC/DC0%0/1
$p->{'time_zone'} || 'none'
$p->{'time_zone'}
Truth table0%0/2
A
A= $p->{'time_zone'}
Aresult
11
00
561 6312 return $date;
562 }
563  
564 sub _parse_timezone
565 {
566 6312 ▶ my ( $date , $p , $extra_args ) = @_;
Subroutine _parse_timezone: called CC=24 SCAR=31.9
567  
568 6312 ▶ while ( my ( $abbrev , $tz ) = each( %{ $extra_args->{tz_map} } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$extra_args{"tz_map"};}) 57 6302 6359
569 {
570 57 ▶ if ( $date =~ m{$abbrev} )
Branch: 1 branch
BranchTrueFalseTotal
if ($date =~ /$abbrev/) 10 47 57
571 {
572 10 $date =~ s{\Q$abbrev\E}{};
573 10 $p->{time_zone} = $tz;
574 10 return ( $date , $p );
575 }
576 }
577  
578 # search for GMT/UTC inside the string
579 # must be surrounded by spaces
580 # 5:30 pm GMT 121065
581 # Tue Feb 28 14:30:00 UTC 2014
582 # Fri Dec 2 22:56:03 GMT+0 1994
583  
584 6302 ▶ if ( my ( $tz ) = $date =~ m{\s(GMT(?:\+0)?|UTC)\s}mx )
Branch: 1 branch
BranchTrueFalseTotal
if (my($tz) = $date =~ /\s(GMT(?:\+0)?|UTC)\s/mx) 3 6299 6302
585 {
586 3 $date =~ s{\Q$tz\E}{};
587 3 $p->{time_zone} = 'UTC';
588 3 return ( $date , $p );
589 }
590  
591 # remove any trailing 'Z' => UTC
592 6299 ▶ if ( $date =~ m{Z\z}mx )
Branch: 1 branch
BranchTrueFalseTotal
if ($date =~ /Z\z/mx) 10 6289 6299
593 {
594 10 $date =~ s{Z\z}{}mx;
595 10 $p->{time_zone} = 'UTC';
596 10 return ( $date , $p );
597 }
598  
599 # set any trailing string timezones. they cannot start with a digit
600 6289 ▶ if ( my ( $tz ) = $date =~ m{.+\s+(\D[^\s]+)\z} )
Branch: 1 branch
BranchTrueFalseTotal
if (my($tz) = $date =~ /.+\s+(\D[^\s]+)\z/) 2206 4083 6289
601 {
602 2206 ▶ printf( "# possible timezone (%s)\n", $tz) if $ENV{DFF_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DFF_DEBUG"} 0 2206 2206
603 2206 my $orig_tz = $tz;
604 2206 ▶ if ( exists $extra_args->{tz_map}->{$tz} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $extra_args->{"tz_map"}{$tz}) 0 2206 2206
605 {
606 0 $tz = $extra_args->{tz_map}->{$tz};
607 }
608 2206 ▶ if ( DateTime::TimeZone->is_valid_name( $tz ) )
Branch: 1 branch
BranchTrueFalseTotal
if ("DateTime::TimeZone"->is_valid_name($tz)) 634 1572 2206
609 {
610 634 ▶ printf( "# timezone matched\n" ) if $ENV{DFF_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DFF_DEBUG"} 0 634 634
611 634 $date =~ s{\Q$orig_tz\E}{};
612 634 $p->{time_zone} = $tz;
613 634 return ( $date , $p );
614 }
615 }
616  
617 # set any trailing offset timezones
618 5655 ▶ if ( my ( $tz ) = $date =~ m{(
Branch: 1 branch
BranchTrueFalseTotal
if (my($tz) = $date =~ /( (?:\s+)?\+\d{2,4} # ' +04', '+04' |\s+\-\d{4} # ' -0400' |(?:\s+)?[-+]\d{2}:\d{2} # '-04:00', '+04:00' )\.?\z/mx) 24 5631 5655
619 (?:\s+)?\+\d{2,4} # ' +04', '+04'
620 |\s+\-\d{4} # ' -0400'
621 |(?:\s+)?[-+]\d{2}:\d{2} # '-04:00', '+04:00'
622 )\.?\z}mx )
623 {
624 24 ▶ printf( "# possible timezone (%s) in (%s)\n", $tz, $date) if $ENV{DFF_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DFF_DEBUG"} 0 24 24
625 24 my $original_tz = $tz;
626 24 $tz =~ s{:}{};
627 # some timezones are 2 digit hours, add the minutes part
628 24 $tz = _clean_whitespace( $tz );
629 24 ▶ $tz .= '00' if ( length( $tz ) == 3 );
Branch: 1 branch
BranchTrueFalseTotal
if length $tz == 3 18 6 24
630 24 ▶ if ( _is_valid_tz_offset( $tz ) )
Branch: 1 branch
BranchTrueFalseTotal
if (_is_valid_tz_offset($tz)) 24 0 24
631 {
632 24 ▶ printf( "# timezone matched (%s)\n" , $tz ) if $ENV{DFF_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DFF_DEBUG"} 0 24 24
633 24 $date =~ s{\Q$original_tz\E\.?\z}{};
634 24 $p->{time_zone} = $tz;
635 24 return ( $date , $p );
636 }
637 }
638  
639 5631 ▶ if ( length( $date ) > 15 and ($date =~ m{\dT\d} or $date =~ m{\d\s\d}))
Branch: 1 branch
BranchTrueFalseTotal
if (length $date > 15 and $date =~ /\dT\d/ || $date =~ /\d\s\d/) 2620 3011 5631
Condition100%6/6
length $date > 15 and $date =~ /\dT\d/ || $date =~ /\d\s\d/
!ll&&!rl&&r
22287912612
$date =~ /\dT\d/ or $date =~ /\d\s\d/
l!l&&r!l&&!r
92611783
MC/DC66%2/3
length $date > 15 and $date =~ /\dT\d/ || $date =~ /\d\s\d/
length $date > 15 $date =~ /\dT\d/ $date =~ /\d\s\d/
Truth table75%3/4
A and B or C
A= length $date > 15B= $date =~ /\dT\d/C= $date =~ /\d\s\d/
ABCresult
0XX0
1000
11X1
1011
640 {
641 # this pattern conflicts with 5-08, 01-02-03, 08-Jan-99, 2006-Dec-08
642 # so we need to check the length and make sure it is long enough to be
643 # a full iso datetime, and that is has a 'T' or ' ' (space) surrounded by digits
644 2620 ▶ if ( my ( $tz ) = $date =~ m{(
Branch: 1 branch
BranchTrueFalseTotal
if (my($tz) = $date =~ /( (?:\s+)?[-+]\d{2,4} # '-0800', '-08', ' -08' )\.?\z/mx) 20 2600 2620
645 (?:\s+)?[-+]\d{2,4} # '-0800', '-08', ' -08'
646 )\.?\z}mx )
647 {
648 20 ▶ printf( "# possible timezone (%s) in (%s)\n", $tz, $date) if $ENV{DFF_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DFF_DEBUG"} 0 20 20
649 20 my $original_tz = $tz;
650 20 $tz =~ s{:}{};
651 # some timezones are 2 digit hours, add the minutes part
652 20 $tz = _clean_whitespace( $tz );
653 20 ▶ $tz .= '00' if ( length( $tz ) == 3 );
Branch: 1 branch
BranchTrueFalseTotal
if length $tz == 3 18 2 20
654 20 ▶ if ( _is_valid_tz_offset( $tz ) )
Branch: 1 branch
BranchTrueFalseTotal
if (_is_valid_tz_offset($tz)) 18 2 20
655 {
656 18 $date =~ s{\Q$original_tz\E\.?\z}{};
657 18 $p->{time_zone} = $tz;
658 18 return ( $date , $p );
659 }
660 }
661 }
662  
663 # search for positive/negative 4 digit timezones that are inside the string
664 # must be surrounded by spaces
665 # Mon Apr 05 17:25:35 +0000 2010
666 5613 ▶ if ( my ( $tz ) = $date =~ m{\s(
Branch: 1 branch
BranchTrueFalseTotal
if (my($tz) = $date =~ /\s( [-+]\d{4} # Mon Apr 05 17:25:35 +0000 2010 |[-+]\d{2}:\d{2} # Mon Apr 05 17:25:35 +00:00 2010 )\s/mx) 7 5606 5613
667 [-+]\d{4} # Mon Apr 05 17:25:35 +0000 2010
668 |[-+]\d{2}:\d{2} # Mon Apr 05 17:25:35 +00:00 2010
669 )\s}mx )
670 {
671 7 my $original_tz = $tz;
672 7 $tz =~ s{:}{};
673 7 ▶ if ( _is_valid_tz_offset( $tz ) )
Branch: 1 branch
BranchTrueFalseTotal
if (_is_valid_tz_offset($tz)) 7 0 7
674 {
675 7 $date =~ s{\Q$original_tz\E}{};
676 7 $p->{time_zone} = $tz;
677 7 return ( $date , $p );
678 }
679 }
680  
681 5606 return ( $date , $p );
682 }
683  
684 sub _is_valid_tz_offset
685 {
686 51 ▶ my ($offset) = @_;
Subroutine _is_valid_tz_offset: called CC=1 SCAR=0.0
687  
688 # https://en.wikipedia.org/wiki/List_of_UTC_time_offsets
689 51 my $valid = {
690 '-1200' => 1,
691 '-1100' => 1,
692 '-1000' => 1,
693 '-0930' => 1,
694 '-0900' => 1,
695 '-0800' => 1,
696 '-0700' => 1,
697 '-0600' => 1,
698 '-0500' => 1,
699 '-0400' => 1,
700 '-0330' => 1,
701 '-0300' => 1,
702 '-0200' => 1,
703 '-0100' => 1,
704 '-0000' => 1,
705 '+0000' => 1,
706 '+0100' => 1,
707 '+0200' => 1,
708 '+0300' => 1,
709 '+0330' => 1,
710 '+0400' => 1,
711 '+0430' => 1,
712 '+0500' => 1,
713 '+0530' => 1,
714 '+0545' => 1,
715 '+0600' => 1,
716 '+0630' => 1,
717 '+0700' => 1,
718 '+0800' => 1,
719 '+0845' => 1,
720 '+0900' => 1,
721 '+0930' => 1,
722 '+1000' => 1,
723 '+1030' => 1,
724 '+1100' => 1,
725 '+1200' => 1,
726 '+1245' => 1,
727 '+1300' => 1,
728 '+1400' => 1,
729 };
730 51 return exists $valid->{$offset};
731 }
732  
733 sub _do_math
734 {
735 0 ▶ my ( $string ) = @_;
Subroutine _do_math: not called CC=4 SCAR=30.0
736 0 ▶ if ( $string =~ m{ago}mx )
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /ago/mx) 0 0 0
737 {
738 0 my $base_dt = __PACKAGE__->base;
739 0 ▶ if ( my ( $amount , $unit ) = $string =~ m{(\d+)\s([^\s]+)}mx )
Branch: 1 branch
BranchTrueFalseTotal
if (() = $string =~ /(\d+)\s([^\s]+)/mx) 0 0 0
740 {
741 0 ▶ $unit .= 's' if ( $unit !~ m{s\z} ); # make sure the unit ends in 's'
Branch: 1 branch
BranchTrueFalseTotal
unless $unit =~ /s\z/ 0 0 0
742 0 return $base_dt->subtract( $unit => $amount );
743 }
744 }
745 0 return $string;
746 }
747  
748 sub _clean_whitespace
749 {
750 6356 ▶ my ( $string ) = @_;
Subroutine _clean_whitespace: called CC=1 SCAR=0.0
751 6356 $string =~ s{\A\s+}{}mx; # trim front
752 6356 $string =~ s{\s+\z}{}mx; # trim back
753  
754 6356 $string =~ s{\s+}{ }gmx; # remove extra whitespace from the middle
755 6356 $string =~ s{"}{}gmx;
756 6356 return $string;
757 }
758  
759 sub _fix_ampm
760 {
761 1455 ▶ my %args = @_;
Subroutine _fix_ampm: called CC=9 SCAR=22.5
762  
763 1455 ▶ return if not defined $args{parsed}{ampm};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"parsed"}{"ampm"} 0 1455 1455
764  
765 1455 ▶ printf( "# have ampm [%s]\n", $args{parsed}{ampm} ) if $ENV{DFF_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DFF_DEBUG"} 0 1455 1455
766  
767 1455 my $ampm = $args{parsed}{ampm};
768 1455 delete $args{parsed}{ampm};
769  
770 1455 ▶ if ( $ampm =~ m{a\.?m?\.?}mix )
Branch: 2 branches
BranchTrueFalseTotal
if ($ampm =~ /a\.?m?\.?/mix) { } 708 747 1455
elsif ($ampm =~ /p\.?m?\.?/mix) { } 747 0 747
771 {
772 708 ▶ printf( "# found am hour=[%s]\n", $args{parsed}{hour} ) if $ENV{DFF_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DFF_DEBUG"} 0 708 708
773 708 ▶ if( $args{parsed}{hour} == 12 )
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"parsed"}{"hour"} == 12) 354 354 708
774 {
775 354 $args{parsed}{hour} = 0;
776 }
777 708 return 1;
778 }
779 elsif ( $ampm =~ m{p\.?m?\.?}mix )
780 {
781 747 ▶ printf( "# found pm hour=[%s]\n", $args{parsed}{hour} ) if $ENV{DFF_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DFF_DEBUG"} 0 747 747
782 747 $args{parsed}{hour} += 12;
783 747 ▶ if ( $args{parsed}{hour} == 24 )
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"parsed"}{"hour"} == 24) 353 394 747
784 {
785 353 $args{parsed}{hour} = 12;
786 }
787 747 return 1;
788 }
789 0 return 1;
790 }
791  
792 sub _fix_zero_month
793 {
794 807 ▶ my %args = @_;
Subroutine _fix_zero_month: called CC=5 SCAR=16.5
795  
796 807 ▶ return 1 if not exists $args{parsed}{month};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"parsed"}{"month"} 0 807 807
797 807 ▶ return 1 if not defined $args{parsed}{month};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"parsed"}{"month"} 0 807 807
798  
799 807 ▶ if ($args{parsed}{month} == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"parsed"}{"month"} == 0) 13 794 807
800 {
801 # they probably meant october
802 13 ▶ print( "# month => 0 => 10\n") if $ENV{DFF_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DFF_DEBUG"} 0 13 13
803 13 $args{parsed}{month} = 10;
804 }
805  
806 807 return 1;
807 }
808  
809 sub _set_default_seconds
810 {
811 2 ▶ my %args = @_;
Subroutine _set_default_seconds: called CC=1 SCAR=0.0
812 2 $args{parsed}{second} = 0;
813 2 return 1;
814 }
815  
816 sub _set_default_year
817 {
818 15 ▶ my %args = @_;
Subroutine _set_default_year: called CC=1 SCAR=0.0
819 15 $args{parsed}{year} = __PACKAGE__->base->year;
820 15 return 1;
821 }
822  
823 sub _set_year
824 {
825 18 ▶ my %args = @_;
Subroutine _set_year: called CC=7 SCAR=29.4
826 18 ▶ my %constructor_args = $args{args} ? @{$args{args}} : ();
Branch: 1 branch
BranchTrueFalseTotal
$args{'args'} ? : 0 18 18
827 18 ▶ return 1 if defined $args{parsed}{year}; # year is already set
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"parsed"}{"year"} 0 18 18
828  
829 18 ▶ if ( $constructor_args{prefer_future} )
Branch: 1 branch
BranchTrueFalseTotal
if ($constructor_args{"prefer_future"}) 0 18 18
830 {
831 0 ▶ if ( $args{parsed}{month} < __PACKAGE__->base->month or
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"parsed"}{"month"} < __PACKAGE__->base->month or $args{"parsed"}{"month"} eq __PACKAGE__->base->month and $args{"parsed"}{"day"} < __PACKAGE__->base->day) 0 0 0
Condition0%0/6
$args{"parsed"}{"month"} < __PACKAGE__->base->month or $args{"parsed"}{"month"} eq __PACKAGE__->base->month and $args{"parsed"}{"day"} < __PACKAGE__->base->day
l!l&&r!l&&!r
000
$args{"parsed"}{"month"} eq __PACKAGE__->base->month and $args{"parsed"}{"day"} < __PACKAGE__->base->day
!ll&&!rl&&r
000
MC/DC0%0/3
$args{"parsed"}{"month"} < __PACKAGE__->base->month or $args{"parsed"}{"month"} eq __PACKAGE__->base->month and $args{"parsed"}{"day"} < __PACKAGE__->base->day
$args{"parsed"}{"month"} < __PACKAGE__->base->month $args{"parsed"}{"month"} eq __PACKAGE__->base->month $args{"parsed"}{"day"} < __PACKAGE__->base->day
Truth table0%0/4
A or B and C
A= $args{"parsed"}{"month"} < __PACKAGE__->base->monthB= $args{"parsed"}{"month"} eq __PACKAGE__->base->monthC= $args{"parsed"}{"day"} < __PACKAGE__->base->day
ABCresult
1XX1
0111
00X0
0100
832 ( $args{parsed}{month} eq __PACKAGE__->base->month and
833 $args{parsed}{day} < __PACKAGE__->base->day ) )
834 {
835 0 $args{parsed}{year} = __PACKAGE__->base->clone->add( years => 1 )->year;
836 0 return 1;
837 }
838 }
839 18 $args{parsed}{year} = __PACKAGE__->base->year;
840 18 return 1;
841 }
842  
843 sub _fix_year
844 {
845 2801 ▶ my %args = @_;
Subroutine _fix_year: called CC=2 SCAR=6.9
846 2801 ▶ return 1 if( length( $args{parsed}{year} ) == 4 );
Branch: 1 branch
BranchTrueFalseTotal
if length $args{"parsed"}{"year"} == 4 2108 693 2801
847 693 my $now = DateTime->now;
848 693 $args{parsed}{year} = __PACKAGE__->_pick_year( $args{parsed}{year} , $now );
849 693 return 1;
850 }
851  
852 sub _pick_year
853 {
854 699 ▶ my ( $self , $year , $dt ) = @_;
Subroutine _pick_year: called CC=4 SCAR=13.9
855  
856 699 ▶ if( $year > 69 )
Branch: 1 branch
BranchTrueFalseTotal
if ($year > 69) { } 139 560 699
857 {
858 139 ▶ if( $dt->strftime( '%y' ) > 69 )
Branch: 1 branch
BranchTrueFalseTotal
if ($dt->strftime('%y') > 69) { } 1 138 139
859 {
860 1 $year = $dt->strftime( '%C' ) . sprintf( '%02s' , $year );
861 }
862 else
863 {
864 138 $year = $dt->subtract( years => 100 )->strftime( '%C' ) .
865 sprintf( '%02s' , $year );
866 }
867 }
868 else
869 {
870 560 ▶ if( $dt->strftime( '%y' ) > 69 )
Branch: 1 branch
BranchTrueFalseTotal
if ($dt->strftime('%y') > 69) { } 1 559 560
871 {
872 1 $year = $dt->add( years => 100 )->strftime( '%C' ) .
873 sprintf( '%02s' , $year );
874 }
875 else
876 {
877 559 $year = $dt->strftime( '%C' ) . sprintf( '%02s' , $year );
878 }
879 }
880 699 return $year;
881 }
882  
883 22 1;
884  
885 __END__
886  
887 =encoding utf-8
888  
889 =head1 NAME
890  
891 DateTime::Format::Flexible - DateTime::Format::Flexible - Flexibly parse strings and turn them into DateTime objects.
892  
893 =head1 SYNOPSIS
894  
895 use DateTime::Format::Flexible;
896 my $dt = DateTime::Format::Flexible->parse_datetime(
897 'January 8, 1999'
898 );
899 # $dt = a DateTime object set at 1999-01-08T00:00:00
900  
901 =head1 DESCRIPTION
902  
903 If you have ever had to use a program that made you type in the
904 date a certain way and thought "Why can't the computer just figure
905 out what date I wanted?", this module is for you.
906  
907 F<DateTime::Format::Flexible> attempts to take any string you give
908 it and parse it into a DateTime object.
909  
910 =head1 USAGE
911  
912 This module uses F<DateTime::Format::Builder> under the covers.
913  
914 =head2 parse_datetime
915  
916 Give it a string and it attempts to parse it and return a DateTime
917 object.
918  
919 If it cannot it will throw an exception.
920  
921 my $dt = DateTime::Format::Flexible->parse_datetime( $date );
922  
923 my $dt = DateTime::Format::Flexible->parse_datetime(
924 $date,
925 strip => [qr{\.\z}], # optional, remove a trailing period
926 tz_map => {EDT => 'America/New_York'}, # optional, map the EDT timezone to America/New_York
927 lang => ['es'], # optional, only parse using spanish
928 european => 1, # optional, catch some cases of DD-MM-YY
929 );
930  
931 =over 4
932  
933 =item * C<base> (optional)
934  
935 Does the same thing as the method C<base>. Sets a base datetime for
936 incomplete dates. Requires a valid DateTime object as an argument.
937  
938 example:
939  
940 my $base_dt = DateTime->new( year => 2005, month => 2, day => 1 );
941 my $dt = DateTime::Format::Flexible->parse_datetime(
942 '18 Mar',
943 base => $base_dt,
944 );
945 # $dt is now 2005-03-18T00:00:00
946  
947 =item * C<strip> (optional)
948  
949 Remove a substring from the string you are trying to parse.
950 You can pass multiple regexes in an arrayref.
951  
952 example:
953  
954 my $dt = DateTime::Format::Flexible->parse_datetime(
955 '2011-04-26 00:00:00 (registry time)',
956 strip => [qr{\(registry time\)\z}],
957 );
958 # $dt is now 2011-04-26T00:00:00
959  
960 This is helpful if you have a load of dates you want to normalize
961 and you know of some weird formatting beforehand.
962  
963 =item * C<tz_map> (optional)
964  
965 Map a given timezone to another recognized timezone
966 Values are given as a hashref.
967  
968 example:
969  
970 my $dt = DateTime::Format::Flexible->parse_datetime(
971 '25-Jun-2009 EDT',
972 tz_map => {EDT => 'America/New_York'},
973 );
974 # $dt is now 2009-06-25T00:00:00 with a timezone of America/New_York
975  
976 This is helpful if you have a load of dates that have timezones that
977 are not recognized by F<DateTime::Timezone>.
978  
979 =item * C<lang> (optional)
980  
981 Specify the language map plugins to use.
982  
983 When DateTime::Format::Flexible parses a date with a string in it,
984 it will search for a way to convert that string to a number. By
985 default it will search through all the language plugins to search
986 for a match.
987  
988 NOTE: as of 0.22, it will only do this search if it detects a string
989 in the given date.
990  
991 Setting C<lang> this lets you limit the scope of the search.
992  
993 example:
994  
995 my $dt = DateTime::Format::Flexible->parse_datetime(
996 'Wed, Jun 10, 2009',
997 lang => ['en'],
998 );
999 # $dt is now 2009-06-10T00:00:00
1000  
1001 Currently supported languages are english (en), spanish (es) and
1002 german (de). Contributions, corrections, requests and examples
1003 are VERY welcome.
1004  
1005 See the F<DateTime::Format::Flexible::lang::en>,
1006 F<DateTime::Format::Flexible::lang::es>, and
1007 F<DateTime::Format::Flexible::lang::de>
1008 for examples of the plugins.
1009  
1010 =item * C<european> (optional)
1011  
1012 If european is set to a true value, an attempt will be made to parse
1013 as a DD-MM-YYYY date instead of the default MM-DD-YYYY. There is a
1014 chance that this will not do the right thing due to ambiguity.
1015  
1016 example:
1017  
1018 my $dt = DateTime::Format::Flexible->parse_datetime(
1019 '16/06/2010' , european => 1,
1020 );
1021 # $dt is now 2010-06-16T00:00:00
1022  
1023 =item * C<MMYY> (optional)
1024  
1025 By default, this module will parse 12/10 as December 10th of the
1026 current year (MM/DD).
1027  
1028 If you want it to parse this as MM/YY instead, you can enable the
1029 C<MMYY> option.
1030  
1031 example:
1032  
1033 my $dt = DateTime::Format::Flexible->parse_datetime('12/10');
1034 # $dt is now [current year]-12-10T00:00:00
1035  
1036 my $dt = DateTime::Format::Flexible->parse_datetime(
1037 '12/10', MMYY => 1,
1038 );
1039 # $dt is now 2010-12-01T00:00:00
1040  
1041 This is useful if you know you are going to be parsing a credit card
1042 expiration date.
1043  
1044 =back
1045  
1046 =head2 base
1047  
1048 gets/sets the base DateTime for incomplete dates. Requires a valid
1049 DateTime object as an argument when setting. This defaults to
1050 DateTime->now.
1051  
1052 example:
1053  
1054 DateTime::Format::Flexible->base( DateTime->new(
1055 year => 2009, month => 6, day => 22
1056 ));
1057 my $dt = DateTime::Format::Flexible->parse_datetime( '23:59' );
1058 # $dt is now 2009-06-22T23:59:00
1059  
1060 =head2 build
1061  
1062 an alias for parse_datetime
1063  
1064 =head2 Example formats
1065  
1066 A small list of supported formats:
1067  
1068 =over 4
1069  
1070 =item YYYYMMDDTHHMMSS
1071  
1072 =item YYYYMMDDTHHMM
1073  
1074 =item YYYYMMDDTHH
1075  
1076 =item YYYYMMDD
1077  
1078 =item YYYYMM
1079  
1080 =item MM-DD-YYYY
1081  
1082 =item MM-D-YYYY
1083  
1084 =item MM-DD-YY
1085  
1086 =item M-DD-YY
1087  
1088 =item YYYY/DD/MM
1089  
1090 =item YYYY/M/DD
1091  
1092 =item YYYY/MM/D
1093  
1094 =item M-D
1095  
1096 =item MM-D
1097  
1098 =item M-D-Y
1099  
1100 =item Month D, YYYY
1101  
1102 =item Mon D, YYYY
1103  
1104 =item Mon D, YYYY HH:MM:SS
1105  
1106 =item ... thousands more
1107  
1108 =back
1109  
1110 there are 9000+ variations that are detected correctly in the test
1111 files (see t/data/* for most of them). If you can think of any that
1112 I do not cover, please let me know.
1113  
1114 =head1 NOTES
1115  
1116 As of version 0.11 you will get a DateTime::Infinite::Future object
1117 if the passed in date is 'infinity' and a DateTime::Infinite::Past
1118 object if the passed in date is '-infinity'. If you are expecting
1119 these types of strings, you might want to check for
1120 'is_infinite()' from the object returned.
1121  
1122 example:
1123  
1124 my $dt = DateTime::Format::Flexible->parse_datetime( 'infinity' );
1125 if ( $dt->is_infinite )
1126 {
1127 # you have a Infinite object.
1128 }
1129  
1130 =head1 BUGS/LIMITATIONS
1131  
1132 You cannot use a 1 or 2 digit year as the first field unless the
1133 year is > 31:
1134  
1135 YY-MM-DD # not supported if YY is <= 31
1136 Y-MM-DD # not supported
1137  
1138 It gets confused with MM-DD-YY
1139  
1140 =head1 AUTHOR
1141  
1142 Tom Heady <cpan@punch.net>
1143  
1144 =head1 COPYRIGHT & LICENSE
1145  
1146 Copyright 2007-2018 Tom Heady.
1147  
1148 This program is free software; you can redistribute it and/or
1149 modify it under the terms of either:
1150  
1151 =over 4
1152  
1153 =item * the GNU General Public License as published by the Free
1154 Software Foundation; either version 1, or (at your option) any
1155 later version, or
1156  
1157 =item * the Artistic License.
1158  
1159 =back
1160  
1161 =head1 SEE ALSO
1162  
1163 F<DateTime::Format::Builder>, F<DateTime::Timezone>, F<DateTime::Format::Natural>
1164  
1165 =cut
↑ Summary Flexible/lang.pm »