DateTimeX/Lite/OlsonDB.pm

filter: Statementstmt 29.5% 121 / 409 Branchbran 13.3% 25 / 188 Conditioncond 3.5% 3 / 84 MC/DCmcdc 2.0% 1 / 48 Subroutinesub 45.2% 33 / 73 Podpod 0.0% 0 / 9 totaltotal 22.5% 183 / 811 CC 136 SCAR 49.8
CC
136
Cov
22%
_first_rule
756.0
add_change
240.0
expand_from_rules
182.0
CRAP 145.6
↑ Summary DateTimeX/Lite/TimeZone/Local/Unix.pm »
1 package DateTimeX::Lite::OlsonDB;
2 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 56 ▶use DateTimeX::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 56 ▶use vars qw( %MONTHS %DAYS $PLUS_ONE_DAY_DUR $MINUS_ONE_DAY_DUR );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 sub DEBUG () { 0 }
9  
10 56 my $x = 1;
11 56 %MONTHS = map { $_ => $x++ }
12 qw( Jan Feb Mar Apr May Jun
13 Jul Aug Sep Oct Nov Dec);
14  
15 56 $x = 1;
16 56 %DAYS = map { $_ => $x++ }
17 qw( Mon Tue Wed Thu Fri Sat Sun );
18  
19 56 $PLUS_ONE_DAY_DUR = DateTimeX::Lite::Duration->new( days => 1 );
20 56 $MINUS_ONE_DAY_DUR = DateTimeX::Lite::Duration->new( days => -1 );
21  
22 sub new
23 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
24 0 my $class = shift;
25  
26 0 return bless { rules => {},
27 zones => {},
28 links => {},
29 }, $class;
30 }
31  
32 0 ▶sub parse_file {
Subroutine parse_file: not called CC=3 SCAR=24.8
Pod undocumented
33 0 my $self = shift;
34 0 my $file = shift;
35  
36 0 ▶ open my $fh, "<$file"
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<$file" or die "Cannot read ${file}: $!" 0 0 0
37 or die "Cannot read $file: $!";
38  
39 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
40 0 chomp;
41 0 $self->_parse_line($_);
42 }
43 }
44  
45 0 ▶sub _parse_line {
Subroutine _parse_line: not called CC=7 SCAR=40.3
46 0 my $self = shift;
47 0 my $line = shift;
48  
49 0 ▶ return if $line =~ /^\s+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s+$/ 0 0 0
50 0 ▶ return if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 0 0
51  
52 # remove any comments at the end of the line
53 0 $line =~ s/\s*#.+$//;
54  
55 0 ▶ if ( $self->{in_zone} && $line =~ /^\t/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_zone"} and $line =~ /^\t/) 0 0 0
Condition0%0/3
$self->{"in_zone"} and $line =~ /^\t/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"in_zone"} and $line =~ /^\t/
$self->{"in_zone"} $line =~ /^\t/
Truth table0%0/3
A and B
A= $self->{"in_zone"}B= $line =~ /^\t/
ABresult
0X0
100
111
56 {
57 0 $self->_parse_zone( $line, $self->{in_zone} );
58 0 return;
59 }
60  
61 0 foreach ( qw( Rule Zone Link ) )
62 {
63 0 ▶ if ( substr( $line, 0, 4 ) eq $_ )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($line, 0, 4) eq $_) 0 0 0
64 {
65 0 my $m = '_parse_' . lc $_;
66 0 $self->$m($line);
67 }
68 }
69 }
70  
71 sub _parse_rule
72 0 ▶{
Subroutine _parse_rule: not called CC=3 SCAR=24.8
73 0 my $self = shift;
74 0 my $rule = shift;
75  
76 0 my @items = split /\s+/, $rule, 10;
77  
78 0 shift @items;
79 0 my $name = shift @items;
80  
81 0 my %rule;
82 0 @rule{ qw( from to type in on at save letter ) } = @items;
83 0 ▶ delete $rule{letter} if $rule{letter} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $rule{"letter"} eq "-" 0 0 0
84  
85 # As of the 2003a data, there are no rules with a type set
86 0 ▶ delete $rule{type} if $rule{type} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $rule{"type"} eq "-" 0 0 0
87  
88 0 push @{ $self->{rules}{$name} },
89 DateTimeX::Lite::OlsonDB::Rule->new( name => $name, %rule );
90  
91 0 undef $self->{in_zone};
92 }
93  
94 sub _parse_zone
95 0 ▶{
Subroutine _parse_zone: not called CC=7 SCAR=40.3
96 0 my $self = shift;
97 0 my $zone = shift;
98 0 my $name = shift;
99  
100 0 ▶ my $expect = $name ? 5 : 6;
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
101 0 ▶ my @items = grep { defined && length } split /\s+/, $zone, $expect;
Condition0%0/3
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
102  
103 0 my %obs;
104 0 ▶ unless ($name)
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
105 {
106 0 shift @items; # remove "Zone"
107 0 $name = shift @items;
108 }
109  
110 0 @obs{ qw( gmtoff rules format until ) } = @items;
111  
112 0 ▶ if ( $obs{rules} =~ /\d\d?:\d\d/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($obs{'rules'} =~ /\d\d?:\d\d/) { } 0 0 0
113 {
114 0 $obs{offset_from_std} = delete $obs{rules};
115 }
116 else
117 {
118 0 ▶ delete $obs{rules} if $obs{rules} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $obs{"rules"} eq "-" 0 0 0
119 }
120  
121 0 ▶ delete $obs{until} unless defined $obs{until};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obs{"until"} 0 0 0
122  
123 0 push @{ $self->{zones}{$name} }, \%obs;
124  
125 0 $self->{in_zone} = $name;
126 }
127  
128 sub _parse_link
129 0 ▶{
Subroutine _parse_link: not called CC=1 SCAR=6.9
130 0 my $self = shift;
131 0 my $link = shift;
132  
133 0 my @items = split /\s+/, $link, 3;
134  
135 0 $self->{links}{ $items[2] } = $items[1];
136  
137 0 undef $self->{in_zone};
138 }
139  
140 0 ▶sub links { %{ $_[0]->{links} } }
Subroutine links: not called CC=1 SCAR=6.9
Pod undocumented
141  
142 0 ▶sub zone_names { keys %{ $_[0]->{zones} } }
Subroutine zone_names: not called CC=1 SCAR=6.9
Pod undocumented
143  
144 sub zone
145 0 ▶{
Subroutine zone: not called CC=2 SCAR=17.9
Pod undocumented
146 0 my $self = shift;
147 0 my $name = shift;
148  
149 die "Invalid zone name $name"
150 0 ▶ unless exists $self->{zones}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"zones"}{$name} 0 0 0
151  
152 return
153 DateTimeX::Lite::OlsonDB::Zone->new
154 ( name => $name,
155 0 observances => $self->{zones}{$name},
156 olson_db => $self,
157 );
158 }
159  
160 sub expanded_zone
161 0 ▶{
Subroutine expanded_zone: not called CC=2 SCAR=17.9
Pod undocumented
162 0 my ($self, %p) = @_;
163  
164 0 ▶ $p{expand_to_year} ||= (localtime)[5] + 1910;
Condition0%0/3
$p{"expand_to_year"} ||= (localtime)[5] + 1910
l!l&&r!l&&!r
000
MC/DC0%0/2
$p{"expand_to_year"} ||= (localtime)[5] + 1910
$p{"expand_to_year"} (localtime)[5] + 1910
Truth table0%0/3
A ||= B
A= $p{"expand_to_year"}B= (localtime)[5] + 1910
ABresult
1X1
011
000
165  
166 0 my $zone = $self->zone( $p{name} );
167  
168 0 $zone->expand_observances( $self, $p{expand_to_year} );
169  
170 0 return $zone;
171 }
172  
173 sub rules_by_name
174 0 ▶{
Subroutine rules_by_name: not called CC=3 SCAR=24.8
Pod undocumented
175 0 my $self = shift;
176 0 my $name = shift;
177  
178 0 ▶ return unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
179  
180 die "Invalid rule name $name"
181 0 ▶ unless exists $self->{rules}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"rules"}{$name} 0 0 0
182  
183 0 return @{ $self->{rules}{$name} };
184 }
185  
186 sub parse_day_spec
187 1246 ▶{
Subroutine parse_day_spec: called CC=7 SCAR=19.7
Pod undocumented
188 1246 my ( $day, $month, $year ) = @_;
189  
190 1246 ▶ return $day if $day =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $day =~ /^\d+$/ 0 1246 1246
191  
192 1246 ▶ if ( $day =~ /^last(\w\w\w)$/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($day =~ /^last(\w\w\w)$/) { } 46 1200 1246
elsif ($day =~ /^(\w\w\w)([><])=(\d\d?)$/) { } 1200 0 1200
193 {
194 46 my $dow = $DateTimeX::Lite::OlsonDB::DAYS{$1};
195  
196 46 my $last_day = DateTimeX::Lite->last_day_of_month( year => $year,
197 month => $month,
198 time_zone => 'floating',
199 );
200  
201 46 my $dt =
202 DateTimeX::Lite->new( year => $year,
203 month => $month,
204 day => $last_day->day,
205 time_zone => 'floating',
206 );
207  
208 46 ▶ while ( $dt->day_of_week != $dow )
Branch: 1 branch
BranchTrueFalseTotal
if ($dt->day_of_week != $dow) 135 46 181
209 {
210 135 $dt -= $PLUS_ONE_DAY_DUR;
211 }
212  
213 46 return $dt->day;
214 }
215 elsif ( $day =~ /^(\w\w\w)([><])=(\d\d?)$/ )
216 {
217 1200 my $dow = $DateTimeX::Lite::OlsonDB::DAYS{$1};
218  
219 1200 my $dt = DateTimeX::Lite->new( year => $year,
220 month => $month,
221 day => $3,
222 time_zone => 'floating',
223 );
224  
225 1200 ▶ my $dur = $2 eq '<' ? $MINUS_ONE_DAY_DUR : $PLUS_ONE_DAY_DUR;
Branch: 1 branch
BranchTrueFalseTotal
$2 eq '<' ? : 0 1200 1200
226  
227 1200 ▶ while ( $dt->day_of_week != $dow )
Branch: 1 branch
BranchTrueFalseTotal
if ($dt->day_of_week != $dow) 3648 1200 4848
228 {
229 3648 $dt += $dur;
230 }
231  
232 1200 return $dt->day;
233 }
234 else
235 {
236 0 die "Invalid on spec for rule: $day\n";
237 }
238 }
239  
240 1246 ▶sub utc_datetime_for_time_spec {
Subroutine utc_datetime_for_time_spec: called CC=7 SCAR=19.9
Pod undocumented
241 1246 my %p = @_;
242  
243 # 'w'all - ignore it, because that's the default
244 1246 $p{spec} =~ s/w$//;
245  
246 # 'g'reenwich, 'u'tc, or 'z'ulu
247 1246 my $is_utc = $p{spec} =~ s/[guz]$//;
248  
249 # 's'tandard time - ignore DS offset
250 1246 my $is_std = $p{spec} =~ s/s$//;
251  
252 1246 my ($hour, $minute, $second) = split /:/, $p{spec};
253 1246 ▶ $minute = 0 unless defined $minute;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $minute 0 1246 1246
254 1246 ▶ $second = 0 unless defined $second;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $second 1246 0 1246
255  
256 1246 my $add_day = 0;
257 1246 ▶ if ( $hour == 24 )
Branch: 1 branch
BranchTrueFalseTotal
if ($hour == 24) 0 1246 1246
258 {
259 0 $hour = 0;
260 0 $add_day = 1;
261 }
262  
263 1246 my $utc;
264 1246 ▶ if ($is_utc)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_utc) { } 46 1200 1246
265 {
266 $utc = DateTimeX::Lite->new( year => $p{year},
267 month => $p{month},
268 day => $p{day},
269 46 hour => $hour,
270 minute => $minute,
271 second => $second,
272 time_zone => 'floating',
273 );
274 }
275 else
276 {
277 my $local = DateTimeX::Lite->new( year => $p{year},
278 month => $p{month},
279 day => $p{day},
280 1200 hour => $hour,
281 minute => $minute,
282 second => $second,
283 time_zone => 'floating',
284 );
285  
286 1200 ▶ $p{offset_from_std} = 0 if $is_std;
Branch: 1 branch
BranchTrueFalseTotal
if $is_std 46 1154 1200
287  
288 my $dur =
289 DateTimeX::Lite::Duration->new
290 1200 ( seconds => $p{offset_from_utc} + $p{offset_from_std} );
291  
292 1200 $utc = $local - $dur;
293 }
294  
295 1246 ▶ $utc->add( days => 1 ) if $add_day;
Branch: 1 branch
BranchTrueFalseTotal
if $add_day 0 1246 1246
296  
297 1246 return $utc;
298 }
299  
300  
301 package DateTimeX::Lite::OlsonDB::Zone;
302  
303 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
304  
305 56 ▶use DateTimeX::Lite::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
306  
307 sub new
308 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
309 0 my ($class, %p) = @_;
310  
311 my $self = { name => $p{name},
312 observances => $p{observances},
313 0 changes => [],
314 infinite_rules => {},
315 };
316  
317 0 return bless $self, $class;
318 }
319  
320 0 ▶sub name { $_[0]->{name} }
Subroutine name: not called CC=1 SCAR=6.9
321  
322 sub expand_observances
323 0 ▶{
Subroutine expand_observances: not called CC=11 SCAR=48.8
324 0 my $self = shift;
325 0 my $odb = shift;
326 0 my $max_year = shift;
327  
328 0 my $prev_until;
329 0 ▶ for ( my $x = 0; $x < @{ $self->{observances} }; $x++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($x < @{$self->{"observances"};}) 0 0 0
330 {
331 0 my %p = %{ $self->{observances}[$x] };
332 0 my $rules_name = delete $p{rules};
333  
334 0 ▶ my $last_offset_from_std =
Branch: 1 branch
BranchTrueFalseTotal
$self->last_change ? : 0 0 0
335 $self->last_change ? $self->last_change->offset_from_std : 0;
336 0 ▶ my $last_offset_from_utc =
Branch: 1 branch
BranchTrueFalseTotal
$self->last_change ? : 0 0 0
337 $self->last_change ? $self->last_change->offset_from_utc : 0;
338  
339 0 my $obs =
340 DateTimeX::Lite::OlsonDB::Observance->new
341 ( %p,
342 utc_start_datetime => $prev_until,
343 rules => [ $odb->rules_by_name($rules_name) ],
344 last_offset_from_utc => $last_offset_from_utc,
345 last_offset_from_std => $last_offset_from_std,
346 );
347  
348 0 my $rule = $obs->first_rule;
349 0 ▶ my $letter = $rule ? $rule->letter : '';
Branch: 1 branch
BranchTrueFalseTotal
$rule ? : 0 0 0
350  
351 0 ▶ my $change =
Branch: 1 branch
BranchTrueFalseTotal
$rule ? : 0 0 0
352 DateTimeX::Lite::OlsonDB::Change->new
353 ( type => 'observance',
354 utc_start_datetime => $obs->utc_start_datetime,
355 local_start_datetime => $obs->local_start_datetime,
356 short_name => sprintf( $obs->format, $letter ),
357 observance => $obs,
358 $rule ? ( rule => $rule ) : (),
359 );
360  
361 0 if (DateTimeX::Lite::OlsonDB::DEBUG)
362 {
363 warn "Adding observance change ...\n";
364  
365 $change->_debug_output;
366 }
367  
368 0 $self->add_change($change);
369  
370 0 ▶ if ( $obs->rules )
Branch: 1 branch
BranchTrueFalseTotal
if ($obs->rules) 0 0 0
371 {
372 0 $obs->expand_from_rules( $self, $max_year );
373 }
374  
375 $prev_until =
376 0 ▶ $obs->until( $self->last_change ? $self->last_change->offset_from_std : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$self->last_change ? : 0 0 0
377  
378 # last observance
379 0 ▶ if ( $x == $#{ $self->{observances} } )
Branch: 1 branch
BranchTrueFalseTotal
if ($x == $#{$$self{"observances"};}) 0 0 0
380 {
381 0 foreach my $rule ( $obs->rules )
382 {
383 0 ▶ if ( $rule->is_infinite )
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->is_infinite) 0 0 0
384 {
385 0 $self->add_infinite_rule($rule);
386 }
387 }
388 }
389 }
390 }
391  
392 sub add_change
393 0 ▶{
Subroutine add_change: not called CC=15 SCAR=54.8
394 0 my $self = shift;
395 0 my $change = shift;
396  
397 0 ▶ if ( defined $change->utc_start_datetime )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $change->utc_start_datetime) { } 0 0 0
398 {
399 0 ▶ if ( @{ $self->{changes} }
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"changes"};} and $self->{"changes"}[-1]->utc_start_datetime and $self->{"changes"}[-1]->utc_start_datetime == $change->utc_start_datetime) 0 0 0
Condition0%0/6
@{$self->{"changes"};} and $self->{"changes"}[-1]->utc_start_datetime and $self->{"changes"}[-1]->utc_start_datetime == $change->utc_start_datetime
!ll&&!rl&&r
000
@{$self->{"changes"};} and $self->{"changes"}[-1]->utc_start_datetime
!ll&&!rl&&r
000
MC/DC0%0/3
@{$self->{"changes"};} and $self->{"changes"}[-1]->utc_start_datetime and $self->{"changes"}[-1]->utc_start_datetime == $change->utc_start_datetime
@{$self->{"changes"};} $self->{"changes"}[-1]->utc_start_datetime $self->{"changes"}[-1]->utc_start_datetime == $change->utc_start_datetime
Truth table0%0/4
A and B and C
A= @{$self->{"changes"};}B= $self->{"changes"}[-1]->utc_start_datetimeC= $self->{"changes"}[-1]->utc_start_datetime == $change->utc_start_datetime
ABCresult
0XX0
10X0
1100
1111
400 && $self->{changes}[-1]->utc_start_datetime
401 && $self->{changes}[-1]->utc_start_datetime == $change->utc_start_datetime
402 )
403 {
404 0 ▶ if ( $self->{changes}[-1]->rule && $change->observance )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"changes"}[-1]->rule and $change->observance) 0 0 0
Condition0%0/3
$self->{"changes"}[-1]->rule and $change->observance
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"changes"}[-1]->rule and $change->observance
$self->{"changes"}[-1]->rule $change->observance
Truth table0%0/3
A and B
A= $self->{"changes"}[-1]->ruleB= $change->observance
ABresult
0X0
100
111
405 {
406 0 warn " Ignoring previous rule change, that starts the same time as current observance change\n\n"
407 if DateTimeX::Lite::OlsonDB::DEBUG;
408  
409 0 $self->{changes}[-1] = $change;
410  
411 0 return;
412 }
413  
414 0 die "Cannot add two different changes that have the same UTC start datetime!\n";
415 }
416  
417 0 my $last_change = $self->last_change;
418  
419 0 ▶ if ( $last_change->short_name eq $change->short_name
Branch: 1 branch
BranchTrueFalseTotal
if ($last_change->short_name eq $change->short_name and $last_change->total_offset == $change->total_offset and $last_change->is_dst == $change->is_dst and $last_change->observance eq $change->observance) 0 0 0
Condition0%0/9
$last_change->short_name eq $change->short_name and $last_change->total_offset == $change->total_offset and $last_change->is_dst == $change->is_dst and $last_change->observance eq $change->observance
!ll&&!rl&&r
000
$last_change->short_name eq $change->short_name and $last_change->total_offset == $change->total_offset and $last_change->is_dst == $change->is_dst
!ll&&!rl&&r
000
$last_change->short_name eq $change->short_name and $last_change->total_offset == $change->total_offset
!ll&&!rl&&r
000
MC/DC0%0/4
$last_change->short_name eq $change->short_name and $last_change->total_offset == $change->total_offset and $last_change->is_dst == $change->is_dst and $last_change->observance eq $change->observance
$last_change->short_name eq $change->short_name $last_change->total_offset == $change->total_offset $last_change->is_dst == $change->is_dst $last_change->observance eq $change->observance
Truth table0%0/5
A and B and C and D
A= $last_change->short_name eq $change->short_nameB= $last_change->total_offset == $change->total_offsetC= $last_change->is_dst == $change->is_dstD= $last_change->observance eq $change->observance
ABCDresult
0XXX0
10XX0
110X0
11100
11111
420 && $last_change->total_offset == $change->total_offset
421 && $last_change->is_dst == $change->is_dst
422 && $last_change->observance eq $change->observance
423 )
424 {
425 0 ▶ my $last_rule = $last_change->rule || '';
Condition0%0/2
$last_change->rule || ''
l!l
00
MC/DC0%0/1
$last_change->rule || ''
$last_change->rule
Truth table0%0/2
A
A= $last_change->rule
Aresult
11
00
426 0 ▶ my $new_rule = $change->rule || '';
Condition0%0/2
$change->rule || ''
l!l
00
MC/DC0%0/1
$change->rule || ''
$change->rule
Truth table0%0/2
A
A= $change->rule
Aresult
11
00
427  
428 0 ▶ if ( $last_rule eq $new_rule )
Branch: 1 branch
BranchTrueFalseTotal
if ($last_rule eq $new_rule) 0 0 0
429 {
430 0 warn "Skipping identical change\n" if DateTimeX::Lite::OlsonDB::DEBUG;
431  
432 0 return;
433 }
434 }
435  
436 0 push @{ $self->{changes} }, $change;
437 }
438 else
439 {
440 0 ▶ if ( $self->{earliest} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'earliest'}) { } 0 0 0
441 {
442 0 die "There can only be one earliest time zone change!";
443 }
444 else
445 {
446 0 $self->{earliest} = $change;
447 }
448 }
449 }
450  
451 sub add_infinite_rule
452 0 ▶{
Subroutine add_infinite_rule: not called CC=1 SCAR=6.9
453 0 $_[0]->{infinite_rules}{ $_[1] } = $_[1];
454 }
455  
456 0 ▶sub last_change { return unless @{ $_[0]->{changes} } || $_[0]->{earliest};
Subroutine last_change: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
unless @{$_[0]{"changes"};} or $_[0]{"earliest"} 0 0 0
Condition0%0/3
@{$_[0]{"changes"};} or $_[0]{"earliest"}
l!l&&r!l&&!r
000
MC/DC0%0/2
@{$_[0]{"changes"};} or $_[0]{"earliest"}
@{$_[0]{"changes"};} $_[0]{"earliest"}
Truth table0%0/3
A or B
A= @{$_[0]{"changes"};}B= $_[0]{"earliest"}
ABresult
1X1
011
000
457 0 return ( @{ $_[0]->{changes} } ?
458 $_[0]->{changes}[-1] :
459 0 ▶ $_[0]->{earliest} ); }
Branch: 1 branch
BranchTrueFalseTotal
@{$_[0]{'changes'};} ? : 0 0 0
460  
461 0 ▶sub sorted_changes { ( ( defined $_[0]->{earliest} ? $_[0]->{earliest} : () ),
Subroutine sorted_changes: not called CC=2 SCAR=17.9
462 0 sort { $a->utc_start_datetime <=> $b->utc_start_datetime }
463 0 ▶ @{ $_[0]->{changes} } ) }
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'earliest'} ? : 0 0 0
464  
465 0 ▶sub infinite_rules { values %{ $_[0]->{infinite_rules} } }
Subroutine infinite_rules: not called CC=1 SCAR=6.9
466  
467  
468 package DateTimeX::Lite::OlsonDB::Observance;
469  
470 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
471  
472 56 ▶use DateTimeX::Lite::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
473  
474 sub new
475 0 ▶{
Subroutine new: not called CC=7 SCAR=40.3
476 0 my ($class, %p) = @_;
477  
478 0 ▶ $p{until} ||= '';
Condition0%0/2
$p{"until"} ||= ""
l!l
00
MC/DC0%0/1
$p{"until"} ||= ""
$p{"until"}
Truth table0%0/2
A
A= $p{"until"}
Aresult
11
00
479 0 ▶ $p{offset_from_std} ||= 0;
Condition0%0/2
$p{"offset_from_std"} ||= 0
l!l
00
MC/DC0%0/1
$p{"offset_from_std"} ||= 0
$p{"offset_from_std"}
Truth table0%0/2
A
A= $p{"offset_from_std"}
Aresult
11
00
480 0 ▶ $p{last_offset_from_utc} ||= 0;
Condition0%0/2
$p{"last_offset_from_utc"} ||= 0
l!l
00
MC/DC0%0/1
$p{"last_offset_from_utc"} ||= 0
$p{"last_offset_from_utc"}
Truth table0%0/2
A
A= $p{"last_offset_from_utc"}
Aresult
11
00
481 0 ▶ $p{last_offset_from_std} ||= 0;
Condition0%0/2
$p{"last_offset_from_std"} ||= 0
l!l
00
MC/DC0%0/1
$p{"last_offset_from_std"} ||= 0
$p{"last_offset_from_std"}
Truth table0%0/2
A
A= $p{"last_offset_from_std"}
Aresult
11
00
482  
483 0 my $offset_from_utc = DateTimeX::Lite::Util::offset_as_seconds( $p{gmtoff} );
484 0 my $offset_from_std = DateTimeX::Lite::Util::offset_as_seconds( $p{offset_from_std} );
485  
486 0 my $last_offset_from_utc = delete $p{last_offset_from_utc};
487 0 my $last_offset_from_std = delete $p{last_offset_from_std};
488  
489 my $self = bless { %p,
490 offset_from_utc => $offset_from_utc,
491 offset_from_std => $offset_from_std,
492 0 until => [ split /\s+/, $p{until} ],
493 }, $class;
494  
495 0 my $local_start_datetime;
496 0 ▶ if ( $p{utc_start_datetime} )
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"utc_start_datetime"}) 0 0 0
497 {
498 $self->{first_rule} =
499 0 $self->_first_rule( $last_offset_from_utc, $last_offset_from_std );
500  
501 0 ▶ $offset_from_std += $self->{first_rule}->offset_from_std if $self->{first_rule};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"first_rule"} 0 0 0
502  
503 0 $local_start_datetime = $p{utc_start_datetime}->clone;
504  
505 0 $local_start_datetime +=
506 DateTimeX::Lite::Duration->new( seconds => $offset_from_utc + $offset_from_std );
507  
508 0 $self->{local_start_datetime} = $local_start_datetime;
509 }
510  
511 0 return $self;
512 }
513  
514 2410 ▶sub offset_from_utc { $_[0]->{offset_from_utc} }
Subroutine offset_from_utc: called CC=1 SCAR=0.0
515 3615 ▶sub offset_from_std { $_[0]->{offset_from_std} }
Subroutine offset_from_std: called CC=1 SCAR=0.0
516 1205 ▶sub total_offset { $_[0]->offset_from_utc + $_[0]->offset_from_std }
Subroutine total_offset: called CC=1 SCAR=0.0
517  
518 0 ▶sub rules { @{ $_[0]->{rules} } }
Subroutine rules: not called CC=1 SCAR=6.9
519 0 ▶sub first_rule { $_[0]->{first_rule} }
Subroutine first_rule: not called CC=1 SCAR=6.9
520  
521 1205 ▶sub format { $_[0]->{format} }
Subroutine format: called CC=1 SCAR=0.0
522  
523 0 ▶sub utc_start_datetime { $_[0]->{utc_start_datetime} }
Subroutine utc_start_datetime: not called CC=1 SCAR=6.9
524 0 ▶sub local_start_datetime { $_[0]->{local_start_datetime} }
Subroutine local_start_datetime: not called CC=1 SCAR=6.9
525  
526 sub expand_from_rules
527 0 ▶{
Subroutine expand_from_rules: not called CC=13 SCAR=52.0
528 0 my $self = shift;
529 0 my $zone = shift;
530 # real max is year + 1 so we include max year
531 0 my $max_year = (shift) + 1;
532  
533 0 my $min_year;
534  
535 0 ▶ if ( $self->utc_start_datetime )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->utc_start_datetime) { } 0 0 0
536 {
537 0 $min_year = $self->utc_start_datetime->year;
538 }
539 else
540 {
541 # There is at least one time zone that has an infinite
542 # observance, but that observance has rules that only start at
543 # a certain point - Pacific/Chatham
544  
545 # In this case we just find the earliest rule and start there
546  
547 0 $min_year = ( sort { $a <=> $b } map { $_->min_year } $self->rules )[0];
548 }
549  
550 0 my $until = $self->until( $zone->last_change->offset_from_std );
551 0 ▶ if ($until)
Branch: 1 branch
BranchTrueFalseTotal
if ($until) { } 0 0 0
552 {
553 0 $max_year = $until->year;
554 }
555 else
556 {
557 # Some zones, like Asia/Tehran, have a predefined fixed set of
558 # rules that go well into the future (2037 for Asia/Tehran)
559 0 my $max_rule_year = 0;
560 0 foreach my $rule ( $self->rules )
561 {
562 0 ▶ $max_rule_year = $rule->max_year
Branch: 1 branch
BranchTrueFalseTotal
if $rule->max_year and $rule->max_year > $max_rule_year 0 0 0
Condition0%0/3
$rule->max_year and $rule->max_year > $max_rule_year
!ll&&!rl&&r
000
MC/DC0%0/2
$rule->max_year and $rule->max_year > $max_rule_year
$rule->max_year $rule->max_year > $max_rule_year
Truth table0%0/3
A and B
A= $rule->max_yearB= $rule->max_year > $max_rule_year
ABresult
0X0
100
111
563 if $rule->max_year && $rule->max_year > $max_rule_year;
564 }
565  
566 0 ▶ $max_year = $max_rule_year if $max_rule_year > $max_year;
Branch: 1 branch
BranchTrueFalseTotal
if $max_rule_year > $max_year 0 0 0
567 }
568  
569 0 foreach my $year ( $min_year .. $max_year )
570 {
571 0 my @rules = $self->_sorted_rules_for_year($year);
572  
573 0 foreach my $rule (@rules)
574 {
575 0 my $dt =
576 $rule->utc_start_datetime_for_year
577 ( $year, $self->offset_from_utc, $zone->last_change->offset_from_std );
578  
579 0 ▶ next if $self->utc_start_datetime && $dt <= $self->utc_start_datetime;
Branch: 1 branch
BranchTrueFalseTotal
if $self->utc_start_datetime and $dt <= $self->utc_start_datetime 0 0 0
Condition0%0/3
$self->utc_start_datetime and $dt <= $self->utc_start_datetime
!ll&&!rl&&r
000
MC/DC0%0/2
$self->utc_start_datetime and $dt <= $self->utc_start_datetime
$self->utc_start_datetime $dt <= $self->utc_start_datetime
Truth table0%0/3
A and B
A= $self->utc_start_datetimeB= $dt <= $self->utc_start_datetime
ABresult
0X0
100
111
580  
581 0 my $until = $self->until( $zone->last_change->offset_from_std );
582  
583 0 ▶ next if $until && $dt >= $until;
Branch: 1 branch
BranchTrueFalseTotal
if $until and $dt >= $until 0 0 0
Condition0%0/3
$until and $dt >= $until
!ll&&!rl&&r
000
MC/DC0%0/2
$until and $dt >= $until
$until $dt >= $until
Truth table0%0/3
A and B
A= $untilB= $dt >= $until
ABresult
0X0
100
111
584  
585 my $change =
586 DateTimeX::Lite::OlsonDB::Change->new
587 ( type => 'rule',
588 utc_start_datetime => $dt,
589 local_start_datetime =>
590 $dt +
591 DateTimeX::Lite::Duration->new
592 ( seconds => $self->total_offset + $rule->offset_from_std ),
593 0 short_name => sprintf( $self->{format}, $rule->letter ),
594 observance => $self,
595 rule => $rule,
596 );
597  
598 0 if (DateTimeX::Lite::OlsonDB::DEBUG)
599 {
600 warn "Adding rule change ...\n";
601  
602 $change->_debug_output;
603 }
604  
605 0 $zone->add_change($change);
606 }
607 }
608 }
609  
610 sub _sorted_rules_for_year
611 0 ▶{
Subroutine _sorted_rules_for_year: not called CC=3 SCAR=24.8
612 0 my $self = shift;
613 0 my $year = shift;
614  
615 return
616 0 ( map { $_->[0] }
617 0 sort { $a->[1] <=> $b->[1] }
618 0 map { my $dt = $_->utc_start_datetime_for_year( $year, $self->offset_from_utc, 0 );
619 0 [ $_, $dt ] }
620 0 ▶ grep { $_->min_year <= $year && ( ( ! $_->max_year ) || $_->max_year >= $year ) }
Condition0%0/6
$_->min_year <= $year && (!$_->max_year || $_->max_year >= $year)
!ll&&!rl&&r
000
not $_->max_year or $_->max_year >= $year
l!l&&r!l&&!r
000
MC/DC0%0/3
$_->min_year <= $year && (!$_->max_year || $_->max_year >= $year)
$_->min_year <= $year not $_->max_year $_->max_year >= $year
Truth table0%0/4
A && not(B) or C
A= $_->min_year <= $yearB= not $_->max_yearC= $_->max_year >= $year
ABCresult
0XX0
1000
11X1
1011
621 $self->rules
622 );
623 }
624  
625 sub until
626 0 ▶{
Subroutine until: not called CC=3 SCAR=24.8
627 0 my $self = shift;
628 0 ▶ my $offset_from_std = shift || $self->offset_from_std;
Condition0%0/3
shift() || $self->offset_from_std
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->offset_from_std
shift() $self->offset_from_std
Truth table0%0/3
A || B
A= shift()B= $self->offset_from_std
ABresult
1X1
011
000
629  
630 0 ▶ return unless defined $self->until_year;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->until_year 0 0 0
631  
632 0 my $utc =
633 DateTimeX::Lite::OlsonDB::utc_datetime_for_time_spec
634 ( spec => $self->until_time_spec,
635 year => $self->until_year,
636 month => $self->until_month,
637 day => $self->until_day,
638 offset_from_utc => $self->offset_from_utc,
639 offset_from_std => $offset_from_std,
640 );
641  
642 0 return $utc;
643 }
644  
645 0 ▶sub until_year { $_[0]->{until}[0] }
Subroutine until_year: not called CC=1 SCAR=6.9
646  
647 sub until_month
648 0 ▶{
Subroutine until_month: not called CC=2 SCAR=17.9
649 ( defined $_[0]->{until}[1] ?
650 0 ▶ $DateTimeX::Lite::OlsonDB::MONTHS{ $_[0]->{until}[1] } :
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'until'}[1] ? : 0 0 0
651 1
652 );
653 }
654  
655 sub until_day
656 0 ▶{
Subroutine until_day: not called CC=2 SCAR=17.9
657 ( defined $_[0]->{until}[2]
658 ? DateTimeX::Lite::OlsonDB::parse_day_spec
659 0 ▶ ( $_[0]->{until}[2], $_[0]->until_month, $_[0]->until_year )
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'until'}[2] ? : 0 0 0
660 : 1
661 );
662 }
663  
664 sub until_time_spec
665 0 ▶{
Subroutine until_time_spec: not called CC=2 SCAR=17.9
666 0 ▶ defined $_[0]->{until}[3] ? $_[0]->{until}[3] : '00:00:00';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'until'}[3] ? : 0 0 0
667 }
668  
669 sub _first_rule
670 0 ▶{
Subroutine _first_rule: not called CC=27 SCAR=66.3
671 0 my $self = shift;
672 0 my $last_offset_from_utc = shift;
673 0 my $last_offset_from_std = shift;
674  
675 0 ▶ return unless $self->utc_start_datetime;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->utc_start_datetime 0 0 0
676 0 ▶ return unless $self->rules;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->rules 0 0 0
677  
678 0 my $date = $self->utc_start_datetime;
679  
680 0 my @rules = $self->rules;
681  
682 0 my %possible_rules;
683  
684 0 my $year = $date->year;
685 0 foreach my $rule (@rules)
686 {
687 # We need to look at what the year _would_ be if we added the
688 # rule's offset to the UTC date. Otherwise we can end up with
689 # a UTC date in year X, and a rule that starts in _local_ year
690 # X + 1, where that rule really does apply to that UTC date.
691 0 my $temp_year =
692 $date->clone->add
693 ( seconds => $self->offset_from_utc + $rule->offset_from_std )->year;
694  
695 # Save the highest value
696 0 ▶ $year = $temp_year if $temp_year > $year;
Branch: 1 branch
BranchTrueFalseTotal
if $temp_year > $year 0 0 0
697  
698 0 ▶ next if $rule->min_year > $temp_year;
Branch: 1 branch
BranchTrueFalseTotal
if $rule->min_year > $temp_year 0 0 0
699  
700 0 $possible_rules{$rule} = $rule;
701 }
702  
703 0 ▶ return unless keys %possible_rules;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %possible_rules 0 0 0
704  
705 0 my $earliest_year = $year - 1;
706 0 foreach my $rule (@rules)
707 {
708 0 ▶ $earliest_year = $rule->min_year
Branch: 1 branch
BranchTrueFalseTotal
if $rule->min_year < $earliest_year 0 0 0
709 if $rule->min_year < $earliest_year;
710 }
711  
712 # figure out what date each rule would start on _if_ that rule
713 # were applied to this current observance. this could be a rule
714 # that started much earlier, but is only now active because of an
715 # observance switch. An obnoxious example of this is
716 # America/Phoenix in 1944, which applies the US rule in April,
717 # thus (re-)instating the "war time" rule from 1942. Can you say
718 # ridiculous crack-smoking stupidity?
719 0 my @rule_dates;
720 0 foreach my $y ( $earliest_year .. $year )
721 {
722 RULE:
723 0 foreach my $rule ( values %possible_rules )
724 {
725 # skip rules that can't have applied the year before the
726 # observance started.
727 0 ▶ if ( $rule->min_year > $y )
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->min_year > $y) 0 0 0
728 {
729 0 warn "Skipping rule beginning in ", $rule->min_year, ". Year is $y.\n"
730 if DateTimeX::Lite::OlsonDB::DEBUG;
731  
732 0 next RULE;
733 }
734  
735 0 ▶ if ( $rule->max_year && $rule->max_year < $y )
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->max_year and $rule->max_year < $y) 0 0 0
Condition0%0/3
$rule->max_year and $rule->max_year < $y
!ll&&!rl&&r
000
MC/DC0%0/2
$rule->max_year and $rule->max_year < $y
$rule->max_year $rule->max_year < $y
Truth table0%0/3
A and B
A= $rule->max_yearB= $rule->max_year < $y
ABresult
0X0
100
111
736 {
737 0 warn "Skipping rule ending in ", $rule->max_year, ". Year is $y.\n"
738 if DateTimeX::Lite::OlsonDB::DEBUG;
739  
740 0 next RULE;
741 }
742  
743 0 my $rule_start =
744 $rule->utc_start_datetime_for_year
745 ( $y, $last_offset_from_utc, $last_offset_from_std );
746  
747 0 push @rule_dates, [ $rule_start, $rule ];
748 }
749 }
750  
751 0 ▶ return unless @rule_dates;
Branch: 1 branch
BranchTrueFalseTotal
unless @rule_dates 0 0 0
752  
753 0 @rule_dates = sort { $a->[0] <=> $b->[0] } @rule_dates;
754  
755 0 warn "Looking for first rule ...\n" if DateTimeX::Lite::OlsonDB::DEBUG;
756 0 warn " Observance starts: ", $date->datetime, "\n\n"
757 if DateTimeX::Lite::OlsonDB::DEBUG;
758  
759 # ... look through the rules to see if any are still in
760 # effect at the beginning of the observance
761 0 ▶ for ( my $x = 0; $x < @rule_dates; $x++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($x < @rule_dates) 0 0 0
762 {
763 0 my ( $dt, $rule ) = @{ $rule_dates[$x] };
764 my ( $next_dt, $next_rule ) =
765 0 ▶ $x < @rule_dates - 1 ? @{ $rule_dates[ $x + 1 ] } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$x < @rule_dates - 1 ? : 0 0 0
766  
767 0 ▶ next if $next_dt && $next_dt < $date;
Branch: 1 branch
BranchTrueFalseTotal
if $next_dt and $next_dt < $date 0 0 0
Condition0%0/3
$next_dt and $next_dt < $date
!ll&&!rl&&r
000
MC/DC0%0/2
$next_dt and $next_dt < $date
$next_dt $next_dt < $date
Truth table0%0/3
A and B
A= $next_dtB= $next_dt < $date
ABresult
0X0
100
111
768  
769 0 warn " This rule starts: ", $dt->datetime, "\n"
770 if DateTimeX::Lite::OlsonDB::DEBUG;
771  
772 0 ▶ warn " Next rule starts: ", $next_dt->datetime, "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $next_dt and 0 0 0 0
Condition0%0/2
$next_dt and 0
!ll
00
MC/DC0%0/1
$next_dt and 0
$next_dt
Truth table0%0/2
A
A= $next_dt
Aresult
00
11
773 if $next_dt && DateTimeX::Lite::OlsonDB::DEBUG;
774  
775 0 ▶ warn " No next rule\n\n"
Branch: 1 branch
BranchTrueFalseTotal
if not $next_dt and 0 0 0 0
Condition0%0/2
not $next_dt and 0
!ll
00
MC/DC0%0/1
not $next_dt and 0
not $next_dt
Truth table0%0/2
not(A)
A= not $next_dt
Aresult
00
11
776 if ! $next_dt && DateTimeX::Lite::OlsonDB::DEBUG;
777  
778 0 ▶ if ( $dt <= $date )
Branch: 1 branch
BranchTrueFalseTotal
if ($dt <= $date) 0 0 0
779 {
780 0 ▶ if ($next_dt)
Branch: 1 branch
BranchTrueFalseTotal
if ($next_dt) { } 0 0 0
781 {
782 0 ▶ return $rule if $date < $next_dt;
Branch: 1 branch
BranchTrueFalseTotal
if $date < $next_dt 0 0 0
783 0 ▶ return $next_rule if $date == $next_dt;
Branch: 1 branch
BranchTrueFalseTotal
if $date == $next_dt 0 0 0
784 }
785 else
786 {
787 0 return $rule;
788 }
789 }
790 }
791  
792 0 return;
793 }
794  
795  
796 package DateTimeX::Lite::OlsonDB::Rule;
797  
798 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
799  
800 56 ▶use DateTimeX::Lite::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
801 56 ▶use DateTimeX::Lite::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
802  
803 sub new
804 0 ▶{
Subroutine new: not called CC=4 SCAR=30.0
805 0 my ($class, %p) = @_;
806 0 ▶ $p{type} ||= undef;
Condition0%0/2
$p{"type"} ||= undef
l!l
00
MC/DC0%0/1
$p{"type"} ||= undef
$p{"type"}
Truth table0%0/2
A
A= $p{"type"}
Aresult
11
00
807 0 ▶ $p{letter} ||= '';
Condition0%0/2
$p{"letter"} ||= ""
l!l
00
MC/DC0%0/1
$p{"letter"} ||= ""
$p{"letter"}
Truth table0%0/2
A
A= $p{"letter"}
Aresult
11
00
808  
809 0 my $save = $p{save};
810  
811 0 ▶ if ($save)
Branch: 1 branch
BranchTrueFalseTotal
if ($save) { } 0 0 0
812 {
813 0 $p{offset_from_std} = DateTimeX::Lite::Util::offset_as_seconds($save);
814 }
815 else
816 {
817 0 $p{offset_from_std} = 0;
818 }
819  
820 0 return bless \%p, $class;
821 }
822  
823 0 ▶sub name { $_[0]->{name} }
Subroutine name: not called CC=1 SCAR=6.9
824 4861 ▶sub offset_from_std { $_[0]->{offset_from_std} }
Subroutine offset_from_std: called CC=1 SCAR=0.0
825 1205 ▶sub letter { $_[0]->{letter} }
Subroutine letter: called CC=1 SCAR=0.0
826 0 ▶sub min_year { $_[0]->{from} }
Subroutine min_year: not called CC=1 SCAR=6.9
827  
828 0 ▶sub max_year { $_[0]->{to} eq 'only' ? $_[0]->min_year :
Subroutine max_year: not called CC=3 SCAR=24.8
829 0 ▶ $_[0]->{to} eq 'max' ? undef : $_[0]->{to} }
Branch: 2 branches
BranchTrueFalseTotal
$_[0]{'to'} eq 'only' ? : 0 0 0
$_[0]{'to'} eq 'max' ? : 0 0 0
830  
831 0 ▶sub is_infinite { $_[0]->{to} eq 'max' ? 1 : 0 }
Subroutine is_infinite: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'to'} eq 'max' ? : 0 0 0
832  
833 2492 ▶sub month { $DateTimeX::Lite::OlsonDB::MONTHS{ $_[0]->{in} } }
Subroutine month: called CC=1 SCAR=0.0
834 1246 ▶sub on { $_[0]->{on} }
Subroutine on: called CC=1 SCAR=0.0
835 1246 ▶sub at { $_[0]->{at} }
Subroutine at: called CC=1 SCAR=0.0
836  
837 sub utc_start_datetime_for_year
838 1246 ▶{
Subroutine utc_start_datetime_for_year: called CC=1 SCAR=0.0
839 1246 my $self = shift;
840 1246 my $year = shift;
841 1246 my $offset_from_utc = shift;
842 # should be the offset of the _previous_ rule
843 1246 my $offset_from_std = shift;
844  
845 1246 my $day =
846 DateTimeX::Lite::OlsonDB::parse_day_spec( $self->on, $self->month, $year );
847  
848 1246 my $utc =
849 DateTimeX::Lite::OlsonDB::utc_datetime_for_time_spec
850 ( spec => $self->at,
851 year => $year,
852 month => $self->month,
853 day => $day,
854 offset_from_utc => $offset_from_utc,
855 offset_from_std => $offset_from_std,
856 );
857  
858 1246 return $utc;
859 }
860  
861  
862 package DateTimeX::Lite::OlsonDB::Change;
863  
864 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
865  
866 sub new
867 1205 ▶{
Subroutine new: called CC=13 SCAR=34.4
868 1205 my ($class, %p) = @_;
869 1205 ▶ $p{rule} ||= undef;
Condition50%1/2
$p{"rule"} ||= undef
l!l
12050
MC/DC0%0/1
$p{"rule"} ||= undef
$p{"rule"}
Truth table50%1/2
A
A= $p{"rule"}
Aresult
11
00
870 1205 ▶ if ($p{type} !~ /^(?:observance|rule)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p{"type"} =~ /^(?:observance|rule)$/) 0 1205 1205
871 0 die "whoa $p{type}";
872 }
873  
874 # These are almost always mutually exclusive, except when adding
875 # an observance change and the last rule has no offset, but the
876 # new observance has an anonymous rule. In that case, prefer the
877 # offset from std defined in the observance to that in the
878 # previous rule (what a mess!).
879 1205 ▶ if ( $p{type} eq 'observance' )
Branch: 1 branch
BranchTrueFalseTotal
if ($p{'type'} eq 'observance') { } 0 1205 1205
880 {
881 0 ▶ $p{offset_from_std} = $p{rule}->offset_from_std if defined $p{rule};
Branch: 1 branch
BranchTrueFalseTotal
if defined $p{"rule"} 0 0 0
882 $p{offset_from_std} = $p{observance}->offset_from_std
883 0 ▶ if $p{observance}->offset_from_std;
Branch: 1 branch
BranchTrueFalseTotal
if $p{"observance"}->offset_from_std 0 0 0
884 0 ▶ $p{offset_from_std} ||= 0;
Condition0%0/2
$p{"offset_from_std"} ||= 0
l!l
00
MC/DC0%0/1
$p{"offset_from_std"} ||= 0
$p{"offset_from_std"}
Truth table0%0/2
A
A= $p{"offset_from_std"}
Aresult
11
00
885 }
886 else
887 {
888 1205 $p{offset_from_std} = $p{observance}->offset_from_std;
889 1205 ▶ $p{offset_from_std} = $p{rule}->offset_from_std if defined $p{rule};
Branch: 1 branch
BranchTrueFalseTotal
if defined $p{"rule"} 1205 0 1205
890 }
891  
892 1205 $p{offset_from_utc} = $p{observance}->offset_from_utc;
893  
894 1205 $p{is_dst} = 0;
895 1205 ▶ $p{is_dst} = 1 if $p{rule} && $p{rule}->offset_from_std;
Branch: 1 branch
BranchTrueFalseTotal
if $p{"rule"} and $p{"rule"}->offset_from_std 594 611 1205
Condition66%2/3
$p{"rule"} and $p{"rule"}->offset_from_std
!ll&&!rl&&r
0611594
MC/DC50%1/2
$p{"rule"} and $p{"rule"}->offset_from_std
$p{"rule"} $p{"rule"}->offset_from_std
Truth table66%2/3
A and B
A= $p{"rule"}B= $p{"rule"}->offset_from_std
ABresult
0X0
100
111
896 1205 ▶ $p{is_dst} = 1 if $p{observance}->offset_from_std;
Branch: 1 branch
BranchTrueFalseTotal
if $p{"observance"}->offset_from_std 0 1205 1205
897  
898 1205 ▶ if ( $p{short_name} =~ m{(\w+)/(\w+)} )
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"short_name"} =~ m[(\w+)/(\w+)]) 0 1205 1205
899 {
900 0 ▶ $p{short_name} = $p{is_dst} ? $2 : $1;
Branch: 1 branch
BranchTrueFalseTotal
$p{'is_dst'} ? : 0 0 0
901 }
902  
903 1205 return bless \%p, $class;
904 }
905  
906 8949 ▶sub utc_start_datetime { $_[0]->{utc_start_datetime} }
Subroutine utc_start_datetime: called CC=1 SCAR=0.0
907 1185 ▶sub local_start_datetime { $_[0]->{local_start_datetime} }
Subroutine local_start_datetime: called CC=1 SCAR=0.0
908 1185 ▶sub short_name { $_[0]->{short_name} }
Subroutine short_name: called CC=1 SCAR=0.0
909 1185 ▶sub is_dst { $_[0]->{is_dst} }
Subroutine is_dst: called CC=1 SCAR=0.0
910 0 ▶sub observance { $_[0]->{observance} }
Subroutine observance: not called CC=1 SCAR=6.9
911 0 ▶sub rule { $_[0]->{rule} }
Subroutine rule: not called CC=1 SCAR=6.9
912 3535 ▶sub offset_from_utc { $_[0]->{offset_from_utc} }
Subroutine offset_from_utc: called CC=1 SCAR=0.0
913 3535 ▶sub offset_from_std { $_[0]->{offset_from_std} }
Subroutine offset_from_std: called CC=1 SCAR=0.0
914 3535 ▶sub total_offset { $_[0]->offset_from_utc + $_[0]->offset_from_std }
Subroutine total_offset: called CC=1 SCAR=0.0
915  
916 sub two_changes_as_span
917 1185 ▶{
Subroutine two_changes_as_span: called CC=2 SCAR=7.1
918 1185 my ( $c1, $c2, $last_total_offset ) = @_;
919  
920 1185 my ( $utc_start, $local_start );
921  
922 1185 ▶ if ( defined $c1->utc_start_datetime )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c1->utc_start_datetime) { } 1185 0 1185
923 {
924 1185 $utc_start = $c1->utc_start_datetime->utc_rd_as_seconds;
925 1185 $local_start = $c1->local_start_datetime->utc_rd_as_seconds;
926 }
927 else
928 {
929 0 $utc_start = $local_start = '-inf';
930 }
931  
932 1185 my $utc_end = $c2->utc_start_datetime->utc_rd_as_seconds;
933 1185 my $local_end = $utc_end + $c1->total_offset;
934  
935 1185 return { utc_start => $utc_start,
936 utc_end => $utc_end,
937 local_start => $local_start,
938 local_end => $local_end,
939 short_name => $c1->short_name,
940 offset => $c1->total_offset,
941 is_dst => $c1->is_dst,
942 };
943 }
944  
945 sub _debug_output
946 0 ▶{
Subroutine _debug_output: not called CC=6 SCAR=37.4
947 0 my $self = shift;
948  
949 0 my $obs = $self->observance;
950  
951 0 ▶ if ( $self->utc_start_datetime )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->utc_start_datetime) { } 0 0 0
952 {
953 0 warn " UTC: ", $self->utc_start_datetime->datetime, "\n";
954 0 warn " Local: ", $self->local_start_datetime->datetime, "\n";
955 }
956 else
957 {
958 0 warn " First change (starts at -inf)\n";
959 }
960  
961 0 warn " Short name: ", $self->short_name, "\n";
962 0 warn " UTC offset: ", $obs->offset_from_utc, "\n";
963  
964 0 ▶ if ( $obs->offset_from_std || $self->rule )
Branch: 1 branch
BranchTrueFalseTotal
if ($obs->offset_from_std or $self->rule) { } 0 0 0
Condition0%0/3
$obs->offset_from_std or $self->rule
l!l&&r!l&&!r
000
MC/DC0%0/2
$obs->offset_from_std or $self->rule
$obs->offset_from_std $self->rule
Truth table0%0/3
A or B
A= $obs->offset_from_stdB= $self->rule
ABresult
1X1
011
000
965 {
966 0 ▶ if ( $obs->offset_from_std )
Branch: 1 branch
BranchTrueFalseTotal
if ($obs->offset_from_std) 0 0 0
967 {
968 0 warn " Std offset: ", $obs->offset_from_std, "\n";
969 }
970  
971 0 ▶ if ( $self->rule )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rule) 0 0 0
972 {
973 0 warn " Std offset: ", $self->rule->offset_from_std, ' - ',
974 $self->rule->name, " rule\n";
975 }
976 }
977 else
978 {
979 0 warn " Std offset: 0 - no rule\n";
980 }
981  
982 0 warn "\n";
983 }
984  
985 56 1;
986  
987 __END__
↑ Summary DateTimeX/Lite/TimeZone/Local/Unix.pm »