lib/Date/Manip/Base.pm

filter: Statementstmt 91.4% 1095 / 1198 Branchbran 81.6% 498 / 610 Conditioncond 69.9% 233 / 333 MC/DCmcdc 40.9% 66 / 161 Subroutinesub 99.1% 111 / 112 Podpod 100% 21 / 21 totaltotal 83.1% 2024 / 2435 CC 433 SCAR 33.6
CC
433
Cov
85%
_offset_fields
104.3
_config_var_encoding
101.6
_config_var_base
98.5
CRAP 28.7
« lib/Date/Manip/Delta.pm ↑ Summary lib/Date/Manip/TZ_Base.pm »
1 package Date::Manip::Base;
2 # Copyright (c) 1995-2026 Sullivan Beck. All rights reserved.
3 # This program is free software; you can redistribute it and/or modify it
4 # under the same terms as Perl itself.
5  
6 ###############################################################################
7 # Any routine that starts with an underscore (_) is NOT intended for
8 # public use. They are for internal use in the the Date::Manip
9 # modules and are subject to change without warning or notice.
10 #
11 # ABSOLUTELY NO USER SUPPORT IS OFFERED FOR THESE ROUTINES!
12 ###############################################################################
13  
14 171 require 5.010000;
15 171 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 171 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 171 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 171 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 171 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 #use re 'debug';
21  
22 171 ▶use Date::Manip::Obj;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 171 ▶use Date::Manip::TZ_Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 171 our @ISA = qw(Date::Manip::Obj Date::Manip::TZ_Base);
25  
26 171 ▶use Encode qw(encode_utf8 from_to find_encoding decode _utf8_off _utf8_on is_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 171 require Date::Manip::Lang::index;
28  
29 171 our $VERSION;
30 171 $VERSION='7.01';
31 171 ▶END { undef $VERSION; }
Subroutine END: called CC=1 SCAR=0.0
32  
33 ###############################################################################
34 # BASE METHODS
35 ###############################################################################
36  
37 508 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
38 508 my($self) = @_;
39  
40 508 $self->_init_cache();
41 508 $self->_init_language();
42 508 $self->_init_config();
43 508 $self->_init_events();
44 508 $self->_init_holidays();
45 508 $self->_init_now();
46  
47 508 return;
48 }
49  
50 # The base object has some config-independant information which is
51 # always reused, and only needs to be initialized once.
52 508 ▶sub _init_cache {
Subroutine _init_cache: called CC=2 SCAR=7.0
53 508 my($self) = @_;
54 508 ▶ return if (exists $$self{'cache'}{'init'});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cache"}{"init"} 0 508 508
55 508 $$self{'cache'}{'init'} = 1;
56  
57 # ly => {Y} = 0/1 1 if it is a leap year
58 # ds1_mon => {Y}{M} = N days since 1BC for Y/M/1
59 # dow_mon => {Y}{M} = DOW day of week of Y/M/1
60  
61 508 $$self{'cache'}{'ly'} = {};
62 508 $$self{'cache'}{'ds1_mon'} = {};
63 508 $$self{'cache'}{'dow_mon'} = {};
64  
65 508 return;
66 }
67  
68 # Config dependent data. Needs to be reset every time the config is reset.
69 509 ▶sub _init_data {
Subroutine _init_data: called CC=3 SCAR=11.0
70 509 my($self,$force) = @_;
71 509 ▶ return if (exists $$self{'data'}{'calc'} && ! $force);
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"data"}{"calc"} and not $force 1 508 509
Condition66%2/3
exists $self->{"data"}{"calc"} and not $force
!ll&&!rl&&r
50801
MC/DC50%1/2
exists $self->{"data"}{"calc"} and not $force
exists $self->{"data"}{"calc"} not $force
Truth table66%2/3
A and not(B)
A= exists $self->{"data"}{"calc"}B= not $force
ABresult
0X0
100
111
72  
73 508 $$self{'data'}{'calc'} = {}; # Calculated values
74  
75 508 return;
76 }
77  
78 # Initializes config dependent data
79 509 ▶sub _init_config {
Subroutine _init_config: called CC=3 SCAR=11.0
80 509 my($self,$force) = @_;
81 509 ▶ return if (exists $$self{'data'}{'sections'}{'conf'} && ! $force);
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"data"}{"sections"}{"conf"} and not $force 0 509 509
Condition66%2/3
exists $self->{"data"}{"sections"}{"conf"} and not $force
!ll&&!rl&&r
50810
MC/DC0%0/2
exists $self->{"data"}{"sections"}{"conf"} and not $force
exists $self->{"data"}{"sections"}{"conf"} not $force
Truth table66%2/3
A and not(B)
A= exists $self->{"data"}{"sections"}{"conf"}B= not $force
ABresult
0X0
100
111
82 509 $self->_init_data();
83  
84 #
85 # Set config defaults
86 #
87  
88 509 $$self{'data'}{'sections'}{'conf'} =
89 {
90 # Reset config, holiday lists, or events lists
91  
92 'defaults' => '',
93 'eraseholidays' => '',
94 'eraseevents' => '',
95  
96 # Which language to use when parsing dates.
97  
98 'language' => '',
99  
100 # 12/10 = Dec 10 (US) or Oct 12 (anything else)
101  
102 'dateformat' => '',
103  
104 # Define the work week (1=monday, 7=sunday)
105 #
106 # These have to be predefined to avoid a bootstrap issue, but
107 # the true defaults are defined below.
108  
109 'workweekbeg' => 1,
110 'workweekend' => 5,
111  
112 # If non-nil, a work day is treated as 24 hours long
113 # (WorkDayBeg/WorkDayEnd ignored)
114  
115 'workday24hr' => '',
116  
117 # Start and end time of the work day (any time format allowed,
118 # seconds ignored). If the defaults change, be sure to change
119 # the starting value of bdlength above.
120  
121 'workdaybeg' => '',
122 'workdayend' => '',
123  
124 # 2 digit years fall into the 100 year period given by [ CURR-N,
125 # CURR+(99-N) ] where N is 0-99. Default behavior is 89, but
126 # other useful numbers might be 0 (forced to be this year or
127 # later) and 99 (forced to be this year or earlier). It can
128 # also be set to 'c' (current century) or 'cNN' (i.e. c18
129 # forces the year to bet 1800-1899). Also accepts the form
130 # cNNNN to give the 100 year period NNNN to NNNN+99.
131  
132 'yytoyyyy' => '',
133  
134 # First day of the week (1=monday, 7=sunday). ISO 8601 says
135 # monday.
136  
137 'firstday' => '',
138  
139 # If this is a value 'jan1' to 'jan7', then it means that the first
140 # week of the year contains that date. If the value is 'dow1' to
141 # 'dow7', then the first week of the year is the one that contains
142 # the first occurence of the DoW given. If the value is 'firstday',
143 # then the first week of the year is the one that contains the first
144 # occurence of the DoW given in the FirstDay config variable.
145 #
146 # The default ISO 8601 definition is the first week is the one which
147 # contains Jan 4 (so at least 4 of the days).
148  
149 'week1ofyear' => '',
150  
151 # Date::Manip printable format
152 # 0 = YYYYMMDDHH:MN:SS
153 # 1 = YYYYHHMMDDHHMNSS
154 # 2 = YYYY-MM-DD-HH:MN:SS
155  
156 'printable' => '',
157  
158 # If 'today' is a holiday, we look either to 'tomorrow' or
159 # 'yesterday' for the nearest business day. By default, we'll
160 # always look 'tomorrow' first.
161  
162 'tomorrowfirst' => 1,
163  
164 # Used to set the current date/time/timezone.
165  
166 'forcedate' => 0,
167 'setdate' => 0,
168  
169 # Use this to set the default range of the recurrence.
170  
171 'recurrange' => '',
172 'maxrecurattempts' => 100,
173  
174 # Use this to set the default time.
175  
176 'defaulttime' => 'midnight',
177  
178 # Whether or not to use a period as a time separator.
179  
180 'periodtimesep' => 0,
181  
182 # How to parse mmm#### strings
183  
184 'format_mmmyyyy' => '',
185  
186 # Whether to use the default printf formats or the POSIX ones.
187  
188 'use_posix_printf' => 0,
189  
190 # The maximum string length to parse (or 0 for no limit)
191  
192 'max_parse_length' => 256,
193  
194 # *** DEPRECATED 7.0 ***
195  
196 'tz' => '',
197 'jan1week1' => '',
198 };
199  
200 #
201 # Calculate delta field lengths
202 #
203  
204 # non-business
205 509 $$self{'data'}{'len'}{'standard'} =
206 { 'yl' => 31556952, # 365.2425 * 24 * 3600
207 'ml' => 2629746, # yl / 12
208 'wl' => 604800, # 6 * 24 * 3600
209 'dl' => 86400, # 24 * 3600
210 };
211 509 $self->_calc_workweek();
212  
213 #
214 # Initialize some config variables that do some additional work.
215 #
216  
217 509 $self->_config_var('workday24hr', 1);
218 509 $self->_config_var('workdaybeg', '08:00:00');
219 509 $self->_config_var('workdayend', '17:00:00');
220 509 $self->_config_var('workday24hr', 0);
221 509 $self->_config_var('dateformat', 'US');
222 509 $self->_config_var('yytoyyyy', 89);
223 509 $self->_config_var('printable', 0);
224 509 $self->_config_var('firstday', 1);
225 509 $self->_config_var('week1ofyear', 'jan4');
226 509 $self->_config_var('workweekbeg', 1);
227 509 $self->_config_var('workweekend', 5);
228 509 $self->_config_var('language', 'english');
229 509 $self->_config_var('recurrange', 'none');
230 509 $self->_config_var('maxrecurattempts', 100);
231 509 $self->_config_var('defaulttime', 'midnight');
232  
233 # Set OS specific defaults
234  
235 509 my $os = $self->_os();
236  
237 509 return;
238 }
239  
240 1555 ▶sub _calc_workweek {
Subroutine _calc_workweek: called CC=3 SCAR=11.0
241 1555 my($self,$beg,$end) = @_;
242  
243 1555 ▶ $beg = $self->_config('workweekbeg') if (! $beg);
Branch: 1 branch
BranchTrueFalseTotal
unless $beg 1032 523 1555
244 1555 ▶ $end = $self->_config('workweekend') if (! $end);
Branch: 1 branch
BranchTrueFalseTotal
unless $end 1032 523 1555
245  
246 1555 $$self{'data'}{'len'}{'workweek'} = $end - $beg + 1;
247  
248 1555 return;
249 }
250  
251 1565 ▶sub _calc_bdlength {
Subroutine _calc_bdlength: called CC=1 SCAR=0.0
252 1565 my($self) = @_;
253  
254 1565 my @beg = @{ $$self{'data'}{'calc'}{'workdaybeg'} };
255 1565 my @end = @{ $$self{'data'}{'calc'}{'workdayend'} };
256  
257 1565 $$self{'data'}{'len'}{'bdlength'} =
258 ($end[0]-$beg[0])*3600 + ($end[1]-$beg[1])*60 + ($end[2]-$beg[2]);
259  
260 1565 return;
261 }
262  
263 2611 ▶sub _init_business_length {
Subroutine _init_business_length: called CC=1 SCAR=0.0
264 2611 my($self) = @_;
265  
266 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
267 2611 my $x = $$self{'data'}{'len'}{'workweek'};
268 2611 my $y_to_d = $x/7 * 365.2425;
269 2611 my $d_to_s = $$self{'data'}{'len'}{'bdlength'};
270 2611 my $w_to_d = $x;
271  
272 2611 $$self{'data'}{'len'}{'business'} = { 'yl' => $y_to_d * $d_to_s,
273 'ml' => $y_to_d * $d_to_s / 12,
274 'wl' => $w_to_d * $d_to_s,
275 'dl' => $d_to_s,
276 };
277  
278 2611 return;
279 }
280  
281 # Events and holidays are reset only when they are read in.
282 522 ▶sub _init_events {
Subroutine _init_events: called CC=3 SCAR=11.1
283 522 my($self,$force) = @_;
284 522 ▶ return if (exists $$self{'data'}{'events'} && ! $force);
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"data"}{"events"} and not $force 0 522 522
Condition66%2/3
exists $self->{"data"}{"events"} and not $force
!ll&&!rl&&r
508140
MC/DC0%0/2
exists $self->{"data"}{"events"} and not $force
exists $self->{"data"}{"events"} not $force
Truth table66%2/3
A and not(B)
A= exists $self->{"data"}{"events"}B= not $force
ABresult
0X0
100
111
285  
286 # {data}{sections}{events} = [ STRING, EVENT_NAME, ... ]
287 #
288 # {data}{events}{I}{type} = TYPE
289 # {name} = NAME
290 # TYPE: specified An event with a start/end date (only parsed once)
291 # {beg} = DATE_OBJECT
292 # {end} = DATE_OBJECT
293 # TYPE: ym
294 # {beg} = YM_STRING
295 # {end} = YM_STRING (only for YM;YM)
296 # {YEAR} = [ DATE_OBJECT, DATE_OBJECT ]
297 # TYPE: date An event specified by a date string and delta
298 # {beg} = DATE_STRING
299 # {end} = DATE_STRING (only for Date;Date)
300 # {delta} = DELTA_OBJECT (only for Date;Delta)
301 # {YEAR} = [ DATE_OBJECT, DATE_OBJECT ]
302 # TYPE: recur
303 # {recur} = RECUR_OBJECT
304 # {delta} = DELTA_OBJECT
305 #
306 # {data}{eventyears}{YEAR} = 0/1
307 # {data}{eventobjs} = 0/1
308  
309 522 $$self{'data'}{'events'} = {};
310 522 $$self{'data'}{'sections'}{'events'} = [];
311 522 $$self{'data'}{'eventyears'} = {};
312 522 $$self{'data'}{'eventobjs'} = 0;
313  
314 522 return;
315 }
316  
317 526 ▶sub _init_holidays {
Subroutine _init_holidays: called CC=3 SCAR=11.2
318 526 my($self,$force) = @_;
319 526 ▶ return if (exists $$self{'data'}{'holidays'} && ! $force);
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"data"}{"holidays"} and not $force 0 526 526
Condition66%2/3
exists $self->{"data"}{"holidays"} and not $force
!ll&&!rl&&r
508180
MC/DC0%0/2
exists $self->{"data"}{"holidays"} and not $force
exists $self->{"data"}{"holidays"} not $force
Truth table66%2/3
A and not(B)
A= exists $self->{"data"}{"holidays"}B= not $force
ABresult
0X0
100
111
320  
321 # {data}{sections}{holidays} = [ STRING, HOLIDAY_NAME, ... ]
322 #
323 # {data}{holidays}{init} = 1 if holidays have been initialized
324 # {ydone} = { Y => 1 }
325 # {yhols} = { Y => NAME => [Y,M,D] }
326 # {hols} = { NAME => Y => [Y,M,D] }
327 # {dates} = { Y => M => D => NAME }
328 # {defs} = [ NAME DEF NAME DEF ... ]
329 # NAME is the name of a holiday (it will
330 # be 'DMunnamed I' for the Ith unnamed
331 # holiday)
332 # DEF is a string or a Recur
333 # {data}{init_holidays} = 1 if currently initializing holidays
334  
335 526 $$self{'data'}{'holidays'} = {};
336 526 $$self{'data'}{'sections'}{'holidays'} = [];
337 526 $$self{'data'}{'init_holidays'} = 0;
338  
339 526 return;
340 }
341  
342 508 ▶sub _init_now {
Subroutine _init_now: called CC=1 SCAR=0.0
343 508 my($self) = @_;
344  
345 # {'data'}{'now'} = {
346 # date => [Y,M,D,H,MN,S] now
347 # isdst => ISDST
348 # offset => [H,MN,S]
349 # abb => ABBREV
350 #
351 # force => 0/1 SetDate/ForceDate information
352 # set => 0/1
353 # setsecs => SECS time (secs since epoch) when
354 # SetDate was called
355 # setdate => [Y,M,D,H,MN,S] date (IN GMT) we're calling
356 # now when SetDate was called
357 #
358 # tz => ZONE timezone we're working in
359 # systz => ZONE timezone of the system
360 # }
361 #
362  
363 508 $$self{'data'}{'now'} = {};
364 508 $$self{'data'}{'now'}{'force'} = 0;
365 508 $$self{'data'}{'now'}{'set'} = 0;
366 508 $$self{'data'}{'tmpnow'} = [];
367  
368 508 return;
369 }
370  
371 # Language information only needs to be initialized if the language changes.
372 1050 ▶sub _init_language {
Subroutine _init_language: called CC=3 SCAR=11.1
373 1050 my($self,$force) = @_;
374 1050 ▶ return if (exists $$self{'data'}{'lang'} && ! $force);
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"data"}{"lang"} and not $force 0 1050 1050
Condition66%2/3
exists $self->{"data"}{"lang"} and not $force
!ll&&!rl&&r
5085420
MC/DC0%0/2
exists $self->{"data"}{"lang"} and not $force
exists $self->{"data"}{"lang"} not $force
Truth table66%2/3
A and not(B)
A= exists $self->{"data"}{"lang"}B= not $force
ABresult
0X0
100
111
375  
376 1050 $$self{'data'}{'lang'} = {}; # Current language info
377 1050 $$self{'data'}{'rx'} = {}; # Regexps generated from language
378 1050 $$self{'data'}{'words'} = {}; # Types of words in the language
379 1050 $$self{'data'}{'wordval'} = {}; # Value of words in the language
380  
381 1050 return;
382 }
383  
384 ###############################################################################
385 # MAIN METHODS
386 ###############################################################################
387  
388 # Use an algorithm from Calendar FAQ (except that I subtract 305 to get
389 # Jan 1, 0001 = day #1).
390 #
391 12393 ▶sub days_since_1BC {
Subroutine days_since_1BC: called CC=3 SCAR=11.0
Pod documented
392 12393 my($self,$arg) = @_;
393  
394 12393 ▶ if (ref($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 7664 4729 12393
395 7664 my($y,$m,$d) = @$arg;
396 7664 $m = ($m + 9) % 12;
397 7664 $y = $y - $m/10;
398 7664 return 365*$y + $y/4 - $y/100 + $y/400 + ($m*306 + 5)/10 + ($d - 1) - 305;
399 } else {
400 4729 my $g = $arg + 305;
401 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
402 4729 my $y = int((10000*$g + 14780)/3652425);
403 171 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
404 4729 my $ddd = $g - (365*$y + $y/4 - $y/100 + $y/400);
405 4729 ▶ if ($ddd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ddd < 0) 6 4723 4729
406 6 $y = $y - 1;
407 6 $ddd = $g - (365*$y + $y/4 - $y/100 + $y/400);
408 }
409 4729 my $mi = (100*$ddd + 52)/3060;
410 4729 my $mm = ($mi + 2) % 12 + 1;
411 4729 $y = $y + ($mi + 2)/12;
412 4729 my $dd = $ddd - ($mi*306 + 5)/10 + 1;
413 4729 return [$y, $mm, $dd];
414 }
415 }
416  
417 # Algorithm from the Calendar FAQ
418 #
419 10622 ▶sub day_of_week {
Subroutine day_of_week: called CC=2 SCAR=6.9
Pod documented
420 10622 my($self,$date) = @_;
421 10622 my($y,$m,$d) = @$date;
422  
423 10622 my $a = (14-$m)/12;
424 10622 $y = $y-$a;
425 10622 $m = $m + 12*$a - 2;
426 10622 my $dow = ($d + $y + $y/4 - $y/100 + $y/400 + (31*$m)/12) % 7;
427 10622 ▶ $dow = 7 if ($dow==0);
Branch: 1 branch
BranchTrueFalseTotal
if $dow == 0 1443 9179 10622
428 10622 return $dow;
429 }
430  
431 3793 ▶sub leapyear {
Subroutine leapyear: called CC=4 SCAR=13.9
Pod documented
432 3793 my($self,$y) = @_;
433 3793 ▶ return 1 if ( ( ($y % 4 == 0) and ($y % 100 != 0) ) or
Branch: 1 branch
BranchTrueFalseTotal
if $y % 4 == 0 and $y % 100 != 0 or $y % 400 == 0 1369 2424 3793
Condition100%6/6
$y % 4 == 0 and $y % 100 != 0 or $y % 400 == 0
l!l&&r!l&&!r
5648052424
$y % 4 == 0 and $y % 100 != 0
!ll&&!rl&&r
2422807564
MC/DC100%3/3
$y % 4 == 0 and $y % 100 != 0 or $y % 400 == 0
$y % 4 == 0 $y % 100 != 0 $y % 400 == 0
Truth table80%4/5
A and B or C
A= $y % 4 == 0B= $y % 100 != 0C= $y % 400 == 0
ABCresult
11X1
0X11
1011
0X00
1000
434 $y % 400 == 0 );
435 2424 return 0;
436 }
437  
438 380 ▶sub days_in_year {
Subroutine days_in_year: called CC=2 SCAR=6.9
Pod documented
439 380 my($self,$y) = @_;
440 380 ▶ return ($self->leapyear($y) ? 366 : 365);
Branch: 1 branch
BranchTrueFalseTotal
$self->leapyear($y) ? : 114 266 380
441 }
442  
443 # Uses algorithm from:
444 # http://www.dispersiondesign.com/articles/time/number_of_days_in_a_month
445 #
446 37206 ▶sub days_in_month {
Subroutine days_in_month: called CC=4 SCAR=13.9
Pod documented
447 37206 my($self,$y,$m) = @_;
448 37206 ▶ if (! $m) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $m) { } 2 37204 37206
elsif ($m == 2) { } 2904 34300 37204
449 2 ▶ return (31,29,31,30, 31,30,31,31, 30,31,30,31) if ($self->leapyear($y));
Branch: 1 branch
BranchTrueFalseTotal
if $self->leapyear($y) 1 1 2
450 1 return (31,28,31,30, 31,30,31,31, 30,31,30,31);
451  
452 } elsif ($m == 2) {
453 2904 return 28 + $self->leapyear($y);
454  
455 } else {
456 34300 return 31 - ($m-1) % 7 % 2;
457 }
458 }
459  
460 {
461 # DinM = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
462 171 my(@doy_days) = ( [0, 31, 59, 90,120,151,181,212,243,273,304,334,365],
463 [0, 31, 60, 91,121,152,182,213,244,274,305,335,366],
464 );
465  
466  
467 698 ▶ sub day_of_year {
Subroutine day_of_year: called CC=9 SCAR=22.0
Pod documented
468 698 my($self,@args) = @_;
469 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
470 698 my($n,$ly,$tmp,$remain,$day,$y,$m,$d,$h,$mn,$s,$time);
471  
472 698 ▶ if (@args == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 2) { } 265 433 698
473 # $date = day_of_year($y,$day);
474  
475 265 ($y,$tmp) = @args;
476  
477 265 $ly = $self->leapyear($y);
478 265 ▶ $time = 1 if ($tmp =~ /\./);
Branch: 1 branch
BranchTrueFalseTotal
if $tmp =~ /\./ 9 256 265
479 265 $n = int($tmp);
480 265 $remain = $tmp - $n;
481  
482 # Calculate the month and the day
483 265 ▶ for ($m=1; $m<=12; $m++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m <= 12) 1065 0 1065
484 1065 ▶ last if ($n<=($doy_days[$ly][$m]));
Branch: 1 branch
BranchTrueFalseTotal
if $n <= $doy_days[$ly][$m] 265 800 1065
485 }
486 265 $d = $n-($doy_days[$ly][$m-1]);
487 265 ▶ return [$y,$m,$d] if (! $time);
Branch: 1 branch
BranchTrueFalseTotal
unless $time 256 9 265
488  
489 # Calculate the hours, minutes, and seconds into the day.
490  
491 9 $s = $remain * 86400;
492 9 $mn = int($s/60);
493 9 $s = $s - ($mn*60);
494 9 ▶ $s = sprintf('%0.2f',$s) if ("$s" ne int($s));
Branch: 1 branch
BranchTrueFalseTotal
if "$s" ne int $s 4 5 9
495 9 $h = int($mn/60);
496 9 $mn = $mn % 60;
497  
498 9 return [$y,$m,$d,$h,$mn,$s];
499  
500 } else {
501 433 ($y,$m,$d,$h,$mn,$s) = @{ $args[0] };
502  
503 433 ▶ $ly = ($m > 2 ? $self->leapyear($y) : 0);
Branch: 1 branch
BranchTrueFalseTotal
$m > 2 ? : 238 195 433
504 433 $day = ($doy_days[$ly][$m-1]+$d);
505  
506 433 ▶ return $day if (! defined $h);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h 430 3 433
507  
508 3 $day += ($h*3600 + $mn*60 + $s)/86400;
509 3 return $day;
510 }
511 }
512 }
513  
514 # Can be the nth DoW of year or month (if $m given). Returns undef if
515 # the date doesn't exists (i.e. 5th Sunday in a month with only 4).
516 #
517 171 ▶sub nth_day_of_week {
Subroutine nth_day_of_week: called CC=13 SCAR=25.8
Pod documented
518 997 my($self,$y,$n,$dow,$m) = @_;
519 997 $y += 0;
520 997 ▶ $m = ($m ? $m+0 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$m ? : 913 84 997
521  
522 # $d is the current DoM (if $m) or DoY
523 # $max is the max value allowed for $d
524 # $ddow is the DoW of $d
525  
526 997 my($d,$max,$ddow);
527  
528 997 ▶ if ($m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m) { } 913 84 997
529 913 $max = $self->days_in_month($y,$m);
530 913 ▶ $d = ($n<0 ? $max : 1);
Branch: 1 branch
BranchTrueFalseTotal
$n < 0 ? : 8 905 913
531 913 $ddow = $self->day_of_week([$y,$m,$d]);
532 } else {
533 84 $max = $self->days_in_year($y);
534 84 ▶ $d = ($n<0 ? $max : 1);
Branch: 1 branch
BranchTrueFalseTotal
$n < 0 ? : 0 84 84
535 84 ▶ if ($n<0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) { } 0 84 84
536 0 $d = $max;
537 0 $ddow = $self->day_of_week([$y,12,31]);
538 } else {
539 84 $d = 1;
540 84 $ddow = $self->day_of_week([$y,1,1]);
541 }
542 }
543  
544 # Find the first occurrence of $dow on or after $d (if $n>0)
545 # or the last occurrence of $dow on or before $d (if ($n<0);
546  
547 997 ▶ if ($dow < $ddow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dow < $ddow) { } 550 447 997
548 550 $d += 7 - ($ddow-$dow);
549 } else {
550 447 $d += ($dow-$ddow);
551 }
552 997 ▶ $d -= 7 if ($d > $max);
Branch: 1 branch
BranchTrueFalseTotal
if $d > $max 8 989 997
553  
554 # Find the nth occurrence of $dow
555  
556 997 ▶ if ($n > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n > 1) { } 847 150 997
elsif ($n < -1) { } 2 148 150
557 847 $d += 7*($n-1);
558 847 ▶ return undef if ($d > $max);
Branch: 1 branch
BranchTrueFalseTotal
if $d > $max 0 847 847
559 } elsif ($n < -1) {
560 2 $d -= 7*(-1*$n-1);
561 2 ▶ return undef if ($d < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $d < 1 0 2 2
562 }
563  
564 # Return the date
565  
566 997 ▶ if ($m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m) 913 84 997
567 913 return [$y,$m,$d];
568 }
569 84 return $self->day_of_year($y,$d);
570 }
571  
572 {
573 # Integer arithmetic doesn't work due to the size of the numbers.
574 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
575 # my $sec_70 =($self->days_since_1BC([1970,1,1])-1)*24*3600;
576 171 my $sec_70 = 62135596800;
577  
578 # Using 'global' variables saves 4%
579 171 my($y,$m,$d,$h,$mn,$s,$sec,$sec_0,$tmp);
580 2560 ▶ sub secs_since_1970 {
Subroutine secs_since_1970: called CC=2 SCAR=6.9
Pod documented
581 2560 my($self,$arg) = @_;
582  
583 2560 ▶ if (ref($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 2555 5 2560
584 2555 ($y,$m,$d,$h,$mn,$s) = @$arg;
585 2555 $sec_0 = ($self->days_since_1BC([$y,$m,$d])-1)*24*3600 + $h*3600 +
586 $mn*60 + $s;
587 2555 $sec = $sec_0 - $sec_70;
588 2555 return $sec;
589  
590 } else {
591 5 ($sec) = $arg;
592 5 $sec_0 = $sec_70 + $sec;
593 5 $tmp = int($sec_0/24/3600)+1;
594 5 my $ymd = $self->days_since_1BC($tmp);
595 5 ($y,$m,$d) = @$ymd;
596 5 $sec_0 -= ($tmp-1)*24*3600;
597 5 $h = int($sec_0/3600);
598 5 $sec_0 -= $h*3600;
599 5 $mn = int($sec_0/60);
600 5 $s = $sec_0 - $mn*60;
601 5 return [$y,$m,$d,$h,$mn,$s];
602 }
603 }
604 }
605  
606 171 ▶sub check {
Subroutine check: called CC=8 SCAR=21.1
Pod documented
607 15367 my($self,$date) = @_;
608 15367 my($y,$m,$d,$h,$mn,$s) = @$date;
609  
610 15367 ▶ return 0 if (! $self->check_time([$h,$mn,$s]) ||
Branch: 1 branch
BranchTrueFalseTotal
if not $self->check_time([$h, $mn, $s]) or $y < 1 or $y > 9999 or $m < 1 or $m > 12 11 15356 15367
Condition75%9/12
not $self->check_time([$h, $mn, $s]) or $y < 1 or $y > 9999 or $m < 1 or $m > 12
l!l&&r!l&&!r
4715356
not $self->check_time([$h, $mn, $s]) or $y < 1 or $y > 9999 or $m < 1
l!l&&r!l&&!r
4015363
not $self->check_time([$h, $mn, $s]) or $y < 1 or $y > 9999
l!l&&r!l&&!r
4015363
not $self->check_time([$h, $mn, $s]) or $y < 1
l!l&&r!l&&!r
4015363
MC/DC40%2/5
not $self->check_time([$h, $mn, $s]) or $y < 1 or $y > 9999 or $m < 1 or $m > 12
not $self->check_time([$h, $mn, $s]) $y < 1 $y > 9999 $m < 1 $m > 12
Truth table50%3/6
not(A) or B or C or D or E
A= not $self->check_time([$h, $mn, $s])B= $y < 1C= $y > 9999D= $m < 1E= $m > 12
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
611 $y<1 || $y>9999 ||
612 $m<1 || $m>12);
613  
614 15356 my $days = $self->days_in_month($y,$m);
615  
616 15356 ▶ return 0 if ($d<1 || $d>$days);
Branch: 1 branch
BranchTrueFalseTotal
if $d < 1 or $d > $days 4 15352 15356
Condition66%2/3
$d < 1 or $d > $days
l!l&&r!l&&!r
0415352
MC/DC50%1/2
$d < 1 or $d > $days
$d < 1 $d > $days
Truth table66%2/3
A or B
A= $d < 1B= $d > $days
ABresult
1X1
011
000
617 15352 return 1;
618 }
619  
620 15431 ▶sub check_time {
Subroutine check_time: called CC=8 SCAR=20.9
Pod documented
621 15431 my($self,$hms) = @_;
622 15431 my($h,$mn,$s) = @$hms;
623  
624 15431 ▶ return 0 if ("$h:$mn:$s" !~ /^\d\d?:\d\d?:\d\d?$/o ||
Branch: 1 branch
BranchTrueFalseTotal
if not +("${h}:${mn}:$s") =~ /^\d\d?:\d\d?:\d\d?$/o or $h > 24 or $mn > 59 or $s > 59 or $h == 24 and $mn || $s 4 15427 15431
Condition83%15/18
not +("${h}:${mn}:$s") =~ /^\d\d?:\d\d?:\d\d?$/o or $h > 24 or $mn > 59 or $s > 59 or $h == 24 and $mn || $s
l!l&&r!l&&!r
3115427
not +("${h}:${mn}:$s") =~ /^\d\d?:\d\d?:\d\d?$/o or $h > 24 or $mn > 59 or $s > 59
l!l&&r!l&&!r
1215428
not +("${h}:${mn}:$s") =~ /^\d\d?:\d\d?:\d\d?$/o or $h > 24 or $mn > 59
l!l&&r!l&&!r
1015430
not +("${h}:${mn}:$s") =~ /^\d\d?:\d\d?:\d\d?$/o or $h > 24
l!l&&r!l&&!r
1015430
$h == 24 and $mn || $s
!ll&&!rl&&r
1542611
$mn or $s
l!l&&r!l&&!r
101
MC/DC57%4/7
not +("${h}:${mn}:$s") =~ /^\d\d?:\d\d?:\d\d?$/o or $h > 24 or $mn > 59 or $s > 59 or $h == 24 and $mn || $s
not +("${h}:${mn}:$s") =~ /^\d\d?:\d\d?:\d\d?$/o $h > 24 $mn > 59 $s > 59 $h == 24 $mn $s
Truth table62%5/8
not(A) or B or C or D or E and F or G
A= not +("${h}:${mn}:$s") =~ /^\d\d?:\d\d?:\d\d?$/oB= $h > 24C= $mn > 59D= $s > 59E= $h == 24F= $mnG= $s
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
000011X1
00001011
00000XX0
00001000
625 $h > 24 || $mn > 59 || $s > 59 ||
626 ($h == 24 && ($mn || $s)));
627 15427 return 1;
628 }
629  
630 735 ▶sub week1_day1 {
Subroutine week1_day1: called CC=1 SCAR=0.0
Pod documented
631 735 my($self,$year) = @_;
632 735 my $firstday = $self->_config('firstday');
633 735 my $week1ofyear = $self->_config('week1ofyear');
634 735 $self->_week1_day1($year,$firstday,$week1ofyear);
635 }
636 1400 ▶sub _week1_day1 {
Subroutine _week1_day1: called CC=8 SCAR=20.8
637 1400 my($self,$year,$firstday,$week1ofyear) = @_;
638 return $$self{'cache'}{'week1day1'}{$firstday}{$week1ofyear}{$year}
639 1400 ▶ if (exists $$self{'cache'}{'week1day1'}{$firstday}{$week1ofyear}{$year});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cache"}{"week1day1"}{$firstday}{$week1ofyear}{$year} 419 981 1400
640  
641 981 my($y,$m,$d,$firstdow);
642  
643 # Get the starting date.
644  
645 981 ▶ if ($week1ofyear =~ /^jan([1-7])$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($week1ofyear =~ /^jan([1-7])$/) { } 581 400 981
646 # First week contains 1/D/YYYY where D is $1
647 581 ($y,$m,$d) = ($year,1,$1);
648 } else {
649 # First week contains the first DOW given by $1 or firstday
650 # We'll start at Jan 1.
651 400 ▶ if ($week1ofyear =~ /^dow([1-7])$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($week1ofyear =~ /^dow([1-7])$/) { } 343 57 400
652 343 $firstdow = $1;
653 } else {
654 57 $firstdow = $firstday;
655 }
656  
657 # Start at Jan 1 and move forward to the first DOW
658 400 ($y,$m,$d) = ($year,1,1);
659 }
660  
661 981 my $dow = $self->day_of_week([$y,$m,$d]);
662  
663 # If we're looking for the first occurence of a DOW, move forward to it
664 # (counting today).
665  
666 981 ▶ if ($firstdow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstdow) 400 581 981
667 400 my $forward = $firstdow - $dow;
668 400 ▶ $forward += 7 if ($forward < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $forward < 0 171 229 400
669 400 $d += $forward;
670 400 $dow = $firstdow;
671 }
672  
673 # Go back to the previous (counting today) $firstday
674  
675 981 my $backward = $dow - $firstday;
676 981 ▶ $backward += 7 if ($backward < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $backward < 0 357 624 981
677 981 $d -= $backward;
678 981 ▶ if ($d<1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d < 1) 419 562 981
679 419 $y--;
680 419 $m = 12;
681 419 $d += 31;
682 }
683  
684 981 $$self{'cache'}{'week1day1'}{$firstday}{$week1ofyear}{$year} = [ $y,$m,$d ];
685 981 return [$y,$m,$d];
686 }
687  
688 352 ▶sub weeks_in_year {
Subroutine weeks_in_year: called CC=4 SCAR=13.9
Pod documented
689 352 my($self,$y) = @_;
690 352 my $firstday = $self->_config('firstday');
691 352 my $week1ofyear = $self->_config('week1ofyear');
692 return $$self{'cache'}{'wiy'}{$firstday}{$week1ofyear}{$y}
693 352 ▶ if (exists $$self{'cache'}{'wiy'}{$firstday}{$week1ofyear}{$y});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cache"}{"wiy"}{$firstday}{$week1ofyear}{$y} 231 121 352
694  
695 # Get the week1 day1 dates for this year and the next one.
696 121 my ($y1,$m1,$d1) = @{ $self->_week1_day1($y,$firstday,$week1ofyear) };
697 121 my ($y2,$m2,$d2) = @{ $self->_week1_day1($y+1,$firstday,$week1ofyear) };
698  
699 # Calculate the number of days between them.
700 121 my $diy = $self->days_in_year($y);
701 121 ▶ if ($y1 < $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < $y) { } 65 56 121
702 65 $diy += (32-$d1);
703 } else {
704 56 $diy -= ($d1-1);
705 }
706 121 ▶ if ($y2 < $y+1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 < $y + 1) { } 63 58 121
707 63 $diy -= (32-$d2);
708 } else {
709 58 $diy += ($d2-1);
710 }
711  
712 121 $diy = $diy/7;
713 121 $$self{'cache'}{'wiy'}{$firstday}{$week1ofyear}{$y} = $diy;
714 121 return $diy;
715 }
716  
717 552 ▶sub week_of_year {
Subroutine week_of_year: called CC=1 SCAR=0.0
Pod documented
718 552 my($self,@args) = @_;
719  
720 552 my $firstday = $self->_config('firstday');
721 552 my $week1ofyear = $self->_config('week1ofyear');
722 552 $self->_week_of_year($firstday,$week1ofyear,@args);
723 }
724 600 ▶sub _week_of_year {
Subroutine _week_of_year: called CC=9 SCAR=22.0
725 600 my($self,$firstday,$week1ofyear,@args) = @_;
726  
727 600 ▶ if ($#args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#args == 1) 325 275 600
728 # (y,m,d) = week_of_year(y,w)
729 325 my($year,$w) = @args;
730  
731 return $$self{'cache'}{'woy'}{$firstday}{$week1ofyear}{$year}{$w}
732 325 ▶ if (exists $$self{'cache'}{'woy'}{$firstday}{$week1ofyear}{$year}{$w});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cache"}{"woy"}{$firstday}{$week1ofyear}{$year}{$w} 177 148 325
733  
734 148 my $ymd = $self->_week1_day1($year,$firstday,$week1ofyear);
735 148 ▶ $ymd = $self->calc_date_days($ymd,($w-1)*7) if ($w > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $w > 1 138 10 148
736  
737 148 $$self{'cache'}{'woy'}{$firstday}{$week1ofyear}{$year}{$w} = $ymd;
738 148 return $ymd;
739 }
740  
741 # (y,w) = week_of_year([y,m,d])
742 275 my($y,$m,$d) = @{ $args[0] };
743  
744 # Get the first day of the first week. If the date is before that,
745 # it's the last week of last year.
746  
747 275 my($y0,$m0,$d0) = @{ $self->_week1_day1($y,$firstday,$week1ofyear) };
748 275 ▶ if ($y0==$y && $m==1 && $d<$d0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y0 == $y and $m == 1 and $d < $d0) 19 256 275
Condition100%6/6
$y0 == $y and $m == 1 and $d < $d0
!ll&&!rl&&r
15010619
$y0 == $y and $m == 1
!ll&&!rl&&r
10644125
MC/DC100%3/3
$y0 == $y and $m == 1 and $d < $d0
$y0 == $y $m == 1 $d < $d0
Truth table100%4/4
A and B and C
A= $y0 == $yB= $m == 1C= $d < $d0
ABCresult
0XX0
10X0
1100
1111
749 19 return($y-1,$self->weeks_in_year($y-1));
750 }
751  
752 # Otherwise, we'll figure out how many days are between the two and
753 # divide by 7 to figure out how many weeks in it is.
754  
755 256 my $n = $self->day_of_year([$y,$m,$d]);
756 256 ▶ if ($y0<$y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y0 < $y) { } 106 150 256
757 106 $n += (32-$d0);
758 } else {
759 150 $n -= ($d0-1);
760 }
761 256 my $w = 1+int(($n-1)/7);
762  
763 # Make sure we're not into the first week of next year.
764  
765 256 ▶ if ($w>$self->weeks_in_year($y)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w > $self->weeks_in_year($y)) 8 248 256
766 8 return($y+1,1);
767 }
768 248 return($y,$w);
769 }
770  
771 ###############################################################################
772 # CALC METHODS
773 ###############################################################################
774  
775 19 ▶sub calc_date_date {
Subroutine calc_date_date: called CC=8 SCAR=20.8
Pod documented
776 19 my($self,$date0,$date1) = @_;
777  
778 # Order them so date0 < date1
779 # If $minus = 1, then the delta is negative
780  
781 19 my $minus = 0;
782 19 my $cmp = $self->cmp($date0,$date1);
783  
784 19 ▶ if ($cmp == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp == 0) { } 5 14 19
elsif ($cmp == 1) { } 7 7 14
785 5 return [0,0,0];
786  
787 } elsif ($cmp == 1) {
788 7 $minus = 1;
789 7 my $tmp = $date1;
790 7 $date1 = $date0;
791 7 $date0 = $tmp;
792 }
793  
794 14 my($y0,$m0,$d0,$h0,$mn0,$s0) = @$date0;
795 14 my($y1,$m1,$d1,$h1,$mn1,$s1) = @$date1;
796  
797 14 ▶ my $sameday = ($y0 == $y1 && $m0 == $m1 && $d0 == $d1 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$y0 == $y1 && $m0 == $m1 && $d0 == $d1 ? : 4 10 14
Condition100%6/6
$y0 == $y1 and $m0 == $m1 and $d0 == $d1
!ll&&!rl&&r
644
$y0 == $y1 and $m0 == $m1
!ll&&!rl&&r
248
MC/DC100%3/3
$y0 == $y1 and $m0 == $m1 and $d0 == $d1
$y0 == $y1 $m0 == $m1 $d0 == $d1
Truth table100%4/4
A and B and C
A= $y0 == $y1B= $m0 == $m1C= $d0 == $d1
ABCresult
0XX0
10X0
1100
1111
798  
799 # Handle the various cases.
800  
801 14 my($dh,$dm,$ds);
802 14 ▶ if ($sameday) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sameday) { } 4 10 14
803 4 ($dh,$dm,$ds) = @{ $self->_calc_hms_hms([$h0,$mn0,$s0],[$h1,$mn1,$s1]) };
804  
805 } else {
806 # y0-m0-d0 h0:mn0:s0 -> y0-m0-d0 24:00:00
807 # y1-m1-d1 h1:mn1:s1 -> y1-m1-d1 00:00:00
808  
809 10 my $t1 = $self->_calc_hms_hms([$h0,$mn0,$s0],[24,0,0]);
810 10 my $t2 = $self->_calc_hms_hms([0,0,0],[$h1,$mn1,$s1]);
811 10 ($dh,$dm,$ds) = @{ $self->calc_time_time($t1,$t2) };
812  
813 10 my $dd0 = $self->days_since_1BC([$y0,$m0,$d0]);
814 10 $dd0++;
815 10 my $dd1 = $self->days_since_1BC([$y1,$m1,$d1]);
816 10 $dh += ($dd1-$dd0)*24;
817 }
818  
819 14 ▶ if ($minus) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minus) 7 7 14
820 7 $dh *= -1;
821 7 $dm *= -1;
822 7 $ds *= -1;
823 }
824 14 return [$dh,$dm,$ds];
825 }
826  
827 4721 ▶sub calc_date_days {
Subroutine calc_date_days: called CC=4 SCAR=13.9
Pod documented
828 4721 my($self,$date,$n,$subtract) = @_;
829 4721 my($y,$m,$d,$h,$mn,$s) = @$date;
830 4721 ▶ my($ymdonly) = (defined $h ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $h ? : 2114 2607 4721
831  
832 4721 ▶ $n *= -1 if ($subtract);
Branch: 1 branch
BranchTrueFalseTotal
if $subtract 75 4646 4721
833 4721 my $d1bc = $self->days_since_1BC([$y,$m,$d]);
834 4721 $d1bc += $n;
835 4721 my $ymd = $self->days_since_1BC($d1bc);
836  
837 4721 ▶ if ($ymdonly) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ymdonly) { } 2607 2114 4721
838 2607 return $ymd;
839 } else {
840 2114 return [@$ymd,$h*1,$mn*1,$s*1];
841 }
842 }
843  
844 8 ▶sub calc_date_delta {
Subroutine calc_date_delta: called CC=1 SCAR=0.0
Pod documented
845 8 my($self,$date,$delta,$subtract) = @_;
846 8 my($y,$m,$d,$h,$mn,$s) = @$date;
847 8 my($dy,$dm,$dw,$dd,$dh,$dmn,$ds) = @$delta;
848  
849 ($y,$m,$d) =
850 8 @{ $self->_calc_date_ymwd([$y,$m,$d], [$dy,$dm,$dw,$dd], $subtract) };
851  
852 8 return $self->calc_date_time([$y,$m,$d,$h,$mn,$s],[$dh,$dmn,$ds],$subtract);
853 }
854  
855 12711 ▶sub calc_date_time {
Subroutine calc_date_time: called CC=13 SCAR=25.7
Pod documented
856 12711 my($self,$date,$time,$subtract) = @_;
857 12711 my($y,$m,$d,$h,$mn,$s) = @$date;
858 12711 my($dh,$dmn,$ds) = @$time;
859  
860 12711 ▶ if ($ds > 59 || $ds < -59) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ds > 59 or $ds < -59) 4 12707 12711
Condition66%2/3
$ds > 59 or $ds < -59
l!l&&r!l&&!r
4012707
MC/DC50%1/2
$ds > 59 or $ds < -59
$ds > 59 $ds < -59
Truth table66%2/3
A or B
A= $ds > 59B= $ds < -59
ABresult
1X1
011
000
861 4 $dmn += int($ds/60);
862 4 $ds = $ds % 60;
863 }
864 12711 ▶ if ($dmn > 59 || $dmn < -59) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmn > 59 or $dmn < -59) 4 12707 12711
Condition66%2/3
$dmn > 59 or $dmn < -59
l!l&&r!l&&!r
4012707
MC/DC50%1/2
$dmn > 59 or $dmn < -59
$dmn > 59 $dmn < -59
Truth table66%2/3
A or B
A= $dmn > 59B= $dmn < -59
ABresult
1X1
011
000
865 4 $dh += int($dmn/60);
866 4 $dmn = $dmn % 60;
867 }
868 12711 my $dd = 0;
869 12711 ▶ if ($dh > 23 || $dh < -23) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dh > 23 or $dh < -23) 34 12677 12711
Condition100%3/3
$dh > 23 or $dh < -23
l!l&&r!l&&!r
26812677
MC/DC100%2/2
$dh > 23 or $dh < -23
$dh > 23 $dh < -23
Truth table100%3/3
A or B
A= $dh > 23B= $dh < -23
ABresult
1X1
011
000
870 34 $dd = int($dh/24);
871 34 $dh = $dh % 24;
872 }
873  
874 # Handle subtraction
875 12711 ▶ if ($subtract) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtract) 5591 7120 12711
876 5591 $dh *= -1;
877 5591 $dmn *= -1;
878 5591 $ds *= -1;
879 5591 $dd *= -1;
880 }
881  
882 12711 ▶ if ($dd == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dd == 0) { } 12677 34 12711
883 12677 $y *= 1;
884 12677 $m *= 1;
885 12677 $d *= 1;
886 } else {
887 34 ($y,$m,$d) = @{ $self->calc_date_days([$y,$m,$d],$dd) };
888 }
889  
890 12711 $self->_mod_add(60,$ds,\$s,\$mn);
891 12711 $self->_mod_add(60,$dmn,\$mn,\$h);
892 12711 $self->_mod_add(24,$dh,\$h,\$d);
893  
894 12711 ▶ if ($d<1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d < 1) { } 9 12702 12711
895 9 $m--;
896 9 ▶ $y--, $m=12 if ($m<1);
Branch: 1 branch
BranchTrueFalseTotal
if $m < 1 4 5 9
897 9 my $day_in_mon = $self->days_in_month($y,$m);
898 9 $d += $day_in_mon;
899 } else {
900 12702 my $day_in_mon = $self->days_in_month($y,$m);
901 12702 ▶ if ($d>$day_in_mon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d > $day_in_mon) 68 12634 12702
902 68 $d -= $day_in_mon;
903 68 $m++;
904 68 ▶ $y++, $m=1 if ($m>12);
Branch: 1 branch
BranchTrueFalseTotal
if $m > 12 59 9 68
905 }
906 }
907  
908 12711 return [$y,$m,$d,$h,$mn,$s];
909 }
910  
911 0 ▶sub _calc_date_time_strings {
Subroutine _calc_date_time_strings: not called CC=2 SCAR=17.9
912 0 my($self,$date,$time,$subtract) = @_;
913 0 my @date = @{ $self->split('date',$date) };
914 0 ▶ return '' if (! @date);
Branch: 1 branch
BranchTrueFalseTotal
unless @date 0 0 0
915 0 my @time = @{ $self->split('time',$time) };
916  
917 0 my @date2 = @{ $self->calc_date_time(\@date,\@time,$subtract) };
918  
919 0 return $self->join('date',\@date2);
920 }
921  
922 2425 ▶sub _calc_date_ymwd {
Subroutine _calc_date_ymwd: called CC=6 SCAR=17.9
923 2425 my($self,$date,$ymwd,$subtract) = @_;
924 2425 my($y,$m,$d,$h,$mn,$s) = @$date;
925 2425 my($dy,$dm,$dw,$dd) = @$ymwd;
926 2425 ▶ my($ymdonly) = (defined $h ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $h ? : 2 2423 2425
927  
928 2425 $dd += $dw*7;
929  
930 2425 ▶ if ($subtract) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtract) { } 1207 1218 2425
931 1207 $y -= $dy;
932 1207 $self->_mod_add(-12,-1*$dm,\$m,\$y);
933 1207 $dd *= -1;
934  
935 } else {
936 1218 $y += $dy;
937 1218 $self->_mod_add(-12,$dm,\$m,\$y);
938 }
939  
940 2425 my $dim = $self->days_in_month($y,$m);
941 2425 ▶ $d = $dim if ($d > $dim);
Branch: 1 branch
BranchTrueFalseTotal
if $d > $dim 1 2424 2425
942  
943 2425 my $ymd;
944 2425 ▶ if ($dd == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dd == 0) { } 2242 183 2425
945 2242 $ymd = [$y,$m,$d];
946 } else {
947 183 $ymd = $self->calc_date_days([$y,$m,$d],$dd);
948 }
949  
950 2425 ▶ if ($ymdonly) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ymdonly) { } 2423 2 2425
951 2423 return $ymd;
952 } else {
953 2 return [@$ymd,$h,$mn,$s];
954 }
955 }
956  
957 24 ▶sub _calc_hms_hms {
Subroutine _calc_hms_hms: called CC=1 SCAR=0.0
958 24 my($self,$hms0,$hms1) = @_;
959 24 my($h0,$m0,$s0,$h1,$m1,$s1) = (@$hms0,@$hms1);
960  
961 24 my($s) = ($h1-$h0)*3600 + ($m1-$m0)*60 + $s1-$s0;
962 24 my($m) = int($s/60);
963 24 $s -= $m*60;
964 24 my($h) = int($m/60);
965 24 $m -= $h*60;
966 24 return [$h,$m,$s];
967 }
968  
969 86 ▶sub calc_time_time {
Subroutine calc_time_time: called CC=2 SCAR=6.9
Pod documented
970 86 my($self,$time0,$time1,$subtract) = @_;
971 86 my($h0,$m0,$s0,$h1,$m1,$s1) = (@$time0,@$time1);
972  
973 86 ▶ if ($subtract) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtract) 51 35 86
974 51 $h1 *= -1;
975 51 $m1 *= -1;
976 51 $s1 *= -1;
977 }
978 86 my($s) = (($h0+$h1)*60 + ($m0+$m1))*60 + $s0+$s1;
979 86 my($m) = int($s/60);
980 86 $s -= $m*60;
981 86 my($h) = int($m/60);
982 86 $m -= $h*60;
983  
984 86 return [$h,$m,$s];
985 }
986  
987 ###############################################################################
988  
989 # Returns -1 if date0 is before date1, 0 if date0 is the same as date1, and
990 # 1 if date0 is after date1.
991 #
992 7427 ▶sub cmp {
Subroutine cmp: called CC=6 SCAR=17.9
Pod documented
993 7427 my($self,$date0,$date1) = @_;
994 7427 ▶ return ($$date0[0] <=> $$date1[0] ||
Condition93%14/15
$date0->[0] <=> $date1->[0] || $date0->[1] <=> $date1->[1] || $date0->[2] <=> $date1->[2] || $date0->[3] <=> $date1->[3] || $date0->[4] <=> $date1->[4] || $date0->[5] <=> $date1->[5]
l!l&&r!l&&!r
71040323
$date0->[0] <=> $date1->[0] or $date0->[1] <=> $date1->[1] or $date0->[2] <=> $date1->[2] or $date0->[3] <=> $date1->[3] or $date0->[4] <=> $date1->[4]
l!l&&r!l&&!r
704856323
$date0->[0] <=> $date1->[0] or $date0->[1] <=> $date1->[1] or $date0->[2] <=> $date1->[2] or $date0->[3] <=> $date1->[3]
l!l&&r!l&&!r
6788260379
$date0->[0] <=> $date1->[0] or $date0->[1] <=> $date1->[1] or $date0->[2] <=> $date1->[2]
l!l&&r!l&&!r
57111077639
$date0->[0] <=> $date1->[0] or $date0->[1] <=> $date1->[1]
l!l&&r!l&&!r
328424271716
MC/DC83%5/6
$date0->[0] <=> $date1->[0] || $date0->[1] <=> $date1->[1] || $date0->[2] <=> $date1->[2] || $date0->[3] <=> $date1->[3] || $date0->[4] <=> $date1->[4] || $date0->[5] <=> $date1->[5]
$date0->[0] <=> $date1->[0] $date0->[1] <=> $date1->[1] $date0->[2] <=> $date1->[2] $date0->[3] <=> $date1->[3] $date0->[4] <=> $date1->[4] $date0->[5] <=> $date1->[5]
Truth table85%6/7
A or B or C or D or E || F
A= $date0->[0] <=> $date1->[0]B= $date0->[1] <=> $date1->[1]C= $date0->[2] <=> $date1->[2]D= $date0->[3] <=> $date1->[3]E= $date0->[4] <=> $date1->[4]F= $date0->[5] <=> $date1->[5]
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
995 $$date0[1] <=> $$date1[1] ||
996 $$date0[2] <=> $$date1[2] ||
997 $$date0[3] <=> $$date1[3] ||
998 $$date0[4] <=> $$date1[4] ||
999 $$date0[5] <=> $$date1[5]);
1000 }
1001  
1002 ###############################################################################
1003 # This determines the OS.
1004  
1005 1027 ▶sub _os {
Subroutine _os: called CC=9 SCAR=31.2
1006 1027 my($self) = @_;
1007  
1008 1027 my $os = '';
1009  
1010 1027 ▶ if ($^O =~ /MSWin32/io ||
Branch: 3 branches
BranchTrueFalseTotal
if ($^O =~ /MSWin32/io or $^O =~ /Windows_95/io or $^O =~ /Windows_NT/io) { } 0 1027 1027
elsif ($^O =~ /MacOS/io or $^O =~ /MPE/io or $^O =~ /OS2/io or $^O =~ /NetWare/io) { } 0 1027 1027
elsif ($^O =~ /VMS/io) { } 0 1027 1027
Condition33%2/6
$^O =~ /MSWin32/io or $^O =~ /Windows_95/io or $^O =~ /Windows_NT/io
l!l&&r!l&&!r
001027
$^O =~ /MSWin32/io or $^O =~ /Windows_95/io
l!l&&r!l&&!r
001027
MC/DC0%0/3
$^O =~ /MSWin32/io or $^O =~ /Windows_95/io or $^O =~ /Windows_NT/io
$^O =~ /MSWin32/io $^O =~ /Windows_95/io $^O =~ /Windows_NT/io
Truth table25%1/4
A or B or C
A= $^O =~ /MSWin32/ioB= $^O =~ /Windows_95/ioC= $^O =~ /Windows_NT/io
ABCresult
1XX1
01X1
0011
0000
1011 $^O =~ /Windows_95/io ||
1012 $^O =~ /Windows_NT/io
1013 ) {
1014 0 $os = 'Windows';
1015  
1016 ▶ } elsif ($^O =~ /MacOS/io ||
Condition33%3/9
$^O =~ /MacOS/io or $^O =~ /MPE/io or $^O =~ /OS2/io or $^O =~ /NetWare/io
l!l&&r!l&&!r
001027
$^O =~ /MacOS/io or $^O =~ /MPE/io or $^O =~ /OS2/io
l!l&&r!l&&!r
001027
$^O =~ /MacOS/io or $^O =~ /MPE/io
l!l&&r!l&&!r
001027
MC/DC0%0/4
$^O =~ /MacOS/io or $^O =~ /MPE/io or $^O =~ /OS2/io or $^O =~ /NetWare/io
$^O =~ /MacOS/io $^O =~ /MPE/io $^O =~ /OS2/io $^O =~ /NetWare/io
Truth table20%1/5
A or B or C or D
A= $^O =~ /MacOS/ioB= $^O =~ /MPE/ioC= $^O =~ /OS2/ioD= $^O =~ /NetWare/io
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1017 $^O =~ /MPE/io ||
1018 $^O =~ /OS2/io ||
1019 $^O =~ /NetWare/io
1020 ) {
1021 0 $os = 'Other';
1022  
1023 } elsif ($^O =~ /VMS/io) {
1024 0 $os = 'VMS';
1025  
1026 } else {
1027 1027 $os = 'Unix';
1028 }
1029  
1030 1027 return $os;
1031 }
1032  
1033 ###############################################################################
1034 # Config variable functions
1035  
1036 # $self->config(SECT);
1037 # Creates a new section (if it doesn't already exist).
1038 #
1039 # $self->config(SECT,'_vars');
1040 # Returns a list of (VAR VAL VAR VAL ...)
1041 #
1042 # $self->config(SECT,VAR,VAL);
1043 # Adds (VAR,VAL) to the list.
1044 #
1045 253 ▶sub _section {
Subroutine _section: called CC=5 SCAR=22.0
1046 253 my($self,$sect,$var,$val) = @_;
1047 253 $sect = lc($sect);
1048  
1049 #
1050 # $self->_section(SECT) creates a new section
1051 #
1052  
1053 253 ▶ if (! defined $var &&
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $var or exists $self->{"data"}{"sections"}{$sect}) 0 253 253
Condition33%1/3
defined $var or exists $self->{"data"}{"sections"}{$sect}
l!l&&r!l&&!r
25300
MC/DC0%0/2
defined $var or exists $self->{"data"}{"sections"}{$sect}
defined $var exists $self->{"data"}{"sections"}{$sect}
Truth table33%1/3
A or B
A= defined $varB= exists $self->{"data"}{"sections"}{$sect}
ABresult
1X1
011
000
1054 ! exists $$self{'data'}{'sections'}{$sect}) {
1055 0 ▶ if ($sect eq 'conf') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sect eq 'conf') { } 0 0 0
1056 0 $$self{'data'}{'sections'}{$sect} = {};
1057 } else {
1058 0 $$self{'data'}{'sections'}{$sect} = [];
1059 }
1060 0 return '';
1061 }
1062  
1063 253 ▶ if ($var eq '_vars') {
Branch: 1 branch
BranchTrueFalseTotal
if ($var eq "_vars") 0 253 253
1064 0 return @{ $$self{'data'}{'sections'}{$sect} };
1065 }
1066  
1067 253 push @{ $$self{'data'}{'sections'}{$sect} },($var,$val);
1068 253 return;
1069 }
1070  
1071 # This sets a config variable. It also performs all side effects from
1072 # setting that variable.
1073 #
1074 8239 ▶sub _config_var_base {
Subroutine _config_var_base: called CC=47 SCAR=45.9
1075 8239 my($self,$var,$val) = @_;
1076  
1077 8239 ▶ if ($var eq 'defaults') {
Branch: 21 branches
BranchTrueFalseTotal
if ($var eq 'defaults') { } 1 8238 8239
elsif ($var eq 'eraseholidays') { } 18 8220 8238
elsif ($var eq 'eraseevents') { } 14 8206 8220
elsif ($var eq 'configfile') { } 0 8206 8206
elsif ($var eq 'encoding') { } 0 8206 8206
elsif ($var eq 'language') { } 542 7664 8206
elsif ($var eq 'yytoyyyy') { } 536 7128 7664
elsif ($var eq 'workweekbeg') { } 523 6605 7128
elsif ($var eq 'workweekend') { } 523 6082 6605
elsif ($var eq 'workday24hr') { } 1032 5050 6082
elsif ($var eq 'workdaybeg') { } 529 4521 5050
elsif ($var eq 'workdayend') { } 527 3994 4521
elsif ($var eq 'firstday') { } 702 3292 3994
elsif ($var eq 'week1ofyear') { } 645 2647 3292
elsif ($var eq 'tz' or $var eq 'forcedate' or $var eq 'setdate') { } 0 2647 2647
elsif ($var eq 'recurrange') { } 523 2124 2647
elsif ($var eq 'defaulttime') { } 525 1599 2124
elsif ($var eq 'periodtimesep') { } 1 1598 1599
elsif ($var eq 'format_mmmyyyy') { } 4 1594 1598
elsif ($var eq 'dateformat' or $var eq 'printable' or $var eq 'tomorrowfirst' or $var eq 'use_posix_printf' or $var eq 'maxrecurattempts' or $var eq 'max_parse_length') { } 1594 0 1594
elsif ($var eq 'jan1week1') { } 0 0 0
1078 # Reset the configuration if desired.
1079 1 $self->_init_config(1);
1080 1 return;
1081  
1082 } elsif ($var eq 'eraseholidays') {
1083 18 $self->_init_holidays(1);
1084 18 return;
1085  
1086 } elsif ($var eq 'eraseevents') {
1087 14 $self->_init_events(1);
1088 14 return;
1089  
1090 } elsif ($var eq 'configfile') {
1091 0 $self->_config_file($val);
1092 0 return;
1093  
1094 } elsif ($var eq 'encoding') {
1095 0 my $err = $self->_config_var_encoding($val);
1096 0 ▶ return if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
1097  
1098 } elsif ($var eq 'language') {
1099 542 my $err = $self->_language($val);
1100 542 ▶ return if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 542 542
1101 542 $err = $self->_config_var_encoding();
1102 542 ▶ return if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 542 542
1103  
1104 } elsif ($var eq 'yytoyyyy') {
1105 536 $val = lc($val);
1106 536 ▶ if ($val ne 'c' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($val ne "c" and not $val =~ /^c\d\d$/o and not $val =~ /^c\d\d\d\d$/o and not $val =~ /^\d+$/o) 0 536 536
Condition88%8/9
$val ne "c" and not $val =~ /^c\d\d$/o and not $val =~ /^c\d\d\d\d$/o and not $val =~ /^\d+$/o
!ll&&!rl&&r
125240
$val ne "c" and not $val =~ /^c\d\d$/o and not $val =~ /^c\d\d\d\d$/o
!ll&&!rl&&r
111524
$val ne "c" and not $val =~ /^c\d\d$/o
!ll&&!rl&&r
110525
MC/DC0%0/4
$val ne "c" and not $val =~ /^c\d\d$/o and not $val =~ /^c\d\d\d\d$/o and not $val =~ /^\d+$/o
$val ne "c" not $val =~ /^c\d\d$/o not $val =~ /^c\d\d\d\d$/o not $val =~ /^\d+$/o
Truth table80%4/5
A and not(B) and not(C) and not(D)
A= $val ne "c"B= not $val =~ /^c\d\d$/oC= not $val =~ /^c\d\d\d\d$/oD= not $val =~ /^\d+$/o
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1107 $val !~ /^c\d\d$/o &&
1108 $val !~ /^c\d\d\d\d$/o &&
1109 $val !~ /^\d+$/o) {
1110 0 carp "ERROR: [config_var] invalid: YYtoYYYY: $val";
1111 0 return;
1112 }
1113  
1114 } elsif ($var eq 'workweekbeg') {
1115 523 my $err = $self->_config_var_workweekbeg($val);
1116 523 ▶ return if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 523 523
1117  
1118 } elsif ($var eq 'workweekend') {
1119 523 my $err = $self->_config_var_workweekend($val);
1120 523 ▶ return if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 523 523
1121  
1122 } elsif ($var eq 'workday24hr') {
1123 1032 my $err = $self->_config_var_workday24hr($val);
1124 1032 ▶ return if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 1032 1032
1125  
1126 } elsif ($var eq 'workdaybeg') {
1127 529 my $err = $self->_config_var_workdaybegend(\$val,'WorkDayBeg');
1128 529 ▶ return if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 529 529
1129  
1130 } elsif ($var eq 'workdayend') {
1131 527 my $err = $self->_config_var_workdaybegend(\$val,'WorkDayEnd');
1132 527 ▶ return if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 527 527
1133  
1134 } elsif ($var eq 'firstday') {
1135 702 my $err = $self->_config_var_firstday($val);
1136 702 ▶ return if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 702 702
1137  
1138 } elsif ($var eq 'week1ofyear') {
1139 645 my $err = $self->_config_var_week1ofyear($val);
1140 645 ▶ return if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 645 645
1141  
1142 ▶ } elsif ($var eq 'tz' ||
Condition33%2/6
$var eq "tz" or $var eq "forcedate" or $var eq "setdate"
l!l&&r!l&&!r
002647
$var eq "tz" or $var eq "forcedate"
l!l&&r!l&&!r
002647
MC/DC0%0/3
$var eq "tz" or $var eq "forcedate" or $var eq "setdate"
$var eq "tz" $var eq "forcedate" $var eq "setdate"
Truth table25%1/4
A or B or C
A= $var eq "tz"B= $var eq "forcedate"C= $var eq "setdate"
ABCresult
1XX1
01X1
0011
0000
1143 $var eq 'forcedate' ||
1144 $var eq 'setdate') {
1145 # These can only be used if the Date::Manip::TZ module has been loaded
1146 0 carp "ERROR: [config_var] $var config variable requires TZ module";
1147 0 return;
1148  
1149 } elsif ($var eq 'recurrange') {
1150 523 my $err = $self->_config_var_recurrange($val);
1151 523 ▶ return if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 523 523
1152  
1153 } elsif ($var eq 'defaulttime') {
1154 525 my $err = $self->_config_var_defaulttime($val);
1155 525 ▶ return if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 525 525
1156  
1157 } elsif ($var eq 'periodtimesep') {
1158 # We have to redo the time regexp
1159 1 delete $$self{'data'}{'rx'}{'time'};
1160  
1161 } elsif ($var eq 'format_mmmyyyy') {
1162 4 my $err = $self->_config_var_format_mmmyyyy($val);
1163 4 ▶ return if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 4 4
1164  
1165 ▶ } elsif ($var eq 'dateformat' ||
Condition93%14/15
$var eq "dateformat" or $var eq "printable" or $var eq "tomorrowfirst" or $var eq "use_posix_printf" or $var eq "maxrecurattempts" or $var eq "max_parse_length"
l!l&&r!l&&!r
159310
$var eq "dateformat" or $var eq "printable" or $var eq "tomorrowfirst" or $var eq "use_posix_printf" or $var eq "maxrecurattempts"
l!l&&r!l&&!r
10845091
$var eq "dateformat" or $var eq "printable" or $var eq "tomorrowfirst" or $var eq "use_posix_printf"
l!l&&r!l&&!r
10813510
$var eq "dateformat" or $var eq "printable" or $var eq "tomorrowfirst"
l!l&&r!l&&!r
106714513
$var eq "dateformat" or $var eq "printable"
l!l&&r!l&&!r
542525527
MC/DC0%0/6
$var eq "dateformat" or $var eq "printable" or $var eq "tomorrowfirst" or $var eq "use_posix_printf" or $var eq "maxrecurattempts" or $var eq "max_parse_length"
$var eq "dateformat" $var eq "printable" $var eq "tomorrowfirst" $var eq "use_posix_printf" $var eq "maxrecurattempts" $var eq "max_parse_length"
Truth table85%6/7
A or B or C or D or E or F
A= $var eq "dateformat"B= $var eq "printable"C= $var eq "tomorrowfirst"D= $var eq "use_posix_printf"E= $var eq "maxrecurattempts"F= $var eq "max_parse_length"
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
1166 $var eq 'printable' ||
1167 $var eq 'tomorrowfirst' ||
1168 $var eq 'use_posix_printf' ||
1169 $var eq 'maxrecurattempts' ||
1170 $var eq 'max_parse_length'
1171 ) {
1172 # do nothing
1173  
1174 } elsif ($var eq 'jan1week1') {
1175 0 carp "WARNING: the jan1week1 Date::Manip config variable is deprecated\n" .
1176 " and will be removed in version 7.00. Please use\n" .
1177 " the Week1ofYear config variable instead.\n";
1178 0 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) { } 0 0 0
1179 0 $self->_config_var_base('week1ofyear','jan1');
1180 } else {
1181 0 $self->_config_var_base('week1ofyear','jan4');
1182 }
1183 0 return;
1184  
1185 } else {
1186 0 carp "ERROR: [config_var] invalid config variable: $var";
1187 0 return '';
1188 }
1189  
1190 8206 $$self{'data'}{'sections'}{'conf'}{$var} = $val;
1191 8206 return;
1192 }
1193  
1194 ###############################################################################
1195 # Specific config variable functions
1196  
1197 542 ▶sub _config_var_encoding {
Subroutine _config_var_encoding: called CC=13 SCAR=46.2
1198 542 my($self,$val) = @_;
1199  
1200 542 ▶ if (! $val) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $val) { } 542 0 542
elsif ($val =~ /^(.*),(.*)$/o) { } 0 0 0
1201 542 $$self{'data'}{'calc'}{'enc_in'} = [ @{ $$self{'data'}{'enc'} } ];
1202 542 $$self{'data'}{'calc'}{'enc_out'} = 'UTF-8';
1203  
1204 } elsif ($val =~ /^(.*),(.*)$/o) {
1205 0 my($in,$out) = ($1,$2);
1206 0 ▶ if ($in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in) 0 0 0
1207 0 my $o = find_encoding($in);
1208 0 ▶ if (! $o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($o) 0 0 0
1209 0 carp "ERROR: [config_var] invalid: Encoding: $in";
1210 0 return 1;
1211 }
1212 }
1213 0 ▶ if ($out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out) 0 0 0
1214 0 my $o = find_encoding($out);
1215 0 ▶ if (! $o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($o) 0 0 0
1216 0 carp "ERROR: [config_var] invalid: Encoding: $out";
1217 0 return 1;
1218 }
1219 }
1220  
1221 0 ▶ if ($in && $out) {
Branch: 3 branches
BranchTrueFalseTotal
if ($in and $out) { } 0 0 0
elsif ($in) { } 0 0 0
elsif ($out) { } 0 0 0
Condition0%0/3
$in and $out
!ll&&!rl&&r
000
MC/DC0%0/2
$in and $out
$in $out
Truth table0%0/3
A and B
A= $inB= $out
ABresult
0X0
100
111
1222 0 $$self{'data'}{'calc'}{'enc_in'} = [ $in ];
1223 0 $$self{'data'}{'calc'}{'enc_out'} = $out;
1224  
1225 } elsif ($in) {
1226 0 $$self{'data'}{'calc'}{'enc_in'} = [ $in ];
1227 0 $$self{'data'}{'calc'}{'enc_out'} = 'UTF-8';
1228  
1229 } elsif ($out) {
1230 0 $$self{'data'}{'calc'}{'enc_in'} = [ @{ $$self{'data'}{'enc'} } ];
1231 0 $$self{'data'}{'calc'}{'enc_out'} = $out;
1232  
1233 } else {
1234 0 $$self{'data'}{'calc'}{'enc_in'} = [ @{ $$self{'data'}{'enc'} } ];
1235 0 $$self{'data'}{'calc'}{'enc_out'} = 'UTF-8';
1236 }
1237  
1238 } else {
1239 0 my $o = find_encoding($val);
1240 0 ▶ if (! $o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($o) 0 0 0
1241 0 carp "ERROR: [config_var] invalid: Encoding: $val";
1242 0 return 1;
1243 }
1244 0 $$self{'data'}{'calc'}{'enc_in'} = [ $val ];
1245 0 $$self{'data'}{'calc'}{'enc_out'} = $val;
1246 }
1247  
1248 542 ▶ if (! @{ $$self{'data'}{'calc'}{'enc_in'} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"data"}{"calc"}{"enc_in"};}) 525 17 542
1249 525 $$self{'data'}{'calc'}{'enc_in'} = [ qw(utf-8 perl) ];
1250 }
1251  
1252 542 return 0;
1253 }
1254  
1255 523 ▶sub _config_var_recurrange {
Subroutine _config_var_recurrange: called CC=2 SCAR=7.9
1256 523 my($self,$val) = @_;
1257  
1258 523 $val = lc($val);
1259 523 ▶ if ($val =~ /^(none|year|month|week|day|all)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^(none|year|month|week|day|all)$/o) 523 0 523
1260 523 return 0;
1261 }
1262  
1263 0 carp "ERROR: [config_var] invalid: RecurRange: $val";
1264 0 return 1;
1265 }
1266  
1267 523 ▶sub _config_var_workweekbeg {
Subroutine _config_var_workweekbeg: called CC=3 SCAR=13.1
1268 523 my($self,$val) = @_;
1269  
1270 523 ▶ if (! $self->_is_int($val,1,7)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_int($val, 1, 7)) 0 523 523
1271 0 carp "ERROR: [config_var] invalid: WorkWeekBeg: $val";
1272 0 return 1;
1273 }
1274 523 ▶ if ($val >= $self->_config('workweekend')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val >= $self->_config("workweekend")) 0 523 523
1275 0 carp "ERROR: [config_var] WorkWeekBeg must be before WorkWeekEnd";
1276 0 return 1;
1277 }
1278  
1279 523 $self->_calc_workweek($val,'');
1280 523 $self->_init_business_length();
1281 523 return 0;
1282 }
1283  
1284 523 ▶sub _config_var_workweekend {
Subroutine _config_var_workweekend: called CC=3 SCAR=13.1
1285 523 my($self,$val) = @_;
1286  
1287 523 ▶ if (! $self->_is_int($val,1,7)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_int($val, 1, 7)) 0 523 523
1288 0 carp "ERROR: [config_var] invalid: WorkWeekBeg: $val";
1289 0 return 1;
1290 }
1291 523 ▶ if ($val <= $self->_config('workweekbeg')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val <= $self->_config("workweekbeg")) 0 523 523
1292 0 carp "ERROR: [config_var] WorkWeekEnd must be after WorkWeekBeg";
1293 0 return 1;
1294 }
1295  
1296 523 $self->_calc_workweek('',$val);
1297 523 $self->_init_business_length();
1298 523 return 0;
1299 }
1300  
1301 1032 ▶sub _config_var_workday24hr {
Subroutine _config_var_workday24hr: called CC=2 SCAR=6.9
1302 1032 my($self,$val) = @_;
1303  
1304 1032 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) 509 523 1032
1305 509 $$self{'data'}{'sections'}{'conf'}{'workdaybeg'} = '00:00:00';
1306 509 $$self{'data'}{'sections'}{'conf'}{'workdayend'} = '24:00:00';
1307 509 $$self{'data'}{'calc'}{'workdaybeg'} = [0,0,0];
1308 509 $$self{'data'}{'calc'}{'workdayend'} = [24,0,0];
1309  
1310 509 $self->_calc_bdlength();
1311 509 $self->_init_business_length();
1312 }
1313  
1314 1032 return 0;
1315 }
1316  
1317 1056 ▶sub _config_var_workdaybegend {
Subroutine _config_var_workdaybegend: called CC=3 SCAR=11.4
1318 1056 my($self,$val,$conf) = @_;
1319  
1320 # Must be a valid time. Entered as H, H:M, or H:M:S
1321  
1322 1056 my $tmp = $self->split('hms',$$val);
1323 1056 ▶ if (! defined $tmp) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp) 0 1056 1056
1324 0 carp "ERROR: [config_var] invalid: $conf: $$val";
1325 0 return 1;
1326 }
1327 1056 $$self{'data'}{'calc'}{lc($conf)} = $tmp;
1328 1056 $$val = $self->join('hms',$tmp);
1329  
1330 # workdaybeg < workdayend
1331  
1332 1056 my @beg = @{ $$self{'data'}{'calc'}{'workdaybeg'} };
1333 1056 my @end = @{ $$self{'data'}{'calc'}{'workdayend'} };
1334 1056 my $beg = $beg[0]*3600 + $beg[1]*60 + $beg[2];
1335 1056 my $end = $end[0]*3600 + $end[1]*60 + $end[2];
1336  
1337 1056 ▶ if ($beg > $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($beg > $end) 0 1056 1056
1338 0 carp "ERROR: [config_var] WorkDayBeg not before WorkDayEnd";
1339 0 return 1;
1340 }
1341  
1342 # Calculate bdlength
1343  
1344 1056 $$self{'data'}{'sections'}{'conf'}{'workday24hr'} = 0;
1345  
1346 1056 $self->_calc_bdlength();
1347 1056 $self->_init_business_length();
1348  
1349 1056 return 0;
1350 }
1351  
1352 702 ▶sub _config_var_firstday {
Subroutine _config_var_firstday: called CC=2 SCAR=8.4
1353 702 my($self,$val) = @_;
1354  
1355 702 ▶ if (! $self->_is_int($val,1,7)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_int($val, 1, 7)) 0 702 702
1356 0 carp "ERROR: [config_var] invalid: FirstDay: $val";
1357 0 return 1;
1358 }
1359  
1360 702 return 0;
1361 }
1362  
1363 645 ▶sub _config_var_week1ofyear {
Subroutine _config_var_week1ofyear: called CC=4 SCAR=14.8
1364 645 my($self,$val) = @_;
1365 645 $val = lc($val);
1366  
1367 645 ▶ if ($val =~ /^jan[1-7]$/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^jan[1-7]$/ or $val =~ /^dow[1-7]$/ or $val eq "firstday") 645 0 645
Condition83%5/6
$val =~ /^jan[1-7]$/ or $val =~ /^dow[1-7]$/ or $val eq "firstday"
l!l&&r!l&&!r
63870
$val =~ /^jan[1-7]$/ or $val =~ /^dow[1-7]$/
l!l&&r!l&&!r
589497
MC/DC0%0/3
$val =~ /^jan[1-7]$/ or $val =~ /^dow[1-7]$/ or $val eq "firstday"
$val =~ /^jan[1-7]$/ $val =~ /^dow[1-7]$/ $val eq "firstday"
Truth table75%3/4
A or B or C
A= $val =~ /^jan[1-7]$/B= $val =~ /^dow[1-7]$/C= $val eq "firstday"
ABCresult
1XX1
01X1
0011
0000
1368 $val =~ /^dow[1-7]$/ ||
1369 $val eq 'firstday') {
1370 645 return 0;
1371 }
1372 0 carp "ERROR: [config_var] invalid: Week1ofYear: $val";
1373 0 return 1;
1374 }
1375  
1376 525 ▶sub _config_var_defaulttime {
Subroutine _config_var_defaulttime: called CC=3 SCAR=12.7
1377 525 my($self,$val) = @_;
1378  
1379 525 ▶ if (lc($val) eq 'midnight' ||
Branch: 1 branch
BranchTrueFalseTotal
if (lc $val eq "midnight" or lc $val eq "curr") 525 0 525
Condition66%2/3
lc $val eq "midnight" or lc $val eq "curr"
l!l&&r!l&&!r
52320
MC/DC0%0/2
lc $val eq "midnight" or lc $val eq "curr"
lc $val eq "midnight" lc $val eq "curr"
Truth table66%2/3
A or B
A= lc $val eq "midnight"B= lc $val eq "curr"
ABresult
1X1
011
000
1380 lc($val) eq 'curr') {
1381 525 return 0;
1382 }
1383 0 carp "ERROR: [config_var] invalid: DefaultTime: $val";
1384 0 return 1;
1385 }
1386  
1387 4 ▶sub _config_var_format_mmmyyyy {
Subroutine _config_var_format_mmmyyyy: called CC=4 SCAR=17.2
1388 4 my($self,$val) = @_;
1389  
1390 4 ▶ if (lc($val) eq 'first' ||
Branch: 1 branch
BranchTrueFalseTotal
if (lc $val eq "first" or lc $val eq "last" or lc $val eq "") 4 0 4
Condition50%3/6
lc $val eq "first" or lc $val eq "last" or lc $val eq ""
l!l&&r!l&&!r
400
lc $val eq "first" or lc $val eq "last"
l!l&&r!l&&!r
220
MC/DC0%0/3
lc $val eq "first" or lc $val eq "last" or lc $val eq ""
lc $val eq "first" lc $val eq "last" lc $val eq ""
Truth table50%2/4
A or B or C
A= lc $val eq "first"B= lc $val eq "last"C= lc $val eq ""
ABCresult
1XX1
01X1
0011
0000
1391 lc($val) eq 'last' ||
1392 lc($val) eq '') {
1393 4 return 0;
1394 }
1395 0 carp "ERROR: [config_var] invalid: Format_MMMYYYY: $val";
1396 0 return 1;
1397 }
1398  
1399 ###############################################################################
1400 # Language functions
1401  
1402 # This reads in a langauge module and sets regular expressions
1403 # and word lists based on it.
1404 #
1405 171 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1406 542 ▶sub _language {
Subroutine _language: called CC=5 SCAR=16.3
1407 542 my($self,$lang) = @_;
1408 542 $lang = lc($lang);
1409  
1410 542 ▶ if (! exists $Date::Manip::Lang::index::Lang{$lang}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Date::Manip::Lang::index::Lang{$lang}) 0 542 542
1411 0 carp "ERROR: [language] invalid: $lang";
1412 0 return 1;
1413 }
1414  
1415 return 0 if (exists $$self{'data'}{'sections'}{'conf'} &&
1416 542 ▶ $$self{'data'}{'sections'}{'conf'} eq $lang);
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"data"}{"sections"}{"conf"} and $self->{"data"}{"sections"}{"conf"} eq $lang 0 542 542
Condition33%1/3
exists $self->{"data"}{"sections"}{"conf"} and $self->{"data"}{"sections"}{"conf"} eq $lang
!ll&&!rl&&r
05420
MC/DC0%0/2
exists $self->{"data"}{"sections"}{"conf"} and $self->{"data"}{"sections"}{"conf"} eq $lang
exists $self->{"data"}{"sections"}{"conf"} $self->{"data"}{"sections"}{"conf"} eq $lang
Truth table33%1/3
A and B
A= exists $self->{"data"}{"sections"}{"conf"}B= $self->{"data"}{"sections"}{"conf"} eq $lang
ABresult
0X0
100
111
1417 542 $self->_init_language(1);
1418  
1419 542 my $mod = $Date::Manip::Lang::index::Lang{$lang};
1420 542 eval "require Date::Manip::Lang::${mod}";
1421 542 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 542 542
1422 0 croak "ERROR: failed to load Date::Manip::Lang::${mod}: $@\n";
1423 }
1424  
1425 171 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
1426 542 $$self{'data'}{'lang'} = ${ "Date::Manip::Lang::${mod}::Language" };
1427 542 $$self{'data'}{'enc'} = [ @{ "Date::Manip::Lang::${mod}::Encodings" } ];
1428  
1429 # Common words
1430 542 $self->_rx_wordlist('at');
1431 542 $self->_rx_wordlist('each');
1432 542 $self->_rx_wordlist('last');
1433 542 $self->_rx_wordlist('of');
1434 542 $self->_rx_wordlist('on');
1435 542 $self->_rx_wordlists('when');
1436  
1437 # Next/prev
1438 542 $self->_rx_wordlists('nextprev');
1439  
1440 # Field names (years, year, yr, ...)
1441 542 $self->_rx_wordlists('fields');
1442  
1443 # Numbers (first, 1st)
1444 542 $self->_rx_wordlists('nth');
1445 542 $self->_rx_wordlists('nth','nth_dom',31); # 1-31
1446 542 $self->_rx_wordlists('nth','nth_wom',5); # 1-5
1447  
1448 # Calendar names (Mon, Tue and Jan, Feb)
1449 542 $self->_rx_wordlists('day_abb');
1450 542 $self->_rx_wordlists('day_char');
1451 542 $self->_rx_wordlists('day_name');
1452 542 $self->_rx_wordlists('month_abb');
1453 542 $self->_rx_wordlists('month_name');
1454  
1455 # H:M:S separators
1456 542 $self->_rx_simple('sephm');
1457 542 $self->_rx_simple('sepms');
1458 542 $self->_rx_simple('sepfr');
1459  
1460 # Time replacement strings
1461 542 $self->_rx_replace('times');
1462  
1463 # Some offset strings
1464 542 $self->_rx_replace('offset_date');
1465 542 $self->_rx_replace('offset_time');
1466  
1467 # AM/PM strings
1468 542 $self->_rx_wordlists('ampm');
1469  
1470 # Business/non-business mode
1471 542 $self->_rx_wordlists('mode');
1472  
1473 542 return 0;
1474 }
1475 171 ▶use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1476  
1477 # This takes a string or strings from the language file which is a
1478 # regular expression and copies it to the regular expression cache.
1479 #
1480 # If the language file contains a list of strings, a list of strings
1481 # is stored in the regexp cache.
1482 #
1483 1626 ▶sub _rx_simple {
Subroutine _rx_simple: called CC=3 SCAR=11.0
1484 1626 my($self,$ele) = @_;
1485  
1486 1626 ▶ if (exists $$self{'data'}{'lang'}{$ele}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'data'}{'lang'}{$ele}) { } 19 1607 1626
1487 19 ▶ if (ref($$self{'data'}{'lang'}{$ele})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'data'}{'lang'}{$ele}) { } 16 3 19
1488 16 @{ $$self{'data'}{'rx'}{$ele} } = @{ $$self{'data'}{'lang'}{$ele} };
1489 } else {
1490 3 $$self{'data'}{'rx'}{$ele} = $$self{'data'}{'lang'}{$ele};
1491 }
1492 } else {
1493 1607 $$self{'data'}{'rx'}{$ele} = undef;
1494 }
1495  
1496 1626 return;
1497 }
1498  
1499 # We need to quote strings that will be used in regexps, but we don't
1500 # want to quote UTF-8 characters.
1501 #
1502 227362 ▶sub _qe_quote {
Subroutine _qe_quote: called CC=1 SCAR=0.0
1503 227362 my($string) = @_;
1504 227362 $string =~ s/([-.+*?])/\\$1/g;
1505 227362 return $string;
1506 }
1507  
1508 # This takes a list of words and creates a simple regexp which matches
1509 # any of them.
1510 #
1511 # The first word in the list is the default way to express the word using
1512 # a normal ASCII character set.
1513 #
1514 # The second word in the list is the default way to express the word using
1515 # a locale character set. If it isn't defined, it defaults to the first word.
1516 #
1517 2710 ▶sub _rx_wordlist {
Subroutine _rx_wordlist: called CC=4 SCAR=13.9
1518 2710 my($self,$ele) = @_;
1519  
1520 2710 ▶ if (exists $$self{'data'}{'lang'}{$ele}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'data'}{'lang'}{$ele}) { } 2710 0 2710
1521 2710 my @tmp = @{ $$self{'data'}{'lang'}{$ele} };
1522  
1523 2710 $$self{'data'}{'wordlist'}{$ele} = $tmp[0];
1524  
1525 2710 my @tmp2;
1526 2710 foreach my $tmp (@tmp) {
1527 4343 ▶ push(@tmp2,_qe_quote($tmp)) if ($tmp);
Branch: 1 branch
BranchTrueFalseTotal
if $tmp 4340 3 4343
1528 }
1529 2710 @tmp2 = sort _sortByLength(@tmp2);
1530  
1531 2710 $$self{'data'}{'rx'}{$ele} = join('|',@tmp2);
1532  
1533 } else {
1534 0 $$self{'data'}{'rx'}{$ele} = undef;
1535 }
1536  
1537 2710 return;
1538 }
1539  
1540 171 ▶no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
1541 1158474 ▶sub _sortByLength {
Subroutine _sortByLength: called CC=1 SCAR=0.0
1542 1158474 return (length $b <=> length $a);
1543 }
1544 171 ▶use strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
1545  
1546 # This takes a hash of the form:
1547 # word => string
1548 # and creates a regular expression to match word (which must be surrounded
1549 # by word boundaries).
1550 #
1551 1626 ▶sub _rx_replace {
Subroutine _rx_replace: called CC=3 SCAR=11.1
1552 1626 my($self,$ele) = @_;
1553  
1554 1626 ▶ if (! exists $$self{'data'}{'lang'}{$ele}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"data"}{"lang"}{$ele}) 0 1626 1626
1555 0 $$self{'data'}{'rx'}{$ele} = [];
1556 0 return;
1557 }
1558  
1559 1626 my(@key) = keys %{ $$self{'data'}{'lang'}{$ele} };
1560 1626 my $i = 1;
1561 1626 foreach my $key (sort(@key)) {
1562 4360 my $val = $$self{'data'}{'lang'}{$ele}{$key};
1563 4360 my $k = _qe_quote($key);
1564 4360 $$self{'data'}{'rx'}{$ele}[$i++] = qr/(?:^|\b)($k)(?:\b|$)/i;
1565 4360 $$self{'data'}{'wordmatch'}{$ele}{lc($key)} = $val;
1566 }
1567  
1568 1626 @key = sort _sortByLength(@key);
1569 1626 @key = map { _qe_quote($_) } @key;
1570 1626 my $rx = join('|',@key);
1571  
1572 1626 $$self{'data'}{'rx'}{$ele}[0] = qr/(?:^|\b)(?:$rx)(?:\b|$)/i;
1573  
1574 1626 return;
1575 }
1576  
1577 # This takes a list of values, each of which can be expressed in multiple
1578 # ways, and gets a regular expression which matches any of them, a default
1579 # way to express each value, and a hash which matches a matched string to
1580 # a value (the value is 1..N where N is the number of values).
1581 #
1582 7046 ▶sub _rx_wordlists {
Subroutine _rx_wordlists: called CC=8 SCAR=20.8
1583 7046 my($self,$ele,$subset,$max) = @_;
1584 7046 ▶ $subset = $ele if (! $subset);
Branch: 1 branch
BranchTrueFalseTotal
unless $subset 5962 1084 7046
1585  
1586 7046 ▶ if (exists $$self{'data'}{'lang'}{$ele}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'data'}{'lang'}{$ele}) { } 7046 0 7046
1587 7046 my @vallist = @{ $$self{'data'}{'lang'}{$ele} };
1588 7046 ▶ $max = $#vallist+1 if (! $max || $max > $#vallist+1);
Branch: 1 branch
BranchTrueFalseTotal
if not $max or $max > $#vallist + 1 5962 1084 7046
Condition66%2/3
not $max or $max > $#vallist + 1
l!l&&r!l&&!r
596201084
MC/DC50%1/2
not $max or $max > $#vallist + 1
not $max $max > $#vallist + 1
Truth table66%2/3
not(A) or B
A= not $maxB= $max > $#vallist + 1
ABresult
1X1
011
000
1589 7046 my (@all);
1590  
1591 7046 ▶ for (my $i=1; $i<=$max; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $max) 80758 7046 87804
1592 80758 my @tmp = @{ $$self{'data'}{'lang'}{$ele}[$i-1] };
1593 80758 $$self{'data'}{'wordlist'}{$subset}[$i-1] = $tmp[0];
1594  
1595 80758 my @str;
1596 80758 foreach my $str (@tmp) {
1597 214303 ▶ next if (! $str);
Branch: 1 branch
BranchTrueFalseTotal
unless $str 1 214302 214303
1598 214302 $$self{'data'}{'wordmatch'}{$subset}{lc($str)} = $i;
1599 214302 push(@str,_qe_quote($str));
1600 }
1601 80758 push(@all,@str);
1602  
1603 80758 @str = sort _sortByLength(@str);
1604 80758 $$self{'data'}{'rx'}{$subset}[$i] = join('|',@str);
1605 }
1606  
1607 7046 @all = sort _sortByLength(@all);
1608 7046 $$self{'data'}{'rx'}{$subset}[0] = join('|',@all);
1609  
1610 } else {
1611 0 $$self{'data'}{'rx'}{$subset} = undef;
1612 }
1613  
1614 7046 return;
1615 }
1616  
1617 ###############################################################################
1618 # Year functions
1619 #
1620 # $self->_method(METHOD) use METHOD as the method for YY->YYYY
1621 # conversions
1622 #
1623 # YEAR = _fix_year(YR) converts a 2-digit to 4-digit year
1624 # _fix_year is in TZ_Base
1625  
1626 4 ▶sub _method {
Subroutine _method: called CC=1 SCAR=0.0
1627 4 my($self,$method) = @_;
1628 4 $self->_config('yytoyyyy',$method);
1629  
1630 4 return;
1631 }
1632  
1633 ###############################################################################
1634 # $self->_mod_add($N,$add,\$val,\$rem);
1635 # This calculates $val=$val+$add and forces $val to be in a certain
1636 # range. This is useful for adding numbers for which only a certain
1637 # range is allowed (for example, minutes can be between 0 and 59 or
1638 # months can be between 1 and 12). The absolute value of $N determines
1639 # the range and the sign of $N determines whether the range is 0 to N-1
1640 # (if N>0) or 1 to N (N<0). $rem is adjusted to force $val into the
1641 # appropriate range.
1642 # Example:
1643 # To add 2 hours together (with the excess returned in days) use:
1644 # $self->_mod_add(-24,$h1,\$h,\$day);
1645 # To add 2 minutes together (with the excess returned in hours):
1646 # $self->_mod_add(60,$mn1,\$mn,\$hr);
1647 41078 ▶sub _mod_add {
Subroutine _mod_add: called CC=7 SCAR=19.5
1648 41078 my($self,$N,$add,$val,$rem)=@_;
1649 41078 ▶ return if ($N==0);
Branch: 1 branch
BranchTrueFalseTotal
if $N == 0 0 41078 41078
1650 41078 $$val+=$add;
1651 41078 ▶ if ($N<0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($N < 0) { } 2809 38269 41078
1652 # 1 to N
1653 2809 $N = -$N;
1654 2809 ▶ if ($$val>$N) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$val > $N) { } 65 2744 2809
elsif ($$val < 1) { } 96 2648 2744
1655 65 $$rem+= int(($$val-1)/$N);
1656 65 $$val = ($$val-1)%$N +1;
1657 } elsif ($$val<1) {
1658 96 $$rem-= int(-$$val/$N)+1;
1659 96 $$val = $N-(-$$val % $N);
1660 }
1661  
1662 } else {
1663 # 0 to N-1
1664 38269 ▶ if ($$val>($N-1)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$val > $N - 1) { } 212 38057 38269
elsif ($$val < 0) { } 159 37898 38057
1665 212 $$rem+= int($$val/$N);
1666 212 $$val = $$val%$N;
1667 } elsif ($$val<0) {
1668 159 $$rem-= int(-($$val+1)/$N)+1;
1669 159 $$val = ($N-1)-(-($$val+1)%$N);
1670 }
1671 }
1672  
1673 41078 return;
1674 }
1675  
1676 # $flag = $self->_is_int($string [,$low, $high]);
1677 # Returns 1 if $string is a valid integer, 0 otherwise. If $low is
1678 # entered, $string must be >= $low. If $high is entered, $string must
1679 # be <= $high. It is valid to check only one of the bounds.
1680 54293 ▶sub _is_int {
Subroutine _is_int: called CC=7 SCAR=19.7
1681 54293 my($self,$N,$low,$high)=@_;
1682 54293 ▶ return 0 if (! defined $N or
Branch: 1 branch
BranchTrueFalseTotal
if not defined $N && $N =~ /^\s*[-+]?\d+\s*$/o or defined $low and $N < $low or defined $high and $N > $high 6 54287 54293
Condition80%12/15
not defined $N && $N =~ /^\s*[-+]?\d+\s*$/o or defined $low and $N < $low or defined $high and $N > $high
l!l&&r!l&&!r
6054287
not defined $N && $N =~ /^\s*[-+]?\d+\s*$/o or defined $low and $N < $low
l!l&&r!l&&!r
5154287
defined $N && $N =~ /^\s*[-+]?\d+\s*$/o
!ll&&!rl&&r
0554288
defined $low and $N < $low
!ll&&!rl&&r
40013142741
defined $high and $N > $high
!ll&&!rl&&r
5167326140
MC/DC50%3/6
not defined $N && $N =~ /^\s*[-+]?\d+\s*$/o or defined $low and $N < $low or defined $high and $N > $high
defined $N $N =~ /^\s*[-+]?\d+\s*$/o defined $low $N < $low defined $high $N > $high
Truth table55%5/9
not(A && B) or C and D or E and F
A= defined $NB= $N =~ /^\s*[-+]?\d+\s*$/oC= defined $lowD= $N < $lowE= defined $highF= $N > $high
ABCDEFresult
0XXXXX1
10XXXX1
1111XX1
110X111
1110111
110X0X0
110X100
11100X0
1110100
1683 $N !~ /^\s*[-+]?\d+\s*$/o or
1684 defined $low && $N<$low or
1685 defined $high && $N>$high);
1686 54287 return 1;
1687 }
1688  
1689 # $flag = $self->_is_num($string [,$low, $high]);
1690 # Returns 1 if $string is a valid number (integer or real), 0 otherwise.
1691 # If $low is entered, $string must be >= $low. If $high is entered,
1692 # $string must be <= $high. It is valid to check only one of the bounds.
1693 40812 ▶sub _is_num {
Subroutine _is_num: called CC=8 SCAR=23.7
1694 40812 my($self,$N,$low,$high)=@_;
1695 40812 ▶ return 0 if (! defined $N or
Branch: 1 branch
BranchTrueFalseTotal
if not defined $N && ($N =~ /^\s*[-+]?\d+(\.\d*)?\s*$/o || $N =~ /^\s*[-+]?\.\d+\s*$/o) or defined $low and $N < $low or defined $high and $N > $high 2 40810 40812
Condition55%10/18
not defined $N && ($N =~ /^\s*[-+]?\d+(\.\d*)?\s*$/o || $N =~ /^\s*[-+]?\.\d+\s*$/o) or defined $low and $N < $low or defined $high and $N > $high
l!l&&r!l&&!r
2040810
not defined $N && ($N =~ /^\s*[-+]?\d+(\.\d*)?\s*$/o || $N =~ /^\s*[-+]?\.\d+\s*$/o) or defined $low and $N < $low
l!l&&r!l&&!r
2040810
defined $N && ($N =~ /^\s*[-+]?\d+(\.\d*)?\s*$/o || $N =~ /^\s*[-+]?\.\d+\s*$/o)
!ll&&!rl&&r
0240810
$N =~ /^\s*[-+]?\d+(\.\d*)?\s*$/o or $N =~ /^\s*[-+]?\.\d+\s*$/o
l!l&&r!l&&!r
4081002
defined $low and $N < $low
!ll&&!rl&&r
4081000
defined $high and $N > $high
!ll&&!rl&&r
4081000
MC/DC14%1/7
not defined $N && ($N =~ /^\s*[-+]?\d+(\.\d*)?\s*$/o || $N =~ /^\s*[-+]?\.\d+\s*$/o) or defined $low and $N < $low or defined $high and $N > $high
defined $N $N =~ /^\s*[-+]?\d+(\.\d*)?\s*$/o $N =~ /^\s*[-+]?\.\d+\s*$/o defined $low $N < $low defined $high $N > $high
Truth table12%2/16
not(A && B or C) or D and E or F and G
A= defined $NB= $N =~ /^\s*[-+]?\d+(\.\d*)?\s*$/oC= $N =~ /^\s*[-+]?\.\d+\s*$/oD= defined $lowE= $N < $lowF= defined $highG= $N > $high
ABCDEFGresult
0XXXXXX1
100XXXX1
11X11XX1
10111XX1
11X0X111
11X10111
1010X111
10110111
11X0X0X0
11X0X100
11X100X0
11X10100
1010X0X0
1010X100
101100X0
10110100
1696 ($N !~ /^\s*[-+]?\d+(\.\d*)?\s*$/o &&
1697 $N !~ /^\s*[-+]?\.\d+\s*$/o) or
1698 defined $low && $N<$low or
1699 defined $high && $N>$high);
1700 40810 return 1;
1701 }
1702  
1703 ###############################################################################
1704 # Split/Join functions
1705  
1706 5764 ▶sub split {
Subroutine split: called CC=31 SCAR=34.5
Pod documented
1707 5764 my($self,$op,$string,$arg) = @_;
1708  
1709 5764 my %opts;
1710 5764 ▶ if (ref($arg) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg eq 'HASH') { } 1 5763 5764
elsif ($arg) { } 1 5762 5763
1711 1 %opts = %{ $arg };
1712 } elsif ($arg) {
1713 # ***DEPRECATED 7.0***
1714 1 %opts = ('nonorm' => 1);
1715 }
1716  
1717 # ***DEPRECATED 7.0***
1718 5764 ▶ if ($op eq 'delta') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq 'delta') { } 81 5683 5764
elsif ($op eq 'business') { } 6 5677 5683
1719 81 $opts{'mode'} = 'standard';
1720 } elsif ($op eq 'business') {
1721 6 $opts{'mode'} = 'business';
1722 6 $op = 'delta';
1723 }
1724  
1725 5764 ▶ if ($op eq 'date') {
Branch: 5 branches
BranchTrueFalseTotal
if ($op eq 'date') { } 4246 1518 5764
elsif ($op eq 'hms') { } 1124 394 1518
elsif ($op eq 'offset') { } 294 100 394
elsif ($op eq 'time') { } 13 87 100
elsif ($op eq 'delta') { } 87 0 87
1726  
1727 4246 ▶ if ($string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d):(\d\d):(\d\d)$/o ||
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d):(\d\d):(\d\d)$/o or $string =~ /^(\d\d\d\d)\-(\d\d)\-(\d\d)\-(\d\d):(\d\d):(\d\d)$/o or $string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/o) { } 1925 2321 4246
Condition100%6/6
$string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d):(\d\d):(\d\d)$/o or $string =~ /^(\d\d\d\d)\-(\d\d)\-(\d\d)\-(\d\d):(\d\d):(\d\d)$/o or $string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/o
l!l&&r!l&&!r
192232321
$string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d):(\d\d):(\d\d)$/o or $string =~ /^(\d\d\d\d)\-(\d\d)\-(\d\d)\-(\d\d):(\d\d):(\d\d)$/o
l!l&&r!l&&!r
47614462324
MC/DC100%3/3
$string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d):(\d\d):(\d\d)$/o or $string =~ /^(\d\d\d\d)\-(\d\d)\-(\d\d)\-(\d\d):(\d\d):(\d\d)$/o or $string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/o
$string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d):(\d\d):(\d\d)$/o $string =~ /^(\d\d\d\d)\-(\d\d)\-(\d\d)\-(\d\d):(\d\d):(\d\d)$/o $string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/o
Truth table100%4/4
A or B or C
A= $string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d):(\d\d):(\d\d)$/oB= $string =~ /^(\d\d\d\d)\-(\d\d)\-(\d\d)\-(\d\d):(\d\d):(\d\d)$/oC= $string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/o
ABCresult
1XX1
01X1
0011
0000
1728 $string =~ /^(\d\d\d\d)\-(\d\d)\-(\d\d)\-(\d\d):(\d\d):(\d\d)$/o ||
1729 $string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/o) {
1730 1925 my($y,$m,$d,$h,$mn,$s) = ($1+0,$2+0,$3+0,$4+0,$5+0,$6+0);
1731 1925 return [$y,$m,$d,$h,$mn,$s];
1732 } else {
1733 2321 return undef;
1734 }
1735  
1736 } elsif ($op eq 'hms') {
1737 1124 ▶ if ($string =~ /^(\d\d)(\d\d)(\d\d)$/o ||
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^(\d\d)(\d\d)(\d\d)$/o or $string =~ /^(\d\d)(\d\d)()$/o or $string =~ /^(\d\d?):(\d\d):(\d\d)$/o or $string =~ /^(\d\d?):(\d\d)()$/o or $string =~ /^(\d\d?)()()$/o) { } 1121 3 1124
Condition75%9/12
$string =~ /^(\d\d)(\d\d)(\d\d)$/o or $string =~ /^(\d\d)(\d\d)()$/o or $string =~ /^(\d\d?):(\d\d):(\d\d)$/o or $string =~ /^(\d\d?):(\d\d)()$/o or $string =~ /^(\d\d?)()()$/o
l!l&&r!l&&!r
111473
$string =~ /^(\d\d)(\d\d)(\d\d)$/o or $string =~ /^(\d\d)(\d\d)()$/o or $string =~ /^(\d\d?):(\d\d):(\d\d)$/o or $string =~ /^(\d\d?):(\d\d)()$/o
l!l&&r!l&&!r
10773710
$string =~ /^(\d\d)(\d\d)(\d\d)$/o or $string =~ /^(\d\d)(\d\d)()$/o or $string =~ /^(\d\d?):(\d\d):(\d\d)$/o
l!l&&r!l&&!r
0107747
$string =~ /^(\d\d)(\d\d)(\d\d)$/o or $string =~ /^(\d\d)(\d\d)()$/o
l!l&&r!l&&!r
001124
MC/DC60%3/5
$string =~ /^(\d\d)(\d\d)(\d\d)$/o or $string =~ /^(\d\d)(\d\d)()$/o or $string =~ /^(\d\d?):(\d\d):(\d\d)$/o or $string =~ /^(\d\d?):(\d\d)()$/o or $string =~ /^(\d\d?)()()$/o
$string =~ /^(\d\d)(\d\d)(\d\d)$/o $string =~ /^(\d\d)(\d\d)()$/o $string =~ /^(\d\d?):(\d\d):(\d\d)$/o $string =~ /^(\d\d?):(\d\d)()$/o $string =~ /^(\d\d?)()()$/o
Truth table66%4/6
A or B or C or D or E
A= $string =~ /^(\d\d)(\d\d)(\d\d)$/oB= $string =~ /^(\d\d)(\d\d)()$/oC= $string =~ /^(\d\d?):(\d\d):(\d\d)$/oD= $string =~ /^(\d\d?):(\d\d)()$/oE= $string =~ /^(\d\d?)()()$/o
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
1738 $string =~ /^(\d\d)(\d\d)()$/o ||
1739 $string =~ /^(\d\d?):(\d\d):(\d\d)$/o ||
1740 $string =~ /^(\d\d?):(\d\d)()$/o ||
1741 $string =~ /^(\d\d?)()()$/o) {
1742 1121 my($err,$h,$mn,$s) = $self->_hms_fields( { 'out' => 'list' },[$1,$2,$3]);
1743 1121 ▶ return undef if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 1 1120 1121
1744 1120 return [$h,$mn,$s];
1745 } else {
1746 3 return undef;
1747 }
1748  
1749 } elsif ($op eq 'offset') {
1750 294 ▶ if ($string =~ /^([-+]?\d\d)(\d\d)(\d\d)$/o ||
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^([-+]?\d\d)(\d\d)(\d\d)$/o or $string =~ /^([-+]?\d\d)(\d\d)()$/o or $string =~ /^([-+]?\d\d?):(\d\d?):(\d\d?)$/o or $string =~ /^([-+]?\d\d?):(\d\d?)()$/o or $string =~ /^([-+]?\d\d?)()()$/o) { } 288 6 294
Condition100%12/12
$string =~ /^([-+]?\d\d)(\d\d)(\d\d)$/o or $string =~ /^([-+]?\d\d)(\d\d)()$/o or $string =~ /^([-+]?\d\d?):(\d\d?):(\d\d?)$/o or $string =~ /^([-+]?\d\d?):(\d\d?)()$/o or $string =~ /^([-+]?\d\d?)()()$/o
l!l&&r!l&&!r
273156
$string =~ /^([-+]?\d\d)(\d\d)(\d\d)$/o or $string =~ /^([-+]?\d\d)(\d\d)()$/o or $string =~ /^([-+]?\d\d?):(\d\d?):(\d\d?)$/o or $string =~ /^([-+]?\d\d?):(\d\d?)()$/o
l!l&&r!l&&!r
2581521
$string =~ /^([-+]?\d\d)(\d\d)(\d\d)$/o or $string =~ /^([-+]?\d\d)(\d\d)()$/o or $string =~ /^([-+]?\d\d?):(\d\d?):(\d\d?)$/o
l!l&&r!l&&!r
10415436
$string =~ /^([-+]?\d\d)(\d\d)(\d\d)$/o or $string =~ /^([-+]?\d\d)(\d\d)()$/o
l!l&&r!l&&!r
2102190
MC/DC100%5/5
$string =~ /^([-+]?\d\d)(\d\d)(\d\d)$/o or $string =~ /^([-+]?\d\d)(\d\d)()$/o or $string =~ /^([-+]?\d\d?):(\d\d?):(\d\d?)$/o or $string =~ /^([-+]?\d\d?):(\d\d?)()$/o or $string =~ /^([-+]?\d\d?)()()$/o
$string =~ /^([-+]?\d\d)(\d\d)(\d\d)$/o $string =~ /^([-+]?\d\d)(\d\d)()$/o $string =~ /^([-+]?\d\d?):(\d\d?):(\d\d?)$/o $string =~ /^([-+]?\d\d?):(\d\d?)()$/o $string =~ /^([-+]?\d\d?)()()$/o
Truth table100%6/6
A or B or C or D or E
A= $string =~ /^([-+]?\d\d)(\d\d)(\d\d)$/oB= $string =~ /^([-+]?\d\d)(\d\d)()$/oC= $string =~ /^([-+]?\d\d?):(\d\d?):(\d\d?)$/oD= $string =~ /^([-+]?\d\d?):(\d\d?)()$/oE= $string =~ /^([-+]?\d\d?)()()$/o
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
1751 $string =~ /^([-+]?\d\d)(\d\d)()$/o ||
1752 $string =~ /^([-+]?\d\d?):(\d\d?):(\d\d?)$/o ||
1753 $string =~ /^([-+]?\d\d?):(\d\d?)()$/o ||
1754 $string =~ /^([-+]?\d\d?)()()$/o) {
1755 288 my($err,$h,$mn,$s) = $self->_offset_fields( { 'source' => 'string',
1756 'out' => 'list'},
1757 [$1,$2,$3]);
1758 288 ▶ return undef if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 1 287 288
1759 287 return [$h,$mn,$s];
1760 } else {
1761 6 return undef;
1762 }
1763  
1764 } elsif ($op eq 'time') {
1765 13 ▶ if ($string =~ /^[-+]?\d+(:[-+]?\d+){0,2}$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^[-+]?\d+(:[-+]?\d+){0,2}$/o) { } 12 1 13
1766 my($err,$dh,$dmn,$ds) =
1767 $self->_time_fields( { 'nonorm' =>
1768 12 ▶ (exists($opts{'nonorm'}) ? $opts{'nonorm'} : 0),
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'nonorm'} ? : 2 10 12
1769 'source' => 'string',
1770 'sign' => -1,
1771 }, [split(/:/,$string)]);
1772 12 ▶ return undef if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 12 12
1773 12 return [$dh,$dmn,$ds];
1774 } else {
1775 1 return undef;
1776 }
1777  
1778 } elsif ($op eq 'delta') {
1779 87 my($err,@delta) = $self->_split_delta($string);
1780 87 ▶ return undef if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 87 87
1781  
1782 ($err,@delta) =
1783 $self->_delta_fields( { 'mode' => $opts{'mode'},
1784 'nonorm' => (exists($opts{'nonorm'}) ?
1785 87 ▶ $opts{'nonorm'} : 0),
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'nonorm'} ? : 0 87 87
1786 'source' => 'string',
1787 'sign' => -1,
1788 }, [@delta]);
1789  
1790 87 ▶ return undef if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 87 87
1791 87 return [@delta];
1792 }
1793 }
1794  
1795 27691 ▶sub join{
Subroutine join: called CC=19 SCAR=29.5
Pod documented
1796 27691 my($self,$op,$data,$arg) = @_;
1797  
1798 27691 my %opts;
1799 27691 ▶ if (ref($arg) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg eq 'HASH') { } 1 27690 27691
elsif ($arg) { } 2 27688 27690
1800 1 %opts = %{ $arg };
1801 } elsif ($arg) {
1802 # ***DEPRECATED 7.0***
1803 2 %opts = ('nonorm' => 1);
1804 }
1805  
1806 # ***DEPRECATED 7.0***
1807 27691 ▶ if ($op eq 'delta') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq 'delta') { } 10 27681 27691
elsif ($op eq 'business') { } 9 27672 27681
1808 10 $opts{'mode'} = 'standard';
1809 } elsif ($op eq 'business') {
1810 9 $opts{'mode'} = 'business';
1811 9 $op = 'delta';
1812 }
1813  
1814 27691 my @data = @$data;
1815  
1816 27691 ▶ if ($op eq 'date') {
Branch: 5 branches
BranchTrueFalseTotal
if ($op eq 'date') { } 24812 2879 27691
elsif ($op eq 'offset') { } 108 2771 2879
elsif ($op eq 'hms') { } 2739 32 2771
elsif ($op eq 'time') { } 13 19 32
elsif ($op eq 'delta') { } 19 0 19
1817  
1818 24812 my($err,$y,$m,$d,$h,$mn,$s) = $self->_date_fields(@data);
1819 24812 ▶ return undef if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 24812 24812
1820 24812 my $form = $self->_config('printable');
1821 24812 ▶ if ($form == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($form == 1) { } 1 24811 24812
elsif ($form == 2) { } 1 24810 24811
1822 1 return "$y$m$d$h$mn$s";
1823 } elsif ($form == 2) {
1824 1 return "$y-$m-$d-$h:$mn:$s";
1825 } else {
1826 24810 return "$y$m$d$h:$mn:$s";
1827 }
1828  
1829 } elsif ($op eq 'offset') {
1830 108 my($err,$h,$mn,$s) = $self->_offset_fields( { 'source' => 'list',
1831 'out' => 'string'},
1832 [@data]);
1833 108 ▶ return undef if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 3 105 108
1834 105 return "$h:$mn:$s";
1835  
1836 } elsif ($op eq 'hms') {
1837 2739 my($err,$h,$mn,$s) = $self->_hms_fields( { 'out' => 'string' },[@data]);
1838 2739 ▶ return undef if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 3 2736 2739
1839 2736 return "$h:$mn:$s";
1840  
1841 } elsif ($op eq 'time') {
1842 my($err,$dh,$dmn,$ds) =
1843 $self->_time_fields( { 'nonorm' =>
1844 13 ▶ (exists($opts{'nonorm'}) ? $opts{'nonorm'} : 0),
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'nonorm'} ? : 2 11 13
1845 'source' => 'list',
1846 'sign' => 0,
1847 }, [@data]);
1848 13 ▶ return undef if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 1 12 13
1849 12 return "$dh:$dmn:$ds";
1850  
1851 } elsif ($op eq 'delta') {
1852 my ($err,@delta) =
1853 $self->_delta_fields( { 'mode' => $opts{'mode'},
1854 'nonorm' => (exists($opts{'nonorm'}) ?
1855 19 ▶ $opts{'nonorm'} : 0),
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'nonorm'} ? : 1 18 19
1856 'source' => 'list',
1857 'sign' => 0,
1858 }, [@data]);
1859 19 ▶ return undef if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 19 19
1860 19 return join(':',@delta);
1861 }
1862 }
1863  
1864 1034 ▶sub _split_delta {
Subroutine _split_delta: called CC=2 SCAR=6.9
1865 1034 my($self,$string) = @_;
1866  
1867 1034 my $sign = '[-+]?';
1868 1034 my $num = '(?:\d+(?:\.\d*)?|\.\d+)';
1869 1034 my $f = "(?:$sign$num)?";
1870  
1871 1034 ▶ if ($string =~ /^$f(:$f){0,6}$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^$f(:$f){0,6}$/o) { } 436 598 1034
1872 436 $string =~ s/::/:0:/go;
1873 436 $string =~ s/^:/0:/o;
1874 436 $string =~ s/:$/:0/o;
1875 436 my(@delta) = split(/:/,$string);
1876 436 return(0,@delta);
1877 } else {
1878 598 return(1);
1879 }
1880 }
1881  
1882 # Check that type is not inconsistent with @delta.
1883 #
1884 # An exact delta cannot have semi-exact or approximate fields set.
1885 # A semi-exact delta cannot have approximate fields set.
1886 # An exact, semi-exact, or approximate delta cannot have non-integer values.
1887 #
1888 # If the type was not explicitly specified, guess what it is.
1889 #
1890 # Returns ($err,$type,$type_from)
1891 #
1892 5547 ▶sub _check_delta_type {
Subroutine _check_delta_type: called CC=25 SCAR=32.2
1893 5547 my($self,$mode,$type,$type_from,@delta) = @_;
1894  
1895 5547 my $est = 0;
1896 5547 foreach my $f (@delta) {
1897 38767 ▶ if (! $self->_is_int($f)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_int($f)) 5 38762 38767
1898 5 $est = 1;
1899 5 last;
1900 }
1901 }
1902  
1903 5547 my $approx = 0;
1904 5547 ▶ if (! $est) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($est) 5542 5 5547
1905 5542 ▶ $approx = 1 if ($delta[0] || $delta[1]);
Branch: 1 branch
BranchTrueFalseTotal
if $delta[0] or $delta[1] 3407 2135 5542
Condition100%3/3
$delta[0] or $delta[1]
l!l&&r!l&&!r
29604472135
MC/DC100%2/2
$delta[0] or $delta[1]
$delta[0] $delta[1]
Truth table100%3/3
A or B
A= $delta[0]B= $delta[1]
ABresult
1X1
011
000
1906 }
1907  
1908 5547 my $semi = 0;
1909 5547 ▶ if (! $est && ! $approx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($est or $approx) 2135 3412 5547
Condition100%3/3
$est or $approx
l!l&&r!l&&!r
534072135
MC/DC100%2/2
$est or $approx
$est $approx
Truth table100%3/3
A or B
A= $estB= $approx
ABresult
1X1
011
000
1910 2135 ▶ if ($mode eq 'business') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'business') { } 287 1848 2135
1911 287 ▶ $semi = 1 if ($delta[2]);
Branch: 1 branch
BranchTrueFalseTotal
if $delta[2] 26 261 287
1912 } else {
1913 1848 ▶ $semi = 1 if ($delta[2] || $delta[3]);
Branch: 1 branch
BranchTrueFalseTotal
if $delta[2] or $delta[3] 365 1483 1848
Condition100%3/3
$delta[2] or $delta[3]
l!l&&r!l&&!r
1302351483
MC/DC100%2/2
$delta[2] or $delta[3]
$delta[2] $delta[3]
Truth table100%3/3
A or B
A= $delta[2]B= $delta[3]
ABresult
1X1
011
000
1914 }
1915 }
1916  
1917 5547 ▶ if ($est) {
Branch: 3 branches
BranchTrueFalseTotal
if ($est) { } 5 5542 5547
elsif ($approx) { } 3407 2135 5542
elsif ($semi) { } 391 1744 2135
1918 # If some of the fields are non-integer, then type must be estimated.
1919  
1920 5 ▶ if ($type ne 'estimated') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "estimated") 5 0 5
1921 5 ▶ if ($type_from eq 'opt') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_from eq "opt") 1 4 5
1922 1 return ("Type must be estimated for non-integers");
1923 }
1924 4 $type = 'estimated';
1925 4 $type_from = 'det';
1926 }
1927  
1928 } elsif ($approx) {
1929 # If some of the approximate fields are set, then type must be
1930 # approx or estimated.
1931  
1932 3407 ▶ if ($type ne 'approx' && $type ne 'estimated') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "approx" and $type ne "estimated") 3397 10 3407
Condition100%3/3
$type ne "approx" and $type ne "estimated"
!ll&&!rl&&r
823397
MC/DC100%2/2
$type ne "approx" and $type ne "estimated"
$type ne "approx" $type ne "estimated"
Truth table100%3/3
A and B
A= $type ne "approx"B= $type ne "estimated"
ABresult
0X0
100
111
1933 3397 ▶ if ($type_from eq 'opt') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_from eq "opt") 5 3392 3397
1934 5 return("Type must be approx/estimated");
1935 }
1936 3392 $type = 'approx';
1937 3392 $type_from = 'det';
1938 }
1939  
1940 } elsif ($semi) {
1941 # If some of the semi-exact fields are set, then type must be
1942 # semi, approx, or estimated
1943  
1944 391 ▶ if ($type ne 'semi' && $type ne 'approx' && $type ne 'estimated') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "semi" and $type ne "approx" and $type ne "estimated") 373 18 391
Condition100%6/6
$type ne "semi" and $type ne "approx" and $type ne "estimated"
!ll&&!rl&&r
126373
$type ne "semi" and $type ne "approx"
!ll&&!rl&&r
48379
MC/DC100%3/3
$type ne "semi" and $type ne "approx" and $type ne "estimated"
$type ne "semi" $type ne "approx" $type ne "estimated"
Truth table100%4/4
A and B and C
A= $type ne "semi"B= $type ne "approx"C= $type ne "estimated"
ABCresult
0XX0
10X0
1100
1111
1945 373 ▶ if ($type_from eq 'opt') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_from eq "opt") 5 368 373
1946 5 return("Type must be semi/approx/estimated");
1947 }
1948 368 $type = 'semi';
1949 368 $type_from = 'det';
1950 }
1951  
1952 } else {
1953  
1954 1744 ▶ if (! $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 266 1478 1744
1955 266 $type = 'exact';
1956 266 $type_from = 'det';
1957 }
1958 }
1959  
1960 5536 return ('',$type,$type_from);
1961 }
1962  
1963 # This function returns the fields in a delta in the desired format.
1964 #
1965 # $opts = { mode => standard/business
1966 # type => exact/semi/approx/estimated
1967 # nonorm => 0/1,
1968 # source => string, list, delta
1969 # sign => 0/1/-1
1970 # }
1971 # $fields = [Y,M,W,D,H,Mn,S]
1972 #
1973 # If the business option is 1, treat it as a business delta.
1974 #
1975 # If the nonorm option is 1, fields are NOT normalized. By default,
1976 # they are normalized.
1977 #
1978 # If source is 'string', then the source of the fields is a string
1979 # that has been split, so we need to handle carrying the signs. If
1980 # the option is 'list', then the source is a valid delta, so each
1981 # field is correctly signed already. In both cases, the type of
1982 # delta will need to be determined. If the source is 'delta', then
1983 # it comes from a Date::Manip::Delta object. In this case the type
1984 # must be specified. If type is not passed in, it will be set.
1985 #
1986 # If the sign option is 1, a sign is added to every field. If the
1987 # sign option is -1, all negative fields are signed. If the sign
1988 # option is 0, the minimum number of signs (for fields who's sign is
1989 # different from the next higher field) will be added.
1990 #
1991 # It returns ($err,@fields)
1992 #
1993 5904 ▶sub _delta_fields {
Subroutine _delta_fields: called CC=18 SCAR=29.0
1994 5904 my($self,$opts,$fields) = @_;
1995 5904 my @fields = @$fields;
1996 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1997  
1998 #
1999 # Make sure that all fields are defined, numerical, and that there
2000 # are 7 of them.
2001 #
2002  
2003 5904 foreach my $f (@fields) {
2004 40805 ▶ $f=0 if (! defined($f));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f 0 40805 40805
2005 40805 ▶ return ("Non-numerical field") if (! $self->_is_num($f));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_num($f) 1 40804 40805
2006 }
2007 5903 ▶ return ("Delta may contain only 7 fields") if (@fields > 7);
Branch: 1 branch
BranchTrueFalseTotal
if @fields > 7 1 5902 5903
2008 5902 ▶ while (@fields < 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields < 7) 518 5902 6420
2009 518 unshift(@fields,0);
2010 }
2011  
2012 #
2013 # Make sure each field is the correct sign so that the math will
2014 # work correctly. Get rid of all positive signs and leading 0's.
2015 #
2016  
2017 5902 my $mode = $$opts{'mode'};
2018 5902 my $source = $$opts{'source'};
2019 5902 @fields = $self->_sign_source($source,@fields);
2020  
2021 #
2022 # Figure out the type of delta. When called from Date::Manip::Base, it'll
2023 # be determined from the data. When called from Date::Manip::Delta, it'll
2024 # be specified.
2025 #
2026  
2027 5902 my ($type,$type_from);
2028 5902 ▶ if (defined $source && $source eq 'delta') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source and $source eq 'delta') { } 5333 569 5902
Condition66%2/3
defined $source and $source eq "delta"
!ll&&!rl&&r
05695333
MC/DC50%1/2
defined $source and $source eq "delta"
defined $source $source eq "delta"
Truth table66%2/3
A and B
A= defined $sourceB= $source eq "delta"
ABresult
0X0
100
111
2029 5333 ▶ if (! exists $$opts{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts->{"type"}) 0 5333 5333
2030 0 return ("Type must be specified");
2031 }
2032 5333 $type = $$opts{'type'};
2033  
2034 } else {
2035 569 my $err;
2036 569 ($err,$type,$type_from) = $self->_check_delta_type($mode,'','init',@fields);
2037 569 $$opts{'type'} = $type;
2038 569 $$opts{'type_from'} = $type_from;
2039 569 ▶ return($err) if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 569 569
2040 }
2041  
2042 #
2043 # Normalize values, if desired.
2044 #
2045  
2046 5902 my $norm = 1-$$opts{'nonorm'};
2047 5902 ▶ if ($norm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($norm) 5540 362 5902
2048 5540 ▶ if ($mode eq 'business') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'business') { } 354 5186 5540
2049  
2050 354 ▶ if ($type eq 'estimated') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'estimated') { } 10 344 354
elsif ($type eq 'approx' or $type eq 'semi') { } 113 231 344
2051 10 @fields = $self->_normalize_bus_est(@fields);
2052  
2053 ▶ } elsif ($type eq 'approx' ||
Condition100%3/3
$type eq "approx" or $type eq "semi"
l!l&&r!l&&!r
8132231
MC/DC100%2/2
$type eq "approx" or $type eq "semi"
$type eq "approx" $type eq "semi"
Truth table100%3/3
A or B
A= $type eq "approx"B= $type eq "semi"
ABresult
1X1
011
000
2054 $type eq 'semi') {
2055 113 @fields = $self->_normalize_bus_approx(@fields);
2056  
2057 } else {
2058 231 @fields = $self->_normalize_bus_exact(@fields);
2059 }
2060  
2061 } else {
2062  
2063 5186 ▶ if ($type eq 'estimated') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'estimated') { } 11 5175 5186
elsif ($type eq 'approx' or $type eq 'semi') { } 3709 1466 5175
2064 11 @fields = $self->_normalize_est(@fields);
2065  
2066 ▶ } elsif ($type eq 'approx' ||
Condition100%3/3
$type eq "approx" or $type eq "semi"
l!l&&r!l&&!r
33433661466
MC/DC100%2/2
$type eq "approx" or $type eq "semi"
$type eq "approx" $type eq "semi"
Truth table100%3/3
A or B
A= $type eq "approx"B= $type eq "semi"
ABresult
1X1
011
000
2067 $type eq 'semi') {
2068 3709 @fields = $self->_normalize_approx(@fields);
2069  
2070 } else {
2071 1466 @fields = $self->_normalize_exact(@fields);
2072 }
2073  
2074 }
2075 }
2076  
2077 #
2078 # Now make sure that the signs are included as appropriate.
2079 #
2080  
2081 5902 @fields = $self->_sign_fields($$opts{'sign'},@fields);
2082  
2083 5902 return (0,@fields);
2084 }
2085  
2086 # If a set of fields came from splitting a string, not all of the fields
2087 # are signed. If it comes from a list, we just want to remove extra '+'
2088 # signs.
2089 #
2090 5926 ▶sub _sign_source {
Subroutine _sign_source: called CC=5 SCAR=16.1
2091 5926 my($self,$source,@fields) = @_;
2092  
2093 # Needed to handle fractional fields
2094 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
2095 5926 ▶ if ($source eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($source eq 'string') { } 562 5364 5926
2096  
2097 # if the source is splitting a delta, not all fields are signed,
2098 # so we need to carry the negative signs.
2099  
2100 562 my $sign = '+';
2101 562 foreach my $f (@fields) {
2102 3886 ▶ if ($f =~ /^([-+])/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /^([-+])/o) { } 356 3530 3886
2103 356 $sign = $1;
2104 } else {
2105 3530 $f = "$sign$f";
2106 }
2107 3886 $f *= 1;
2108 }
2109  
2110 } else {
2111 5364 foreach my $f (@fields) {
2112 37500 $f *= 1;
2113 }
2114 }
2115  
2116 5926 return @fields;
2117 }
2118  
2119 # This applies the correct sign to each field based on the $sign option:
2120 #
2121 # 1 : all fields signed
2122 # -1 : all negative fields signed
2123 # 0 : minimum number of signs for a joined set of fields
2124 #
2125 5926 ▶sub _sign_fields {
Subroutine _sign_fields: called CC=12 SCAR=25.5
2126 5926 my($self,$sign,@fields) = @_;
2127 5926 ▶ $sign = 0 if (! defined $sign);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sign 0 5926 5926
2128  
2129 5926 ▶ if ($sign == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sign == 1) { } 0 5926 5926
elsif ($sign == 0) { } 370 5556 5926
2130 # All fields signed
2131 0 foreach my $f (@fields) {
2132 0 ▶ $f = "+$f" if ($f > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $f > 0 0 0 0
2133 }
2134  
2135 } elsif ($sign == 0) {
2136 # Minimum number of signs
2137 370 ▶ my $s = ($fields[0] < 0 ? '-' : '+');
Branch: 1 branch
BranchTrueFalseTotal
$fields[0] < 0 ? : 31 339 370
2138 370 foreach my $f (@fields[1..$#fields]) {
2139 2172 ▶ if ($f > 0 && $s eq '-') {
Branch: 2 branches
BranchTrueFalseTotal
if ($f > 0 and $s eq '-') { } 26 2146 2172
elsif ($f < 0) { } 323 1823 2146
Condition100%3/3
$f > 0 and $s eq "-"
!ll&&!rl&&r
162851826
MC/DC100%2/2
$f > 0 and $s eq "-"
$f > 0 $s eq "-"
Truth table100%3/3
A and B
A= $f > 0B= $s eq "-"
ABresult
0X0
100
111
2140 26 $f = "+$f";
2141 26 $s = '+';
2142 } elsif ($f < 0) {
2143 323 ▶ if ($s eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($s eq '-') { } 197 126 323
2144 197 $f *= -1;
2145 } else {
2146 126 $s = '-';
2147 }
2148 }
2149 }
2150 }
2151  
2152 5926 return @fields;
2153 }
2154  
2155 # $opts = { nonorm => 0/1,
2156 # source => string, list
2157 # sign => 0/1/-1
2158 # }
2159 # $fields = [H,M,S]
2160 #
2161 # This function formats the fields in an amount of time measured in
2162 # hours, minutes, and seconds.
2163 #
2164 # It is similar to how _delta_fields (above) works.
2165 #
2166 25 ▶sub _time_fields {
Subroutine _time_fields: called CC=7 SCAR=19.5
2167 25 my($self,$opts,$fields) = @_;
2168 25 my @fields = @$fields;
2169  
2170 #
2171 # Make sure that all fields are defined, numerical, and that there
2172 # are 3 of them.
2173 #
2174  
2175 25 foreach my $f (@fields) {
2176 67 ▶ $f=0 if (! defined($f));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f 0 67 67
2177 67 ▶ return (1) if (! $self->_is_int($f));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_int($f) 0 67 67
2178 }
2179 25 ▶ return (1) if (@fields > 3);
Branch: 1 branch
BranchTrueFalseTotal
if @fields > 3 1 24 25
2180 24 ▶ while (@fields < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields < 3) 9 24 33
2181 9 unshift(@fields,0);
2182 }
2183  
2184 #
2185 # Make sure each field is the correct sign so that the math will
2186 # work correctly. Get rid of all positive signs and leading 0's.
2187 #
2188  
2189 24 my $source = $$opts{'source'};
2190 24 @fields = $self->_sign_source($source,@fields);
2191  
2192 #
2193 # Normalize them. Values will be signed only if they are
2194 # negative.
2195 #
2196  
2197 24 my $norm = 1-$$opts{'nonorm'};
2198 24 ▶ if ($norm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($norm) 20 4 24
2199 20 my($h,$mn,$s) = @fields;
2200 20 $s += $h*3600 + $mn*60;
2201 20 @fields = __normalize_hms($h,$mn,$s);
2202 }
2203  
2204 #
2205 # Now make sure that the signs are included as appropriate.
2206 #
2207  
2208 24 @fields = $self->_sign_fields($$opts{'sign'},@fields);
2209  
2210 24 return (0,@fields);
2211 }
2212  
2213 # $opts = { out => string, list
2214 # }
2215 # $fields = [H,M,S]
2216 #
2217 # This function formats the fields in an HMS.
2218 #
2219 # If the out options is string, it prepares the fields to be joined (i.e.
2220 # they are all 2 digits long). Otherwise, they are just numerical values
2221 # (not necessarily 2 digits long).
2222 #
2223 # HH:MN:SS is always between 00:00:00 and 24:00:00.
2224 #
2225 # It returns ($err,@fields)
2226 #
2227 3894 ▶sub _hms_fields {
Subroutine _hms_fields: called CC=16 SCAR=28.6
2228 3894 my($self,$opts,$fields) = @_;
2229 3894 my @fields = @$fields;
2230  
2231 #
2232 # Make sure that all fields are defined, numerical (with no sign),
2233 # and that there are 3 of them.
2234 #
2235  
2236 3894 foreach my $f (@fields) {
2237 11661 ▶ $f=0 if (! $f);
Branch: 1 branch
BranchTrueFalseTotal
unless $f 5447 6214 11661
2238 11661 ▶ return (1) if (! $self->_is_int($f,0));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_int($f, 0) 1 11660 11661
2239 }
2240 3893 ▶ return (1) if (@fields > 3);
Branch: 1 branch
BranchTrueFalseTotal
if @fields > 3 1 3892 3893
2241 3892 ▶ while (@fields < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields < 3) 20 3892 3912
2242 20 push(@fields,0);
2243 }
2244  
2245 #
2246 # Check validity.
2247 #
2248  
2249 3892 my ($h,$m,$s) = @fields;
2250 3892 ▶ return (1) if ($h > 24 || $m > 59 || $s > 59 ||
Branch: 1 branch
BranchTrueFalseTotal
if $h > 24 or $m > 59 or $s > 59 or $h == 24 and $m > 0 || $s > 0 2 3890 3892
Condition46%7/15
$h > 24 or $m > 59 or $s > 59 or $h == 24 and $m > 0 || $s > 0
l!l&&r!l&&!r
203890
$h > 24 or $m > 59 or $s > 59
l!l&&r!l&&!r
203890
$h > 24 or $m > 59
l!l&&r!l&&!r
023890
$h == 24 and $m > 0 || $s > 0
!ll&&!rl&&r
389000
$m > 0 or $s > 0
l!l&&r!l&&!r
000
MC/DC16%1/6
$h > 24 or $m > 59 or $s > 59 or $h == 24 and $m > 0 || $s > 0
$h > 24 $m > 59 $s > 59 $h == 24 $m > 0 $s > 0
Truth table28%2/7
A or B or C or D and E or F
A= $h > 24B= $m > 59C= $s > 59D= $h == 24E= $m > 0F= $s > 0
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
00011X1
0001011
0000XX0
0001000
2251 ($h==24 && ($m > 0 || $s > 0)));
2252  
2253 #
2254 # Format
2255 #
2256  
2257 3890 ▶ if ($$opts{'out'} eq 'list') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'out'} eq 'list') { } 1154 2736 3890
2258 1154 foreach my $f ($h,$m,$s) {
2259 3462 $f *= 1;
2260 }
2261  
2262 } else {
2263 2736 foreach my $f ($h,$m,$s) {
2264 8208 ▶ $f = "0$f" if (length($f)<2);
Branch: 1 branch
BranchTrueFalseTotal
if length $f < 2 6727 1481 8208
2265 }
2266 }
2267  
2268 3890 return (0,$h,$m,$s);
2269 }
2270  
2271 # $opts = { source => string, list
2272 # out => string, list
2273 # }
2274 # $fields = [H,M,S]
2275 #
2276 # This function formats the fields in a timezone offset measured in
2277 # hours, minutes, and seconds.
2278 #
2279 # All offsets must be -23:59:59 <= offset <= 23:59:59 .
2280 #
2281 # The data comes from an offset in string or list format, and is
2282 # formatted so that it can be used to create a string or list format
2283 # output.
2284 #
2285 396 ▶sub _offset_fields {
Subroutine _offset_fields: called CC=47 SCAR=46.5
2286 396 my($self,$opts,$fields) = @_;
2287 396 my @fields = @$fields;
2288  
2289 #
2290 # Make sure that all fields are defined, numerical, and that there
2291 # are 3 of them.
2292 #
2293  
2294 396 foreach my $f (@fields) {
2295 1184 ▶ $f=0 if (! defined $f || $f eq '');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $f or $f eq "" 147 1037 1184
Condition66%2/3
not defined $f or $f eq ""
l!l&&r!l&&!r
01471037
MC/DC50%1/2
not defined $f or $f eq ""
not defined $f $f eq ""
Truth table66%2/3
not(A) or B
A= not defined $fB= $f eq ""
ABresult
1X1
011
000
2296 1184 ▶ return (1) if (! $self->_is_int($f));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_int($f) 0 1184 1184
2297 }
2298 396 ▶ return (1) if (@fields > 3);
Branch: 1 branch
BranchTrueFalseTotal
if @fields > 3 1 395 396
2299 395 ▶ while (@fields < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields < 3) 5 395 400
2300 5 push(@fields,0);
2301 }
2302  
2303 #
2304 # Check validity.
2305 #
2306  
2307 395 my ($h,$m,$s) = @fields;
2308 395 ▶ if ($$opts{'source'} eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'source'} eq 'string') { } 288 107 395
2309 # Values = -23 59 59 to +23 59 59
2310 288 ▶ return (1) if ($h < -23 || $h > 23 ||
Branch: 1 branch
BranchTrueFalseTotal
if $h < -23 or $h > 23 or $m < 0 or $m > 59 or $s < 0 or $s > 59 1 287 288
Condition53%8/15
$h < -23 or $h > 23 or $m < 0 or $m > 59 or $s < 0 or $s > 59
l!l&&r!l&&!r
10287
$h < -23 or $h > 23 or $m < 0 or $m > 59 or $s < 0
l!l&&r!l&&!r
10287
$h < -23 or $h > 23 or $m < 0 or $m > 59
l!l&&r!l&&!r
01287
$h < -23 or $h > 23 or $m < 0
l!l&&r!l&&!r
00288
$h < -23 or $h > 23
l!l&&r!l&&!r
00288
MC/DC16%1/6
$h < -23 or $h > 23 or $m < 0 or $m > 59 or $s < 0 or $s > 59
$h < -23 $h > 23 $m < 0 $m > 59 $s < 0 $s > 59
Truth table28%2/7
A or B or C or D or E or F
A= $h < -23B= $h > 23C= $m < 0D= $m > 59E= $s < 0F= $s > 59
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
2311 $m < 0 || $m > 59 ||
2312 $s < 0 || $s > 59);
2313 } else {
2314 # Values (-23,-59,-59) to (23,59,59)
2315 # Non-zero values must have the same sign
2316 107 ▶ if ($h >0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($h > 0) { } 33 74 107
elsif ($h < 0) { } 54 20 74
elsif ($m > 0) { } 2 18 20
elsif ($m < 0) { } 0 18 18
2317 33 ▶ return (1) if ( $h > 23 ||
Branch: 1 branch
BranchTrueFalseTotal
if $h > 23 or $m < 0 or $m > 59 or $s < 0 or $s > 59 2 31 33
Condition75%9/12
$h > 23 or $m < 0 or $m > 59 or $s < 0 or $s > 59
l!l&&r!l&&!r
2031
$h > 23 or $m < 0 or $m > 59 or $s < 0
l!l&&r!l&&!r
2031
$h > 23 or $m < 0 or $m > 59
l!l&&r!l&&!r
1131
$h > 23 or $m < 0
l!l&&r!l&&!r
0132
MC/DC40%2/5
$h > 23 or $m < 0 or $m > 59 or $s < 0 or $s > 59
$h > 23 $m < 0 $m > 59 $s < 0 $s > 59
Truth table50%3/6
A or B or C or D or E
A= $h > 23B= $m < 0C= $m > 59D= $s < 0E= $s > 59
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
2318 $m < 0 || $m > 59 ||
2319 $s < 0 || $s > 59);
2320 } elsif ($h < 0) {
2321 54 ▶ return (1) if ($h < -23 ||
Branch: 1 branch
BranchTrueFalseTotal
if $h < -23 or $m < -59 or $m > 0 or $s < -59 or $s > 0 0 54 54
Condition33%4/12
$h < -23 or $m < -59 or $m > 0 or $s < -59 or $s > 0
l!l&&r!l&&!r
0054
$h < -23 or $m < -59 or $m > 0 or $s < -59
l!l&&r!l&&!r
0054
$h < -23 or $m < -59 or $m > 0
l!l&&r!l&&!r
0054
$h < -23 or $m < -59
l!l&&r!l&&!r
0054
MC/DC0%0/5
$h < -23 or $m < -59 or $m > 0 or $s < -59 or $s > 0
$h < -23 $m < -59 $m > 0 $s < -59 $s > 0
Truth table16%1/6
A or B or C or D or E
A= $h < -23B= $m < -59C= $m > 0D= $s < -59E= $s > 0
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
2322 $m < -59 || $m > 0 ||
2323 $s < -59 || $s > 0);
2324 } elsif ($m > 0) {
2325 2 ▶ return (1) if ( $m > 59 ||
Branch: 1 branch
BranchTrueFalseTotal
if $m > 59 or $s < 0 or $s > 59 0 2 2
Condition33%2/6
$m > 59 or $s < 0 or $s > 59
l!l&&r!l&&!r
002
$m > 59 or $s < 0
l!l&&r!l&&!r
002
MC/DC0%0/3
$m > 59 or $s < 0 or $s > 59
$m > 59 $s < 0 $s > 59
Truth table25%1/4
A or B or C
A= $m > 59B= $s < 0C= $s > 59
ABCresult
1XX1
01X1
0011
0000
2326 $s < 0 || $s > 59);
2327 } elsif ($m < 0) {
2328 0 ▶ return (1) if ($m < -59 ||
Branch: 1 branch
BranchTrueFalseTotal
if $m < -59 or $s < -59 or $s > 0 0 0 0
Condition0%0/6
$m < -59 or $s < -59 or $s > 0
l!l&&r!l&&!r
000
$m < -59 or $s < -59
l!l&&r!l&&!r
000
MC/DC0%0/3
$m < -59 or $s < -59 or $s > 0
$m < -59 $s < -59 $s > 0
Truth table0%0/4
A or B or C
A= $m < -59B= $s < -59C= $s > 0
ABCresult
1XX1
01X1
0011
0000
2329 $s < -59 || $s > 0);
2330 } else {
2331 18 ▶ return (1) if ($s < -59 || $s > 59);
Branch: 1 branch
BranchTrueFalseTotal
if $s < -59 or $s > 59 0 18 18
Condition33%1/3
$s < -59 or $s > 59
l!l&&r!l&&!r
0018
MC/DC0%0/2
$s < -59 or $s > 59
$s < -59 $s > 59
Truth table33%1/3
A or B
A= $s < -59B= $s > 59
ABresult
1X1
011
000
2332 }
2333 }
2334  
2335 #
2336 # Make sure each field is the correct sign so that the math will
2337 # work correctly. Get rid of all positive signs and leading 0's.
2338 #
2339  
2340 392 ▶ if ($$opts{'source'} eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'source'} eq 'string') { } 287 105 392
2341  
2342 # In a string offset, only the first field is signed, so we need
2343 # to carry negative signs.
2344  
2345 287 ▶ if ($h =~ /^\-/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($h =~ /^\-/) { } 196 91 287
elsif ($m =~ /^\-/) { } 0 91 91
2346 196 $h *= 1;
2347 196 $m *= -1;
2348 196 $s *= -1;
2349 } elsif ($m =~ /^\-/) {
2350 0 $h *= 1;
2351 0 $m *= 1;
2352 0 $s *= -1;
2353 } else {
2354 91 $h *= 1;
2355 91 $m *= 1;
2356 91 $s *= 1;
2357 }
2358  
2359 } else {
2360 105 foreach my $f (@fields) {
2361 315 $f *= 1;
2362 }
2363 }
2364  
2365 #
2366 # Format them. They're already done for 'list' output.
2367 #
2368  
2369 392 ▶ if ($$opts{'out'} eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"out"} eq "string") 105 287 392
2370 105 my $sign;
2371 105 ▶ if ($h<0 || $m<0 || $s<0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h < 0 or $m < 0 or $s < 0) { } 54 51 105
Condition66%4/6
$h < 0 or $m < 0 or $s < 0
l!l&&r!l&&!r
54051
$h < 0 or $m < 0
l!l&&r!l&&!r
54051
MC/DC33%1/3
$h < 0 or $m < 0 or $s < 0
$h < 0 $m < 0 $s < 0
Truth table50%2/4
A or B or C
A= $h < 0B= $m < 0C= $s < 0
ABCresult
1XX1
01X1
0011
0000
2372 54 $h = abs($h);
2373 54 $m = abs($m);
2374 54 $s = abs($s);
2375 54 $sign = '-';
2376 } else {
2377 51 $sign = '+';
2378 }
2379  
2380 105 ▶ $h = "0$h" if (length($h) < 2);
Branch: 1 branch
BranchTrueFalseTotal
if length $h < 2 98 7 105
2381 105 ▶ $m = "0$m" if (length($m) < 2);
Branch: 1 branch
BranchTrueFalseTotal
if length $m < 2 87 18 105
2382 105 ▶ $s = "0$s" if (length($s) < 2);
Branch: 1 branch
BranchTrueFalseTotal
if length $s < 2 103 2 105
2383 105 $h = "$sign$h";
2384 }
2385  
2386 392 return (0,$h,$m,$s);
2387 }
2388  
2389 # ($err,$y,$m,$d,$h,$mn,$s) = $self->_date_fields($y,$m,$d,$h,$mn,$s);
2390 #
2391 # Makes sure the fields are the right length.
2392 #
2393 55055 ▶sub _date_fields {
Subroutine _date_fields: called CC=9 SCAR=22.0
2394 55055 my($self,@fields) = @_;
2395 55055 ▶ return (1) if (@fields != 6);
Branch: 1 branch
BranchTrueFalseTotal
if @fields != 6 0 55055 55055
2396  
2397 55055 my($y,$m,$d,$h,$mn,$s) = @fields;
2398  
2399 55055 ▶ $y = "0$y" while (length($y) < 4);
Branch: 1 branch
BranchTrueFalseTotal
length $y < 4 and do { $y = "0$y" } 91 55055 55146
2400 55055 ▶ $m = "0$m" if (length($m)==1);
Branch: 1 branch
BranchTrueFalseTotal
if length $m == 1 44023 11032 55055
2401 55055 ▶ $d = "0$d" if (length($d)==1);
Branch: 1 branch
BranchTrueFalseTotal
if length $d == 1 37025 18030 55055
2402 55055 ▶ $h = "0$h" if (length($h)==1);
Branch: 1 branch
BranchTrueFalseTotal
if length $h == 1 41965 13090 55055
2403 55055 ▶ $mn = "0$mn" if (length($mn)==1);
Branch: 1 branch
BranchTrueFalseTotal
if length $mn == 1 45425 9630 55055
2404 55055 ▶ $s = "0$s" if (length($s)==1);
Branch: 1 branch
BranchTrueFalseTotal
if length $s == 1 48166 6889 55055
2405  
2406 55055 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 24812 30243 55055
2407 24812 return (0,$y,$m,$d,$h,$mn,$s);
2408 } else {
2409 30243 return "$y$m$d$h:$mn:$s";
2410 }
2411 }
2412  
2413 # $self->_delta_convert(FORMAT,DELTA)
2414 # This converts delta into the given format. Returns '' if invalid.
2415 #
2416 94 ▶sub _delta_convert {
Subroutine _delta_convert: called CC=2 SCAR=6.9
2417 94 my($self,$format,$delta)=@_;
2418 94 my $fields = $self->split($format,$delta);
2419 94 ▶ return undef if (! defined $fields);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fields 1 93 94
2420 93 return $self->join($format,$fields);
2421 }
2422  
2423 ###############################################################################
2424 # Normalize the different types of deltas
2425  
2426 3845 ▶sub __normalize_ym {
Subroutine __normalize_ym: called CC=2 SCAR=6.9
2427 3845 my($y,$m,$s,$mon) = @_;
2428 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
2429  
2430 3845 ▶ if (defined($s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s) { } 21 3824 3845
2431 21 $m = int($s/$mon);
2432 21 $s -= int(sprintf('%f',$m*$mon));
2433 21 $y = int($m/12);
2434 21 $m -= $y*12;
2435  
2436 21 return($y,$m,$s);
2437 } else {
2438 3824 $m += $y*12;
2439 3824 $y = int($m/12);
2440 3824 $m -= $y*12;
2441  
2442 3824 return($y,$m);
2443 }
2444 }
2445 3845 ▶sub __normalize_wd {
Subroutine __normalize_wd: called CC=1 SCAR=0.0
2446 3845 my($w,$d,$s,$wk,$day) = @_;
2447 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
2448  
2449 3845 $d = int($s/$day);
2450 3845 $s -= int($d*$day);
2451 3845 $w = int($d/$wk);
2452 3845 $d -= $w*$wk;
2453  
2454 3845 return($w,$d,$s);
2455 }
2456 5568 ▶sub __normalize_hms {
Subroutine __normalize_hms: called CC=1 SCAR=0.0
2457 5568 my($h,$mn,$s) = @_;
2458 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
2459  
2460 5568 $h = int($s/3600);
2461 5568 $s -= $h*3600;
2462 5568 $mn = int($s/60);
2463 5568 $s -= $mn*60;
2464 5568 $s = int($s);
2465  
2466 5568 return($h,$mn,$s);
2467 }
2468  
2469 11 ▶sub _normalize_est {
Subroutine _normalize_est: called CC=1 SCAR=0.0
2470 11 my($self,$y,$m,$w,$d,$h,$mn,$s) = @_;
2471 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
2472  
2473 # Figure out how many seconds there are in the estimated delta
2474 #
2475 # 365.2425/12 days/month * 24 hours/day * 3600 sec/hour = 2629746 sec/month
2476  
2477 11 my $mon = 2629746;
2478 11 my $day = 86400;
2479 11 my $wk = 7;
2480 11 $s += ($y*12+$m)*$mon + ($w*$wk + $d)*$day +
2481 $h*3600 + $mn*60;
2482  
2483 11 ($y,$m,$s) = __normalize_ym($y,$m,$s,$mon);
2484 11 ($w,$d,$s) = __normalize_wd($w,$d,$s,$wk,$day);
2485 11 ($h,$mn,$s) = __normalize_hms($h,$mn,$s);
2486  
2487 11 return ($y,$m,$w,$d,$h,$mn,$s);
2488 }
2489 10 ▶sub _normalize_bus_est {
Subroutine _normalize_bus_est: called CC=1 SCAR=0.0
2490 10 my($self,$y,$m,$w,$d,$h,$mn,$s) = @_;
2491 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
2492  
2493 # Figure out how many seconds there are in the estimated delta
2494 #
2495 # 365.2425/12 * wk_len/7 days/month * day sec/day = X sec/month
2496  
2497 10 my $day = $$self{'data'}{'len'}{'bdlength'};
2498 10 my $wk = $$self{'data'}{'len'}{'workweek'};
2499 10 my $mon = 365.2425/12 * $wk/7 * $day;
2500  
2501 10 $s += ($y*12+$m)*$mon + ($w*$wk + $d)*$day +
2502 $h*3600 + $mn*60;
2503  
2504 10 ($y,$m,$s) = __normalize_ym($y,$m,$s,$mon);
2505 10 ($w,$d,$s) = __normalize_wd($w,$d,$s,$wk,$day);
2506 10 ($h,$mn,$s) = __normalize_hms($h,$mn,$s);
2507  
2508 10 return ($y,$m,$w,$d,$h,$mn,$s);
2509 }
2510  
2511 3710 ▶sub _normalize_approx {
Subroutine _normalize_approx: called CC=1 SCAR=0.0
2512 3710 my($self,$y,$m,$w,$d,$h,$mn,$s) = @_;
2513 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
2514  
2515 3710 my $wk = 7;
2516 3710 my $day = 86400;
2517 3710 $s += ($w*$wk + $d)*$day + $h*3600 + $mn*60;
2518  
2519 3710 ($y,$m) = __normalize_ym($y,$m);
2520 3710 ($w,$d,$s) = __normalize_wd($w,$d,$s,$wk,$day);
2521 3710 ($h,$mn,$s) = __normalize_hms($h,$mn,$s);
2522  
2523 3710 return ($y,$m,$w,$d,$h,$mn,$s);
2524 }
2525 114 ▶sub _normalize_bus_approx {
Subroutine _normalize_bus_approx: called CC=1 SCAR=0.0
2526 114 my($self,$y,$m,$w,$d,$h,$mn,$s) = @_;
2527 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
2528  
2529 114 my $wk = $$self{'data'}{'len'}{'workweek'};
2530 114 my $day = $$self{'data'}{'len'}{'bdlength'};
2531 114 $s += ($w*$wk + $d)*$day + $h*3600 + $mn*60;
2532  
2533 114 ($y,$m) = __normalize_ym($y,$m);
2534 114 ($w,$d,$s) = __normalize_wd($w,$d,$s,$wk,$day);
2535 114 ($h,$mn,$s) = __normalize_hms($h,$mn,$s);
2536  
2537 114 return ($y,$m,$w,$d,$h,$mn,$s);
2538 }
2539  
2540 1469 ▶sub _normalize_exact {
Subroutine _normalize_exact: called CC=1 SCAR=0.0
2541 1469 my($self,$y,$m,$w,$d,$h,$mn,$s) = @_;
2542 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
2543  
2544 1469 $s += $h*3600 + $mn*60;
2545  
2546 1469 ($h,$mn,$s) = __normalize_hms($h,$mn,$s);
2547  
2548 1469 return ($y,$m,$w,$d,$h,$mn,$s);
2549 }
2550 234 ▶sub _normalize_bus_exact {
Subroutine _normalize_bus_exact: called CC=1 SCAR=0.0
2551 234 my($self,$y,$m,$w,$d,$h,$mn,$s) = @_;
2552 171 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
2553  
2554 234 my $day = $$self{'data'}{'len'}{'bdlength'};
2555  
2556 234 $s += $d*$day + $h*3600 + $mn*60;
2557  
2558 # Calculate d
2559  
2560 234 $d = int($s/$day);
2561 234 $s -= $d*$day;
2562  
2563 234 ($h,$mn,$s) = __normalize_hms($h,$mn,$s);
2564  
2565 234 return ($y,$m,$w,$d,$h,$mn,$s);
2566 }
2567  
2568 ###############################################################################
2569 # Timezone critical dates
2570  
2571 # NOTE: Although I would prefer to stick this routine in the
2572 # Date::Manip::TZ module where it would be more appropriate, it must
2573 # appear here as it will be used to generate the data that will be
2574 # used by the Date::Manip::TZ module.
2575 #
2576 # This calculates a critical date based on timezone information. The
2577 # critical date is the date (usually in the current time) at which
2578 # the current timezone period ENDS.
2579 #
2580 # Input is:
2581 # $year,$mon,$flag,$num,$dow
2582 # This is information from the appropriate Rule line from the
2583 # zoneinfo files. These are used to determine the date (Y/M/D)
2584 # when the timezone period will end.
2585 # $isdst
2586 # Whether or not the next timezone period is a Daylight Saving
2587 # Time period.
2588 # $time,$timetype
2589 # The time of day when the change occurs. The timetype can be
2590 # 'w' (wallclock time in the current period), 's' (standard
2591 # time which will match wallclock time in a non-DST period, or
2592 # be off an hour in a DST period), and 'u' (universal time).
2593 #
2594 # Output is:
2595 # $endUT, $endLT, $begUT, $begLT
2596 # endUT is the actual last second of the current timezone
2597 # period. endLT is the same time expressed in local time.
2598 # begUT is the start (in UT) of the next time period. Note that
2599 # the begUT date is the one which actually corresponds to the
2600 # date/time specified in the input. begLT is the time in the new
2601 # local time. The endUT/endLT are the time one second earlier.
2602 #
2603 43 ▶sub _critical_date {
Subroutine _critical_date: called CC=12 SCAR=25.5
2604 43 my($self,$year,$mon,$flag,$num,$dow,
2605 $isdst,$time,$timetype,$stdoff,$dstoff) = @_;
2606  
2607 #
2608 # Get the predicted Y/M/D
2609 #
2610  
2611 43 my($y,$m,$d) = ($year+0,$mon+0,1);
2612  
2613 43 ▶ if ($flag eq 'dom') {
Branch: 4 branches
BranchTrueFalseTotal
if ($flag eq 'dom') { } 1 42 43
elsif ($flag eq 'last') { } 4 38 42
elsif ($flag eq 'ge') { } 38 0 38
elsif ($flag eq 'le') { } 0 0 0
2614 1 $d = $num;
2615  
2616 } elsif ($flag eq 'last') {
2617 4 my $ymd = $self->nth_day_of_week($year,-1,$dow,$mon);
2618 4 $d = $$ymd[2];
2619  
2620 } elsif ($flag eq 'ge') {
2621 38 my $ymd = $self->nth_day_of_week($year,1,$dow,$mon);
2622 38 $d = $$ymd[2];
2623 38 ▶ while ($d < $num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d < $num) 24 38 62
2624 24 $d += 7;
2625 }
2626  
2627 } elsif ($flag eq 'le') {
2628 0 my $ymd = $self->nth_day_of_week($year,-1,$dow,$mon);
2629 0 $d = $$ymd[2];
2630 0 ▶ while ($d > $num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d > $num) 0 0 0
2631 0 $d -= 7;
2632 }
2633 }
2634  
2635 #
2636 # Get the predicted time and the date (not yet taking into
2637 # account time type).
2638 #
2639  
2640 43 my($h,$mn,$s) = @{ $self->split('hms',$time) };
2641 43 my $date = [ $y,$m,$d,$h,$mn,$s ];
2642  
2643 #
2644 # Calculate all the relevant dates.
2645 #
2646  
2647 43 my($endUT,$endLT,$begUT,$begLT,$offset);
2648 43 $stdoff = $self->split('offset',$stdoff);
2649 43 $dstoff = $self->split('offset',$dstoff);
2650  
2651 43 ▶ if ($timetype eq 'w') {
Branch: 2 branches
BranchTrueFalseTotal
if ($timetype eq 'w') { } 39 4 43
elsif ($timetype eq 'u') { } 2 2 4
2652 39 ▶ $begUT = $self->calc_date_time($date,($isdst ? $stdoff : $dstoff), 1);
Branch: 1 branch
BranchTrueFalseTotal
$isdst ? : 25 14 39
2653 } elsif ($timetype eq 'u') {
2654 2 $begUT = $date;
2655 } else {
2656 2 $begUT = $self->calc_date_time($date,$stdoff, 1);
2657 }
2658  
2659 43 $endUT = $self->calc_date_time($begUT,[0,0,-1]);
2660 43 ▶ $endLT = $self->calc_date_time($endUT,($isdst ? $stdoff : $dstoff));
Branch: 1 branch
BranchTrueFalseTotal
$isdst ? : 27 16 43
2661 43 ▶ $begLT = $self->calc_date_time($begUT,($isdst ? $dstoff : $stdoff));
Branch: 1 branch
BranchTrueFalseTotal
$isdst ? : 27 16 43
2662  
2663 43 return ($endUT,$endLT,$begUT,$begLT);
2664 }
2665  
2666 ###############################################################################
2667 # Get a list of strings to try to parse.
2668  
2669 4620 ▶sub _encoding {
Subroutine _encoding: called CC=6 SCAR=17.9
2670 4620 my($self,$string) = @_;
2671 4620 my @ret;
2672  
2673 4620 foreach my $enc (@{ $$self{'data'}{'calc'}{'enc_in'} }) {
2674 9593 ▶ if (lc($enc) eq 'utf-8') {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $enc eq 'utf-8') { } 4620 4973 9593
elsif (lc $enc eq 'perl') { } 4620 353 4973
2675 4620 _utf8_on($string);
2676 4620 ▶ push(@ret,$string) if is_utf8($string, 1);
Branch: 1 branch
BranchTrueFalseTotal
if &is_utf8($string, 1) 4599 21 4620
2677 } elsif (lc($enc) eq 'perl') {
2678 4620 push(@ret,encode_utf8($string));
2679 } else {
2680 353 my $tmp = $string;
2681 353 _utf8_off($tmp);
2682 353 $tmp = encode_utf8(decode($enc, $tmp));
2683 353 _utf8_on($tmp);
2684 353 ▶ push(@ret,$tmp) if is_utf8($tmp, 1);;
Branch: 1 branch
BranchTrueFalseTotal
if &is_utf8($tmp, 1) 353 0 353
2685 }
2686 }
2687  
2688 4620 return @ret;
2689 }
2690  
2691 171 1;
2692 # Local Variables:
2693 # mode: cperl
2694 # indent-tabs-mode: nil
2695 # cperl-indent-level: 3
2696 # cperl-continued-statement-offset: 2
2697 # cperl-continued-brace-offset: 0
2698 # cperl-brace-offset: 0
2699 # cperl-brace-imaginary-offset: 0
2700 # cperl-label-offset: 0
2701 # End:
« lib/Date/Manip/Delta.pm ↑ Summary lib/Date/Manip/TZ_Base.pm »