lib/Date/Manip/TZ_Base.pm

filter: Statementstmt 83.0% 162 / 195 Branchbran 74.1% 83 / 112 Conditioncond 93.3% 14 / 15 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 16 / 16 Podpod n/a totaltotal 81.4% 281 / 345 CC 63 SCAR 27.3
CC
63
Cov
80%
_now
33.6
_config_file
15.1
_fix_year
11.0
CRAP 15.3
« lib/Date/Manip/Base.pm ↑ Summary lib/Date/Manip/Obj.pm »
1 package Date::Manip::TZ_Base;
2 # Copyright (c) 2010-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 ########################################################################
8  
9 170 require 5.010000;
10 170 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 170 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 170 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 170 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 170 our ($VERSION);
16 170 $VERSION='7.00';
17 170 ▶END { undef $VERSION; }
Subroutine END: called CC=1 SCAR=0.0
18  
19 ########################################################################
20 # METHODS
21 ########################################################################
22  
23 8587 ▶sub _config_var {
Subroutine _config_var: called CC=9 SCAR=22.1
24 8587 my($self,$var,$val) = @_;
25 8587 $var = lc($var);
26  
27 # A simple flag used to force a new configuration, but has
28 # no other affect.
29 8587 ▶ return if ($var eq 'ignore');
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "ignore" 6 8581 8587
30  
31 8581 my $istz = ref($self) eq 'Date::Manip::TZ';
32  
33 8581 ▶ if ($istz && ($var eq 'tz' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($istz and $var eq 'tz' || $var eq 'forcedate' || $var eq 'setdate' || $var eq 'configfile') { } 388 8193 8581
Condition91%11/12
$istz and $var eq "tz" || $var eq "forcedate" || $var eq "setdate" || $var eq "configfile"
!ll&&!rl&&r
7651542388
$var eq "tz" or $var eq "forcedate" or $var eq "setdate" or $var eq "configfile"
l!l&&r!l&&!r
36127542
$var eq "tz" or $var eq "forcedate" or $var eq "setdate"
l!l&&r!l&&!r
190171569
$var eq "tz" or $var eq "forcedate"
l!l&&r!l&&!r
0190740
MC/DC80%4/5
$istz and $var eq "tz" || $var eq "forcedate" || $var eq "setdate" || $var eq "configfile"
$istz $var eq "tz" $var eq "forcedate" $var eq "setdate" $var eq "configfile"
Truth table83%5/6
A and B or C or D or E
A= $istzB= $var eq "tz"C= $var eq "forcedate"D= $var eq "setdate"E= $var eq "configfile"
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
34 $var eq 'forcedate' ||
35 $var eq 'setdate' ||
36 $var eq 'configfile')) {
37 388 ▶ if ($var eq 'tz') {
Branch: 1 branch
BranchTrueFalseTotal
if ($var eq "tz") 0 388 388
38 0 carp "WARNING: the TZ Date::Manip config variable is deprecated\n" .
39 " and will be removed in version 7.00. Please use\n" .
40 " the SetDate or ForceDate config variables instead.\n";
41 }
42 388 return $self->_config_var_tz($var,$val);
43 } else {
44 8193 ▶ my $base = ($istz ? $$self{'base'} : $self);
Branch: 1 branch
BranchTrueFalseTotal
$istz ? : 542 7651 8193
45 8193 return $base->_config_var_base($var,$val);
46 }
47 }
48  
49 # This reads a config file
50 #
51 27 ▶sub _config_file {
Subroutine _config_file: called CC=14 SCAR=27.2
52 27 my($self,$file) = @_;
53  
54 27 ▶ return if (! $file);
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 27 27
55  
56 27 ▶ if (! -f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 27 27
57 0 carp "ERROR: [config_file] file doesn't exist: $file";
58 0 return;
59 }
60 27 ▶ if (! -r $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $file) 0 27 27
61 0 carp "ERROR: [config_file] file not readable: $file";
62 0 return;
63 }
64  
65 27 my $in = new IO::File;
66 27 ▶ if (! $in->open($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in->open($file)) 0 27 27
67 0 carp "ERROR: [config_file] unable to open file: $file: $!";
68 0 return;
69 }
70 27 my @in = <$in>;
71 27 $in->close();
72  
73 27 my $sect = 'conf';
74 27 my %sect;
75  
76 27 chomp(@in);
77 27 foreach my $line (@in) {
78 1179 $line =~ s/^\s+//o;
79 1179 $line =~ s/\s+$//o;
80 1179 ▶ next if (! $line or $line =~ /^\043/o);
Branch: 1 branch
BranchTrueFalseTotal
if not $line or $line =~ /^\043/o 675 504 1179
Condition100%3/3
not $line or $line =~ /^\043/o
l!l&&r!l&&!r
297378504
MC/DC100%2/2
not $line or $line =~ /^\043/o
not $line $line =~ /^\043/o
Truth table100%3/3
not(A) or B
A= not $lineB= $line =~ /^\043/o
ABresult
1X1
011
000
81  
82 504 ▶ if ($line =~ /^\*/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\*/o) { } 27 477 504
83 # New section
84 27 $sect = $self->_config_file_section($line);
85 } else {
86 477 $sect{$sect} = 1;
87 477 $self->_config_file_var($sect,$line);
88 }
89 }
90  
91 # If we did a holidays section, we need to create a regular
92 # expression with all of the holiday names.
93  
94 27 my $istz = ref($self) eq 'Date::Manip::TZ';
95 27 ▶ my $base = ($istz ? $$self{'base'} : $self);
Branch: 1 branch
BranchTrueFalseTotal
$istz ? : 27 0 27
96  
97 27 ▶ if (exists $sect{'holidays'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sect{"holidays"}) 24 3 27
98 24 my @hol = @{ $$base{'data'}{'sections'}{'holidays'} };
99 24 my @nam;
100 24 ▶ while (@hol) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hol) 225 24 249
101 225 my $junk = shift(@hol);
102 225 my $hol = shift(@hol);
103 225 ▶ push(@nam,$hol) if ($hol);
Branch: 1 branch
BranchTrueFalseTotal
if $hol 210 15 225
104 }
105  
106 24 ▶ if (@nam) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nam) 24 0 24
107 24 @nam = sort _sortByLength(@nam);
108 24 my $hol = '(?<holiday>' . join('|',map { "\Q$_\E" } @nam) . ')';
109 24 my $yr = '(?<y>\d\d\d\d|\d\d)';
110  
111 24 my $rx = "$hol\\s*$yr|" . # Christmas 2009
112 "$yr\\s*$hol|" . # 2009 Christmas
113 "$hol"; # Christmas
114  
115 24 $$base{'data'}{'rx'}{'holidays'} = qr/^(?:$rx)$/i;
116 }
117 }
118 }
119  
120 27 ▶sub _config_file_section {
Subroutine _config_file_section: called CC=3 SCAR=11.7
121 27 my($self,$line) = @_;
122  
123 27 my $istz = ref($self) eq 'Date::Manip::TZ';
124 27 ▶ my $base = ($istz ? $$self{'base'} : $self);
Branch: 1 branch
BranchTrueFalseTotal
$istz ? : 27 0 27
125  
126 27 $line =~ s/^\*//o;
127 27 $line =~ s/\s*$//o;
128 27 my $sect = lc($line);
129 27 ▶ if (! exists $$base{'data'}{'sections'}{$sect}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $base->{"data"}{"sections"}{$sect}) 0 27 27
130 0 carp "WARNING: [config_file] unknown section created: $sect";
131 0 $base->_section($sect);
132 }
133 27 return $sect;
134 }
135  
136 477 ▶sub _config_file_var {
Subroutine _config_file_var: called CC=4 SCAR=14.1
137 477 my($self,$sect,$line) = @_;
138  
139 477 my $istz = ref($self) eq 'Date::Manip::TZ';
140 477 ▶ my $base = ($istz ? $$self{'base'} : $self);
Branch: 1 branch
BranchTrueFalseTotal
$istz ? : 477 0 477
141  
142 477 my($var,$val);
143 477 ▶ if ($line =~ /^\s*(.*?)\s*=\s*(.*?)\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(.*?)\s*=\s*(.*?)\s*$/o) { } 477 0 477
144 477 ($var,$val) = ($1,$2);
145 } else {
146 0 croak "ERROR: invalid Date::Manip config file line:\n $line\n";
147 }
148  
149 477 ▶ if ($sect eq 'conf') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sect eq 'conf') { } 224 253 477
150 224 $var = lc($var);
151 224 $self->_config($var,$val);
152 } else {
153 253 $base->_section($sect,$var,$val);
154 }
155 }
156  
157 # $val = $self->config(VAR);
158 # Returns the value of a variable.
159 #
160 # $self->config([SECT], VAR, VAL) sets the value of a variable
161 # Sets the value of a variable.
162 #
163 48537 ▶sub _config {
Subroutine _config: called CC=3 SCAR=11.3
164 48537 my($self,$var,$val) = @_;
165  
166 48537 my $sect = 'conf';
167  
168 #
169 # $self->_conf(VAR, VAL) sets the value of a variable
170 #
171  
172 48537 $var = lc($var);
173 48537 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 229 48308 48537
174 229 return $self->_config_var($var,$val);
175 }
176  
177 #
178 # $self->_conf(VAR) returns the value of a variable
179 #
180  
181 48308 ▶ if (exists $$self{'data'}{'sections'}{$sect}{$var}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'data'}{'sections'}{$sect}{$var}) { } 48308 0 48308
182 48308 return $$self{'data'}{'sections'}{$sect}{$var};
183 } else {
184 0 carp "ERROR: [config] invalid config variable: $var";
185 0 return '';
186 }
187 }
188  
189 ########################################################################
190  
191 2678 ▶sub _fix_year {
Subroutine _fix_year: called CC=11 SCAR=24.0
192 2678 my($self,$y) = @_;
193 2678 my $istz = ref($self) eq 'Date::Manip::TZ';
194 2678 ▶ my $base = ($istz ? $self->base() : $self);
Branch: 1 branch
BranchTrueFalseTotal
$istz ? : 2659 19 2678
195  
196 2678 my $method = $base->_config('yytoyyyy');
197  
198 2678 ▶ return $y if (length($y)==4);
Branch: 1 branch
BranchTrueFalseTotal
if length $y == 4 2422 256 2678
199 256 ▶ return undef if (length($y)!=2);
Branch: 1 branch
BranchTrueFalseTotal
if length $y != 2 0 256 256
200  
201 256 my $curr_y;
202 256 ▶ if (ref($self) eq 'Date::Manip::TZ') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq 'Date::Manip::TZ') { } 241 15 256
203 241 $curr_y = $self->_now('y',1);
204 } else {
205 15 $curr_y = ( localtime(time) )[5];
206 15 $curr_y += 1900;
207 }
208  
209 256 ▶ if ($method eq 'c') {
Branch: 3 branches
BranchTrueFalseTotal
if ($method eq 'c') { } 3 253 256
elsif ($method =~ /^c(\d\d)$/) { } 3 250 253
elsif ($method =~ /^c(\d\d)(\d\d)$/) { } 2 248 250
210 3 return substr($curr_y,0,2) . $y;
211  
212 } elsif ($method =~ /^c(\d\d)$/) {
213 3 return "$1$y";
214  
215 } elsif ($method =~ /^c(\d\d)(\d\d)$/) {
216 2 ▶ return "$1$y" + ($y<$2 ? 100 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$y < $2 ? : 1 1 2
217  
218 } else {
219 248 my $y1 = $curr_y - $method;
220 248 my $y2 = $y1 + 99;
221 248 $y1 =~ /^(\d\d)/;
222 248 $y = "$1$y";
223 248 ▶ if ($y<$y1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $y1) 46 202 248
224 46 $y += 100;
225 }
226 248 ▶ if ($y>$y2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y > $y2) 0 248 248
227 0 $y -= 100;
228 }
229 248 return $y;
230 }
231 }
232  
233 ###############################################################################
234 # Functions for setting the default date/time
235  
236 # Many date operations use a default time and/or date to set some
237 # or all values. This function may be used to set or examine the
238 # default time.
239 #
240 # _now allows you to get the current date and/or time in the
241 # local timezone.
242 #
243 # The function performed depends on $op and are described in the
244 # following table:
245 #
246 # $op function
247 # ------------------ ----------------------------------
248 # undef Returns the current default values
249 # (y,m,d,h,mn,s) without updating
250 # the time (it'll update if it has
251 # never been set).
252 #
253 # 'now' Updates now and returns
254 # (y,m,d,h,mn,s)
255 #
256 # 'time' Updates now and Returns (h,mn,s)
257 #
258 # 'y' Returns the default value of one
259 # 'm' of the fields (no update)
260 # 'd'
261 # 'h'
262 # 'mn'
263 # 's'
264 #
265 # 'systz' Returns the system timezone
266 #
267 # 'isdst' Returns the 'now' values if set,
268 # 'tz' or system time values otherwise.
269 # 'offset'
270 # 'abb'
271 #
272 12994 ▶sub _now {
Subroutine _now: called CC=21 SCAR=35.1
273 12994 my($self,$op,$noupdate) = @_;
274 12994 my $istz = ref($self) eq 'Date::Manip::TZ';
275 12994 ▶ my $base = ($istz ? $self->base() : $self);
Branch: 1 branch
BranchTrueFalseTotal
$istz ? : 12994 0 12994
276  
277 # Update "NOW" if we're checking 'now', 'time', or the date
278 # is not set already.
279  
280 12994 ▶ if (! defined $noupdate) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $noupdate) 5614 7380 12994
281 5614 ▶ if ($op =~ /(?:now|time)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ /(?:now|time)/) { } 0 5614 5614
282 0 $noupdate = 0;
283 } else {
284 5614 $noupdate = 1;
285 }
286 }
287 12994 ▶ $noupdate = 0 if (! exists $$base{'data'}{'now'}{'date'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $base->{"data"}{"now"}{"date"} 4 12990 12994
288 12994 ▶ $self->_update_now() unless ($noupdate);
Branch: 1 branch
BranchTrueFalseTotal
unless $noupdate 1212 11782 12994
289  
290 # Now return the value of the operation
291  
292 12994 my @tmpnow = @{ $$base{'data'}{'tmpnow'} };
293 12994 ▶ my @now = (@tmpnow ? @tmpnow : @{ $$base{'data'}{'now'}{'date'} });
Branch: 1 branch
BranchTrueFalseTotal
@tmpnow ? : 5574 7420 12994
294  
295 12994 ▶ if ($op eq 'tz') {
Branch: 13 branches
BranchTrueFalseTotal
if ($op eq 'tz') { } 10712 2282 12994
elsif ($op eq 'systz') { } 0 2282 2282
elsif ($op eq 'isdst') { } 436 1846 2282
elsif ($op eq 'offset') { } 0 1846 1846
elsif ($op eq 'abb') { } 0 1846 1846
elsif ($op eq 'now') { } 194 1652 1846
elsif ($op eq 'y') { } 1519 133 1652
elsif ($op eq 'time') { } 16 117 133
elsif ($op eq 'm') { } 61 56 117
elsif ($op eq 'd') { } 56 0 56
elsif ($op eq 'h') { } 0 0 0
elsif ($op eq 'mn') { } 0 0 0
elsif ($op eq 's') { } 0 0 0
296 10712 ▶ if (exists $$base{'data'}{'now'}{'tz'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $base->{'data'}{'now'}{'tz'}) { } 10702 10 10712
297 10702 return $$base{'data'}{'now'}{'tz'};
298 } else {
299 10 return $$base{'data'}{'now'}{'systz'};
300 }
301  
302 } elsif ($op eq 'systz') {
303 0 return $$base{'data'}{'now'}{'systz'};
304  
305 } elsif ($op eq 'isdst') {
306 436 return $$base{'data'}{'now'}{'isdst'};
307  
308 } elsif ($op eq 'offset') {
309 0 return @{ $$base{'data'}{'now'}{'offset'} };
310  
311 } elsif ($op eq 'abb') {
312 0 return $$base{'data'}{'now'}{'abb'};
313  
314 } elsif ($op eq 'now') {
315 194 return @now;
316  
317 } elsif ($op eq 'y') {
318 1519 return $now[0];
319  
320 } elsif ($op eq 'time') {
321 16 return @now[3..5];
322  
323 } elsif ($op eq 'm') {
324 61 return $now[1];
325  
326 } elsif ($op eq 'd') {
327 56 return $now[2];
328  
329 } elsif ($op eq 'h') {
330 0 return $now[3];
331  
332 } elsif ($op eq 'mn') {
333 0 return $now[4];
334  
335 } elsif ($op eq 's') {
336 0 return $now[5];
337  
338 } else {
339 0 carp "ERROR: [now] invalid argument list: $op";
340 0 return ();
341 }
342 }
343  
344 2223 ▶sub _update_now {
Subroutine _update_now: called CC=5 SCAR=17.6
345 2223 my($self) = @_;
346 2223 my $istz = ref($self) eq 'Date::Manip::TZ';
347 2223 ▶ my $base = ($istz ? $self->base() : $self);
Branch: 1 branch
BranchTrueFalseTotal
$istz ? : 2223 0 2223
348  
349 # If we've called ForceDate, don't change it.
350 2223 ▶ return if ($$base{'data'}{'now'}{'force'});
Branch: 1 branch
BranchTrueFalseTotal
if $base->{"data"}{"now"}{"force"} 2219 4 2223
351  
352 # If we've called SetDate (which will only happen if a
353 # Date::Manip:TZ object is available), figure out what 'now' is
354 # based on the number of seconds that have elapsed since it was
355 # set. This will ONLY happen if TZ has been loaded.
356  
357 4 ▶ if ($$base{'data'}{'now'}{'set'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base->{"data"}{"now"}{"set"}) 0 4 4
358 0 my $date = $$base{'data'}{'now'}{'setdate'};
359 0 my $secs = time - $$base{'data'}{'now'}{'setsecs'};
360  
361 0 $date = $base->calc_date_time($date,[0,0,$secs]); # 'now' in GMT
362 0 my $zone = $self->_now('tz',1);
363 0 my ($err,$date2,$offset,$isdst,$abbrev) = $self->convert_from_gmt($date,$zone);
364  
365 0 $$base{'data'}{'now'}{'date'} = $date2;
366 0 $$base{'data'}{'now'}{'isdst'} = $isdst;
367 0 $$base{'data'}{'now'}{'offset'} = $offset;
368 0 $$base{'data'}{'now'}{'abb'} = $abbrev;
369 0 return;
370 }
371  
372 # Otherwise, we'll use the system time.
373  
374 4 my $time = time;
375 4 my($s,$mn,$h,$d,$m,$y,$wday,$yday,$isdst) = localtime($time);
376 4 my($s0,$mn0,$h0,$d0,$m0,$y0) = gmtime($time);
377  
378 4 $y += 1900;
379 4 $m++;
380  
381 4 $y0 += 1900;
382 4 $m0++;
383  
384 4 my $off = $base->calc_date_date([$y,$m,$d,$h,$mn,$s],[$y0,$m0,$d0,$h0,$mn0,$s0],1);
385  
386 4 $$base{'data'}{'now'}{'date'} = [$y,$m,$d,$h,$mn,$s];
387 4 $$base{'data'}{'now'}{'isdst'} = $isdst;
388 4 $$base{'data'}{'now'}{'offset'}= $off;
389  
390 4 my $abb = '???';
391 4 ▶ if (ref($self) eq 'Date::Manip::TZ') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq "Date::Manip::TZ") 4 0 4
392 4 my $zone = $self->_now('tz',1);
393 4 my $per = $self->date_period([$y,$m,$d,$h,$mn,$s],$zone,1,$isdst);
394 4 $abb = $$per[4];
395 }
396  
397 4 $$base{'data'}{'now'}{'abb'} = $abb;
398  
399 4 return;
400 }
401  
402 ###############################################################################
403 # This sorts from longest to shortest element
404 #
405 170 ▶no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
406 474 ▶sub _sortByLength {
Subroutine _sortByLength: called CC=1 SCAR=0.0
407 474 return (length $b <=> length $a);
408 }
409 170 ▶use strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
410  
411 170 1;
412 # Local Variables:
413 # mode: cperl
414 # indent-tabs-mode: nil
415 # cperl-indent-level: 3
416 # cperl-continued-statement-offset: 2
417 # cperl-continued-brace-offset: 0
418 # cperl-brace-offset: 0
419 # cperl-brace-imaginary-offset: 0
420 # cperl-label-offset: 0
421 # End:
« lib/Date/Manip/Base.pm ↑ Summary lib/Date/Manip/Obj.pm »