blib/lib/Date/EzDate2.pm

filter: Statementstmt 80.9% 421 / 520 Branchbran 70.2% 205 / 292 Conditioncond 70.5% 67 / 95 MC/DCmcdc 50.9% 28 / 55 Subroutinesub 83.3% 40 / 48 Podpod 0.0% 0 / 9 totaltotal 74.6% 761 / 1019 CC 184 SCAR 37.4
CC
184
Cov
76%
FETCH
81.8
STORE
57.1
timefromstring
49.1
CRAP 42.1
↑ Summary
1 package Date::EzDate2;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # debugging tools
6 # use Debug::ShowStuff ':all';
7 # use Debug::ShowStuff::ShowVar;
8  
9 # version
10 1 our $VERSION = '0.03';
11  
12  
13 #------------------------------------------------------------------------------
14 # pod
15 #
16  
17 =head1 NAME
18  
19 Date::EzDate2 - Date and time manipulation made easy
20  
21 =head1 EzDate2 vs EzDate
22  
23 EzDate2 is the next generation of the module Date::EzDate. EzDate2 provides
24 the same easy interface for dealing with date and time, but uses DateTime
25 to provide a wider and more accurate set of services.
26  
27 EzDate2 is *NOT* a drop-in replacement for EzDate. That's why it's not just
28 an updated version of EzDate.
29  
30 This early release is just to test EzDate2's distribution on CPAN. It's not yet
31 ready for prime time.
32  
33 =head1 SYNOPSIS
34  
35 An EzDate2 object represents a single point in time and exposes all properties
36 of that point. It also makes it easy to change those properties to produce
37 a different point in time. EzDate2 has many features, here are a few:
38  
39 use Date::EzDate2;
40 my $mydate = Date::EzDate2->new();
41  
42 # output some date information
43 print $mydate, "\n"; # e.g. output: Fri Jul 22, 2016 16:53:33
44  
45 =cut
46  
47 #
48 # pod
49 #------------------------------------------------------------------------------
50  
51  
52 # warn levels
53 1 ▶use constant WARN_NONE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use constant WARN_STDERR => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use constant WARN_CROAK => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 our $default_warning = WARN_STDERR;
57  
58 # default time zone
59 1 our $default_time_zone = undef;
60  
61  
62 #------------------------------------------------------------------------------
63 # object overloading
64 #
65 use overload
66 6 ▶ '""' => sub{$_[0]->{'default'}}, # stringification
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
67 1 '<=>' => \&compare, # comparison
68 '+' => \&addition, # addition
69 '-' => \&subtraction, # subtraction
70 1 fallback => 1; # operations not defined here
71 #
72 # object overloading
73 #------------------------------------------------------------------------------
74  
75  
76 #------------------------------------------------------------------------------
77 # new
78 #
79 40 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
80 40 my ($class, $time, %opts) = @_;
81 40 my ($rv, %tiehash);
82
83 # TESTING
84 # println subname(); ##i
85
86 # create tied hash
87 40 ▶ tie(%tiehash, $class . '::Tie', $time, %opts) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
tie %tiehash, $class . '::Tie', $time, %opts or return undef 40 0 40
88
89 # create blessed reference to tied hash
90 40 $rv = bless(\%tiehash, $class);
91
92 # return
93 40 return $rv;
94 }
95 #
96 # new
97 #------------------------------------------------------------------------------
98  
99  
100 #------------------------------------------------------------------------------
101 # clone
102 #
103 16 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
104 16 my ($ezdate) = @_;
105
106 # TESTING
107 # println subname(), ' - ', __PACKAGE__; ##i
108
109 # call tied object's new()
110 16 return ref($ezdate)->new($ezdate);
111 }
112 #
113 # clone
114 #------------------------------------------------------------------------------
115  
116  
117 #------------------------------------------------------------------------------
118 # next_month
119 #
120 3 ▶sub next_month {
Subroutine next_month: called CC=1 SCAR=0.0
Pod undocumented
121 3 my ($ezdate, $count) = @_;
122
123 # TESTING
124 # println subname(); ##i
125
126 # call tied object's next_month()
127 3 return tied(%$ezdate)->next_month($count);
128 }
129 #
130 # next_month
131 #------------------------------------------------------------------------------
132  
133  
134  
135 #------------------------------------------------------------------------------
136 # settings and utility subs
137 #
138 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 undocumented
139  
140 1 ▶sub set_format {return $_[0]->tie_ob->set_format(@_[1..$#_])}
Subroutine set_format: called CC=1 SCAR=0.0
Pod undocumented
141  
142 0 ▶sub get_format {
Subroutine get_format: not called CC=1 SCAR=6.9
Pod undocumented
143 0 my ($self, $key) = @_;
144 0 my $ob = $self->tie_ob;
145 0 $key =~ s|\s||gs;
146 0 $key = lc($key);
147 0 return join('', @{$ob->{'formats'}->{$key}});
148 }
149  
150 0 ▶sub del_format {return delete $_[0]->{$_[1]}}
Subroutine del_format: not called CC=1 SCAR=6.9
Pod undocumented
151 18 ▶sub tie_ob{return tied(%{$_[0]})}
Subroutine tie_ob: called CC=1 SCAR=0.0
Pod undocumented
152  
153 # warnings level
154 1 ▶sub set_warnings {
Subroutine set_warnings: called CC=6 SCAR=22.9
Pod undocumented
155 1 my ($ezdate, $level) = @_;
156
157 # TESTING
158 # println subname(); ##i
159
160 # level must be defined
161 1 ▶ if (! defined $level)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $level) 0 1 1
162 0 { croak 'level-not-defined: warning level is not defined' }
163
164 # normalize
165 1 $level = lc($level);
166 1 $level =~ s|\s||gs;
167
168 # plain english
169 1 ▶ if ($level eq 'none')
Branch: 4 branches
BranchTrueFalseTotal
if ($level eq 'none') { } 0 1 1
elsif ($level eq 'stderr') { } 0 1 1
elsif ($level eq 'croak') { } 1 0 1
elsif ($level eq 'default') { } 0 0 0
170 0 { $level = WARN_NONE }
171 elsif ($level eq 'stderr')
172 0 { $level = WARN_STDERR }
173 elsif ($level eq 'croak')
174 1 { $level = WARN_CROAK }
175 elsif ($level eq 'default')
176 0 { $level = $default_warning }
177
178 # set tied object
179 1 return $ezdate->tie_ob->{'warnings'} = $level;
180 }
181  
182 #
183 # settings and utility subs
184 #------------------------------------------------------------------------------
185  
186  
187 ###############################################################################
188 # Date::EzDate2::Tie
189 #
190 package Date::EzDate2::Tie;
191 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
193 1 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
194 1 ▶use Carp qw{carp croak};
Subroutine BEGIN: called CC=1 SCAR=0.0
195 1 ▶use Clone;
Subroutine BEGIN: called CC=1 SCAR=0.0
196 1 ▶use base 'Tie::Hash';
Subroutine BEGIN: called CC=1 SCAR=0.0
197  
198 # debugging tools
199 # use Debug::ShowStuff ':all';
200  
201  
202 #------------------------------------------------------------------------------
203 # globals
204 #
205 our (
206 1 @ltimefields, @OrdWords, $OrdWordsRx, %PCodes, %WeekDayNums,
207 %OrdWordsNums, @OrdNums, %MonthNums, $pcode,
208 );
209  
210 # localtime() fields
211 # @ltimefields = qw[sec min hour dayofmonth monthnum year weekdaynum yearday dst];
212 1 @ltimefields = qw[sec min hour dayofmonth monthnum year];
213  
214 # words for the days of the month
215 1 @OrdWords = qw[
216 Zeroth First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth
217 Eleventh Twelfth Thirteenth Fourteenth Fifteenth Sixteenth Seventeenth
218 Eighteenth Ninteenth Twentieth Twentyfirst Twentysecond Twentythird
219 Twentyfourth Twentyfifth Twentysixth Twentyseventh Twentyeighth Twentyninth
220 Thirtieth Thirtyfirst
221 ];
222  
223 # regular expression versions of OrdWords
224 1 $OrdWordsRx = '\b(' . join('|', @OrdWords[1..$#OrdWords]) . ')\b';
225  
226 # build hash or ord words
227 1 foreach my $i (1..$#OrdWords)
228 31 { $OrdWordsNums{lc($OrdWords[$i])} = $i }
229  
230 # number ordinals
231 1 @OrdNums = qw[
232 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th
233 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th 31st
234 ];
235  
236 # month numbers
237 1 @MonthNums{qw[jan feb mar apr may jun jul aug sep oct nov dec]} = (1..12);
238  
239 # weekday numbers
240 1 $WeekDayNums{'mon'} = 1;
241 1 $WeekDayNums{'tue'} = 2;
242 1 $WeekDayNums{'wed'} = 3;
243 1 $WeekDayNums{'thu'} = 4;
244 1 $WeekDayNums{'fri'} = 5;
245 1 $WeekDayNums{'sat'} = 6;
246 1 $WeekDayNums{'sun'} = 7;
247  
248 # @WeekDayShort = qw[Sun Mon Tue Wed Thu Fri Sat];
249 # @WeekDayLong = qw[Sunday Monday Tuesday Wednesday Thursday Friday Saturday];
250 # @MonthShort = qw[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec];
251 # @MonthLong = qw[January February March April May June July August September October November December];
252 # @MonthDays = qw[31 x 31 30 31 30 31 31 30 31 30 31];
253  
254 # percent code regex
255 1 $pcode = '^\%[\w\%]$';
256  
257 # warn levels
258 1 ▶use constant WARN_NONE => Date::EzDate2::WARN_NONE;
Subroutine BEGIN: called CC=1 SCAR=0.0
259 1 ▶use constant WARN_STDERR => Date::EzDate2::WARN_STDERR;
Subroutine BEGIN: called CC=1 SCAR=0.0
260 1 ▶use constant WARN_CROAK => Date::EzDate2::WARN_CROAK;
Subroutine BEGIN: called CC=1 SCAR=0.0
261  
262 1 %PCodes = qw[
263 yearlong year
264 yearshort yeartwodigit
265 dayofyear yearday
266 dayofyear3d yearday3d
267 dayofweeknum weekdaynum
268 dayofweeknum2d weekdaynum2d
269 %Y year
270 %y yeartwodigit
271 %a weekdayshort
272 %A weekdaylong
273 %d dayofmonth
274 %D {monthnum2d}/{dayofmonth2d}/{year2d}
275 %H hour2d
276 %h monthshort
277 %b ampmhournozero
278 %B hournozero
279 %e monthnum
280 %m monthnum2d
281 %j dayofyear3d
282 %f dayofmonthnozero
283 %k ampmhour
284 %M min2d
285 %P ampmuc
286 %p ampmlc
287 %s epochsec
288 %S sec2d
289 %w weekdaynum
290 %y yeartwodigit
291 %T %H:%M:%S
292 %n newline
293 %t tab
294 %% percent
295 ];
296  
297 1 $PCodes{'%c'} = '{weekdayshort} %h %d {hour2d}:{min2d}:{sec2d} %Y';
298 1 $PCodes{'%r'} = '%k:%M:%S %P';
299  
300 #
301 # globals
302 #------------------------------------------------------------------------------
303  
304  
305 #------------------------------------------------------------------------------
306 # TIEHASH
307 #
308 40 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=6 SCAR=18.1
309 40 my ($class, $time, %opts) = @_;
310 40 my $eztied = bless ({}, $class);
311
312 # TESTING
313 # println subname(); ##i
314
315 # set some non-date properties
316 # $eztied->{'zero_hour_ampm'} = defined($opts{'zero_hour_ampm'}) ? $opts{'zero_hour_ampm'} : 1;
317 40 $eztied->{'formats'} = {};
318 40 $eztied->{'settings'} = {'dst_kludge' => 1};
319
320 # default builtin formats
321 40 $eztied->set_format('fullday', '{month short} {day of month no zero}, {year}');
322 40 $eztied->set_format('fulldate', '{fullday}');
323 40 $eztied->set_format('dayandtime', '{month short} {day of month}, {year} {ampmhour no zero}:{minute}{ampm}');
324 40 $eztied->set_format('default', '{full}');
325
326 # if clone
327 40 ▶ if (UNIVERSAL::isa $time, 'Date::EzDate2'){
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($time, 'Date::EzDate2')) { } 16 24 40
328 16 $eztied->clone($time);
329 }
330
331 # else
332 else {
333 # set DateTime object to current time
334 24 ▶ $eztied->{'dt'} = DateTime->now(
Condition33%1/3
$Date::EzDate::default_time_zone || 'DateTime::TimeZone'->new('name', 'local')->name
l!l&&r!l&&!r
2400
MC/DC0%0/2
$Date::EzDate::default_time_zone || 'DateTime::TimeZone'->new('name', 'local')->name
$Date::EzDate::default_time_zone 'DateTime::TimeZone'->new('name', 'local')->name
Truth table33%1/3
A || B
A= $Date::EzDate::default_time_zoneB= 'DateTime::TimeZone'->new('name', 'local')->name
ABresult
1X1
011
000
335 time_zone =>
336 $Date::EzDate::default_time_zone ||
337 DateTime::TimeZone->new( name => 'local' )->name(),
338 );
339
340 # if time is a scalar and has content, call timefromstring()
341 24 ▶ if ( defined($time) && (! ref $time) && ($time =~ m|\S|s) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time and not ref $time and $time =~ /\S/s) 23 1 24
Condition66%4/6
defined $time and not ref $time and $time =~ /\S/s
!ll&&!rl&&r
1023
defined $time and not ref $time
!ll&&!rl&&r
1023
MC/DC33%1/3
defined $time and not ref $time and $time =~ /\S/s
defined $time not ref $time $time =~ /\S/s
Truth table50%2/4
A and not(B) and C
A= defined $timeB= not ref $timeC= $time =~ /\S/s
ABCresult
0XX0
10X0
1100
1111
342 23 $eztied->timefromstring($time);
343 }
344 }
345
346 # return
347 40 return $eztied;
348 }
349 #
350 # TIEHASH
351 #------------------------------------------------------------------------------
352  
353  
354 #------------------------------------------------------------------------------
355 # clone
356 #
357 16 ▶sub clone {
Subroutine clone: called CC=2 SCAR=7.0
358 16 my ($new, $old) = @_;
359
360 # TESTING
361 # println subname(), ' - ', __PACKAGE__; ##i
362
363 # ensure we're using the tied hash
364 16 ▶ if ( UNIVERSAL::isa $old, 'Date::EzDate2' )
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($old, "Date::EzDate2")) 16 0 16
365 16 { $old = $old->tie_ob() }
366
367 # formats and settings
368 16 $new->{'formats'} = Clone::clone($old->{'formats'});
369 16 $new->{'settings'} = Clone::clone($old->{'settings'});
370
371 # datetime object
372 16 $new->{'dt'} = $old->{'dt'}->clone();
373
374 # return
375 16 return $new;
376 }
377 #
378 # clone
379 #------------------------------------------------------------------------------
380  
381  
382  
383 #------------------------------------------------------------------------------
384 # STORE
385 #
386 1 our $left_brace_rx = quotemeta('{');
387  
388 98 ▶sub STORE {
Subroutine STORE: called CC=50 SCAR=40.4
389 98 my ($eztied, $key, $val) = @_;
390 98 my (%set, $orgkey, $orgval, $dt);
391
392 # TESTING
393 # println subname(); ##i
394
395 # hold on to original values
396 98 $orgkey = $key;
397 98 $orgval = $val;
398
399 # error checking
400 98 ▶ if (! defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 98 98
401 0 return $eztied->warn(
402 'value-not-defined-in-store: ' .
403 'Must send a defined value when setting a ' .
404 'property of an EzDate object'
405 );
406 }
407
408 # if value contains {, assume they're assigning a format
409 98 ▶ $val =~ m|$left_brace_rx| and return $eztied->set_format($key, $val);
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /$Date::EzDate2::Tie::left_brace_rx/ and return $eztied->set_format($key, $val) 0 98 98
410
411 # normalize key
412 98 normalize_key($key);
413 98 $key = $eztied->get_alias($key, 'strip_no_zero'=>1);
414
415 # TESTING
416 # showvar $key;
417
418 # normalize value
419 98 $val = lc($val);
420 98 $val =~ s|^\s+||gs;
421 98 $val =~ s|\s+$||gs;
422 98 $val =~ s|\s+| |gs;
423
424 # TESTING
425 # println $key, ': ', $val;
426
427 # get DateTime object
428 98 $dt = $eztied->{'dt'};
429
430 # day of month
431 98 ▶ if ($key eq 'dayofmonth') {
Branch: 12 branches
BranchTrueFalseTotal
if ($key eq 'dayofmonth') { } 17 81 98
elsif ($key eq 'year') { } 17 64 81
elsif ($key eq 'month' or $key eq 'monthnum' or $key eq 'monthlong' or $key eq 'monthshort') { } 21 43 64
elsif ($key eq 'weekday' or $key eq 'weekdaynum' or $key eq 'weekdayshort' or $key eq 'weekdaylong') { } 6 37 43
elsif ($key eq 'yearday' or $key eq 'yearday3d') { } 3 34 37
elsif ($key eq 'hour' or $key eq 'hour2d') { } 3 31 34
elsif ($key eq 'min' or $key eq 'min2d') { } 3 28 31
elsif ($key eq 'sec' or $key eq 'sec2d') { } 3 25 28
elsif ($key eq 'ampm' or $key eq 'ampmlc' or $key eq 'ampmuc') { } 20 5 25
elsif ($key eq 'ampmhour') { } 2 3 5
elsif ($key eq 'miltime') { } 2 1 3
elsif ($key eq 'dmy') { } 1 0 1
432 17 $set{'day'} = $val;
433 }
434
435 # year
436 elsif ($key eq 'year') {
437 17 $set{'year'} = $val;
438 }
439
440 # month
441 ▶ elsif ( ($key eq 'month') || ($key eq 'monthnum') || ($key eq 'monthlong') || ($key eq 'monthshort') ) {
Condition100%9/9
$key eq "month" or $key eq "monthnum" or $key eq "monthlong" or $key eq "monthshort"
l!l&&r!l&&!r
19243
$key eq "month" or $key eq "monthnum" or $key eq "monthlong"
l!l&&r!l&&!r
18145
$key eq "month" or $key eq "monthnum"
l!l&&r!l&&!r
15346
MC/DC100%4/4
$key eq "month" or $key eq "monthnum" or $key eq "monthlong" or $key eq "monthshort"
$key eq "month" $key eq "monthnum" $key eq "monthlong" $key eq "monthshort"
Truth table100%5/5
A or B or C or D
A= $key eq "month"B= $key eq "monthnum"C= $key eq "monthlong"D= $key eq "monthshort"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
442 # letters
443 21 ▶ unless ( $val =~ m|^\d+$|si ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val =~ /^\d+$/is) 18 3 21
444 # get just first three letters
445 18 $val =~ s|\s||gs;
446 18 $val = substr($val, 0, 3);
447
448 # if a month number exists, use it, else throw error
449 # KLUDGE: assuming English names for now, will work with
450 # DateTime's locale functions later.
451 18 ▶ if (my $num = $MonthNums{$val}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num = $Date::EzDate2::Tie::MonthNums{$val}) { } 18 0 18
452 18 $val = $num;
453 }
454 else {
455 0 return $eztied->warn(
456 "invalid-month-name: do not know this month name: $orgval"
457 )
458 }
459 }
460
461 # set month
462 21 $set{'month'} = $val;
463 }
464
465 # weekday
466 ▶ elsif ( ($key eq 'weekday') || ($key eq 'weekdaynum') || ($key eq 'weekdayshort') || ($key eq 'weekdaylong') ) {
Condition88%8/9
$key eq "weekday" or $key eq "weekdaynum" or $key eq "weekdayshort" or $key eq "weekdaylong"
l!l&&r!l&&!r
4237
$key eq "weekday" or $key eq "weekdaynum" or $key eq "weekdayshort"
l!l&&r!l&&!r
1339
$key eq "weekday" or $key eq "weekdaynum"
l!l&&r!l&&!r
0142
MC/DC75%3/4
$key eq "weekday" or $key eq "weekdaynum" or $key eq "weekdayshort" or $key eq "weekdaylong"
$key eq "weekday" $key eq "weekdaynum" $key eq "weekdayshort" $key eq "weekdaylong"
Truth table80%4/5
A or B or C or D
A= $key eq "weekday"B= $key eq "weekdaynum"C= $key eq "weekdayshort"D= $key eq "weekdaylong"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
467 # if not integer, get integer value
468 6 ▶ unless ($val =~ m|^\d+$|s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val =~ /^\d+$/s) 5 1 6
469 # get just first three letters
470 5 $val = lc($val);
471 5 $val =~ s|\s||gs;
472 5 $val = substr($val, 0, 3);
473
474 # attempt to get value
475 5 ▶ unless ($val = $WeekDayNums{$val}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val = $Date::EzDate2::Tie::WeekDayNums{$val}) 0 5 5
476 0 return $eztied->warn(
477 "invalid-weekday-name: do not know this weekday name: $orgval"
478 )
479 }
480 }
481
482 # set day
483 6 ▶ unless ( $val == $dt->day_of_week ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val == $dt->day_of_week) 5 1 6
484 5 $set{'day'} = $dt->day + $val - $dt->day_of_week;
485 }
486 }
487
488 # yearday
489 ▶ elsif ( ($key eq 'yearday') || ($key eq 'yearday3d') ) {
Condition100%3/3
$key eq "yearday" or $key eq "yearday3d"
l!l&&r!l&&!r
2134
MC/DC100%2/2
$key eq "yearday" or $key eq "yearday3d"
$key eq "yearday" $key eq "yearday3d"
Truth table100%3/3
A or B
A= $key eq "yearday"B= $key eq "yearday3d"
ABresult
1X1
011
000
490 3 my ($current);
491
492 # check for integer and range
493 3 ▶ $eztied->integer_check($key, $val)
Branch: 1 branch
BranchTrueFalseTotal
$eztied->integer_check($key, $val) or return 0 3 0 3
494 or return 0;
495
496 # get current value
497 3 $current = $dt->day_of_year();
498
499 # if they're not equal, set to new day of year
500 3 ▶ unless ($current == $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($current == $val) 3 0 3
501 # add days
502 3 $dt->add(
503 days => $val - $current,
504 );
505
506 # return
507 3 return 1;
508 }
509 }
510
511 # hour
512 ▶ elsif ( ($key eq 'hour') || ($key eq 'hour2d') ) {
Condition100%3/3
$key eq "hour" or $key eq "hour2d"
l!l&&r!l&&!r
2131
MC/DC100%2/2
$key eq "hour" or $key eq "hour2d"
$key eq "hour" $key eq "hour2d"
Truth table100%3/3
A or B
A= $key eq "hour"B= $key eq "hour2d"
ABresult
1X1
011
000
513 # check for integer and range
514 3 ▶ $eztied->integer_check($key, $val)
Branch: 1 branch
BranchTrueFalseTotal
$eztied->integer_check($key, $val) or return 0 3 0 3
515 or return 0;
516
517 # assign value
518 3 $set{'hour'} = $val;
519 }
520
521 # minute
522 ▶ elsif ( ($key eq 'min') || ($key eq 'min2d') ) {
Condition100%3/3
$key eq "min" or $key eq "min2d"
l!l&&r!l&&!r
2128
MC/DC100%2/2
$key eq "min" or $key eq "min2d"
$key eq "min" $key eq "min2d"
Truth table100%3/3
A or B
A= $key eq "min"B= $key eq "min2d"
ABresult
1X1
011
000
523 # check for integer
524 3 ▶ $eztied->integer_check($key, $val)
Branch: 1 branch
BranchTrueFalseTotal
$eztied->integer_check($key, $val) or return 0 3 0 3
525 or return 0;
526
527 # assign value
528 3 $set{'minute'} = $val;
529 }
530
531 # second
532 ▶ elsif ( ($key eq 'sec') || ($key eq 'sec2d') ) {
Condition100%3/3
$key eq "sec" or $key eq "sec2d"
l!l&&r!l&&!r
2125
MC/DC100%2/2
$key eq "sec" or $key eq "sec2d"
$key eq "sec" $key eq "sec2d"
Truth table100%3/3
A or B
A= $key eq "sec"B= $key eq "sec2d"
ABresult
1X1
011
000
533 # check for integer and range
534 3 ▶ $eztied->integer_check($key, $val)
Branch: 1 branch
BranchTrueFalseTotal
$eztied->integer_check($key, $val) or return 0 3 0 3
535 or return 0;
536
537 # assign value
538 3 $set{'second'} = $val;
539 }
540
541 # ampm
542 ▶ elsif ( ($key eq 'ampm') || ($key eq 'ampmlc') || ($key eq 'ampmuc') ) {
Condition100%6/6
$key eq "ampm" or $key eq "ampmlc" or $key eq "ampmuc"
l!l&&r!l&&!r
1825
$key eq "ampm" or $key eq "ampmlc"
l!l&&r!l&&!r
1627
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
543 20 my ($current);
544
545 # add 'm' to just or or p
546 20 ▶ if (length($val) == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (length $val == 1) 1 19 20
547 1 {$val .= 'm'}
548
549 # normalize
550 20 $val =~ s|\s||gs;
551
552 # error checking
553 20 ▶ unless ( ($val eq 'am') || ($val eq 'pm') )
Branch: 1 branch
BranchTrueFalseTotal
unless ($val eq "am" or $val eq "pm") 0 20 20
Condition66%2/3
$val eq "am" or $val eq "pm"
l!l&&r!l&&!r
5150
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
554 0 { return $eztied->warn('invalid-ampm: ampm may only be set to am or pm') }
555
556 # get dt object
557 20 $current = $dt->hour;
558
559 # if no change, we're done
560 20 ▶ if ($current < 12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current < 12) { } 16 4 20
561 16 ▶ if ($val eq 'am')
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "am") 1 15 16
562 1 { return 1}
563 }
564 else {
565 4 ▶ if ($val eq 'pm')
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "pm") 0 4 4
566 0 { return 1}
567 }
568
569 # set change
570 19 ▶ if ($val eq 'am')
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq 'am') { } 4 15 19
571 4 { $set{'hour'} = $current - 12 }
572 else
573 15 { $set{'hour'} = $current + 12 }
574 }
575
576 # ampmhour
577 elsif ($key eq 'ampmhour') {
578 # check for integer
579 2 ▶ $eztied->integer_check($key, $val)
Branch: 1 branch
BranchTrueFalseTotal
$eztied->integer_check($key, $val) or return 0 2 0 2
580 or return 0;
581
582 # add twelve if necessary
583 2 ▶ if ($dt->hour >= 12)
Branch: 1 branch
BranchTrueFalseTotal
if ($dt->hour >= 12) 2 0 2
584 2 { $val += 12 }
585
586 # set
587 2 $set{'hour'} = $val;
588 }
589
590 # miltime
591 elsif ($key eq 'miltime') {
592 # must ve exactly four digits
593 2 ▶ unless ($val =~ m|^\d{4}$|s)
Branch: 1 branch
BranchTrueFalseTotal
unless ($val =~ /^\d{4}$/s) 0 2 2
594 0 { return $eztied->warn('invalid-miltime: miltime must consist of exactly four digits') }
595
596 # set
597 2 $set{'hour'} = substr($val, 0, 2);
598 2 $set{'minute'} = substr($val, 2, 2);
599 }
600
601 # dmy
602 elsif ($key eq 'dmy') {
603 1 my (@tokens, $day, $month, $year);
604
605 # split
606 1 $val = lc($val);
607 1 @tokens = split(m/(\d+)|([a-z]+)/si, $val);
608 1 @tokens = grep {defined $_} @tokens;
609 1 @tokens = grep {m|\S|s} @tokens;
610
611 # get values
612 1 ($day, $month, $year) = @tokens;
613
614 # check day
615 1 ▶ $eztied->integer_check("$key - day", $day)
Branch: 1 branch
BranchTrueFalseTotal
$eztied->integer_check("$key - day", $day) or return 0 1 0 1
616 or return 0;
617
618 # normalize month
619 1 $month = substr($month, 0, 3);
620
621 # if a month number exists, use it, else throw error
622 # KLUDGE: assuming English names for now, will work with
623 # DateTime's locale functions later.
624 1 ▶ if (my $num = $MonthNums{$month}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num = $Date::EzDate2::Tie::MonthNums{$month}) { } 1 0 1
625 1 $month = $num;
626 }
627 else {
628 0 return $eztied->warn(
629 "invalid-month-name: do not know this month name: $orgval"
630 )
631 }
632
633 # check year
634 1 ▶ $eztied->integer_check("$key - year", $year)
Branch: 1 branch
BranchTrueFalseTotal
$eztied->integer_check("$key - year", $year) or return 0 1 0 1
635 or return 0;
636
637 # set
638 1 $set{'day'} = $day;
639 1 $set{'month'} = $month;
640 1 $set{'year'} = $year;
641 }
642
643 # elsif ($key eq 'minofday') {
644 # $eztied->setfromtime (
645 # DST_ADJUST_NO,
646 # $eztied->{'epochsec'} - ($eztied->{'hour'} * t_60_60) - ($eztied->{'min'} * 60) + ($val * 60)
647 # )
648 # }
649 #
650 # elsif ($key eq 'hour') {
651 # $val = timelocal(
652 # $eztied->{'sec'},
653 # $eztied->{'min'},
654 # $val,
655 # $eztied->{'dayofmonth'},
656 # $eztied->{'monthnum'},
657 # $eztied->{'year'},
658 # );
659 #
660 # $eztied->setfromtime(DST_ADJUST_NO, $val);
661 # }
662 #
663 # # hour and minute
664 # elsif ( ($key eq 'clocktime') || ($key =~ m|^mil(itary)?time$|) ) {
665 # my ($changed, $hour, $min, $sec) = $eztied->gettime($val);
666 #
667 # unless (defined $hour)
668 # {$hour = $eztied->{'hour'}}
669 # unless (defined $min)
670 # {$min = $eztied->{'min'}}
671 # unless (defined $sec)
672 # {$sec = $eztied->{'sec'}}
673 #
674 # $eztied->setfromtime
675 # (
676 # 0,
677 # $eztied->{'epochsec'}
678 #
679 # - ($eztied->{'sec'})
680 # - ($eztied->{'min'} * 60)
681 # - ($eztied->{'hour'} * t_60_60)
682 #
683 # + ($sec)
684 # + ($min * 60)
685 # + ($hour * t_60_60)
686 # );
687 # }
688 #
689 # elsif ($key eq 'dst')
690 # {return $eztied->warn('dst property is read-only')}
691 #
692 # elsif ($key eq 'epochsec')
693 # {$eztied->setfromtime(DST_ADJUST_NO, $val)}
694 #
695 # elsif ($key eq 'epochmin')
696 # {$eztied->setfromtime(DST_ADJUST_NO, $eztied->{'epochsec'} - ($eztied->getepochmin * 60) + ($val * 60) )}
697 #
698 # elsif ($key eq 'epochhour')
699 # {$eztied->setfromtime(DST_ADJUST_NO, $eztied->{'epochsec'} - ($eztied->getepochhour * t_60_60) + ($val * t_60_60) )}
700 #
701 # elsif ($key eq 'epochday') {
702 # my ($oldhour, $oldepochsec, $oldmin);
703 #
704 # $eztied->setfromtime(
705 # DST_ADJUST_YES,
706 # $eztied->{'epochsec'} - ($eztied->getepochday * t_60_60_24) + (int($val) * t_60_60_24)
707 # );
708 # }
709 #
710 # # ordinals
711 # elsif ($key =~ m/dayofmonthord(word|num)?/) {
712 # # if numeric
713 # if ($val =~ s|^(\d+)\s*\w*$|$1|s)
714 # {$eztied->STORE('dayofmonth', $val)}
715 #
716 # # else word
717 # else {
718 # my $nval = $val;
719 # $nval =~ tr/A-Z/a-z/;
720 # $nval =~ s|\W||gs;
721 #
722 # # if no such ordinal exists
723 # unless ($nval = $OrdWordsNums{$nval})
724 # { return $eztied->warn("Invalid ordinal: $val") }
725 #
726 # $eztied->STORE('dayofmonth', $nval);
727 # }
728 # }
729 #
730 # elsif ($key eq 'year') {
731 # my ($maxday, $targetday);
732 #
733 # # if same year, nothing to do
734 # if ($eztied->{'year'} == $val)
735 # {return}
736 #
737 # # make sure day of month isn't greater than maximum day of target month
738 # $maxday = daysinmonth($eztied->{'monthnum'}, $val);
739 #
740 # if ($eztied->{'dayofmonth'} > $maxday) {
741 # $eztied->warn(
742 # "Changing the year sets day of month ($eztied->{'dayofmonth'}) to higher than days in month ($maxday); ",
743 # "setting the day down to $maxday"
744 # );
745 # $targetday = $maxday;
746 # }
747 # else
748 # {$targetday = $eztied->{'dayofmonth'}}
749 #
750 # $val = timelocal($eztied->{'sec'}, $eztied->{'min'}, $eztied->{'hour'}, $targetday, $eztied->{'monthnum'}, $val);
751 # $eztied->setfromtime(DST_ADJUST_YES, $val);
752 # }
753 #
754 # elsif ($key =~ m/^year(two|2)digit/) {
755 # $val =~ s|^.*(..)$|$1|;
756 # $eztied->STORE('year', substr($eztied->{'year'}, 0, 2) . zeropad_2($val));
757 # }
758 #
759 # elsif ($key =~ m/^monthnumbase(one|1)/)
760 # {$eztied->STORE('monthnum', $val - 1)}
761 #
762 #
763 # #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
764 # # monthnum
765 # #
766 # elsif ($key eq 'monthnum') {
767 # my ($target, $epoch_second);
768 # my $month = $eztied->{'monthnum'};
769 # my $year = $eztied->{'year'};
770 # my $orgday = $eztied->{'dayofmonth'};
771 # my $dayofmonth = $eztied->{'dayofmonth'};
772 # my $jumps = $val - $month;
773 #
774 # # if nothing to do
775 # $jumps or return;
776 #
777 # $target = $jumps;
778 # $target = abs($target);
779 #
780 # # jumping forward
781 # if ($jumps > 0) {
782 # foreach (1..$target) {
783 # # if end of year
784 # if ($month == 11) {
785 # $month = 0;
786 # $year++;
787 # }
788 # else
789 # {$month++}
790 # }
791 # }
792 #
793 # # jumping backward
794 # else {
795 # foreach (1..$target) {
796 # # if beginning of year
797 # if ($month == 0) {
798 # $month = 11;
799 # $year--;
800 # }
801 # else
802 # {$month--}
803 # }
804 # }
805 #
806 #
807 # # adjust day for shorter month (if necessary)
808 # if ($dayofmonth > 28) {
809 # my $dim = daysinmonth($month, $year);
810 #
811 # if ($dim < $dayofmonth)
812 # { $dayofmonth = $dim }
813 # }
814 #
815 # # get epoch second from timelocal
816 # $epoch_second = timelocal($eztied->{'sec'}, $eztied->{'min'}, $eztied->{'hour'}, $dayofmonth, $month, $year);
817 # $eztied->setfromtime(DST_ADJUST_NO, $epoch_second);
818 # }
819 # #
820 # # monthnum
821 # #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
822 #
823 #
824 # elsif ( ($key eq 'monthshort') || ($key eq 'monthlong') )
825 # {$eztied->STORE('monthnum', $MonthNums{lc(substr($val, 0, 3))})}
826 #
827 # elsif (
828 # ($key eq 'weekdayshort') ||
829 # ($key eq 'weekdaylong') ||
830 # ($key eq 'dayofweekshort') ||
831 # ($key eq 'dayofweeklong')
832 # ) {
833 # $eztied->STORE(
834 # 'weekdaynum',
835 # $WeekDayNums{lc(substr($val, 0, 3))})
836 # }
837 #
838 # else
839 # { return $eztied->warn("Do not understand key: $orgkey") }
840
841 # else don't know key
842 else {
843 # TESTING
844 # die "unknown-name-for-store: do not know this property name: $orgkey";
845
846 # return with warning
847 0 return $eztied->warn(
848 "unknown-name-for-store: do not know this property name: $orgkey"
849 )
850 }
851
852 # set new date and time
853 94 $eztied->new_date_time(\%set);
854
855 # return value
856 94 return $val;
857 }
858 #
859 # STORE
860 #------------------------------------------------------------------------------
861  
862  
863 #------------------------------------------------------------------------------
864 # integer_check
865 #
866 16 ▶sub integer_check {
Subroutine integer_check: called CC=5 SCAR=23.4
867 16 my ($eztied, $key, $val) = @_;
868
869 # TESTING
870 # println subname(); ##i
871
872 # must be all digit and greater than zero
873 16 ▶ unless ( defined($val) && ($val =~ m|^\d+$|s) && ($val > 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val and $val =~ /^\d+$/s and $val > 0) 0 16 16
Condition33%2/6
defined $val and $val =~ /^\d+$/s and $val > 0
!ll&&!rl&&r
0016
defined $val and $val =~ /^\d+$/s
!ll&&!rl&&r
0016
MC/DC0%0/3
defined $val and $val =~ /^\d+$/s and $val > 0
defined $val $val =~ /^\d+$/s $val > 0
Truth table25%1/4
A and B and C
A= defined $valB= $val =~ /^\d+$/sC= $val > 0
ABCresult
0XX0
10X0
1100
1111
874 # warn
875 0 ▶ $eztied->warn(
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 0 0 0
876 "not-integer-for-$key: " .
877 "the value for $key must be a positive integer but is " .
878 (defined($val) ? "\"$val\"" : 'undef') . ' ' .
879 "instead"
880 );
881
882 # return false
883 0 return 0;
884 }
885
886 # return true
887 16 return 1;
888 }
889 #
890 # integer_check
891 #------------------------------------------------------------------------------
892  
893  
894  
895 #------------------------------------------------------------------------------
896 # timefromstring
897 #
898  
899 # today, now, yesterday, tomorrow
900 1 our %relative = (
901 today => 1,
902 now => 1,
903 yesterday => 1,
904 tomorrow => 1,
905 );
906  
907 # map time units to duractions
908 #my %duration_map = (
909 # year => 'years',
910 # month => 'months',
911 # day => 'days',
912 # hour => 'hours',
913 # minute => 'minutes',
914 # second => 'seconds',
915 #);
916  
917 23 ▶sub timefromstring {
Subroutine timefromstring: called CC=20 SCAR=38.9
918 23 my ($eztied, $val, %opts) = @_;
919
920 # TESTING
921 # println subname(); ##i
922
923 # error checking
924 23 ▶ if (! defined $val)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 23 23
925 0 { return $eztied->warn('time-string-not-defined: did not get a defined time string') }
926
927 # alias hour am/pm to hour:00 am/pm
928 # $eztied->{'zero_hour_ampm'} and $val =~ s/(^|[^:\d])(\d+)\s*([ap]m)/$1$2:00:00 $3/gis;
929 # $eztied->{'zero_hour_ampm'} and $val =~ s/(^|[^:\d])(\d+)\s*([ap]m?\b)/$1$2:00:00 $3/gis;
930
931 # normalize
932 # $val = lc($val);
933 23 $val =~ s|^\s+||s;
934 23 $val =~ s|\s+$||s;
935 23 $val =~ s|\s+| |s;
936
937 # if just an integer
938 23 ▶ if ($val =~ m/^\d+$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val =~ /^\d+$/i) { } 0 23 23
elsif ($Date::EzDate2::Tie::relative{lc $val}) { } 0 23 23
939 0 my ($year, $month, $day, $hour, $min, $sec, $doy, $dow, $dst) = Gmtime($val);
940
941 # set from gmtime
942 0 $eztied->{'year'} = $year;
943 0 $eztied->{'monthnum'} = $month;
944 0 $eztied->{'dayofmonth'} = $day;
945 0 $eztied->{'hour'} = $hour;
946 0 $eztied->{'min'} = $min;
947 0 $eztied->{'sec'} = $sec;
948 }
949
950 # elsif today, now, yesterday, tomorrow
951 elsif ($relative{lc $val}) {
952 # lowercase
953 0 $val = lc($val);
954
955 # today
956 0 ▶ if ( ($val eq 'today') || ($val eq 'now') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "today" or $val eq "now") 0 0 0
Condition0%0/3
$val eq "today" or $val eq "now"
l!l&&r!l&&!r
000
MC/DC0%0/2
$val eq "today" or $val eq "now"
$val eq "today" $val eq "now"
Truth table0%0/3
A or B
A= $val eq "today"B= $val eq "now"
ABresult
1X1
011
000
957 0 $eztied->setfromtime();
958 }
959
960 # tomorrow
961 0 ▶ if ($val eq 'tomorrow') {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "tomorrow") 0 0 0
962 0 $eztied->setfromtime();
963
964 @$eztied{qw{year monthnum dayofmonth}} =
965 0 Add_Delta_Days(@$eztied{qw{year monthnum dayofmonth}}, 1);
966 }
967
968 # yesterday
969 0 ▶ if ($val eq 'yesterday') {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "yesterday") 0 0 0
970 0 $eztied->setfromtime();
971
972 @$eztied{qw{year monthnum dayofmonth}} =
973 0 Add_Delta_Days(@$eztied{qw{year monthnum dayofmonth}}, -1);
974 }
975 }
976
977 # else a date string
978 else {
979 23 my (%set);
980
981 # TESTING
982 # println 'date string'; ##i
983 # showvar $val;
984
985 # special case: ##:##.#####
986 # In some time formats, the hour, min, second is
987 # followed by fractional seconds. We don't handle those
988 # fractions, so we'll just remove them.
989 23 $val =~ s/(\d+\:\d+)\.[\d\-]+/$1/g;
990
991 # Another special case: A.M. to AM and P.M. to PM
992 23 $val =~ s/a\.m\b/am/gis;
993 23 $val =~ s/p\.m\b/pm/gis;
994
995 # remove time zone if it exists
996 23 ▶ if ( $val =~ s|[a-z_]+\s*/\s*[a-z_]+$||si ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ s[[a-z_]+\s*/\s*[a-z_]+$][]is) 0 23 23
997 0 $set{'time_zone'} = $&;
998 0 $val =~ s|\s+$||s;
999 }
1000
1001 # normalize
1002 23 $val = lc($val);
1003 23 $val =~ s/[^\w:]/ /g;
1004 23 $val =~ s/\s*:\s*/:/g;
1005
1006 # change ordinals to numbers
1007 23 $val =~ s|$OrdWordsRx|$OrdWordsNums{$1}|gis;
1008 23 $val =~ s/(\d)(th|rd|st|nd)\b/$1/gis;
1009
1010 # noon to 12:00:00
1011 # midnight to 00:00:00
1012 23 $val =~ s/\bnoon\b/ 12:00:00 /gis;
1013 23 $val =~ s/\bmidnight\b/ 00:00:00 /gis;
1014
1015 # normalize some more
1016 23 $val =~ s/(\d)([a-z])/$1 $2/g;
1017 23 $val =~ s/([a-z])(\d)/$1 $2/g;
1018 23 $val =~ s/\s+/ /g;
1019 23 $val =~ s/^\s*//;
1020 23 $val =~ s/\s*$//;
1021 23 $val =~ s/([a-z]{3})[a-z]+/$1/gs;
1022
1023 # remove weekday
1024 # TOD0: use localized names of weekdays
1025 23 $val =~ s/((sun)|(mon)|(tue)|(wed)|(thu)|(fri)|(sat))\s*//;
1026 23 $val =~ s/\s*$//;
1027
1028 # attempt to get time
1029 23 ▶ unless ($opts{'dateonly'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"dateonly"}) 23 0 23
1030 23 ($val, @set{qw{hour minute second}}) = $eztied->gettime($val, 'skipjustdigits'=>1);
1031 }
1032
1033 # attempt to get date
1034 23 ▶ unless ($opts{'timeonly'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"timeonly"}) 23 0 23
1035 23 ▶ if (length $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $val) 23 0 23
1036 23 ($val, @set{qw{day month year}}) = getdate($val);
1037 }
1038 }
1039
1040 # attempt to get time again
1041 23 ▶ unless ($opts{'dateonly'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"dateonly"}) 23 0 23
1042 23 ▶ if (length($val) && (! defined($set{'hour'})) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $val and not defined $set{"hour"}) 22 1 23
Condition66%2/3
length $val and not defined $set{"hour"}
!ll&&!rl&&r
1022
MC/DC50%1/2
length $val and not defined $set{"hour"}
length $val not defined $set{"hour"}
Truth table66%2/3
A and not(B)
A= length $valB= not defined $set{"hour"}
ABresult
0X0
100
111
1043 22 my ($hour_new, $min_new, $sec_new);
1044
1045 # parse again
1046 22 ($val, $hour_new, $min_new, $sec_new) = $eztied->gettime($val, 'skipjustdigits'=>1, 'croakonfail'=>1);
1047
1048 # set from new valued if they are defined
1049 22 ▶ defined($hour_new) and $set{'hour'} = $hour_new;
Branch: 1 branch
BranchTrueFalseTotal
defined $hour_new and $set{"hour"} = $hour_new 22 0 22
1050 22 ▶ defined($min_new) and $set{'minute'} = $min_new;
Branch: 1 branch
BranchTrueFalseTotal
defined $min_new and $set{"minute"} = $min_new 22 0 22
1051 22 ▶ defined($sec_new) and $set{'second'} = $sec_new;
Branch: 1 branch
BranchTrueFalseTotal
defined $sec_new and $set{"second"} = $sec_new 22 0 22
1052 }
1053 }
1054
1055 # is somehow we don't still have a defined string, return
1056 23 ▶ if (! defined $val)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 23 23
1057 0 { return 0 }
1058
1059 # trim
1060 23 $val =~ s/^\s*//;
1061
1062 # create new datetime object
1063 23 ▶ if (%set) {
Branch: 1 branch
BranchTrueFalseTotal
if (%set) 23 0 23
1064 23 $eztied->new_date_time(\%set);
1065 }
1066 }
1067
1068 # return
1069 23 return 1;
1070 }
1071 #
1072 # timefromstring
1073 #------------------------------------------------------------------------------
1074  
1075  
1076 #------------------------------------------------------------------------------
1077 # new_date_time
1078 #
1079 117 ▶sub new_date_time {
Subroutine new_date_time: called CC=6 SCAR=18.0
1080 117 my ($eztied, $set) = @_;
1081 117 my ($old, $new, %offset);
1082
1083 # TESTING
1084 # println subname(); ##i
1085
1086 # old datetime object
1087 117 $old = $eztied->{'dt'};
1088
1089 # create new datetime object with just year
1090 $new = DateTime->new(
1091 year => coal($set->{'year'}, $old->year),
1092 117 ▶ time_zone => $set->{'time_zone'} || $old->time_zone,
Condition33%1/3
$set->{'time_zone'} || $old->time_zone
l!l&&r!l&&!r
01170
MC/DC0%0/2
$set->{'time_zone'} || $old->time_zone
$set->{'time_zone'} $old->time_zone
Truth table33%1/3
A || B
A= $set->{'time_zone'}B= $old->time_zone
ABresult
1X1
011
000
1093 );
1094
1095 # delete year
1096 117 delete $set->{'year'};
1097
1098 # build duration
1099 117 foreach my $key (keys %$set) {
1100 194 ▶ if (defined $set->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $set->{$key}) 191 3 194
1101 191 $offset{$key . 's'} = $set->{$key};
1102 }
1103 }
1104
1105 # decrement months and days
1106 117 foreach my $key (qw{months days}) {
1107 234 ▶ if (defined $offset{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offset{$key}) 91 143 234
1108 91 { $offset{$key}-- }
1109 }
1110
1111 # add offset to new datetime object
1112 $new->add(
1113 months => coal($offset{'months'}, $old->month - 1 ),
1114 days => coal($offset{'days'}, $old->day - 1 ),
1115 hours => coal($offset{'hours'}, $old->hour ),
1116 minutes => coal($offset{'minutes'}, $old->minute ),
1117 117 seconds => coal($offset{'seconds'}, $old->second ),
1118 );
1119
1120 # set new dattime object
1121 117 $eztied->{'dt'} = $new;
1122 }
1123 #
1124 # new_date_time
1125 #------------------------------------------------------------------------------
1126  
1127  
1128 #------------------------------------------------------------------------------
1129 # coal
1130 # short for coalesce
1131 #
1132 702 ▶sub coal {
Subroutine coal: called CC=2 SCAR=6.9
1133 702 my ($new, $old) = @_;
1134 702 ▶ return defined($new) ? $new : $old;
Branch: 1 branch
BranchTrueFalseTotal
defined $new ? : 232 470 702
1135 }
1136 #
1137 # coal
1138 #------------------------------------------------------------------------------
1139  
1140  
1141 #------------------------------------------------------------------------------
1142 # getdate
1143 # attempt to get date
1144 # supported date formats
1145 # 14 Jan 2001
1146 # 14 JAN 01
1147 # 14JAN2001
1148 # Jan 14, 2001
1149 # Jan 14, 01
1150 # 01-14-01
1151 # 1-14-01
1152 # 1-7-01
1153 # 01-14-2001
1154 #
1155 23 ▶sub getdate {
Subroutine getdate: called CC=7 SCAR=27.0
1156 23 my ($val) = @_;
1157 23 my ($day, $month, $year);
1158
1159 # TESTING
1160 # println subname(); ##i
1161
1162 # Tue Jun 12 13:03:28 2012
1163 23 ▶ if ($val =~ s/^([a-z]+) (\d+) (\S+) (\d+)$/$3/) {
Branch: 6 branches
BranchTrueFalseTotal
if ($val =~ s/^([a-z]+) (\d+) (\S+) (\d+)$/$3/) { } 0 23 23
elsif ($val =~ /^\d+ [a-z]+ \d{4}$/) { } 1 22 23
elsif ($val =~ s/^([a-z]+) (\d+) (\d+)//) { } 22 0 22
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
1164 0 $year = $4;
1165 0 $month = $MonthNums{$1};
1166 0 $day = $2;
1167 }
1168
1169 # 14 Jan 2001
1170 # 14 JAN 01
1171 # 14JAN2001 # will be normalized to have spaces
1172 elsif ($val =~ m/^\d+ [a-z]+ \d{4}$/) {
1173 1 my @tokens = split(' ', $val);
1174 1 $day = $tokens[0];
1175 1 $month = $MonthNums{$tokens[1]};
1176 1 $year = $tokens[2];
1177 1 $val = '';
1178 }
1179
1180 # Jan 14, 2001
1181 # Jan 14, 01
1182 elsif ($val =~ s/^([a-z]+) (\d+) (\d+)//) {
1183 22 $month = $MonthNums{$1};
1184 22 $day = $2;
1185 22 $year = $3;
1186 }
1187
1188 # Jan 2001
1189 # Jan 01
1190 elsif ($val =~ s/^([a-z]+) (\d+)//) {
1191 0 $month = $MonthNums{$1};
1192 0 $year = $2;
1193 }
1194
1195 # 2001-01-14
1196 elsif ($val =~ s/^(\d{4}) (\d+) (\d+)//) {
1197 0 $year = $1;
1198 0 $month = $2 - 1;
1199 0 $day = $3;
1200 }
1201
1202 # 01-14-01
1203 # 1-14-01
1204 # 1-7-01
1205 # 01-14-2001
1206 elsif ($val =~ s/^(\d+) (\d+) (\d+)//) {
1207 0 $month = $1 - 1;
1208 0 $day = $2;
1209 0 $year = $3;
1210 }
1211
1212 # return
1213 23 return ($val, $day, $month, $year);
1214 }
1215 #
1216 # getdate
1217 #------------------------------------------------------------------------------
1218  
1219  
1220 #------------------------------------------------------------------------------
1221 # gettime
1222 # supported time formats:
1223 # 5pm
1224 # 5:34 pm
1225 # 17:34
1226 # 17:34:13
1227 # 5:34:13
1228 # 5:34:13 pm
1229 # 2330 (military time)
1230 #
1231 45 ▶sub gettime {
Subroutine gettime: called CC=10 SCAR=28.7
1232 45 my ($eztied, $str, %opts)= @_;
1233 45 my ($hour, $min, $sec);
1234
1235 # TESTING
1236 # println subname(); ##i
1237
1238 # string must be defined
1239 45 ▶ unless (defined $str) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $str) 0 45 45
1240 0 croak 'strin-not-defined: $str is not defined';
1241 }
1242
1243 # clean up a little
1244 45 $str =~ s/^\s+//;
1245 45 $str =~ s/\s+$//;
1246 45 $str =~ s/^://;
1247 45 $str =~ s/:$//;
1248 45 $str =~ s/(\d)(am|pm)/$1 $2/;
1249
1250 # 5:34:13 pm
1251 # 5:34:13 p
1252 45 ▶ 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*//) { } 19 26 45
elsif ($str =~ s/^(\d+):(\d+):(\d+)\s*//) { } 3 23 26
elsif ($str =~ s/^(\d+):(\d+) (a|p)m?\s*//) { } 0 23 23
elsif ($str =~ s/^(\d+):(\d+)\s*//) { } 0 23 23
elsif ($str =~ s/^(\d+) (a|p)m?\b\s*//) { } 0 23 23
elsif (not $opts{'skipjustdigits'} and $str =~ /^\d+$/) { } 0 23 23
elsif ($opts{'croakonfail'}) { } 0 23 23
1253 19 $hour = ampmhour($1, $4);
1254 19 $min = $2;
1255 19 $sec = $3;
1256 }
1257
1258 # 17:34:13
1259 elsif ($str =~ s/^(\d+):(\d+):(\d+)\s*//) {
1260 3 $hour = $1;
1261 3 $min = $2;
1262 3 $sec = $3;
1263 }
1264
1265 # 5:34 pm
1266 elsif ($str =~ s/^(\d+):(\d+) (a|p)m?\s*//) {
1267 0 $hour = ampmhour($1, $3);
1268 0 $min = $2;
1269 }
1270
1271 # 17:34
1272 elsif ($str =~ s/^(\d+):(\d+)\s*//) {
1273 0 $hour = $1;
1274 0 $min = $2;
1275 }
1276
1277 # 5 pm
1278 elsif ($str =~ s/^(\d+) (a|p)m?\b\s*//) {
1279 0 $hour = ampmhour($1, $2);
1280 0 $min = 0;
1281 0 $sec = 0;
1282 }
1283
1284 # elsif just digits
1285 ▶ elsif ( (! $opts{'skipjustdigits'}) && ($str =~ m/^\d+$/) ) {
Condition33%1/3
not $opts{"skipjustdigits"} and $str =~ /^\d+$/
!ll&&!rl&&r
2300
MC/DC0%0/2
not $opts{"skipjustdigits"} and $str =~ /^\d+$/
not $opts{"skipjustdigits"} $str =~ /^\d+$/
Truth table33%1/3
not(A) and B
A= not $opts{"skipjustdigits"}B= $str =~ /^\d+$/
ABresult
0X0
100
111
1286 0 $str = zeropad_open($str, 4);
1287 0 $hour = substr($str, 0, 2);
1288 0 $min = substr($str, 2, 2);
1289 }
1290
1291 # else don't recognize format
1292 elsif ($opts{'croakonfail'}) {
1293 0 return $eztied->warn("unrecognized-format: don't recognize time format: $str");
1294 }
1295
1296 # return
1297 45 return ($str, $hour, $min, $sec);
1298 }
1299 #
1300 # gettime
1301 #------------------------------------------------------------------------------
1302  
1303  
1304 #------------------------------------------------------------------------------
1305 # ampmhour
1306 #
1307 19 ▶sub ampmhour {
Subroutine ampmhour: called CC=4 SCAR=14.0
1308 19 my ($hour, $ampm) = @_;
1309
1310 # if 12
1311 19 ▶ if ($hour == 12) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hour == 12) { } 3 16 19
elsif ($ampm =~ /^p/) { } 5 11 16
1312 # if am, set to 0
1313 3 ▶ if ($ampm =~ m/^a/)
Branch: 1 branch
BranchTrueFalseTotal
if ($ampm =~ /^a/) 0 3 3
1314 0 {$hour = 0}
1315 }
1316
1317 # else if pm, add 12
1318 elsif ($ampm =~ m/^p/) {
1319 5 $hour += 12;
1320 }
1321
1322 # return
1323 19 return $hour;
1324 }
1325 #
1326 # ampmhour
1327 #------------------------------------------------------------------------------
1328  
1329  
1330 #------------------------------------------------------------------------------
1331 # set_format
1332 #
1333 161 ▶sub set_format {
Subroutine set_format: called CC=1 SCAR=0.0
1334 161 my ($eztied, $name, $format) = @_;
1335
1336 # normalize name
1337 161 $name =~ s|\s||g;
1338 161 $name =~ lc($name);
1339
1340 161 $eztied->{'formats'}->{$name} = format_split($format);
1341 }
1342  
1343 223 ▶sub format_split {
Subroutine format_split: called CC=3 SCAR=11.0
1344 # split
1345 223 my @rv = split(m/(\{[^\{\}]*\}|\%.)/, $_[0]);
1346
1347 # normalize
1348 223 foreach my $el (@rv) {
1349 1417 ▶ if ($el =~ m|^\{.*\}$|s)
Branch: 1 branch
BranchTrueFalseTotal
if ($el =~ /^\{.*\}$/s) 558 859 1417
1350 558 { normalize_key($el) }
1351 }
1352
1353 # remove empties
1354 223 @rv = grep {length $_} @rv;
1355
1356 # return
1357 223 return \@rv;
1358 }
1359 #
1360 # set_format
1361 #------------------------------------------------------------------------------
1362  
1363  
1364 #------------------------------------------------------------------------------
1365 # setfromtime
1366 #
1367 0 ▶sub setfromtime {
Subroutine setfromtime: not called CC=1 SCAR=6.9
1368 0 my ($eztied, $time) = @_;
1369
1370 # TESTING
1371 # println subname(); ##i
1372
1373 # set time fields
1374 0 @$eztied{@ltimefields} = localtime();
1375
1376 # add 1900 to year
1377 0 $eztied->{'year'} += 1900;
1378
1379 # increment monthnum, which was base zero, to base one
1380 0 $eztied->{'monthnum'}++;
1381 }
1382 #
1383 # setfromtime
1384 #------------------------------------------------------------------------------
1385  
1386  
1387 #------------------------------------------------------------------------------
1388 # normalize_key
1389 #
1390 1581 ▶sub normalize_key {
Subroutine normalize_key: called CC=2 SCAR=6.9
1391 1581 $_[0] =~ s|\s||gs;
1392 1581 ▶ $_[0] =~ tr/A-Z/a-z/ unless $_[0] =~ m|^\%\w$|;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^\%\w$/ 1565 16 1581
1393 1581 $_[0] =~ s|ordinal|ord|sg;
1394
1395 1581 $_[0] =~ s|hours|hour|sg;
1396
1397 1581 $_[0] =~ s|minute|min|sg;
1398 1581 $_[0] =~ s|mins|min|sg;
1399
1400 1581 $_[0] =~ s|second|sec|sg;
1401 1581 $_[0] =~ s|secs|sec|sg;
1402
1403 1581 $_[0] =~ s/two/2/gs;
1404 1581 $_[0] =~ s/three/3/gs;
1405 1581 $_[0] =~ s/digits?/d/gs;
1406
1407 1581 $_[0] =~ s/timezone/tz/gs;
1408
1409 1581 $_[0] =~ s|number|num|sg;
1410 }
1411 #
1412 # normalize_key
1413 #------------------------------------------------------------------------------
1414  
1415  
1416  
1417  
1418 #------------------------------------------------------------------------------
1419 # zeropad_open, zeropad_2
1420 #
1421 30 ▶sub zeropad_open {
Subroutine zeropad_open: called CC=2 SCAR=7.1
1422 30 my ($rv, $length) = @_;
1423 30 ▶ $length ||= 2;
Condition50%1/2
$length ||= 2
l!l
300
MC/DC0%0/1
$length ||= 2
$length
Truth table50%1/2
A
A= $length
Aresult
11
00
1424 # return ('0' x ($length - length($rv))) . $rv;
1425 30 return sprintf "%0${length}d", $rv;
1426 }
1427  
1428 456 ▶sub zeropad_2 {
Subroutine zeropad_2: called CC=2 SCAR=7.6
1429 456 my ($val) = @_;
1430
1431 # $val must be defined
1432 456 ▶ if (! defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 456 456
1433 0 croak 'zeropad_2~val-not-defined: the value sent to zeropad_2 is not defined';
1434 }
1435
1436 # return
1437 456 return sprintf "%02d", $_[0];
1438 }
1439 #
1440 # zeropad_open, zeropad_2
1441 #------------------------------------------------------------------------------
1442  
1443  
1444 #------------------------------------------------------------------------------
1445 # month_short
1446 #
1447 #sub month_short {
1448 # my ($eztied) = @_;
1449 # my ($rv);
1450 #
1451 # # get name of month, return just the first three letters
1452 # $rv = Month_to_Text($eztied->{'monthnum'});
1453 # $rv = substr($rv, 0, 3);
1454 #
1455 # # return
1456 # return $rv;
1457 #}
1458 #
1459 # month_short
1460 #------------------------------------------------------------------------------
1461  
1462  
1463 #------------------------------------------------------------------------------
1464 # FETCH
1465 #
1466 1268 ▶sub FETCH {
Subroutine FETCH: called CC=64 SCAR=44.0
1467 1268 my ($eztied, $key, %opts) = @_;
1468 1268 my ($orgkey, $dt);
1469
1470 # TESTING
1471 # println subname(); ##i
1472
1473 # hold on to original key
1474 1268 $orgkey = $key;
1475
1476 # get key from aliases if necessary
1477 1268 $key = $eztied->get_alias($key);
1478
1479
1480 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1481 # nested properties
1482 #
1483 1268 ▶ if ( (! ref $key) && ($key =~ m|[\{\%]|) && ($key !~ m|$pcode|o) )
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $key and $key =~ /[\{\%]/ and not $key =~ /$Date::EzDate2::Tie::pcode/o) 62 1206 1268
Condition83%5/6
not ref $key and $key =~ /[\{\%]/ and not $key =~ /$Date::EzDate2::Tie::pcode/o
!ll&&!rl&&r
1206062
not ref $key and $key =~ /[\{\%]/
!ll&&!rl&&r
7119962
MC/DC66%2/3
not ref $key and $key =~ /[\{\%]/ and not $key =~ /$Date::EzDate2::Tie::pcode/o
not ref $key $key =~ /[\{\%]/ not $key =~ /$Date::EzDate2::Tie::pcode/o
Truth table75%3/4
not(A) and B and not(C)
A= not ref $keyB= $key =~ /[\{\%]/C= not $key =~ /$Date::EzDate2::Tie::pcode/o
ABCresult
0XX0
10X0
1100
1111
1484 62 { $key = format_split($key) }
1485
1486 1268 ▶ if (ref $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key) 69 1199 1268
1487 69 my @rv = @$key;
1488
1489 69 foreach my $el (@rv) {
1490 # if this is one of the format elements
1491 # then fetch the value of the given key
1492 480 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($el =~ s/\{([^\}]+)\}/$1/ or $el =~ /$Date::EzDate2::Tie::pcode/o) 274 206 480
Condition100%3/3
$el =~ s/\{([^\}]+)\}/$1/ or $el =~ /$Date::EzDate2::Tie::pcode/o
l!l&&r!l&&!r
124150206
MC/DC100%2/2
$el =~ s/\{([^\}]+)\}/$1/ or $el =~ /$Date::EzDate2::Tie::pcode/o
$el =~ s/\{([^\}]+)\}/$1/ $el =~ /$Date::EzDate2::Tie::pcode/o
Truth table100%3/3
A or B
A= $el =~ s/\{([^\}]+)\}/$1/B= $el =~ /$Date::EzDate2::Tie::pcode/o
ABresult
1X1
011
000
1493 ($el =~ s|\{([^\}]+)\}|$1|) || # if it is enclosed in {}
1494 ($el =~ m|$pcode|o) # if it is a %x code
1495 ) {
1496 274 $el =~ s|['"\s]||g;
1497 274 $el = $eztied->FETCH($el, normalized=>1);
1498 }
1499 }
1500
1501 # ensure defined values in @rv
1502 69 foreach my $val (@rv) {
1503 480 ▶ if (! defined $val)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 480 480
1504 0 { $val = '' }
1505 }
1506
1507 # return
1508 69 return join('', @rv);
1509 }
1510 #
1511 # nested properties
1512 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1513
1514
1515 # clean up key
1516 1199 ▶ $opts{'normalized'} or normalize_key($key);
Branch: 1 branch
BranchTrueFalseTotal
$opts{"normalized"} or normalize_key($key) 274 925 1199
1517
1518 # datetime object
1519 1199 $dt = $eztied->{'dt'};
1520
1521 # datetime object
1522 1199 ▶ if ( ($key eq 'dt') || $key eq 'datetime' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "dt" or $key eq "datetime") 0 1199 1199
Condition33%1/3
$key eq "dt" or $key eq "datetime"
l!l&&r!l&&!r
001199
MC/DC0%0/2
$key eq "dt" or $key eq "datetime"
$key eq "dt" $key eq "datetime"
Truth table33%1/3
A or B
A= $key eq "dt"B= $key eq "datetime"
ABresult
1X1
011
000
1523 0 return $dt;
1524 }
1525
1526 # already or mostly calculated
1527 #if (exists $eztied->{$key}) {
1528 # # ensure two digits for several properties
1529 # if ( $key =~ m/^(dayofmonth|monthnum|hour|min|sec)$/ )
1530 # { return zeropad_2($eztied->{$key}) }
1531 #
1532 # # all other properties, return key as-is
1533 # return $eztied->{$key};
1534 #}
1535
1536 # character codes
1537 1199 ▶ $key eq 'newline' and return "\n";
Branch: 1 branch
BranchTrueFalseTotal
$key eq "newline" and return "\n" 15 1184 1199
1538 1184 ▶ $key eq 'tab' and return "\t";
Branch: 1 branch
BranchTrueFalseTotal
$key eq "tab" and return "\t" 15 1169 1184
1539 1169 ▶ $key eq 'leftbrace' and return '{';
Branch: 1 branch
BranchTrueFalseTotal
$key eq "leftbrace" and return "{" 0 1169 1169
1540 1169 ▶ $key eq 'lb' and return '{';
Branch: 1 branch
BranchTrueFalseTotal
$key eq "lb" and return "{" 0 1169 1169
1541 1169 ▶ $key eq 'rightbrace' and return '}';
Branch: 1 branch
BranchTrueFalseTotal
$key eq "rightbrace" and return "}" 0 1169 1169
1542 1169 ▶ $key eq 'rb' and return '}';
Branch: 1 branch
BranchTrueFalseTotal
$key eq "rb" and return "}" 0 1169 1169
1543 1169 ▶ $key eq 'percent' and return '%';
Branch: 1 branch
BranchTrueFalseTotal
$key eq "percent" and return "%" 15 1154 1169
1544
1545 # nozero's
1546 1154 ▶ if ($key =~ s/no(zero|0)//)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ s/no(zero|0)//) 75 1079 1154
1547 75 { return $eztied->FETCH($key) + 0 }
1548
1549 # day of month
1550 1079 ▶ if ($key eq 'dayofmonth')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "dayofmonth") 66 1013 1079
1551 66 { return $dt->day }
1552
1553 # day of month, two digits
1554 1013 ▶ if ($key eq 'dayofmonth2d')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "dayofmonth2d") 30 983 1013
1555 30 { return zeropad_2($dt->day) }
1556
1557 # day of month ord
1558 983 ▶ if ($key =~ m|^dayofmonthord(word)?$|)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^dayofmonthord(word)?$/) 0 983 983
1559 0 { return $OrdWords[$eztied->{'dayofmonth'}] }
1560 983 ▶ if ($key eq 'dayofmonthordnum')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "dayofmonthordnum") 0 983 983
1561 0 { return $OrdNums[$eztied->{'dayofmonth'}] }
1562
1563 # weekdaynum
1564 983 ▶ if ($key eq 'weekdaynum')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "weekdaynum") 30 953 983
1565 30 { return $dt->day_of_week() }
1566
1567 # weekdaynum2d
1568 953 ▶ if ($key eq 'weekdaynum2d')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "weekdaynum2d") 0 953 953
1569 0 { return zeropad_2($dt->day_of_week()) }
1570
1571 # weekday
1572 953 ▶ if ($key =~ m/^(weekdayshort|dayofweekshort|dayofweek)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(weekdayshort|dayofweekshort|dayofweek)$/) 45 908 953
1573 45 { return $dt->day_abbr() }
1574 908 ▶ if ($key =~ m/^(weekdaylong|dayofweeklong)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(weekdaylong|dayofweeklong)$/) 31 877 908
1575 31 { return $dt->day_name() }
1576
1577 # month
1578 877 ▶ if ($key eq 'monthshort')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "monthshort") 47 830 877
1579 47 { return $dt->month_abbr() }
1580 830 ▶ if ( ($key eq 'month') || ($key eq 'monthlong') )
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "month" or $key eq "monthlong") 16 814 830
Condition66%2/3
$key eq "month" or $key eq "monthlong"
l!l&&r!l&&!r
016814
MC/DC50%1/2
$key eq "month" or $key eq "monthlong"
$key eq "month" $key eq "monthlong"
Truth table66%2/3
A or B
A= $key eq "month"B= $key eq "monthlong"
ABresult
1X1
011
000
1581 16 { return $dt->month_name() }
1582
1583 # month number
1584 814 ▶ if ($key eq 'monthnum')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "monthnum") 31 783 814
1585 31 { return $dt->month }
1586 783 ▶ if ($key eq 'monthnum2d')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "monthnum2d") 45 738 783
1587 45 { return zeropad_2($dt->month) }
1588
1589 # day of year
1590 738 ▶ if ($key eq 'yearday')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "yearday") 16 722 738
1591 16 { return $dt->day_of_year() }
1592 722 ▶ if ($key eq 'yearday3d')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "yearday3d") 30 692 722
1593 30 { return zeropad_open($dt->day_of_year(), 3) }
1594
1595 # year
1596 692 ▶ if ($key eq 'year')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "year") 48 644 692
1597 48 { return $dt->year }
1598 644 ▶ if ($key eq 'year2d')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "year2d") 45 599 644
1599 45 { return substr($dt->year, -2) }
1600
1601 # epoch
1602 599 ▶ if ( ($key eq 'epoch') || ($key eq 'epochsec') )
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "epoch" or $key eq "epochsec") 0 599 599
Condition33%1/3
$key eq "epoch" or $key eq "epochsec"
l!l&&r!l&&!r
00599
MC/DC0%0/2
$key eq "epoch" or $key eq "epochsec"
$key eq "epoch" $key eq "epochsec"
Truth table33%1/3
A or B
A= $key eq "epoch"B= $key eq "epochsec"
ABresult
1X1
011
000
1603 0 { return Date_to_Time(@$eztied{qw{year monthnum dayofmonth hour min sec}}) }
1604
1605 # leapyear
1606 599 ▶ if ($key =~ m/^(is)?leapyear/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(is)?leapyear/) 30 569 599
1607 30 { return $dt->is_leap_year() }
1608
1609 # days in month
1610 569 ▶ if ($key eq 'daysinmonth') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "daysinmonth") 30 539 569
1611 30 return DateTime->last_day_of_month(
1612 year => $dt->year,
1613 month => $dt->month,
1614 time_zone => $dt->time_zone_long_name()
1615 )->day;
1616 }
1617
1618 # DMY: eg 15JAN2001
1619 539 ▶ if ($key eq 'dmy') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "dmy") 1 538 539
1620 return
1621 1 zeropad_2($dt->day) .
1622 uc($dt->month_abbr()) .
1623 $dt->year;
1624 }
1625
1626 # full
1627 538 ▶ if ($key eq 'full') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "full") 11 527 538
1628 11 my ($weekday);
1629
1630 # return
1631 return
1632 11 $dt->day_abbr() . ' ' .
1633 $dt->month_abbr() . ' ' .
1634 $dt->day() . ', ' .
1635 $dt->year() . ' ' .
1636 zeropad_2($dt->hour) . ':' .
1637 zeropad_2($dt->minute) . ':' .
1638 zeropad_2($dt->second);
1639 }
1640
1641 # military time, aka "miltime"
1642 527 ▶ if ($key =~ m|^mil(itary)?time$|)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^mil(itary)?time$/) 30 497 527
1643 30 { return zeropad_2($dt->hour) . zeropad_2($dt->minute) }
1644
1645 # iso8601
1646 497 ▶ if ($key eq 'iso8601') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "iso8601") 0 497 497
1647 return
1648 $eztied->{'year'} . '-' .
1649 zeropad_2($eztied->{'monthnum'}) . '-' .
1650 zeropad_2($eztied->{'dayofmonth'}) . ' ' .
1651
1652 zeropad_2($eztied->{'hour'}) . ':' .
1653 zeropad_2($eztied->{'min'}) . ':' .
1654 0 zeropad_2($eztied->{'sec'});
1655 }
1656
1657 # hour
1658 497 ▶ if ($key eq 'hour')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "hour") 15 482 497
1659 15 { return $dt->hour }
1660
1661 # hour two digits
1662 482 ▶ if ($key eq 'hour2d')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "hour2d") 60 422 482
1663 60 { return zeropad_2($dt->hour) }
1664
1665 # minute
1666 422 ▶ if ($key eq 'min')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "min") 45 377 422
1667 45 { return $dt->minute }
1668
1669 # minute two digits
1670 377 ▶ if ($key eq 'min2d')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "min2d") 75 302 377
1671 75 { return zeropad_2($dt->minute) }
1672
1673 # second
1674 302 ▶ if ($key eq 'sec')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "sec") 60 242 302
1675 60 { return $dt->second }
1676
1677 # second two digits
1678 242 ▶ if ($key eq 'sec2d')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "sec2d") 75 167 242
1679 75 { return zeropad_2($dt->second) }
1680
1681 # variable
1682 167 my ($ampm);
1683
1684 # calculate ampm, which is needed in most results from here down
1685 167 ▶ $ampm = ($dt->hour >= 12) ? 'pm' : 'am';
Branch: 1 branch
BranchTrueFalseTotal
$dt->hour >= 12 ? : 2 165 167
1686
1687 # am/pm
1688 167 ▶ if ( ($key eq 'ampm') || ($key eq 'ampmlc') )
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "ampm" or $key eq "ampmlc") 45 122 167
Condition100%3/3
$key eq "ampm" or $key eq "ampmlc"
l!l&&r!l&&!r
1530122
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
1689 45 { return $ampm }
1690
1691 # AM/PM uppercase
1692 122 ▶ if ($key eq 'ampmuc')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "ampmuc") 45 77 122
1693 45 { return uc($ampm) }
1694
1695 # variable
1696 77 my ($ampmhour);
1697
1698 # calculate ampmhour, which is needed from here down
1699 77 ▶ if ( ($dt->hour == 0) || ($dt->hour == 12) )
Branch: 2 branches
BranchTrueFalseTotal
if ($dt->hour == 0 or $dt->hour == 12) { } 2 75 77
elsif ($dt->hour > 12) { } 0 75 75
Condition66%2/3
$dt->hour == 0 or $dt->hour == 12
l!l&&r!l&&!r
0275
MC/DC50%1/2
$dt->hour == 0 or $dt->hour == 12
$dt->hour == 0 $dt->hour == 12
Truth table66%2/3
A or B
A= $dt->hour == 0B= $dt->hour == 12
ABresult
1X1
011
000
1700 2 { $ampmhour = 12 }
1701 elsif ($dt->hour > 12)
1702 0 { $ampmhour = $dt->hour - 12 }
1703 else
1704 75 { $ampmhour = $dt->hour }
1705
1706 # am/pm hour
1707 77 ▶ if ($key eq 'ampmhour')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "ampmhour") 60 17 77
1708 60 { return zeropad_2($ampmhour) }
1709
1710 # hour and minute with ampm
1711 17 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "clocktime" or $key eq "clocktimestrict") 17 0 17
Condition33%1/3
$key eq "clocktime" or $key eq "clocktimestrict"
l!l&&r!l&&!r
1700
MC/DC0%0/2
$key eq "clocktime" or $key eq "clocktimestrict"
$key eq "clocktime" $key eq "clocktimestrict"
Truth table33%1/3
A or B
A= $key eq "clocktime"B= $key eq "clocktimestrict"
ABresult
1X1
011
000
1712 ($key eq 'clocktime') ||
1713 ($key eq 'clocktimestrict')
1714 ) {
1715
1716 return
1717 17 $ampmhour . ':' .
1718 zeropad_2($dt->minute) . ' ' .
1719 $ampm;
1720 }
1721
1722 # tz
1723 0 ▶ if ( $key eq 'tz' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "tz") 0 0 0
1724 0 return $dt->time_zone_short_name;
1725 }
1726
1727 # olson
1728 0 ▶ if ( $key eq 'olson' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "olson") 0 0 0
1729 0 return $dt->time_zone_long_name;
1730 }
1731
1732 # else we don't know what property is needed
1733 0 return $eztied->warn("unknown-format: do not know this format: $orgkey");
1734 }
1735 #
1736 # FETCH
1737 #------------------------------------------------------------------------------
1738  
1739  
1740 #------------------------------------------------------------------------------
1741 # warn
1742 #
1743 0 ▶sub warn {
Subroutine warn: not called CC=4 SCAR=30.0
1744 0 my $eztied = shift;
1745 0 ▶ my $level = defined($eztied->{'warnings'}) ? $eztied->{'warnings'} : $Date::EzDate2::default_warning;
Branch: 1 branch
BranchTrueFalseTotal
defined $eztied->{'warnings'} ? : 0 0 0
1746
1747 # TESTING
1748 # println subname(); ##i
1749
1750 # if no level, return undef
1751 0 ▶ $level or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$level or return undef 0 0 0
1752
1753 0 ▶ if ($level == WARN_STDERR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 1) 0 0 0
1754 # showstack();
1755 0 carp 'WARNING: ', @_;
1756 0 return undef;
1757 }
1758
1759 # croak
1760 0 croak @_;
1761 }
1762 #
1763 # warn
1764 #------------------------------------------------------------------------------
1765  
1766  
1767 #------------------------------------------------------------------------------
1768 # get_alias
1769 #
1770 1970 ▶sub get_alias {
Subroutine get_alias: called CC=5 SCAR=16.1
1771 1970 my ($eztied, $key, %opts) = @_;
1772
1773 # normalize
1774 1970 ▶ unless ($key =~ m|[\{\%]|) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /[\{\%]/) 1352 618 1970
1775 1352 $key =~ s|\s||g;
1776 1352 $key = lc($key);
1777
1778 # strip "nozero" if that option was sent
1779 1352 ▶ $opts{'strip_no_zero'} and $key =~ s|nozero||g;
Branch: 1 branch
BranchTrueFalseTotal
$opts{"strip_no_zero"} and $key =~ s/nozero//g 100 1252 1352
1780 }
1781
1782 # if this key has an alias
1783 1970 ▶ if (exists $PCodes{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Date::EzDate2::Tie::PCodes{$key}) 604 1366 1970
1784 604 {return $eztied->get_alias($PCodes{$key}, %opts)}
1785
1786 # if this is a named format
1787 1366 ▶ if (exists $eztied->{'formats'}->{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $eztied->{"formats"}{$key}) 7 1359 1366
1788 7 {return $eztied->{'formats'}->{$key}}
1789
1790 1359 return $key;
1791 }
1792 #
1793 # get_alias
1794 #------------------------------------------------------------------------------
1795  
1796  
1797 #------------------------------------------------------------------------------
1798 # ym, ymd
1799 #
1800 0 ▶sub ym {
Subroutine ym: not called CC=1 SCAR=6.9
1801 0 my ($eztied) = @_;
1802 0 return @$eztied{qw{year monthnum}};
1803 }
1804  
1805 0 ▶sub ymd {
Subroutine ymd: not called CC=1 SCAR=6.9
1806 0 my ($eztied) = @_;
1807 0 return @$eztied{qw{year monthnum dayofmonth}};
1808 }
1809 #
1810 # ym, ymd
1811 #------------------------------------------------------------------------------
1812  
1813  
1814 #------------------------------------------------------------------------------
1815 # next_month
1816 #
1817 3 ▶sub next_month {
Subroutine next_month: called CC=1 SCAR=0.0
1818 3 my ($eztied, $count) = @_;
1819
1820 # TESTING
1821 # println subname(); ##i
1822
1823 # add months and return
1824 3 return $eztied->{'dt'}->add(months => $count, end_of_month=>'limit');
1825 }
1826 #
1827 # next_month
1828 #------------------------------------------------------------------------------
1829  
1830  
1831 #------------------------------------------------------------------------------
1832 # DELETE
1833 #
1834 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
1835 0 my ($eztied, $key) = @_;
1836
1837 # normalize key
1838 0 $key =~ s|\s||gs;
1839 0 $key = lc($key);
1840
1841 # delete from formats, but not properties
1842 0 return delete $eztied->{'formats'}->{$key};
1843 }
1844 #
1845 # DELETE
1846 #------------------------------------------------------------------------------
1847  
1848 #
1849 # Date::EzDate2::Tie
1850 ###############################################################################
1851  
1852  
1853 # return true
1854 1 1;
1855  
1856 __END__
↑ Summary