lib/Date/Manip/Base.pm

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