blib/lib/Date/EzDate.pm

filter: Statementstmt 83.2% 512 / 615 Branchbran 74.1% 267 / 360 Conditioncond 63.4% 66 / 104 MC/DCmcdc 33.9% 20 / 59 Subroutinesub 84.3% 54 / 64 Podpod 60.0% 9 / 15 totaltotal 76.2% 928 / 1217 CC 222 SCAR 37.1
CC
222
Cov
78%
STORE
105.4
FETCH
59.2
timefromfull
39.1
CRAP 40.8
↑ Summary
1 package Date::EzDate;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION @ltimefields $overload $default_warning);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # version
7 1 $VERSION = '1.16';
8  
9  
10 # documentation at end of file
11  
12 # debugging tools
13 # use Debug::ShowStuff ':all';
14  
15  
16 # object overloading
17 use overload
18 7 ▶ '""' => sub{$_[0]->{'default'}}, # stringification
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
19 1 '<=>' => \&compare, # comparison
20 '+' => \&addition, # addition
21 '-' => \&subtraction, # subtraction
22 1 fallback => 1; # operations not defined here
23  
24  
25 # constants and globals
26 1 ▶use constant WARN_NONE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use constant WARN_STDERR => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use constant WARN_CROAK => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 $default_warning = WARN_STDERR;
30 1 $overload = 'epochday';
31  
32 # psuedo-constants
33 1 @ltimefields = qw[sec min hour dayofmonth monthnum year weekdaynum yearday dst];
34  
35  
36 #------------------------------------------------------------------------------
37 # export
38 #
39 1 ▶use vars qw[@EXPORT_OK %EXPORT_TAGS @ISA];
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 @ISA = 'Exporter';
41  
42 1 @EXPORT_OK = qw[
43 date_range_string
44 time_range_string
45 day_lumps
46 ];
47  
48 1 %EXPORT_TAGS = ('all' => [@EXPORT_OK]);
49 #
50 # export
51 #------------------------------------------------------------------------------
52  
53  
54 #------------------------------------------------------------------------------
55 # new
56 #
57 81 ▶sub new {
Subroutine new: called CC=9 SCAR=25.6
Pod documented
58 81 my ($class, $init, %opts) = @_;
59 81 my ($rv, %tiehash);
60
61 # if date is an EzDate object
62 81 ▶ if ($init && UNIVERSAL::isa($init, 'Date::EzDate'))
Branch: 1 branch
BranchTrueFalseTotal
if ($init and UNIVERSAL::isa($init, "Date::EzDate")) 0 81 81
Condition66%2/3
$init and UNIVERSAL::isa($init, "Date::EzDate")
!ll&&!rl&&r
1800
MC/DC0%0/2
$init and UNIVERSAL::isa($init, "Date::EzDate")
$init UNIVERSAL::isa($init, "Date::EzDate")
Truth table66%2/3
A and B
A= $initB= UNIVERSAL::isa($init, "Date::EzDate")
ABresult
0X0
100
111
63 0 { return $init->clone }
64
65 # if string doesn't have any content, undef it
66 81 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $init and not $init =~ /\S/s) 0 81 81
Condition66%2/3
defined $init and not $init =~ /\S/s
!ll&&!rl&&r
1800
MC/DC0%0/2
defined $init and not $init =~ /\S/s
defined $init not $init =~ /\S/s
Truth table66%2/3
A and not(B)
A= defined $initB= not $init =~ /\S/s
ABresult
0X0
100
111
67 defined($init) &&
68 (! ($init =~ m|\S|s) )
69 ) {
70 0 undef $init;
71 }
72
73 # preprocess full time
74 81 ▶ $class->can('preprocess_time_string') and
Branch: 1 branch
BranchTrueFalseTotal
$class->can("preprocess_time_string") and defined $init and $init = $class->preprocess_time_string($init) 0 81 81
Condition33%1/3
$class->can("preprocess_time_string") and defined $init
!ll&&!rl&&r
8100
MC/DC0%0/2
$class->can("preprocess_time_string") and defined $init
$class->can("preprocess_time_string") defined $init
Truth table33%1/3
A and B
A= $class->can("preprocess_time_string")B= defined $init
ABresult
0X0
100
111
75 defined($init) and
76 $init = $class->preprocess_time_string($init);
77
78 81 ▶ tie(%tiehash, $class . '::Tie', $init, %opts) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
tie %tiehash, $class . '::Tie', $init, %opts or return undef 81 0 81
79
80 81 $rv = bless(\%tiehash, $class);
81 81 ▶ $rv->can('after_create') and $rv->after_create();
Branch: 1 branch
BranchTrueFalseTotal
$rv->can("after_create") and $rv->after_create 0 81 81
82
83 81 return $rv;
84 }
85 #
86 # new
87 #------------------------------------------------------------------------------
88  
89  
90 #------------------------------------------------------------------------------
91 # clone
92 #
93 21 ▶sub clone {
Subroutine clone: called CC=2 SCAR=7.0
Pod documented
94 21 my ($self) = @_;
95 21 my $ob = $self->tie_ob;
96 21 my ($rv);
97
98 $rv = ref($self)->new([
99 $ob->{'sec'},
100 $ob->{'min'},
101 $ob->{'hour'},
102 $ob->{'dayofmonth'},
103 $ob->{'monthnum'},
104 $ob->{'year'},
105 $ob->{'weekdaynum'},
106 $ob->{'yearday'},
107 $ob->{'dst'},
108 21 $ob->{'epochsec'}
109 ]);
110
111 21 %{$ob->{'formats'}} and
112 21 ▶ $rv->tie_ob->{'formats'} = {%{$ob->{'formats'}}};
Branch: 1 branch
BranchTrueFalseTotal
%{$ob->{"formats"};} and $rv->tie_ob->{"formats"} = {%{$ob->{"formats"};}} 21 0 21
113
114 21 return $rv;
115 }
116 #
117 # clone
118 #------------------------------------------------------------------------------
119  
120  
121 #------------------------------------------------------------------------------
122 # format subs
123 #
124 0 ▶sub set_warnings {return $_[0]->tie_ob->{'warnings'} = $_[1]}
Subroutine set_warnings: not called CC=1 SCAR=6.9
Pod documented
125 0 ▶sub zero_hour_ampm {return $_[0]->tie_ob->{'zero_hour_ampm'} = $_[1]}
Subroutine zero_hour_ampm: not called CC=1 SCAR=6.9
Pod documented
126  
127 1 ▶sub set_format {return $_[0]->tie_ob->set_format(@_[1..$#_])}
Subroutine set_format: called CC=1 SCAR=0.0
Pod documented
128  
129 0 ▶sub get_format {
Subroutine get_format: not called CC=1 SCAR=6.9
Pod undocumented
130 0 my ($self, $key) = @_;
131 0 my $ob = $self->tie_ob;
132 0 $key =~ s|\s||gs;
133 0 $key = lc($key);
134 0 return join('', @{$ob->{'formats'}->{$key}});
135 }
136  
137 0 ▶sub del_format {return delete $_[0]->{$_[1]}}
Subroutine del_format: not called CC=1 SCAR=6.9
Pod undocumented
138 46 ▶sub tie_ob{return tied(%{$_[0]})}
Subroutine tie_ob: called CC=1 SCAR=0.0
Pod undocumented
139 #
140 # format subs
141 #------------------------------------------------------------------------------
142  
143  
144 #------------------------------------------------------------------------------
145 # next_month
146 #
147 3 ▶sub next_month {
Subroutine next_month: called CC=1 SCAR=0.0
Pod documented
148 3 my ($self, $jump) = @_;
149 3 my $ob = $self->tie_ob;
150 3 return $ob->next_month($jump);
151 }
152 #
153 # next_month
154 #------------------------------------------------------------------------------
155  
156  
157 #------------------------------------------------------------------------------
158 # compare
159 #
160 1 ▶sub compare {
Subroutine compare: called CC=2 SCAR=7.1
Pod undocumented
161 1 my ($left, $right) = @_;
162 1 ▶ ref($right) or $right = Date::EzDate->new($right);
Branch: 1 branch
BranchTrueFalseTotal
ref $right or $right = "Date::EzDate"->new($right) 1 0 1
163 1 $left->{$overload} <=> $right->{$overload};
164 }
165 #
166 # compare
167 #------------------------------------------------------------------------------
168  
169  
170 #------------------------------------------------------------------------------
171 # addition and subtraction
172 #
173 1 ▶sub addition {
Subroutine addition: called CC=1 SCAR=0.0
Pod undocumented
174 1 my ($self, $val) = @_;
175 1 $self->{$overload} += $val;
176 1 return $self;
177 }
178  
179 1 ▶sub subtraction {
Subroutine subtraction: called CC=1 SCAR=0.0
Pod undocumented
180 1 my ($self, $val) = @_;
181 1 $self->{$overload} -= $val;
182 1 return $self;
183 }
184 #
185 # addition and subtraction
186 #------------------------------------------------------------------------------
187  
188  
189 #------------------------------------------------------------------------------
190 # date_range_string
191 #
192 9 ▶sub date_range_string {
Subroutine date_range_string: called CC=13 SCAR=26.8
Pod documented
193 9 my ($class, $start, $end);
194
195 # if first argument is a reference, use it as start
196 9 ▶ if (UNIVERSAL::isa($_[0], 'Date::EzDate')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'Date::EzDate')) { } 0 9 9
197 0 ($start, $end) = @_;
198 0 $class = ref($start);
199 }
200
201 # else this sub is being called statically
202 else {
203 9 my (@args);
204
205 # expand array references
206 9 foreach my $arg (@_) {
207 14 ▶ if (UNIVERSAL::isa($arg, 'ARRAY'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, 'ARRAY')) { } 4 10 14
208 4 { push @args, @$arg }
209 else
210 10 { push @args, $arg }
211 }
212
213 # if called using a class
214 9 ▶ if ( int(@args/2)!=(@args/2) )
Branch: 1 branch
BranchTrueFalseTotal
if (int @args / 2 != @args / 2) { } 0 9 9
215 0 { ($class, $start, $end) = @args }
216
217 # else called w/o an explicit class
218 else {
219 9 $class = 'Date::EzDate';
220 9 ($start, $end) = @args;
221 }
222 }
223
224
225 # both $start and $end must be defined
226 9 ▶ unless ( defined($start) && defined($end) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $start and defined $end) 0 9 9
Condition33%1/3
defined $start and defined $end
!ll&&!rl&&r
009
MC/DC0%0/2
defined $start and defined $end
defined $start defined $end
Truth table33%1/3
A and B
A= defined $startB= defined $end
ABresult
0X0
100
111
227 0 { croak 'date_range_string requires defined start and end dates' }
228
229 # set as EzDate objects
230 9 ▶ ref($start) or $start = $class->new($start);
Branch: 1 branch
BranchTrueFalseTotal
ref $start or $start = $class->new($start) 3 6 9
231 9 ▶ ref($end) or $end = $class->new($end);
Branch: 1 branch
BranchTrueFalseTotal
ref $end or $end = $class->new($end) 3 6 9
232
233 # rearrange as necessary
234 9 ▶ if ($start->{'epoch day'} > $end->{'epoch day'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($start->{"epoch day"} > $end->{"epoch day"}) 0 9 9
235 0 ($start, $end) = ($end, $start);
236 }
237
238 # if same year
239 9 ▶ if ($start->{'year'} == $end->{'year'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start->{'year'} == $end->{'year'}) { } 8 1 9
240 # if same month
241 8 ▶ if ($start->{'monthnum'} == $end->{'monthnum'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start->{'monthnum'} == $end->{'monthnum'}) { } 7 1 8
242 # same day
243 7 ▶ if ($start->{'day of month'} == $end->{'day of month'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start->{'day of month'} == $end->{'day of month'}) { } 2 5 7
244 2 return $start->{'{month short} {day of month no zero}, {year}'};
245 }
246
247 # else different days
248 else {
249 5 my $format = '{day of month no zero}';
250
251 return
252 $end->{'month short'} . ' ' .
253 $start->{$format} .
254 '-' .
255 $end->{$format} .
256 ', ' .
257 5 $end->{'year'};
258 }
259 }
260
261 # else different months
262 else {
263 1 my $format = '{month short} {day of month no zero}';
264
265 return
266 $start->{$format} .
267 '-' .
268 $end->{$format} .
269 ', ' .
270 1 $end->{'year'};
271 }
272 }
273
274 # else different years, so do full range
275 else {
276 1 my $format = '{month short} {day of month no zero}, {year}';
277
278 return
279 $start->{$format} .
280 '-' .
281 1 $end->{$format};
282 }
283 }
284 #
285 # date_range_string
286 #------------------------------------------------------------------------------
287  
288  
289 #------------------------------------------------------------------------------
290 # time_range_string
291 #
292 2 ▶sub time_range_string {
Subroutine time_range_string: called CC=9 SCAR=25.5
Pod documented
293 2 my ($class, $start, $end, $rv);
294
295 # if first argument is a reference, use it as start
296 2 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 2 2
297 0 ($start, $end) = @_;
298 0 $class = ref($start);
299 }
300
301 # else this sub is being called statically
302 else {
303 # if called using a class
304 2 ▶ if ( int(@_/2)!=(@_/2) )
Branch: 1 branch
BranchTrueFalseTotal
if (int @_ / 2 != @_ / 2) { } 0 2 2
305 0 { ($class, $start, $end) = @_ }
306
307 # else called w/o an explicit class
308 else {
309 2 $class = 'Date::EzDate';
310 2 ($start, $end) = @_;
311 }
312 }
313
314 # both $start and $end must be defined
315 2 ▶ unless ( defined($start) && defined($end) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $start and defined $end) 0 2 2
Condition33%1/3
defined $start and defined $end
!ll&&!rl&&r
002
MC/DC0%0/2
defined $start and defined $end
defined $start defined $end
Truth table33%1/3
A and B
A= defined $startB= defined $end
ABresult
0X0
100
111
316 0 { croak 'time_range_string requires defined start and end dates' }
317
318 # set as EzDate objects
319 2 ▶ ref($start) or $start = $class->new($start);
Branch: 1 branch
BranchTrueFalseTotal
ref $start or $start = $class->new($start) 0 2 2
320 2 ▶ ref($end) or $end = $class->new($end);
Branch: 1 branch
BranchTrueFalseTotal
ref $end or $end = $class->new($end) 0 2 2
321
322 # rearrange as necessary
323 2 ▶ if ($start->{'epochsec'} > $end->{'epochsec'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($start->{"epochsec"} > $end->{"epochsec"}) 0 2 2
324 0 ($start, $end) = ($end, $start);
325 }
326
327 2 $rv = $start->{'ampm hour no zero'} . ':' . $start->{'min'};
328
329 # if same ampm
330 2 ▶ if ($start->{'ampm'} ne $end->{'ampm'})
Branch: 1 branch
BranchTrueFalseTotal
if ($start->{"ampm"} ne $end->{"ampm"}) 1 1 2
331 1 { $rv .= $start->{'ampm'} }
332
333 $rv .=
334 '-' .
335 $end->{'ampm hour no zero'} . ':' . $end->{'min'} .
336 2 $end->{'ampm'};
337
338 2 return $rv;
339 }
340 #
341 # time_range_string
342 #------------------------------------------------------------------------------
343  
344  
345 #------------------------------------------------------------------------------
346 # day_lumps
347 #
348 1 ▶sub day_lumps {
Subroutine day_lumps: called CC=6 SCAR=18.0
Pod documented
349 1 my (@org) = @_;
350 1 my ($class, @rv, $current);
351 1 $class = 'Date::EzDate';
352
353 1 foreach my $date (@org) {
354 8 ▶ ref($date) or $date = $class->new($date);
Branch: 1 branch
BranchTrueFalseTotal
ref $date or $date = $class->new($date) 0 8 8
355
356 8 ▶ if ($current) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current) { } 7 1 8
357 7 ▶ if ( $date->{'epochday'} == ($current->[1]->{'epochday'} + 1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date->{'epochday'} == $current->[1]{'epochday'} + 1) { } 5 2 7
358 5 $current->[1] = $date;
359 }
360
361 else {
362 2 push @rv, $current;
363 2 $current = [ $date, $date ];
364 }
365 }
366
367 else {
368 1 $current = [ $date, $date ];
369 }
370 }
371
372 # add last current
373 1 ▶ $current and push @rv, $current;
Branch: 1 branch
BranchTrueFalseTotal
$current and push @rv, $current 1 0 1
374
375 1 return @rv;
376 }
377 #
378 # day_lumps
379 #------------------------------------------------------------------------------
380  
381  
382  
383 ###############################################################################
384 # Date::EzDate::Tie
385 #
386 package Date::EzDate::Tie;
387 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
388 1 ▶use Carp 'croak', 'carp';
Subroutine BEGIN: called CC=1 SCAR=0.0
389 1 ▶use Tie::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
390 1 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
391 1 ▶use re 'taint';
Subroutine BEGIN: called CC=1 SCAR=0.0
392 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
393  
394 # debugging tools
395 # use Debug::ShowStuff ':all';
396  
397 1 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
398 @ISA
399 %WeekDayNums
400 %MonthNums
401 @MonthDays
402 @MonthLong
403 @MonthShort
404 @WeekDayLong
405 @WeekDayLong
406 @WeekDayShort
407 @DayOfMonthRd
408 %PCodes
409 $pcode
410 $epoch_offset
411 @OrdWords $OrdWordsRx %OrdWordsNums
412 @OrdNums $OrdNumsRx
413 1 );
414  
415 1 @ISA = 'Tie::StdHash';
416  
417  
418 # globals
419 1 @WeekDayShort = qw[Sun Mon Tue Wed Thu Fri Sat];
420 1 @WeekDayLong = qw[Sunday Monday Tuesday Wednesday Thursday Friday Saturday];
421 1 @MonthShort = qw[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec];
422 1 @MonthLong = qw[January February March April May June July August September October November December];
423 1 @MonthDays = qw[31 x 31 30 31 30 31 31 30 31 30 31];
424 1 @WeekDayNums{qw[sun mon tue wed thu fri sat]}=(0..6);
425 1 @MonthNums{qw[jan feb mar apr may jun jul aug sep oct nov dec]}=(0..11);
426  
427  
428 # ordinals
429 1 @OrdWords = qw[Zeroth First Second Third Fourth Fifth Sixth Seventh Eighth Ninth
430 Tenth Eleventh Twelfth Thirteenth Fourteenth Fifteenth Sixteenth Seventeenth Eighteenth Ninteenth
431 Twentieth Twentyfirst Twentysecond Twentythird Twentyfourth Twentyfifth Twentysixth Twentyseventh
432 Twentyeighth Twentyninth Thirtieth Thirtyfirst];
433 1 $OrdWordsRx = '\b(' . join('|', @OrdWords[1..$#OrdWords]) . ')\b';
434 1 foreach my $i (1..$#OrdWords)
435 31 {$OrdWordsNums{lc($OrdWords[$i])} = $i}
436 1 @OrdNums = qw[0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th
437 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th
438 20th 21st 22nd 23rd 24th 25th 26th 27th
439 28th 29th 30th 31st];
440  
441 # percent code regex
442 1 $pcode = '^\%[\w\%]$';
443  
444 1 %PCodes = qw[
445 yearlong year
446 yearshort yeartwodigits
447 month monthlong
448 weekday weekdayshort
449 dayofyear yearday
450 dayofyearbase1 yeardaybase1
451 %Y year
452 %y yeartwodigits
453 %a weekdayshort
454 %A weekdaylong
455 %d dayofmonth
456 %D %m/%d/%y
457 %H hour
458 %h monthshort
459 %b ampmhournozero
460 %B hournozero
461 %e monthnumbase1nozero
462 %f dayofmonthnozero
463 %j yeardaybase1
464 %k ampmhour
465 %m monthnumbase1
466 %M min
467 %P ampmuc
468 %p ampmlc
469 %s epochsec
470 %S sec
471 %w weekdaynum
472 %y yeartwodigits
473 %T %H:%M:%S
474 %n newline
475 %t tab
476 %% percent
477 ];
478 1 $PCodes{'%c'} = '{weekdayshort} %h %d %H:%M:%S %Y';
479 1 $PCodes{'%r'} = '%k:%M:%S %P';
480  
481  
482 # constants
483 1 ▶use constant t_60 => 60;
Subroutine BEGIN: called CC=1 SCAR=0.0
484 1 ▶use constant t_60_60 => 3600;
Subroutine BEGIN: called CC=1 SCAR=0.0
485 1 ▶use constant t_60_60_24 => 86400;
Subroutine BEGIN: called CC=1 SCAR=0.0
486 1 ▶use constant WARN_NONE => Date::EzDate::WARN_NONE;
Subroutine BEGIN: called CC=1 SCAR=0.0
487 1 ▶use constant WARN_STDERR => Date::EzDate::WARN_STDERR;
Subroutine BEGIN: called CC=1 SCAR=0.0
488 1 ▶use constant WARN_CROAK => Date::EzDate::WARN_CROAK;
Subroutine BEGIN: called CC=1 SCAR=0.0
489  
490 1 ▶use constant DST_ADJUST_NO => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
491 1 ▶use constant DST_ADJUST_YES => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
492  
493  
494 #------------------------------------------------------------------------------
495 # TIEHASH
496 #
497 81 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=6 SCAR=18.0
498 81 my ($class, $time, %opts)=@_;
499 81 my $self = bless ({}, $class);
500
501 # TESTING
502 # println '$class->TIEHASH(', $time, ')';
503 # my $indent = indent();
504
505 # set some non-date properties
506 81 ▶ $self->{'zero_hour_ampm'} = defined($opts{'zero_hour_ampm'}) ? $opts{'zero_hour_ampm'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'zero_hour_ampm'} ? : 0 81 81
507 81 $self->{'formats'} = {};
508 81 $self->{'settings'} = {'dst_kludge' => 1};
509
510 # default builtin formats
511 81 $self->set_format('fullday', '{month short} {day of month no zero}, {year}');
512 81 $self->set_format('fulldate', '{fullday}');
513 81 $self->set_format('dayandtime', '{month short} {day of month}, {year} {ampmhour no zero}:{minute}{ampm}');
514 81 $self->set_format('default', '{dayandtime}');
515
516
517 # if clone
518 81 ▶ if (ref $time){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $time) { } 21 60 81
519 21 ▶ if (! UNIVERSAL::isa($time, 'ARRAY') )
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($time, "ARRAY")) 0 21 21
520 0 { croak '$time is not an array' }
521
522 21 @{$self}{@Date::EzDate::ltimefields, 'epochsec'}=@{$time};
523 }
524
525 else {
526 # calculate and set properties of current time
527 # set time from timefromfull
528 60 $self->setfromtime(DST_ADJUST_NO, time());
529
530 60 ▶ if (defined $time) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time) 59 1 60
531 59 $time = $self->timefromfull($time);
532
533 59 ▶ defined($time) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $time or return undef 59 0 59
534
535 59 $self->setfromtime(DST_ADJUST_NO, $time);
536 }
537 }
538
539 81 return $self;
540 }
541 #
542 # TIEHASH
543 #------------------------------------------------------------------------------
544  
545  
546 #------------------------------------------------------------------------------
547 # setfromtime
548 #
549 1794 ▶sub setfromtime {
Subroutine setfromtime: called CC=8 SCAR=24.5
550 1794 my ($self, $dst_adjust, $time) = @_;
551 1794 my ($dst_before, @timevals);
552 1794 ▶ $self->{'settings'}->{'dst_kludge'} or $dst_adjust = 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"settings"}{"dst_kludge"} or $dst_adjust = 0 1794 0 1794
553
554 # TESTING
555 # println '$self->setfromtime';
556 # my $indent = indent();
557 # println '$dst_adjust: ', $dst_adjust;
558 # println '$time: ', $time;
559
560 # if we should adjust the hour if necessary
561 1794 ▶ if ($dst_adjust)
Branch: 1 branch
BranchTrueFalseTotal
if ($dst_adjust) 1628 166 1794
562 1628 { $dst_before = $self->{'dst'} }
563
564 # set current epoch second
565 1794 $self->{'epochsec'} = $time;
566
567 # get current time values
568 1794 @timevals = localtime($time);
569
570 1794 ▶ if (! @timevals)
Branch: 1 branch
BranchTrueFalseTotal
unless (@timevals) 0 1794 1794
571 0 { croak "cannot: get localtime values from this epoch value: $time" }
572
573 1794 @{$self}{@Date::EzDate::ltimefields} = @timevals;
574
575 1794 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($dst_adjust and defined $dst_before and $dst_before != $self->{"dst"}) 0 1794 1794
Condition66%4/6
$dst_adjust and defined $dst_before and $dst_before != $self->{"dst"}
!ll&&!rl&&r
16616280
$dst_adjust and defined $dst_before
!ll&&!rl&&r
16601628
MC/DC0%0/3
$dst_adjust and defined $dst_before and $dst_before != $self->{"dst"}
$dst_adjust defined $dst_before $dst_before != $self->{"dst"}
Truth table50%2/4
A and B and C
A= $dst_adjustB= defined $dst_beforeC= $dst_before != $self->{"dst"}
ABCresult
0XX0
10X0
1100
1111
576 $dst_adjust &&
577 defined($dst_before) &&
578 ($dst_before != $self->{'dst'})
579 ) {
580
581 0 ▶ if ($dst_before)
Branch: 1 branch
BranchTrueFalseTotal
if ($dst_before) { } 0 0 0
582 0 { $self->{'epochsec'} += t_60_60 }
583 else
584 0 { $self->{'epochsec'} -= t_60_60 }
585
586 0 @{$self}{@Date::EzDate::ltimefields}=localtime($self->{'epochsec'});
587 }
588
589 1794 $self->{'year'} += 1900;
590 }
591 #
592 # setfromtime
593 #------------------------------------------------------------------------------
594  
595  
596 #------------------------------------------------------------------------------
597 # set_format
598 #
599 325 ▶sub set_format {
Subroutine set_format: called CC=1 SCAR=0.0
600 325 my ($self, $name, $format) = @_;
601
602 # normalize name
603 325 $name =~ s|\s||g;
604 325 $name =~ tr/A-Z/a-z/;
605
606 325 $self->{'formats'}->{$name} = format_split($format);
607 }
608  
609 425 ▶sub format_split {
Subroutine format_split: called CC=3 SCAR=11.0
610 425 my @rv = split(m/(\{[^\{\}]*\}|\%.)/, $_[0]);
611
612 425 foreach my $el (@rv) {
613 2553 ▶ if ($el =~ m|^\{.*\}$|s)
Branch: 1 branch
BranchTrueFalseTotal
if ($el =~ /^\{.*\}$/s) 956 1597 2553
614 956 {normalize_key($el)}
615 }
616
617 425 return \@rv;
618 }
619 #
620 # set_format
621 #------------------------------------------------------------------------------
622  
623  
624 #------------------------------------------------------------------------------
625 # warn
626 #
627 0 ▶sub warn {
Subroutine warn: not called CC=4 SCAR=30.0
628 0 my $self = shift;
629 0 ▶ my $level = defined($self->{'warnings'}) ? $self->{'warnings'} : $Date::EzDate::default_warning;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'warnings'} ? : 0 0 0
630
631 0 ▶ $level or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$level or return undef 0 0 0
632
633 0 ▶ if ($level == WARN_STDERR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 1) 0 0 0
634 0 carp 'WARNING: ', @_;
635 0 return undef;
636 }
637
638 0 croak @_;
639 }
640 #
641 # warn
642 #------------------------------------------------------------------------------
643  
644  
645 #------------------------------------------------------------------------------
646 # normalize_key
647 #
648 11780 ▶sub normalize_key {
Subroutine normalize_key: called CC=2 SCAR=6.9
649 11780 $_[0] =~ s|\s||gs;
650 11780 ▶ $_[0] =~ tr/A-Z/a-z/ unless $_[0] =~ m|^\%\w$|;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^\%\w$/ 11760 20 11780
651 11780 $_[0] =~ s|ordinal|ord|sg;
652
653 11780 $_[0] =~ s|hours|hour|sg;
654 # $_[0] =~ s|days\b|day|sg;
655
656 11780 $_[0] =~ s|minute|min|sg;
657 11780 $_[0] =~ s|mins|min|sg;
658
659 11780 $_[0] =~ s|second|sec|sg;
660 11780 $_[0] =~ s|secs|sec|sg;
661
662 11780 $_[0] =~ s|number|num|sg;
663 }
664 #
665 # normalize_key
666 #------------------------------------------------------------------------------
667  
668  
669 #------------------------------------------------------------------------------
670 # next_month
671 #
672 3 ▶sub next_month {
Subroutine next_month: called CC=1 SCAR=0.0
673 3 my ($self, $jumps) = @_;
674 3 return $self->STORE('monthnum', $self->{'monthnum'} + $jumps);
675 }
676 #
677 # next_month
678 #------------------------------------------------------------------------------
679  
680  
681 #------------------------------------------------------------------------------
682 # STORE
683 #
684 1715 ▶sub STORE {
Subroutine STORE: called CC=61 SCAR=46.6
685 1715 my ($self, $key, $val) = @_;
686 1715 my $orgkey = $key;
687 1715 my $orgval = $val;
688
689 # error checking
690 1715 ▶ if (! defined $val)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 1715 1715
691 0 {return $self->warn('Must send a defined value when setting a property of an EzDate object')}
692
693 # if value contains {, assume they're assigning a format
694 1715 ▶ $val =~ m|[\{\%]| and return $self->set_format($key, $val);
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /[\{\%]/ and return $self->set_format($key, $val) 0 1715 1715
695
696 # clean a little
697 1715 normalize_key($key);
698
699 # get key from aliases if necessary
700 1715 $key = $self->get_alias($key, 'strip_no_zero'=>1);
701
702
703 # dayofmonth, weekdaynum, yearday
704 1715 ▶ if ($key =~ m/^(dayofmonth|weekdaynum|dayofweeknum|yearday)$/s) {
Branch: 22 branches
BranchTrueFalseTotal
if ($key =~ /^(dayofmonth|weekdaynum|dayofweeknum|yearday)$/s) { } 36 1679 1715
elsif ($key eq 'sec') { } 3 1676 1679
elsif ($key eq 'min') { } 3 1673 1676
elsif ($key eq 'minofday') { } 1 1672 1673
elsif ($key eq 'hour') { } 6 1666 1672
elsif ($key eq 'clocktime' or $key =~ /^mil(itary)?time$/) { } 3 1663 1666
elsif ($key eq 'ampmhour') { } 3 1660 1663
elsif ($key eq 'ampm' or $key eq 'ampmlc' or $key eq 'ampmuc') { } 25 1635 1660
elsif ($key eq 'dst') { } 0 1635 1635
elsif ($key eq 'epochsec') { } 0 1635 1635
elsif ($key eq 'epochmin') { } 0 1635 1635
elsif ($key eq 'epochhour') { } 0 1635 1635
elsif ($key eq 'epochday') { } 1543 92 1635
elsif ($key =~ /dayofmonthord(word|num)?/) { } 0 92 92
elsif ($key eq 'year') { } 24 68 92
elsif ($key =~ /^year(two|2)digit/) { } 2 66 68
elsif ($key =~ /^monthnumbase(one|1)/) { } 3 63 66
elsif ($key eq 'monthnum') { } 31 32 63
elsif ($key eq 'monthshort' or $key eq 'monthlong') { } 23 9 32
elsif ($key eq 'weekdayshort' or $key eq 'weekdaylong' or $key eq 'dayofweekshort' or $key eq 'dayofweeklong') { } 5 4 9
elsif ($key =~ /^yeardaybase(one|1)$/) { } 3 1 4
elsif ($key eq 'default' or $key eq 'full' or $key eq 'dayandtime' or $key eq 'dmy') { } 1 0 1
705 # warn if setting day of month greater than month has days
706 36 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "dayofmonth" and $val > daysinmonth($self->{"monthnum"}, $self->{"year"})) 0 36 36
Condition66%2/3
$key eq "dayofmonth" and $val > daysinmonth($self->{"monthnum"}, $self->{"year"})
!ll&&!rl&&r
12240
MC/DC0%0/2
$key eq "dayofmonth" and $val > daysinmonth($self->{"monthnum"}, $self->{"year"})
$key eq "dayofmonth" $val > daysinmonth($self->{"monthnum"}, $self->{"year"})
Truth table66%2/3
A and B
A= $key eq "dayofmonth"B= $val > daysinmonth($self->{"monthnum"}, $self->{"year"})
ABresult
0X0
100
111
707 ($key eq 'dayofmonth') &&
708 ($val > daysinmonth($self->{'monthnum'}, $self->{'year'}))
709 ) {
710 $self->warn(
711 "setting day of month ($val) to higher than days in month (",
712 0 daysinmonth($self->{'monthnum'}, $self->{'year'}),
713 ')',
714 );
715 }
716
717 $self->setfromtime(
718 DST_ADJUST_YES,
719 36 $self->{'epochsec'} - ($self->{$key} * t_60_60_24) + ($val * t_60_60_24)
720 );
721 }
722
723 elsif ($key eq 'sec')
724 3 {$self->setfromtime(DST_ADJUST_NO, $self->{'epochsec'} - $self->{'sec'} + $val)}
725
726 elsif ($key eq 'min') {
727 $self->setfromtime(
728 DST_ADJUST_NO,
729 3 $self->{'epochsec'} - ($self->{'min'} * 60) + ($val * 60)
730 )
731 }
732
733 elsif ($key eq 'minofday') {
734 $self->setfromtime (
735 DST_ADJUST_NO,
736 1 $self->{'epochsec'} - ($self->{'hour'} * t_60_60) - ($self->{'min'} * 60) + ($val * 60)
737 )
738 }
739
740 elsif ($key eq 'hour') {
741 $val = timelocal(
742 $self->{'sec'},
743 $self->{'min'},
744 $val,
745 $self->{'dayofmonth'},
746 $self->{'monthnum'},
747 6 $self->{'year'},
748 );
749
750 6 $self->setfromtime(DST_ADJUST_NO, $val);
751 }
752
753 # hour and minute
754 ▶ elsif ( ($key eq 'clocktime') || ($key =~ m|^mil(itary)?time$|) ) {
Condition100%3/3
$key eq "clocktime" or $key =~ /^mil(itary)?time$/
l!l&&r!l&&!r
121663
MC/DC100%2/2
$key eq "clocktime" or $key =~ /^mil(itary)?time$/
$key eq "clocktime" $key =~ /^mil(itary)?time$/
Truth table100%3/3
A or B
A= $key eq "clocktime"B= $key =~ /^mil(itary)?time$/
ABresult
1X1
011
000
755 3 my ($changed, $hour, $min, $sec) = $self->gettime($val);
756
757 3 ▶ unless (defined $hour)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hour) 0 3 3
758 0 {$hour = $self->{'hour'}}
759 3 ▶ unless (defined $min)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $min) 0 3 3
760 0 {$min = $self->{'min'}}
761 3 ▶ unless (defined $sec)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sec) 2 1 3
762 2 {$sec = $self->{'sec'}}
763
764 $self->setfromtime
765 (
766 0,
767 $self->{'epochsec'}
768
769 - ($self->{'sec'})
770 - ($self->{'min'} * 60)
771 3 - ($self->{'hour'} * t_60_60)
772
773 + ($sec)
774 + ($min * 60)
775 + ($hour * t_60_60)
776 );
777 }
778
779 elsif ($key eq 'ampmhour') {
780 3 ▶ if ($self->{'hour'} >= 12)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"hour"} >= 12) 2 1 3
781 2 {$val += 12}
782 3 $self->STORE('hour', $val);
783 }
784
785 ▶ elsif (
Condition100%6/6
$key eq "ampm" or $key eq "ampmlc" or $key eq "ampmuc"
l!l&&r!l&&!r
2321635
$key eq "ampm" or $key eq "ampmlc"
l!l&&r!l&&!r
2121637
MC/DC100%3/3
$key eq "ampm" or $key eq "ampmlc" or $key eq "ampmuc"
$key eq "ampm" $key eq "ampmlc" $key eq "ampmuc"
Truth table100%4/4
A or B or C
A= $key eq "ampm"B= $key eq "ampmlc"C= $key eq "ampmuc"
ABCresult
1XX1
01X1
0011
0000
786 ($key eq 'ampm') ||
787 ($key eq 'ampmlc') ||
788 ($key eq 'ampmuc')
789 ) {
790 25 my ($multiplier);
791
792 25 ▶ if (length($val) == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (length $val == 1) 1 24 25
793 1 {$val .= 'm'}
794 25 $val = lc($val);
795
796 # error checking
797 25 ▶ unless ( ($val eq 'am') || ($val eq 'pm') )
Branch: 1 branch
BranchTrueFalseTotal
unless ($val eq "am" or $val eq "pm") 0 25 25
Condition66%2/3
$val eq "am" or $val eq "pm"
l!l&&r!l&&!r
5200
MC/DC0%0/2
$val eq "am" or $val eq "pm"
$val eq "am" $val eq "pm"
Truth table66%2/3
A or B
A= $val eq "am"B= $val eq "pm"
ABresult
1X1
011
000
798 0 {return $self->warn('ampm may only be set to am or pm')}
799
800 # if no change, we're done
801 25 ▶ if ($self->{'hour'} < 12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'hour'} < 12) { } 21 4 25
802 21 ▶ if ($val eq 'am') {return}
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "am") 1 20 21
803 }
804 else {
805 0 ▶ if ($val eq 'pm') {return}
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "pm") 0 4 4
806 }
807
808 24 ▶ if ($val eq 'am')
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq 'am') { } 4 20 24
809 4 {$multiplier = -1}
810 else
811 20 {$multiplier = 1}
812
813 24 $self->setfromtime(DST_ADJUST_YES, $self->{'epochsec'} + (12 * t_60_60 * $multiplier) );
814 }
815
816 elsif ($key eq 'dst')
817 0 {return $self->warn('dst property is read-only')}
818
819 elsif ($key eq 'epochsec')
820 0 {$self->setfromtime(DST_ADJUST_NO, $val)}
821
822 elsif ($key eq 'epochmin')
823 0 {$self->setfromtime(DST_ADJUST_NO, $self->{'epochsec'} - ($self->getepochmin * 60) + ($val * 60) )}
824
825 elsif ($key eq 'epochhour')
826 0 {$self->setfromtime(DST_ADJUST_NO, $self->{'epochsec'} - ($self->getepochhour * t_60_60) + ($val * t_60_60) )}
827
828 elsif ($key eq 'epochday') {
829 1543 my ($oldhour, $oldepochsec, $oldmin);
830
831 $self->setfromtime(
832 DST_ADJUST_YES,
833 1543 $self->{'epochsec'} - ($self->getepochday * t_60_60_24) + (int($val) * t_60_60_24)
834 );
835 }
836
837 # ordinals
838 elsif ($key =~ m/dayofmonthord(word|num)?/) {
839 # if numeric
840 0 ▶ if ($val =~ s|^(\d+)\s*\w*$|$1|s)
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ s/^(\d+)\s*\w*$/$1/s) { } 0 0 0
841 0 {$self->STORE('dayofmonth', $val)}
842
843 # else word
844 else {
845 0 my $nval = $val;
846 0 $nval =~ tr/A-Z/a-z/;
847 0 $nval =~ s|\W||gs;
848
849 # if no such ordinal exists
850 0 ▶ unless ($nval = $OrdWordsNums{$nval})
Branch: 1 branch
BranchTrueFalseTotal
unless ($nval = $Date::EzDate::Tie::OrdWordsNums{$nval}) 0 0 0
851 0 {return $self->warn("Invalid ordinal: $val")}
852
853 0 $self->STORE('dayofmonth', $nval);
854 }
855 }
856
857 elsif ($key eq 'year') {
858 24 my ($maxday, $targetday);
859
860 # if same year, nothing to do
861 24 ▶ if ($self->{'year'} == $val)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"year"} == $val) 0 24 24
862 0 {return}
863
864 # make sure day of month isn't greater than maximum day of target month
865 24 $maxday = daysinmonth($self->{'monthnum'}, $val);
866
867 24 ▶ if ($self->{'dayofmonth'} > $maxday) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'dayofmonth'} > $maxday) { } 0 24 24
868 0 $self->warn(
869 "Changing the year sets day of month ($self->{'dayofmonth'}) to higher than days in month ($maxday); ",
870 "setting the day down to $maxday"
871 );
872 0 $targetday = $maxday;
873 }
874 else
875 24 {$targetday = $self->{'dayofmonth'}}
876
877 24 $val = timelocal($self->{'sec'}, $self->{'min'}, $self->{'hour'}, $targetday, $self->{'monthnum'}, $val);
878 24 $self->setfromtime(DST_ADJUST_YES, $val);
879 }
880
881 elsif ($key =~ m/^year(two|2)digit/) {
882 2 $val =~ s|^.*(..)$|$1|;
883 2 $self->STORE('year', substr($self->{'year'}, 0, 2) . zeropad_2($val));
884 }
885
886 elsif ($key =~ m/^monthnumbase(one|1)/)
887 3 {$self->STORE('monthnum', $val - 1)}
888
889
890 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
891 # monthnum
892 #
893 elsif ($key eq 'monthnum') {
894 31 my ($target, $epoch_second);
895 31 my $month = $self->{'monthnum'};
896 31 my $year = $self->{'year'};
897 31 my $orgday = $self->{'dayofmonth'};
898 31 my $dayofmonth = $self->{'dayofmonth'};
899 31 my $jumps = $val - $month;
900
901 # if nothing to do
902 31 ▶ $jumps or return;
Branch: 1 branch
BranchTrueFalseTotal
$jumps or return 31 0 31
903
904 31 $target = $jumps;
905 31 $target = abs($target);
906
907 # jumping forward
908 31 ▶ if ($jumps > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jumps > 0) { } 23 8 31
909 23 foreach (1..$target) {
910 # if end of year
911 35 ▶ if ($month == 11) {
Branch: 1 branch
BranchTrueFalseTotal
if ($month == 11) { } 2 33 35
912 2 $month = 0;
913 2 $year++;
914 }
915 else
916 33 {$month++}
917 }
918 }
919
920 # jumping backward
921 else {
922 8 foreach (1..$target) {
923 # if beginning of year
924 69 ▶ if ($month == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($month == 0) { } 2 67 69
925 2 $month = 11;
926 2 $year--;
927 }
928 else
929 67 {$month--}
930 }
931 }
932
933
934 # adjust day for shorter month (if necessary)
935 31 ▶ if ($dayofmonth > 28) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dayofmonth > 28) 9 22 31
936 9 my $dim = daysinmonth($month, $year);
937
938 9 ▶ if ($dim < $dayofmonth)
Branch: 1 branch
BranchTrueFalseTotal
if ($dim < $dayofmonth) 2 7 9
939 2 { $dayofmonth = $dim }
940 }
941
942 # get epoch second from timelocal
943 31 $epoch_second = timelocal($self->{'sec'}, $self->{'min'}, $self->{'hour'}, $dayofmonth, $month, $year);
944 31 $self->setfromtime(DST_ADJUST_NO, $epoch_second);
945 }
946 #
947 # monthnum
948 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
949
950
951 ▶ elsif ( ($key eq 'monthshort') || ($key eq 'monthlong') )
Condition100%3/3
$key eq "monthshort" or $key eq "monthlong"
l!l&&r!l&&!r
2219
MC/DC100%2/2
$key eq "monthshort" or $key eq "monthlong"
$key eq "monthshort" $key eq "monthlong"
Truth table100%3/3
A or B
A= $key eq "monthshort"B= $key eq "monthlong"
ABresult
1X1
011
000
952 23 {$self->STORE('monthnum', $MonthNums{lc(substr($val, 0, 3))})}
953
954 ▶ elsif (
Condition77%7/9
$key eq "weekdayshort" or $key eq "weekdaylong" or $key eq "dayofweekshort" or $key eq "dayofweeklong"
l!l&&r!l&&!r
504
$key eq "weekdayshort" or $key eq "weekdaylong" or $key eq "dayofweekshort"
l!l&&r!l&&!r
504
$key eq "weekdayshort" or $key eq "weekdaylong"
l!l&&r!l&&!r
324
MC/DC50%2/4
$key eq "weekdayshort" or $key eq "weekdaylong" or $key eq "dayofweekshort" or $key eq "dayofweeklong"
$key eq "weekdayshort" $key eq "weekdaylong" $key eq "dayofweekshort" $key eq "dayofweeklong"
Truth table60%3/5
A or B or C or D
A= $key eq "weekdayshort"B= $key eq "weekdaylong"C= $key eq "dayofweekshort"D= $key eq "dayofweeklong"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
955 ($key eq 'weekdayshort') ||
956 ($key eq 'weekdaylong') ||
957 ($key eq 'dayofweekshort') ||
958 ($key eq 'dayofweeklong')
959 ) {
960 $self->STORE(
961 'weekdaynum',
962 5 $WeekDayNums{lc(substr($val, 0, 3))})
963 }
964
965 # year day
966 elsif ($key =~ m/^yeardaybase(one|1)$/)
967 3 {$self->STORE('yearday', $val - 1)}
968
969 # default, full, dmy
970 ▶ elsif ( ($key eq 'default') || ($key eq 'full') || ($key eq 'dayandtime') || ($key eq 'dmy') ){
Condition33%3/9
$key eq "default" or $key eq "full" or $key eq "dayandtime" or $key eq "dmy"
l!l&&r!l&&!r
010
$key eq "default" or $key eq "full" or $key eq "dayandtime"
l!l&&r!l&&!r
001
$key eq "default" or $key eq "full"
l!l&&r!l&&!r
001
MC/DC0%0/4
$key eq "default" or $key eq "full" or $key eq "dayandtime" or $key eq "dmy"
$key eq "default" $key eq "full" $key eq "dayandtime" $key eq "dmy"
Truth table20%1/5
A or B or C or D
A= $key eq "default"B= $key eq "full"C= $key eq "dayandtime"D= $key eq "dmy"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
971 1 my (%opts);
972
973 1 ▶ if ( $key eq 'dmy')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "dmy") 1 0 1
974 1 {$opts{'dateonly'} = 1}
975
976 1 $self->setfromtime(DST_ADJUST_YES, $self->timefromfull($val, %opts));
977 }
978
979 else
980 0 {return $self->warn("Do not understand key: $orgkey")}
981 }
982 #
983 # STORE
984 #------------------------------------------------------------------------------
985  
986  
987 #------------------------------------------------------------------------------
988 # FETCH
989 #
990 9651 ▶sub FETCH {
Subroutine FETCH: called CC=52 SCAR=40.8
991 9651 my ($self, $key, %opts) = @_;
992 9651 my $orgkey = $key;
993
994 # get key from aliases if necessary
995 9651 $key = $self->get_alias($key);
996
997
998 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
999 # nested properties
1000 #
1001 9651 ▶ if ( (! ref $key) && ($key =~ m|[\{\%]|) && ($key !~ m|$pcode|o) )
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $key and $key =~ /[\{\%]/ and not $key =~ /$Date::EzDate::Tie::pcode/o) 100 9551 9651
Condition83%5/6
not ref $key and $key =~ /[\{\%]/ and not $key =~ /$Date::EzDate::Tie::pcode/o
!ll&&!rl&&r
95510100
not ref $key and $key =~ /[\{\%]/
!ll&&!rl&&r
159536100
MC/DC66%2/3
not ref $key and $key =~ /[\{\%]/ and not $key =~ /$Date::EzDate::Tie::pcode/o
not ref $key $key =~ /[\{\%]/ not $key =~ /$Date::EzDate::Tie::pcode/o
Truth table75%3/4
not(A) and B and not(C)
A= not ref $keyB= $key =~ /[\{\%]/C= not $key =~ /$Date::EzDate::Tie::pcode/o
ABCresult
0XX0
10X0
1100
1111
1002 100 {$key = format_split($key)}
1003
1004 9651 ▶ if (ref $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key) 115 9536 9651
1005 115 my @rv = @$key;
1006
1007 115 foreach my $el (@rv) {
1008 # if this is one of the format elements
1009 # then fetch the value of the given key
1010 869 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($el =~ s/\{([^\}]+)\}/$1/ or $el =~ /$Date::EzDate::Tie::pcode/o) 434 435 869
Condition100%3/3
$el =~ s/\{([^\}]+)\}/$1/ or $el =~ /$Date::EzDate::Tie::pcode/o
l!l&&r!l&&!r
114320435
MC/DC100%2/2
$el =~ s/\{([^\}]+)\}/$1/ or $el =~ /$Date::EzDate::Tie::pcode/o
$el =~ s/\{([^\}]+)\}/$1/ $el =~ /$Date::EzDate::Tie::pcode/o
Truth table100%3/3
A or B
A= $el =~ s/\{([^\}]+)\}/$1/B= $el =~ /$Date::EzDate::Tie::pcode/o
ABresult
1X1
011
000
1011 ($el =~ s|\{([^\}]+)\}|$1|) || # if it is enclosed in {}
1012 ($el =~ m|$pcode|o) # if it is a %x code
1013 ) {
1014 434 $el =~ s|['"\s]||g;
1015 434 $el = $self->FETCH($el, normalized=>1);
1016 }
1017 }
1018
1019 115 return join('', @rv);
1020 }
1021 #
1022 # nested properties
1023 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1024
1025
1026 # clean up key
1027 9536 ▶ $opts{'normalized'} or normalize_key($key);
Branch: 1 branch
BranchTrueFalseTotal
$opts{"normalized"} or normalize_key($key) 427 9109 9536
1028
1029
1030 # already or mostly calculated
1031 9536 ▶ if (exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$key}) 5333 4203 9536
1032 5333 ▶ if ($key =~ m/^(dayofmonth|monthnum|hour|min|sec)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(dayofmonth|monthnum|hour|min|sec)$/) 3650 1683 5333
1033 3650 {return zeropad_2($self->{$key})}
1034
1035 1683 return $self->{$key};
1036 }
1037
1038 # nozero's
1039 4203 ▶ if ($key =~ s/no(zero|0)//)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ s/no(zero|0)//) 190 4013 4203
1040 190 {return $self->FETCH($key) + 0}
1041
1042 # day of month ord
1043 4013 ▶ if ($key =~ m|^dayofmonthord(word)?$|)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^dayofmonthord(word)?$/) 0 4013 4013
1044 0 {return $OrdWords[$self->{'dayofmonth'}]}
1045 4013 ▶ if ($key eq 'dayofmonthordnum')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "dayofmonthordnum") 0 4013 4013
1046 0 {return $OrdNums[$self->{'dayofmonth'}]}
1047
1048 # weekday
1049 4013 ▶ if ($key =~ m/^(weekdayshort|dayofweekshort|dayofweek)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(weekdayshort|dayofweekshort|dayofweek)$/) 60 3953 4013
1050 60 {return $WeekDayShort[$self->{'weekdaynum'}]}
1051 3953 ▶ if ($key =~ m/^(weekdaylong|dayofweeklong)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(weekdaylong|dayofweeklong)$/) 41 3912 3953
1052 41 {return $WeekDayLong[$self->{'weekdaynum'}]}
1053 3912 ▶ if ($key eq 'dayofweeknum')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "dayofweeknum") 0 3912 3912
1054 0 {return $self->{'weekdaynum'}}
1055
1056 # month
1057 3912 ▶ if ($key eq 'monthshort') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "monthshort") 82 3830 3912
1058 82 ▶ defined($self->{'monthnum'}) or die 'undefined monthnum';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"monthnum"} or die "undefined monthnum" 82 0 82
1059 82 return $MonthShort[$self->{'monthnum'}];
1060 }
1061
1062 3830 ▶ if ($key eq 'monthlong')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "monthlong") 21 3809 3830
1063 21 { return $MonthLong[$self->{'monthnum'}] }
1064 3809 ▶ if ($key =~ m/^monthnumbase(one|1)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^monthnumbase(one|1)/) 80 3729 3809
1065 80 { return zeropad_2($self->{'monthnum'} + 1) }
1066
1067 3729 ▶ if ($key =~ m/^yeardaybase(one|1)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^yeardaybase(one|1)/) 61 3668 3729
1068 61 { return zeropad_open($self->{'yearday'} + 1, 3) }
1069
1070 # year
1071 3668 ▶ if ($key =~ m/^yeartwodigit/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^yeartwodigit/) 60 3608 3668
1072 60 { return substr($self->{'year'}, 2) }
1073
1074 # epochs
1075 3608 ▶ if ($key eq 'epochmin')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "epochmin") 0 3608 3608
1076 0 { return $self->getepochmin }
1077 3608 ▶ if ($key eq 'epochhour')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "epochhour") 0 3608 3608
1078 0 { return $self->getepochhour }
1079 3608 ▶ if ($key eq 'epochday')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "epochday") 1589 2019 3608
1080 1589 { return $self->getepochday }
1081
1082 # leapyear
1083 2019 ▶ if ($key =~ m/^(is)?leapyear/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(is)?leapyear/) 40 1979 2019
1084 40 {return isleapyear($self->{'year'})}
1085
1086 # days in month
1087 1979 ▶ if ($key eq 'daysinmonth')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "daysinmonth") 40 1939 1979
1088 40 {return daysinmonth($self->{'monthnum'}, $self->{'year'}) }
1089
1090 # DMY: eg 15JAN2001
1091 1939 ▶ if ($key eq 'dmy')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "dmy") 1 1938 1939
1092 1 {return zeropad_2($self->{'dayofmonth'}) . uc($MonthShort[$self->{'monthnum'}]) . $self->{'year'} }
1093
1094 # full
1095 1938 ▶ if ($key eq 'full') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "full") 1524 414 1938
1096 return
1097 $WeekDayShort[$self->{'weekdaynum'}] . ' ' .
1098 $MonthShort[$self->{'monthnum'}] . ' ' .
1099 $self->{'dayofmonth'} . ', ' .
1100 $self->{'year'} . ' ' .
1101 zeropad_2($self->{'hour'}) . ':' .
1102 zeropad_2($self->{'min'}) . ':' .
1103 1524 zeropad_2($self->{'sec'});
1104 }
1105
1106 # dayparam: A convenient string for passing as a parameter. Consists
1107 # of just alphanumerics, is easily human readable, and is completely unambiguous.
1108 414 ▶ if ($key eq 'dayparam') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "dayparam") 0 414 414
1109 return
1110 zeropad_2($self->{'dayofmonth'}) .
1111 lc($MonthShort[$self->{'monthnum'}]) .
1112 0 $self->{'year'};
1113 }
1114
1115 # military time, aka "miltime"
1116 414 ▶ if ($key =~ m|^mil(itary)?time$|)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^mil(itary)?time$/) 43 371 414
1117 43 {return zeropad_2($self->{'hour'}) . zeropad_2($self->{'min'}) }
1118
1119 # iso8601
1120 371 ▶ if ($key eq 'iso8601') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "iso8601") 0 371 371
1121 return
1122 $self->{'year'} . '-' .
1123 zeropad_2($self->{'monthnum'}+1) . '-' .
1124 zeropad_2($self->{'dayofmonth'}) . ' ' .
1125
1126 zeropad_2($self->{'hour'}) . ':' .
1127 zeropad_2($self->{'min'}) . ':' .
1128 0 zeropad_2($self->{'sec'});
1129 }
1130
1131 # minuteofday, aka minofday
1132 371 ▶ if ($key eq 'minofday')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "minofday") 42 329 371
1133 42 {return $self->{'min'} + ($self->{'hour'} * 60) }
1134
1135 # variable
1136 329 my ($ampm);
1137
1138 # calculate ampm, which is needed in most results from here down
1139 329 ▶ $ampm = ($self->{'hour'} >= 12) ? 'pm' : 'am';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'hour'} >= 12 ? : 7 322 329
1140
1141 # am/pm
1142 329 ▶ if ( ($key eq 'ampm') || ($key eq 'ampmlc') )
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "ampm" or $key eq "ampmlc") 74 255 329
Condition100%3/3
$key eq "ampm" or $key eq "ampmlc"
l!l&&r!l&&!r
3440255
MC/DC100%2/2
$key eq "ampm" or $key eq "ampmlc"
$key eq "ampm" $key eq "ampmlc"
Truth table100%3/3
A or B
A= $key eq "ampm"B= $key eq "ampmlc"
ABresult
1X1
011
000
1143 74 {return $ampm}
1144
1145 # AM/PM uppercase
1146 255 ▶ if ($key eq 'ampmuc')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "ampmuc") 60 195 255
1147 60 {return uc($ampm)}
1148
1149 # variable
1150 195 my ($ampmhour);
1151
1152 # calculate ampmhour, which is needed from here down
1153 195 ▶ if ( ($self->{'hour'} == 0) || ($self->{'hour'} == 12) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'hour'} == 0 or $self->{'hour'} == 12) { } 4 191 195
elsif ($self->{'hour'} > 12) { } 1 190 191
Condition66%2/3
$self->{"hour"} == 0 or $self->{"hour"} == 12
l!l&&r!l&&!r
04191
MC/DC50%1/2
$self->{"hour"} == 0 or $self->{"hour"} == 12
$self->{"hour"} == 0 $self->{"hour"} == 12
Truth table66%2/3
A or B
A= $self->{"hour"} == 0B= $self->{"hour"} == 12
ABresult
1X1
011
000
1154 4 {$ampmhour = 12}
1155 elsif ($self->{'hour'} > 12)
1156 1 {$ampmhour = $self->{'hour'} - 12}
1157 else
1158 190 {$ampmhour = $self->{'hour'}}
1159
1160 # am/pm hour
1161 195 ▶ if ($key eq 'ampmhour')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "ampmhour") 113 82 195
1162 113 {return zeropad_2($ampmhour)}
1163
1164 # hour and minute with ampm
1165 82 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "clocktime" or $key eq "clocktimestrict") 22 60 82
Condition66%2/3
$key eq "clocktime" or $key eq "clocktimestrict"
l!l&&r!l&&!r
22060
MC/DC50%1/2
$key eq "clocktime" or $key eq "clocktimestrict"
$key eq "clocktime" $key eq "clocktimestrict"
Truth table66%2/3
A or B
A= $key eq "clocktime"B= $key eq "clocktimestrict"
ABresult
1X1
011
000
1166 ($key eq 'clocktime') ||
1167 ($key eq 'clocktimestrict')
1168 ) {
1169 22 my $minofday = $self->FETCH('minofday');
1170
1171 22 ▶ if ($key eq 'clocktime') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "clocktime") 22 0 22
1172 22 ▶ if ($minofday == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($minofday == 0) 0 22 22
1173 0 {return 'midnight'}
1174 22 ▶ if ($minofday == 12 * t_60)
Branch: 1 branch
BranchTrueFalseTotal
if ($minofday == 720) 0 22 22
1175 0 {return 'noon'}
1176 }
1177
1178 return
1179 $self->FETCH('ampmhournozero') .
1180 22 ':' . zeropad_2($self->{'min'}) . ' ' .
1181 $ampm;
1182 }
1183
1184 # character codes
1185 60 ▶ $key eq 'newline' && return "\n";
Branch: 1 branch
BranchTrueFalseTotal
$key eq "newline" and return "\n" 20 40 60
1186 40 ▶ $key eq 'tab' && return "\t";
Branch: 1 branch
BranchTrueFalseTotal
$key eq "tab" and return "\t" 20 20 40
1187 20 ▶ $key eq 'leftbrace' && return '{';
Branch: 1 branch
BranchTrueFalseTotal
$key eq "leftbrace" and return "{" 0 20 20
1188 20 ▶ $key eq 'rightbrace' && return '}';
Branch: 1 branch
BranchTrueFalseTotal
$key eq "rightbrace" and return "}" 0 20 20
1189 20 ▶ $key eq 'percent' && return '%';
Branch: 1 branch
BranchTrueFalseTotal
$key eq "percent" and return "%" 20 0 20
1190
1191 # else we don't know what property is needed
1192 0 return $self->warn("do not know this format: $orgkey");
1193 }
1194 #
1195 # FETCH
1196 #------------------------------------------------------------------------------
1197  
1198  
1199 #------------------------------------------------------------------------------
1200 # DELETE
1201 #
1202 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
1203 0 my ($self, $key) = @_;
1204
1205 0 $key =~ s|\s||gs;
1206 0 $key = lc($key);
1207
1208 0 return delete $self->{'formats'}->{$key};
1209 }
1210 #
1211 # DELETE
1212 #------------------------------------------------------------------------------
1213  
1214  
1215 0 ▶sub del_format {return delete $_[0]->tie_ob->{'formats'}->{$_[1]}}
Subroutine del_format: not called CC=1 SCAR=6.9
1216  
1217  
1218 #------------------------------------------------------------------------------
1219 # isleapyear
1220 #
1221 82 ▶sub isleapyear {
Subroutine isleapyear: called CC=4 SCAR=14.6
1222 82 my ($year) = @_;
1223
1224 82 ▶ return 1 if ( ($year % 4 == 0) && ( ($year % 100) || ($year % 400 == 0) ) );
Branch: 1 branch
BranchTrueFalseTotal
if $year % 4 == 0 and $year % 100 || $year % 400 == 0 61 21 82
Condition50%3/6
$year % 4 == 0 and $year % 100 || $year % 400 == 0
!ll&&!rl&&r
21061
$year % 100 or $year % 400 == 0
l!l&&r!l&&!r
0610
MC/DC33%1/3
$year % 4 == 0 and $year % 100 || $year % 400 == 0
$year % 4 == 0 $year % 100 $year % 400 == 0
Truth table50%2/4
A and B or C
A= $year % 4 == 0B= $year % 100C= $year % 400 == 0
ABCresult
0XX0
1000
11X1
1011
1225 21 return 0;
1226 }
1227 #
1228 # isleapyear
1229 #------------------------------------------------------------------------------
1230  
1231  
1232 #------------------------------------------------------------------------------
1233 # get_alias
1234 #
1235 12309 ▶sub get_alias {
Subroutine get_alias: called CC=5 SCAR=16.1
1236 12309 my ($self, $key, %opts) = @_;
1237
1238 # normalize
1239 12309 ▶ unless ($key =~ m|[\{\%]|) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /[\{\%]/) 11349 960 12309
1240 11349 $key =~ s|\s||g;
1241 11349 $key = lc($key);
1242
1243 # strip "nozero" if that option was sent
1244 11349 ▶ $opts{'strip_no_zero'} and $key =~ s|nozero||g;
Branch: 1 branch
BranchTrueFalseTotal
$opts{"strip_no_zero"} and $key =~ s/nozero//g 1737 9612 11349
1245 }
1246
1247 # if this has an alias
1248 12309 ▶ if (exists $PCodes{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Date::EzDate::Tie::PCodes{$key}) 943 11366 12309
1249 943 {return $self->get_alias($PCodes{$key}, %opts)}
1250
1251 # if this is a named format
1252 11366 ▶ if (exists $self->{'formats'}->{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"formats"}{$key}) 15 11351 11366
1253 15 {return $self->{'formats'}->{$key}}
1254
1255 11351 return $key;
1256 }
1257 #
1258 # get_alias
1259 #------------------------------------------------------------------------------
1260  
1261  
1262  
1263 #------------------------------------------------------------------------------
1264 # getepochday
1265 #
1266 3132 ▶sub getepochday {
Subroutine getepochday: called CC=3 SCAR=11.0
1267 3132 my ($self, $epochsec) = @_;
1268
1269 # TESTING
1270 # println '$date->getepochday';
1271 # my $indent = indent();
1272
1273 # get epochsecond of date object
1274 3132 ▶ defined($epochsec) or $epochsec = $self->{'epochsec'};
Branch: 1 branch
BranchTrueFalseTotal
defined $epochsec or $epochsec = $self->{"epochsec"} 0 3132 3132
1275
1276 # calculate $epoch_offset
1277 # $epoch_offset represents the number of seconds
1278 # into the epoch day that the actual epoch moment occurs
1279 3132 ▶ unless (defined $epoch_offset) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Date::EzDate::Tie::epoch_offset) 1 3131 3132
1280 1 my %date;
1281 1 @date{@Date::EzDate::ltimefields} = localtime(0);
1282 $epoch_offset =
1283 1 ( ($date{'hour'} * t_60_60) + ($date{'min'} * 60) + $date{'sec'});
1284 }
1285
1286 # TESTING
1287 # println '$epochsec: ', $epochsec;
1288 # println '$epoch_offset: ', $epoch_offset;
1289
1290 # return
1291 3132 return floor( ($epochsec + $epoch_offset) / t_60_60_24);
1292 }
1293 #
1294 # getepochday
1295 #------------------------------------------------------------------------------
1296  
1297  
1298 #------------------------------------------------------------------------------
1299 # getepochhour, getepochmin
1300 #
1301 0 ▶sub getepochhour {
Subroutine getepochhour: not called CC=1 SCAR=6.9
1302 0 my ($self) = @_;
1303 0 return int($self->{'epochsec'} / t_60_60);
1304 }
1305  
1306 0 ▶sub getepochmin {
Subroutine getepochmin: not called CC=1 SCAR=6.9
1307 0 my ($self) = @_;
1308 0 return int($self->{'epochsec'} / 60);
1309 }
1310 #
1311 # getepochhour, getepochmin
1312 #------------------------------------------------------------------------------
1313  
1314  
1315  
1316 #------------------------------------------------------------------------------
1317 # daysinmonth
1318 #
1319 101 ▶sub daysinmonth {
Subroutine daysinmonth: called CC=3 SCAR=11.0
1320 101 my ($monthnum, $year) = @_;
1321
1322 101 ▶ if ($monthnum != 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($monthnum != 1) 59 42 101
1323 59 {return $MonthDays[$monthnum]}
1324 42 ▶ if (isleapyear($year))
Branch: 1 branch
BranchTrueFalseTotal
if (isleapyear($year)) 41 1 42
1325 41 {return 29}
1326 1 return 28;
1327 }
1328 #
1329 # daysinmonth
1330 #------------------------------------------------------------------------------
1331  
1332  
1333 #------------------------------------------------------------------------------
1334 # timefromfull
1335 #
1336 60 ▶sub timefromfull {
Subroutine timefromfull: called CC=28 SCAR=36.7
1337 60 my ($self, $val, %opts) = @_;
1338 60 my ($hour, $min, $sec, $day, $month, $year, $rv);
1339 60 my $orgval = $val;
1340
1341 # error checking
1342 60 ▶ if (! defined $val)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 60 60
1343 0 {return $self->warn("did not get a time string")}
1344
1345 # quick return: if they just put in an integer
1346 60 ▶ if ($val =~ m/^\d+$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^\d+$/) 0 60 60
1347 0 {return $val}
1348
1349
1350 # alias hour am/pm to hour:00 am/pm
1351 # $self->{'zero_hour_ampm'} and $val =~ s/(^|[^:\d])(\d+)\s*([ap]m)/$1$2:00:00 $3/gis;
1352 60 ▶ $self->{'zero_hour_ampm'} and $val =~ s/(^|[^:\d])(\d+)\s*([ap]m?\b)/$1$2:00:00 $3/gis;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"zero_hour_ampm"} and $val =~ s/(^|[^:\d])(\d+)\s*([ap]m?\b)/$1$2:00:00 $3/sig 60 0 60
1353
1354 # special case: ##:##.#####
1355 # In some time formats, the hour, min, second is
1356 # followed by fractional seconds. We don't handle those
1357 # fractions, so we'll just remove them.
1358 60 $val =~ s/(\d+\:\d+)\.[\d\-]+/$1/g;
1359
1360
1361 # Another special case: A.M. to AM and P.M. to PM
1362 60 $val =~ s/a\.m\b/am/gis;
1363 60 $val =~ s/p\.m\b/pm/gis;
1364
1365 # normalize
1366 60 $val =~ tr/A-Z/a-z/;
1367 60 $val =~ s/[^\w:]/ /g;
1368 60 $val =~ s/\s*:\s*/:/g;
1369
1370 # change ordinals to numbers
1371 60 $val =~ s|$OrdWordsRx|$OrdWordsNums{$1}|gis;
1372 60 $val =~ s/(\d)(th|rd|st|nd)\b/$1/gis;
1373
1374
1375 # noon to 12:00:00
1376 # midnight to 00:00:00
1377 60 $val =~ s/\bnoon\b/ 12:00:00 /gis;
1378 60 $val =~ s/\bmidnight\b/ 00:00:00 /gis;
1379
1380 # normalize some more
1381 60 $val =~ s/(\d)([a-z])/$1 $2/g;
1382 60 $val =~ s/([a-z])(\d)/$1 $2/g;
1383 60 $val =~ s/\s+/ /g;
1384 60 $val =~ s/^\s*//;
1385 60 $val =~ s/\s*$//;
1386
1387
1388 # today, tomorrow, and yesterday
1389 60 ▶ if ( ($val eq 'today') || ($val eq 'now') )
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "today" or $val eq "now") 0 60 60
Condition33%1/3
$val eq "today" or $val eq "now"
l!l&&r!l&&!r
0060
MC/DC0%0/2
$val eq "today" or $val eq "now"
$val eq "today" $val eq "now"
Truth table33%1/3
A or B
A= $val eq "today"B= $val eq "now"
ABresult
1X1
011
000
1390 0 {return time()}
1391 60 ▶ if ($val eq 'tomorrow')
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "tomorrow") 0 60 60
1392 0 {return time() + t_60_60_24}
1393 60 ▶ if ($val eq 'yesterday')
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "yesterday") 0 60 60
1394 0 {return time() - t_60_60_24}
1395
1396 # normalize further
1397 60 $val =~ s/([a-z]{3})[a-z]+/$1/gs;
1398
1399 # remove weekday
1400 60 $val =~ s/((sun)|(mon)|(tue)|(wed)|(thu)|(fri)|(sat))\s*//;
1401 60 $val =~ s/\s*$//;
1402
1403
1404 # attempt to get time
1405 60 ▶ unless ($opts{'dateonly'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"dateonly"}) 59 1 60
1406 59 ($val, $hour, $min, $sec) = $self->gettime($val, 'skipjustdigits'=>1);
1407 }
1408
1409 # attempt to get date
1410 60 ▶ unless ($opts{'timeonly'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"timeonly"}) 60 0 60
1411 60 ▶ if (length $val)
Branch: 1 branch
BranchTrueFalseTotal
if (length $val) 56 4 60
1412 56 {($val, $day, $month, $year) = getdate($val)}
1413 }
1414
1415 # trim
1416 60 $val =~ s/^\s*//;
1417
1418 # attempt to get time again
1419 60 ▶ unless ($opts{'dateonly'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"dateonly"}) 59 1 60
1420 59 ▶ if (length($val) && (! defined($hour)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $val and not defined $hour) 34 25 59
Condition66%2/3
length $val and not defined $hour
!ll&&!rl&&r
25034
MC/DC50%1/2
length $val and not defined $hour
length $val not defined $hour
Truth table66%2/3
A and not(B)
A= length $valB= not defined $hour
ABresult
0X0
100
111
1421 34 ($val, $hour, $min, $sec) = $self->gettime($val, 'skipjustdigits'=>1, 'croakonfail'=>1);
1422 }
1423 }
1424
1425
1426 60 ▶ if (defined($val) && length($val))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and length $val) 0 60 60
Condition33%1/3
defined $val and length $val
!ll&&!rl&&r
0600
MC/DC0%0/2
defined $val and length $val
defined $val length $val
Truth table33%1/3
A and B
A= defined $valB= length $val
ABresult
0X0
100
111
1427 0 {return $self->warn("Did not recognize date/time pattern ($val): $orgval")}
1428
1429 # if we didn't get a day, hour, year, or month we didn't recognize the pattern
1430 60 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hour or defined $day or defined $month or defined $year) 0 60 60
Condition44%4/9
defined $hour or defined $day or defined $month or defined $year
l!l&&r!l&&!r
6000
defined $hour or defined $day or defined $month
l!l&&r!l&&!r
6000
defined $hour or defined $day
l!l&&r!l&&!r
38220
MC/DC0%0/4
defined $hour or defined $day or defined $month or defined $year
defined $hour defined $day defined $month defined $year
Truth table40%2/5
A or B or C or D
A= defined $hourB= defined $dayC= defined $monthD= defined $year
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1431 defined($hour) ||
1432 defined($day) ||
1433 defined($month) ||
1434 defined($year)
1435 )
1436 0 {return undef}
1437
1438
1439 # default everything that isn't defined
1440 60 ▶ unless (defined $hour)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hour) 22 38 60
1441 22 {$hour = $self->{'hour'}}
1442 60 ▶ unless (defined $min)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $min) 22 38 60
1443 22 {$min = $self->{'min'}}
1444 60 ▶ unless (defined $sec)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sec) 27 33 60
1445 27 {$sec = $self->{'sec'}}
1446
1447 60 ▶ unless (defined $month)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $month) 4 56 60
1448 4 {$month = $self->{'monthnum'}}
1449 60 ▶ unless (defined $year)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $year) 4 56 60
1450 4 {$year = $self->{'year'}}
1451 60 ▶ unless (defined $day)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $day) 4 56 60
1452 4 {$day = maxday($self->{'dayofmonth'}, $month, $year)}
1453  
1454 # set year to four digits
1455 60 ▶ if (length($year) == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (length $year == 2) 0 60 60
1456 0 {$year = substr($self->{'year'}, 0, 2) . $year}
1457
1458
1459 # BUG: this is where ezdate crashes on out-of-bounds dates
1460 60 eval {
1461 60 $rv = timelocal($sec, $min, $hour, $day, $month, $year);
1462 };
1463
1464
1465 60 ▶ if (! defined $rv)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 60 60
1466 0 { $self->warn("Do not recognize date format: $orgval") }
1467
1468 60 return $rv;
1469
1470 # get date sub
1471 # attempt to get date
1472 # supported date formats
1473 # 14 Jan 2001
1474 # 14 JAN 01
1475 # 14JAN2001
1476 # Jan 14, 2001
1477 # Jan 14, 01
1478 # 01-14-01
1479 # 1-14-01
1480 # 1-7-01
1481 # 01-14-2001
1482 56 ▶ sub getdate {
Subroutine getdate: called CC=7 SCAR=28.5
1483 56 my ($val, $day, $month, $year) = @_;
1484
1485 # Tue Jun 12 13:03:28 2012
1486 56 ▶ if ($val =~ s/^([a-z]+) (\d+) (\S+) (\d+)$/$3/) {
Branch: 6 branches
BranchTrueFalseTotal
if ($val =~ s/^([a-z]+) (\d+) (\S+) (\d+)$/$3/) { } 0 56 56
elsif ($val =~ s/^(\d+) ([a-z]+) (\d+)//) { } 2 54 56
elsif ($val =~ s/^([a-z]+) (\d+) (\d+)//) { } 54 0 54
elsif ($val =~ s/^([a-z]+) (\d+)//) { } 0 0 0
elsif ($val =~ s/^(\d{4}) (\d+) (\d+)//) { } 0 0 0
elsif ($val =~ s/^(\d+) (\d+) (\d+)//) { } 0 0 0
1487 0 $year = $4;
1488 0 $month = $MonthNums{$1};
1489 0 $day = $2;
1490 }
1491
1492 # 14 Jan 2001
1493 # 14 JAN 01
1494 # 14JAN2001 # will be normalized to have spaces
1495 elsif ($val =~ s/^(\d+) ([a-z]+) (\d+)//) {
1496 2 $day = $1;
1497 2 $month = $MonthNums{$2};
1498 2 $year = $3;
1499 }
1500
1501 # Jan 14, 2001
1502 # Jan 14, 01
1503 elsif ($val =~ s/^([a-z]+) (\d+) (\d+)//) {
1504 54 $month = $MonthNums{$1};
1505 54 $day = $2;
1506 54 $year = $3;
1507 }
1508
1509 # Jan 2001
1510 # Jan 01
1511 elsif ($val =~ s/^([a-z]+) (\d+)//) {
1512 0 $month = $MonthNums{$1};
1513 0 $year = $2;
1514 }
1515
1516 # 2001-01-14
1517 elsif ($val =~ s/^(\d{4}) (\d+) (\d+)//) {
1518 0 $year = $1;
1519 0 $month = $2 - 1;
1520 0 $day = $3;
1521 }
1522
1523 # 01-14-01
1524 # 1-14-01
1525 # 1-7-01
1526 # 01-14-2001
1527 elsif ($val =~ s/^(\d+) (\d+) (\d+)//) {
1528 0 $month = $1 - 1;
1529 0 $day = $2;
1530 0 $year = $3;
1531 }
1532  
1533 56 return ($val, $day, $month, $year);
1534 }
1535  
1536 35 ▶ sub ampmhour {
Subroutine ampmhour: called CC=4 SCAR=14.0
1537 35 my ($hour, $ampm)=@_;
1538
1539 # if 12
1540 35 ▶ if ($hour == 12) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hour == 12) { } 3 32 35
elsif ($ampm =~ /^p/) { } 12 20 32
1541 # if am, set to 0
1542 3 ▶ if ($ampm =~ m/^a/)
Branch: 1 branch
BranchTrueFalseTotal
if ($ampm =~ /^a/) 0 3 3
1543 0 {$hour = 0}
1544 }
1545
1546 # else if pm, add 12
1547 elsif ($ampm =~ m/^p/)
1548 12 {$hour += 12}
1549 35 return $hour;
1550 }
1551 }
1552 #
1553 # timefromfull
1554 #------------------------------------------------------------------------------
1555  
1556  
1557 #------------------------------------------------------------------------------
1558 # gettime
1559 #
1560 # supported time formats:
1561 # 5pm
1562 # 5:34 pm
1563 # 17:34
1564 # 17:34:13
1565 # 5:34:13
1566 # 5:34:13 pm
1567 # 2330 (military time)
1568 #
1569 96 ▶sub gettime {
Subroutine gettime: called CC=9 SCAR=22.7
1570 96 my ($self, $str, %opts)= @_;
1571 96 my ($hour, $min, $sec);
1572
1573 # clean up a little
1574 96 $str =~ s/^://;
1575 96 $str =~ s/:$//;
1576 96 $str =~ s/(\d)(am|pm)/$1 $2/;
1577
1578
1579 # 5:34:13 pm
1580 # 5:34:13 p
1581 96 ▶ if ($str =~ s/^(\d+):(\d+):(\d+) (a|p)(m|\b)\s*//) {
Branch: 7 branches
BranchTrueFalseTotal
if ($str =~ s/^(\d+):(\d+):(\d+) (a|p)(m|\b)\s*//) { } 30 66 96
elsif ($str =~ s/^(\d+):(\d+):(\d+)\s*//) { } 4 62 66
elsif ($str =~ s/^(\d+):(\d+) (a|p)m?\s*//) { } 5 57 62
elsif ($str =~ s/^(\d+):(\d+)\s*//) { } 0 57 57
elsif ($str =~ s/^(\d+) (a|p)m?\b\s*//) { } 0 57 57
elsif (not $opts{'skipjustdigits'} and $str =~ /^\d+$/) { } 2 55 57
elsif ($opts{'croakonfail'}) { } 0 55 55
1582  
1583 30 $hour = ampmhour($1, $4);
1584 30 $min = $2;
1585 30 $sec = $3;
1586 }
1587
1588 # 17:34:13
1589 elsif ($str =~ s/^(\d+):(\d+):(\d+)\s*//) {
1590 4 $hour = $1;
1591 4 $min = $2;
1592 4 $sec = $3;
1593 }
1594
1595 # 5:34 pm
1596 elsif ($str =~ s/^(\d+):(\d+) (a|p)m?\s*//) {
1597 5 $hour = ampmhour($1, $3);
1598 5 $min = $2;
1599 }
1600
1601 # 17:34
1602 elsif ($str =~ s/^(\d+):(\d+)\s*//) {
1603 0 $hour = $1;
1604 0 $min = $2;
1605 }
1606
1607 # 5 pm
1608 elsif ($str =~ s/^(\d+) (a|p)m?\b\s*//)
1609 0 {$hour = ampmhour($1, $2)}
1610
1611 # elsif just digits
1612 ▶ elsif ( (! $opts{'skipjustdigits'}) && ($str =~ m/^\d+$/) ) {
Condition66%2/3
not $opts{"skipjustdigits"} and $str =~ /^\d+$/
!ll&&!rl&&r
5502
MC/DC50%1/2
not $opts{"skipjustdigits"} and $str =~ /^\d+$/
not $opts{"skipjustdigits"} $str =~ /^\d+$/
Truth table66%2/3
not(A) and B
A= not $opts{"skipjustdigits"}B= $str =~ /^\d+$/
ABresult
0X0
100
111
1613 2 $str = zeropad_open($str, 4);
1614 2 $hour = substr($str, 0, 2);
1615 2 $min = substr($str, 2, 2);
1616 }
1617
1618 # else don't recognize format
1619 elsif ($opts{'croakonfail'})
1620 0 {return $self->warn("don't recognize time format: $str")}
1621  
1622 96 return ($str, $hour, $min, $sec);
1623 }
1624 #
1625 # gettime
1626 #------------------------------------------------------------------------------
1627  
1628  
1629  
1630 #------------------------------------------------------------------------------
1631 # maxday
1632 #
1633 # if the input day is too high for given months,
1634 # returns the highest possible day for that month,
1635 # otherwise returns the input day
1636 #
1637 4 ▶sub maxday {
Subroutine maxday: called CC=2 SCAR=7.0
1638 4 my ($day, $month, $year) = @_;
1639 4 my $maxday = daysinmonth($month, $year);
1640
1641 4 ▶ $day > $maxday and return $maxday;
Branch: 1 branch
BranchTrueFalseTotal
$day > $maxday and return $maxday 0 4 4
1642 4 return $day;
1643 }
1644 #
1645 # maxday
1646 #------------------------------------------------------------------------------
1647  
1648  
1649 #------------------------------------------------------------------------------
1650 # zeropad_open, zeropad_2
1651 #
1652 63 ▶sub zeropad_open {
Subroutine zeropad_open: called CC=2 SCAR=7.1
1653 63 my ($rv, $length) = @_;
1654 63 ▶ $length ||= 2;
Condition50%1/2
$length ||= 2
l!l
630
MC/DC0%0/1
$length ||= 2
$length
Truth table50%1/2
A
A= $length
Aresult
11
00
1655 # return ('0' x ($length - length($rv))) . $rv;
1656 63 return sprintf "%0${length}d", $rv;
1657 }
1658  
1659 8526 ▶sub zeropad_2 {
Subroutine zeropad_2: called CC=1 SCAR=0.0
1660 8526 return sprintf "%02d", $_[0];
1661 }
1662 #
1663 # zeropad_open, zeropad_2
1664 #------------------------------------------------------------------------------
1665  
1666  
1667 #------------------------------------------------------------------------------
1668 # clone
1669 #
1670 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
1671 0 my ($ob) = @_;
1672
1673 return ref($ob)->TIEHASH([
1674 $ob->{'sec'},
1675 $ob->{'min'},
1676 $ob->{'hour'},
1677 $ob->{'dayofmonth'},
1678 $ob->{'monthnum'},
1679 $ob->{'year'},
1680 $ob->{'weekdaynum'},
1681 $ob->{'yearday'},
1682 $ob->{'dst'},
1683 0 $ob->{'epochsec'}
1684 ]);
1685 }
1686 #
1687 # clone
1688 #------------------------------------------------------------------------------
1689  
1690  
1691 #
1692 # Date::EzDate::Tie
1693 ###############################################################################
1694  
1695  
1696 # return true
1697 1 1;
1698 __END__
↑ Summary