blib/lib/DateTime/TimeZone/Tzfile.pm

filter: Statementstmt 98.4% 246 / 250 Branchbran 87.5% 119 / 136 Conditioncond 70.0% 35 / 50 MC/DCmcdc 60.7% 17 / 28 Subroutinesub 100% 34 / 34 Podpod 100% 11 / 11 totaltotal 90.7% 462 / 509 CC 89 SCAR 37.5
CC
89
Cov
92%
new
75.6
offset_for_local_datetime
10.0
_type_for_rdn_sod
4.0
CRAP 42.4
↑ Summary
1 =head1 NAME
2  
3 DateTime::TimeZone::Tzfile - tzfile (zoneinfo) timezone files
4  
5 =head1 SYNOPSIS
6  
7 use DateTime::TimeZone::Tzfile;
8  
9 $tz = DateTime::TimeZone::Tzfile->new(
10 name => "local timezone",
11 filename => "/etc/localtime");
12 $tz = DateTime::TimeZone::Tzfile->new("/etc/localtime");
13  
14 if($tz->is_floating) { ...
15 if($tz->is_utc) { ...
16 if($tz->is_olson) { ...
17 $category = $tz->category;
18 $tz_string = $tz->name;
19  
20 if($tz->has_dst_changes) { ...
21 if($tz->is_dst_for_datetime($dt)) { ...
22 $offset = $tz->offset_for_datetime($dt);
23 $abbrev = $tz->short_name_for_datetime($dt);
24 $offset = $tz->offset_for_local_datetime($dt);
25  
26 =head1 DESCRIPTION
27  
28 An instance of this class represents a timezone that was encoded in a
29 file in the L<tzfile(5)> format. These can express arbitrary patterns
30 of offsets from Universal Time, changing over time. Offsets and change
31 times are limited to a resolution of one second.
32  
33 This class implements the L<DateTime::TimeZone> interface, so that its
34 instances can be used with L<DateTime> objects.
35  
36 =cut
37  
38 package DateTime::TimeZone::Tzfile;
39  
40 6 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 6 ▶use Date::ISO8601 0.000 qw(present_ymd);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 6 ▶use IO::File 1.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 6 ▶use IO::Handle 1.08;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 6 ▶use Params::Classify 0.000 qw(is_undef is_string is_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 6 our $VERSION = "0.011";
51  
52 6 my $rdn_epoch_cjdn = 1721425;
53  
54 # _fdiv(A, B), _fmod(A, B): divide A by B, flooring remainder
55 #
56 # B must be a positive Perl integer. A must be a Perl integer.
57  
58 14406 ▶sub _fdiv($$) {
Subroutine _fdiv: called CC=2 SCAR=6.9
59 14406 my($a, $b) = @_;
60 14406 ▶ if($a < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a < 0) { } 2507 11899 14406
61 6 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2507 return -(($b - 1 - $a) / $b);
63 } else {
64 6 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 11899 return $a / $b;
66 }
67 }
68  
69 14406 ▶sub _fmod($$) { $_[0] % $_[1] }
Subroutine _fmod: called CC=1 SCAR=0.0
70  
71 =head1 CONSTRUCTOR
72  
73 =over
74  
75 =item DateTime::TimeZone::Tzfile->new(ATTR => VALUE, ...)
76  
77 Reads and parses a L<tzfile(5)> format file, then constructs and returns
78 a L<DateTime>-compatible timezone object that implements the timezone
79 encoded in the file. The following attributes may be given:
80  
81 =over
82  
83 =item B<name>
84  
85 Name for the timezone object. This will be returned by the C<name>
86 method described below, and will be included in certain error messages.
87  
88 =item B<category>
89  
90 The string or C<undef> that will be returned by the C<category> method
91 described below. Default C<undef>.
92  
93 =item B<is_olson>
94  
95 The truth value that will be returned by the C<is_olson> method described
96 below. Default false.
97  
98 =item B<filename>
99  
100 Name of the file from which to read the timezone data. The filename
101 must be understood by L<IO::File>.
102  
103 =item B<filehandle>
104  
105 An L<IO::Handle> object from which the timezone data can be read.
106 This does not need to be a regular seekable file; it is read sequentially.
107 After the constructor has finished, the handle can still be used to read
108 any data that follows the timezone data.
109  
110 =back
111  
112 Either a filename or filehandle must be given. If a timezone name is not
113 given, then the filename is used instead if supplied; a timezone name
114 must be given explicitly if no filename is given.
115  
116 =item DateTime::TimeZone::Tzfile->new(FILENAME)
117  
118 Simpler way to invoke the above constructor in the usual case. Only the
119 filename is given; this will also be used as the timezone name.
120  
121 =cut
122  
123 16532 ▶sub _saferead($$) {
Subroutine _saferead: called CC=3 SCAR=11.2
124 16532 my($fh, $len) = @_;
125 16532 my $data;
126 16532 my $rlen = $fh->read($data, $len);
127 16532 ▶ croak "can't read tzfile: $!" unless defined($rlen);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rlen 0 16532 16532
128 16532 ▶ croak "bad tzfile: premature EOF" unless $rlen == $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $rlen == $len 0 16532 16532
129 16532 return $data;
130 }
131  
132 9153 ▶sub _read_u32($) { unpack("N", _saferead($_[0], 4)) }
Subroutine _read_u32: called CC=1 SCAR=0.0
133  
134 6040 ▶sub _read_s32($) {
Subroutine _read_s32: called CC=2 SCAR=6.9
135 6040 my $uval = _read_u32($_[0]);
136 6040 ▶ return ($uval & 0x80000000) ? ($uval & 0x7fffffff) - 0x80000000 :
Branch: 1 branch
BranchTrueFalseTotal
$uval & 2147483648 ? : 2557 3483 6040
137 $uval;
138 }
139  
140 6329 ▶sub _read_u8($) { ord(_saferead($_[0], 1)) }
Subroutine _read_u8: called CC=1 SCAR=0.0
141  
142 6 my $unix_epoch_rdn = 719163;
143  
144 2866 ▶sub _read_tm32($) {
Subroutine _read_tm32: called CC=1 SCAR=0.0
145 2866 my $t = _read_s32($_[0]);
146 2866 return [ $unix_epoch_rdn + _fdiv($t, 86400), _fmod($t, 86400) ];
147 }
148  
149 2885 ▶sub _read_tm64($) {
Subroutine _read_tm64: called CC=1 SCAR=0.0
150 2885 my($fh) = @_;
151 2885 my $th = _read_s32($fh);
152 2885 my $tl = _read_u32($fh);
153 2885 my $dh = _fdiv($th, 86400);
154 2885 $th = (_fmod($th, 86400) << 10) | ($tl >> 22);
155 2885 my $d2 = _fdiv($th, 86400);
156 2885 $th = (_fmod($th, 86400) << 10) | (($tl >> 12) & 0x3ff);
157 2885 my $d3 = _fdiv($th, 86400);
158 2885 $th = (_fmod($th, 86400) << 12) | ($tl & 0xfff);
159 2885 my $d4 = _fdiv($th, 86400);
160 2885 $th = _fmod($th, 86400);
161 2885 my $d = $dh * 4294967296 + $d2 * 4194304 + (($d3 << 12) + $d4);
162 2885 return [ $unix_epoch_rdn + $d, $th ];
163 }
164  
165 6 my $factory_abbr = "Local time zone must be set--see zic manual page";
166  
167 43 ▶sub new {
Subroutine new: called CC=66 SCAR=43.2
Pod documented
168 43 my $class = shift;
169 43 ▶ unshift @_, "filename" if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 13 30 43
170 43 my $self = bless({}, $class);
171 43 my($filename, $fh);
172 43 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 59 24 83
173 59 my $attr = shift;
174 59 my $value = shift;
175 59 ▶ if($attr eq "name") {
Branch: 5 branches
BranchTrueFalseTotal
if ($attr eq 'name') { } 13 46 59
elsif ($attr eq 'category') { } 7 39 46
elsif ($attr eq 'is_olson') { } 4 35 39
elsif ($attr eq 'filename') { } 27 8 35
elsif ($attr eq 'filehandle') { } 7 1 8
176 croak "timezone name specified redundantly"
177 13 ▶ if exists $self->{name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"name"} 1 12 13
178 12 ▶ croak "timezone name must be a string"
Branch: 1 branch
BranchTrueFalseTotal
unless rand $value 4 8 12
179 unless is_string($value);
180 8 $self->{name} = $value;
181 } elsif($attr eq "category") {
182 croak "category value specified redundantly"
183 7 ▶ if exists $self->{category};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"category"} 1 6 7
184 6 ▶ croak "category value must be a string or undef"
Branch: 1 branch
BranchTrueFalseTotal
unless rand $value or rand $value 3 3 6
Condition100%3/3
rand $value or rand $value
l!l&&r!l&&!r
123
MC/DC100%2/2
rand $value or rand $value
rand $value rand $value
Truth table100%3/3
A or B
A= rand $valueB= rand $value
ABresult
1X1
011
000
185 unless is_undef($value) || is_string($value);
186 3 $self->{category} = $value;
187 } elsif($attr eq "is_olson") {
188 croak "is_olson flag specified redundantly"
189 4 ▶ if exists $self->{is_olson};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"is_olson"} 1 3 4
190 3 $self->{is_olson} = !!$value;
191 } elsif($attr eq "filename") {
192 27 ▶ croak "filename specified redundantly"
Branch: 1 branch
BranchTrueFalseTotal
if defined $filename or defined $fh 2 25 27
Condition100%3/3
defined $filename or defined $fh
l!l&&r!l&&!r
1125
MC/DC100%2/2
defined $filename or defined $fh
defined $filename defined $fh
Truth table100%3/3
A or B
A= defined $filenameB= defined $fh
ABresult
1X1
011
000
193 if defined($filename) || defined($fh);
194 25 ▶ croak "filename must be a string"
Branch: 1 branch
BranchTrueFalseTotal
unless rand $value 4 21 25
195 unless is_string($value);
196 21 $filename = $value;
197 } elsif($attr eq "filehandle") {
198 7 ▶ croak "filehandle specified redundantly"
Branch: 1 branch
BranchTrueFalseTotal
if defined $filename or defined $fh 2 5 7
Condition100%3/3
defined $filename or defined $fh
l!l&&r!l&&!r
115
MC/DC100%2/2
defined $filename or defined $fh
defined $filename defined $fh
Truth table100%3/3
A or B
A= defined $filenameB= defined $fh
ABresult
1X1
011
000
199 if defined($filename) || defined($fh);
200 5 $fh = $value;
201 } else {
202 1 croak "unrecognised attribute `$attr'";
203 }
204 }
205 24 ▶ croak "file not specified" unless defined($filename) || defined($fh);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename or defined $fh 2 22 24
Condition100%3/3
defined $filename or defined $fh
l!l&&r!l&&!r
1932
MC/DC100%2/2
defined $filename or defined $fh
defined $filename defined $fh
Truth table100%3/3
A or B
A= defined $filenameB= defined $fh
ABresult
1X1
011
000
206 22 ▶ unless(exists $self->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"name"}) 16 6 22
207 16 ▶ croak "timezone name not specified" unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 1 15 16
208 15 $self->{name} = $filename;
209 }
210 21 ▶ unless(exists $self->{category}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"category"}) 19 2 21
211 19 $self->{category} = undef;
212 }
213 21 ▶ unless(exists $self->{is_olson}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"is_olson"}) 19 2 21
214 19 $self->{is_olson} = !!0;
215 }
216 21 ▶ if(defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 19 2 21
217 19 ▶ ($fh = IO::File->new($filename, "r")) && $fh->binmode
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new($filename, "r") and $fh->binmode or croak("can't read ${filename}: $!") 18 1 19
Condition66%2/3
$fh = "IO::File"->new($filename, "r") and $fh->binmode
!ll&&!rl&&r
1018
MC/DC50%1/2
$fh = "IO::File"->new($filename, "r") and $fh->binmode
$fh = "IO::File"->new($filename, "r") $fh->binmode
Truth table66%2/3
A and B
A= $fh = "IO::File"->new($filename, "r")B= $fh->binmode
ABresult
0X0
100
111
218 or croak "can't read $filename: $!";
219 }
220 20 ▶ croak "bad tzfile: wrong magic number"
Branch: 1 branch
BranchTrueFalseTotal
unless &_saferead($fh, 4) eq "TZif" 1 19 20
221 unless _saferead($fh, 4) eq "TZif";
222 19 my $fmtversion = _saferead($fh, 1);
223 19 ▶ croak "bad tzfile: malformed version number"
Branch: 1 branch
BranchTrueFalseTotal
unless $fmtversion =~ /\A[2-9\0]\z/ 0 19 19
224 unless $fmtversion =~ /\A[2-9\0]\z/;
225 19 _saferead($fh, 15);
226 my($ttisgmtcnt, $ttisstdcnt, $leapcnt, $timecnt, $typecnt, $charcnt) =
227 19 map { _read_u32($fh) } 1 .. 6;
228 19 ▶ croak "bad tzfile: no local time types" if $typecnt == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $typecnt == 0 0 19 19
229 19 my @trn_times = map { _read_tm32($fh) } 1 .. $timecnt;
230 19 my @obs_types = map { _read_u8($fh) } 1 .. $timecnt;
231 my @types = map {
232 19 [ _read_s32($fh), !!_read_u8($fh), _read_u8($fh) ]
233 } 1 .. $typecnt;
234 19 my $chars = _saferead($fh, $charcnt);
235 0 ▶ for(my $i = $leapcnt; $i--; ) { _saferead($fh, 8); }
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 0 19 19
236 19 ▶ for(my $i = $ttisstdcnt; $i--; ) { _saferead($fh, 1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 136 19 155
237 19 ▶ for(my $i = $ttisgmtcnt; $i--; ) { _saferead($fh, 1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 136 19 155
238 19 my $late_rule;
239 19 ▶ if($fmtversion ge "2") {
Branch: 1 branch
BranchTrueFalseTotal
if ($fmtversion ge 2) 19 0 19
240 19 ▶ croak "bad tzfile: wrong magic number"
Branch: 1 branch
BranchTrueFalseTotal
unless &_saferead($fh, 4) eq "TZif" 0 19 19
241 unless _saferead($fh, 4) eq "TZif";
242 19 _saferead($fh, 16);
243 ($ttisgmtcnt, $ttisstdcnt, $leapcnt,
244 $timecnt, $typecnt, $charcnt) =
245 19 map { _read_u32($fh) } 1 .. 6;
246 19 ▶ croak "bad tzfile: no local time types" if $typecnt == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $typecnt == 0 0 19 19
247 19 @trn_times = map { _read_tm64($fh) } 1 .. $timecnt;
248 19 @obs_types = map { _read_u8($fh) } 1 .. $timecnt;
249 @types = map {
250 19 [ _read_s32($fh), !!_read_u8($fh), _read_u8($fh) ]
251 } 1 .. $typecnt;
252 19 $chars = _saferead($fh, $charcnt);
253 0 ▶ for(my $i = $leapcnt; $i--; ) { _saferead($fh, 12); }
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 0 19 19
254 19 ▶ for(my $i = $ttisstdcnt; $i--; ) { _saferead($fh, 1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 153 19 172
255 19 ▶ for(my $i = $ttisgmtcnt; $i--; ) { _saferead($fh, 1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 153 19 172
256 19 ▶ croak "bad tzfile: missing newline"
Branch: 1 branch
BranchTrueFalseTotal
unless &_saferead($fh, 1) eq "\n" 0 19 19
257 unless _saferead($fh, 1) eq "\x0a";
258 19 $late_rule = "";
259 19 while(1) {
260 319 my $c = _saferead($fh, 1);
261 319 ▶ last if $c eq "\x0a";
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "\n" 19 300 319
262 300 $late_rule .= $c;
263 }
264 }
265 19 $fh = undef;
266 19 ▶ for(my $i = @trn_times - 1; $i-- > 0; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i-- > 0) 2866 19 2885
267 2866 ▶ unless(($trn_times[$i]->[0] <=> $trn_times[$i+1]->[0] ||
Branch: 1 branch
BranchTrueFalseTotal
unless (($trn_times[$i][0] <=> $trn_times[$i + 1][0] || $trn_times[$i][1] <=> $trn_times[$i + 1][1]) == -1) 0 2866 2866
Condition33%1/3
$trn_times[$i][0] <=> $trn_times[$i + 1][0] || $trn_times[$i][1] <=> $trn_times[$i + 1][1]
l!l&&r!l&&!r
286600
MC/DC0%0/2
$trn_times[$i][0] <=> $trn_times[$i + 1][0] || $trn_times[$i][1] <=> $trn_times[$i + 1][1]
$trn_times[$i][0] <=> $trn_times[$i + 1][0] $trn_times[$i][1] <=> $trn_times[$i + 1][1]
Truth table33%1/3
A || B
A= $trn_times[$i][0] <=> $trn_times[$i + 1][0]B= $trn_times[$i][1] <=> $trn_times[$i + 1][1]
ABresult
1X1
011
000
268 $trn_times[$i]->[1] <=> $trn_times[$i+1]->[1]) == -1) {
269 0 croak "bad tzfile: unsorted change times";
270 }
271 }
272 19 my $first_std_type_index;
273 my %offsets;
274 19 ▶ for(my $i = 0; $i != $typecnt; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i != $typecnt) 153 19 172
275 153 my $abbrind = $types[$i]->[2];
276 153 ▶ croak "bad tzfile: invalid abbreviation index"
Branch: 1 branch
BranchTrueFalseTotal
if $abbrind > $charcnt 0 153 153
277 if $abbrind > $charcnt;
278 153 pos($chars) = $abbrind;
279 153 $chars =~ /\G([^\0]*)/g;
280 153 $types[$i]->[2] = $1;
281 153 ▶ $first_std_type_index = $i
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first_std_type_index or $types[$i][1] 19 134 153
Condition66%2/3
defined $first_std_type_index or $types[$i][1]
l!l&&r!l&&!r
134019
MC/DC50%1/2
defined $first_std_type_index or $types[$i][1]
defined $first_std_type_index $types[$i][1]
Truth table66%2/3
A or B
A= defined $first_std_type_indexB= $types[$i][1]
ABresult
1X1
011
000
282 if !defined($first_std_type_index) && !$types[$i]->[1];
283 153 ▶ $self->{has_dst} = 1 if $types[$i]->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $types[$i][1] 55 98 153
284 153 ▶ if($types[$i]->[0] == 0 && !$types[$i]->[1] &&
Branch: 1 branch
BranchTrueFalseTotal
if ($types[$i][0] == 0 and not $types[$i][1] and $types[$i][2] eq 'zzz') { } 6 147 153
Condition83%5/6
$types[$i][0] == 0 and not $types[$i][1] and $types[$i][2] eq "zzz"
!ll&&!rl&&r
117306
$types[$i][0] == 0 and not $types[$i][1]
!ll&&!rl&&r
117036
MC/DC66%2/3
$types[$i][0] == 0 and not $types[$i][1] and $types[$i][2] eq "zzz"
$types[$i][0] == 0 not $types[$i][1] $types[$i][2] eq "zzz"
Truth table75%3/4
A and not(B) and C
A= $types[$i][0] == 0B= not $types[$i][1]C= $types[$i][2] eq "zzz"
ABCresult
0XX0
10X0
1100
1111
285 $types[$i]->[2] eq "zzz") {
286 # "zzz" means the zone is not defined at this time,
287 # due for example to the location being uninhabited
288 6 $types[$i] = "zone disuse";
289 } else {
290 147 $offsets{$types[$i]->[0]} = undef;
291 }
292 }
293 19 ▶ unshift @obs_types,
Branch: 1 branch
BranchTrueFalseTotal
defined $first_std_type_index ? : 19 0 19
294 defined($first_std_type_index) ? $first_std_type_index : 0;
295 19 foreach my $obs_type (@obs_types) {
296 2904 ▶ croak "bad tzfile: invalid local time type index"
Branch: 1 branch
BranchTrueFalseTotal
if $obs_type >= $typecnt 0 2904 2904
297 if $obs_type >= $typecnt;
298 2904 $obs_type = $types[$obs_type];
299 }
300 19 ▶ if(defined($late_rule) && $late_rule eq "<$factory_abbr>0" &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $late_rule and $late_rule eq "<$factory_abbr>0" and defined $obs_types[-1] and $obs_types[-1][0] == 0 and not $obs_types[-1][1] and $obs_types[-1][2] eq $factory_abbr) 0 19 19
Condition33%5/15
defined $late_rule and $late_rule eq "<$factory_abbr>0" and defined $obs_types[-1] and $obs_types[-1][0] == 0 and not $obs_types[-1][1] and $obs_types[-1][2] eq $factory_abbr
!ll&&!rl&&r
1900
defined $late_rule and $late_rule eq "<$factory_abbr>0" and defined $obs_types[-1] and $obs_types[-1][0] == 0 and not $obs_types[-1][1]
!ll&&!rl&&r
1900
defined $late_rule and $late_rule eq "<$factory_abbr>0" and defined $obs_types[-1] and $obs_types[-1][0] == 0
!ll&&!rl&&r
1900
defined $late_rule and $late_rule eq "<$factory_abbr>0" and defined $obs_types[-1]
!ll&&!rl&&r
1900
defined $late_rule and $late_rule eq "<$factory_abbr>0"
!ll&&!rl&&r
0190
MC/DC0%0/6
defined $late_rule and $late_rule eq "<$factory_abbr>0" and defined $obs_types[-1] and $obs_types[-1][0] == 0 and not $obs_types[-1][1] and $obs_types[-1][2] eq $factory_abbr
defined $late_rule $late_rule eq "<$factory_abbr>0" defined $obs_types[-1] $obs_types[-1][0] == 0 not $obs_types[-1][1] $obs_types[-1][2] eq $factory_abbr
Truth table14%1/7
A and B and C and D and not(E) and F
A= defined $late_ruleB= $late_rule eq "<$factory_abbr>0"C= defined $obs_types[-1]D= $obs_types[-1][0] == 0E= not $obs_types[-1][1]F= $obs_types[-1][2] eq $factory_abbr
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
301 defined($obs_types[-1]) && $obs_types[-1]->[0] == 0 &&
302 !$obs_types[-1]->[1] &&
303 $obs_types[-1]->[2] eq $factory_abbr) {
304 # This bizarre timezone abbreviation is used in the Factory
305 # timezone in the Olson database. It's not valid in a
306 # SysV-style TZ value, because it contains spaces, but zic
307 # puts it into one anyway because the file format demands
308 # it. DT:TZ:SystemV would object, so as a special
309 # exception we ignore the TZ value in this case.
310 0 $late_rule = undef;
311 }
312 19 ▶ if(defined $late_rule) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $late_rule) 19 0 19
313 19 ▶ if($late_rule eq "") {
Branch: 2 branches
BranchTrueFalseTotal
if ($late_rule eq '') { } 2 17 19
elsif ($late_rule =~ /\A(?:zzz|<zzz>)[-+]?00?(?::00(?::00)?)?\z/) { } 2 15 17
314 2 $obs_types[-1] = "missing data";
315 } elsif($late_rule =~
316 /\A(?:zzz|<zzz>)[-+]?00?(?::00(?::00)?)?\z/) {
317 2 $obs_types[-1] = "zone disuse";
318 } else {
319 15 require DateTime::TimeZone::SystemV;
320 15 DateTime::TimeZone::SystemV->VERSION("0.009");
321 15 ▶ $obs_types[-1] =
Branch: 1 branch
BranchTrueFalseTotal
$fmtversion ge '3' ? : 1 14 15
322 DateTime::TimeZone::SystemV->new(
323 system => $fmtversion ge "3" ?
324 "tzfile3" : "posix",
325 recipe => $late_rule);
326 }
327 }
328 19 $self->{trn_times} = \@trn_times;
329 19 $self->{obs_types} = \@obs_types;
330 19 $self->{offsets} = [ sort { $a <=> $b } keys %offsets ];
331 19 return $self;
332 }
333  
334 60 ▶sub _present_rdn_sod($$) {
Subroutine _present_rdn_sod: called CC=1 SCAR=0.0
335 60 my($rdn, $sod) = @_;
336 60 return sprintf("%sT%02d:%02d:%02d",
337 present_ymd($rdn + $rdn_epoch_cjdn),
338 int($sod/3600), int($sod/60)%60, $sod%60);
339 }
340  
341 =back
342  
343 =head1 METHODS
344  
345 These methods are all part of the L<DateTime::TimeZone> interface.
346 See that class for the general meaning of these methods; the documentation
347 below only comments on the specific behaviour of this class.
348  
349 =head2 Identification
350  
351 =over
352  
353 =item $tz->is_floating
354  
355 Returns false.
356  
357 =cut
358  
359 3 ▶sub is_floating { 0 }
Subroutine is_floating: called CC=1 SCAR=0.0
Pod documented
360  
361 =item $tz->is_utc
362  
363 Returns false.
364  
365 =cut
366  
367 3 ▶sub is_utc { 0 }
Subroutine is_utc: called CC=1 SCAR=0.0
Pod documented
368  
369 =item $tz->is_olson
370  
371 Returns the truth value that was provided to the constructor for this
372 purpose, default false. This nominally indicates whether the timezone
373 data is from the Olson database. The files interpreted by this class
374 are very likely to be from the Olson database, but there is no explicit
375 indicator for this in the file, so this information must be supplied to
376 the constructor if required.
377  
378 =cut
379  
380 3 ▶sub is_olson { $_[0]->{is_olson} }
Subroutine is_olson: called CC=1 SCAR=0.0
Pod documented
381  
382 =item $tz->category
383  
384 Returns the value that was provided to the constructor for this purpose,
385 default C<undef>. This is intended to indicate the general region
386 (continent or ocean) in which a geographical timezone is used, when
387 the timezone is named according to the hierarchical scheme of the Olson
388 timezone database.
389  
390 =cut
391  
392 3 ▶sub category { $_[0]->{category} }
Subroutine category: called CC=1 SCAR=0.0
Pod documented
393  
394 =item $tz->name
395  
396 Returns the timezone name. Usually this is the filename that was supplied
397 to the constructor, but it can be overridden by the constructor's B<name>
398 attribute.
399  
400 =cut
401  
402 8 ▶sub name { $_[0]->{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
403  
404 =back
405  
406 =head2 Offsets
407  
408 =over
409  
410 =item $tz->has_dst_changes
411  
412 Returns a truth value indicating whether any of the observances in the file
413 are marked as DST. These DST flags are potentially arbitrary, and don't
414 affect any of the zone's behaviour.
415  
416 =cut
417  
418 3 ▶sub has_dst_changes { $_[0]->{has_dst} }
Subroutine has_dst_changes: called CC=1 SCAR=0.0
Pod documented
419  
420 #
421 # observance lookup
422 #
423  
424 1986 ▶sub _type_for_rdn_sod {
Subroutine _type_for_rdn_sod: called CC=4 SCAR=13.9
425 1986 my($self, $utc_rdn, $utc_sod) = @_;
426 1986 my $lo = 0;
427 1986 my $hi = @{$self->{trn_times}};
428 1986 ▶ while($lo != $hi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo != $hi) 14395 1986 16381
429 6 ▶ my $try = do { use integer; ($lo + $hi) / 2 };
Subroutine BEGIN: called CC=1 SCAR=0.0
430 14395 ▶ if(($utc_rdn <=> $self->{trn_times}->[$try]->[0] ||
Branch: 1 branch
BranchTrueFalseTotal
if (($utc_rdn <=> $self->{'trn_times'}[$try][0] || $utc_sod <=> $self->{'trn_times'}[$try][1]) == -1) { } 6871 7524 14395
Condition100%3/3
$utc_rdn <=> $self->{'trn_times'}[$try][0] || $utc_sod <=> $self->{'trn_times'}[$try][1]
l!l&&r!l&&!r
12647917831
MC/DC100%2/2
$utc_rdn <=> $self->{'trn_times'}[$try][0] || $utc_sod <=> $self->{'trn_times'}[$try][1]
$utc_rdn <=> $self->{'trn_times'}[$try][0] $utc_sod <=> $self->{'trn_times'}[$try][1]
Truth table100%3/3
A || B
A= $utc_rdn <=> $self->{'trn_times'}[$try][0]B= $utc_sod <=> $self->{'trn_times'}[$try][1]
ABresult
1X1
011
000
431 $utc_sod <=> $self->{trn_times}->[$try]->[1]) == -1) {
432 6871 $hi = $try;
433 } else {
434 7524 $lo = $try + 1;
435 }
436 }
437 1986 return $self->{obs_types}->[$lo];
438 }
439  
440 1752 ▶sub _type_for_datetime {
Subroutine _type_for_datetime: called CC=3 SCAR=11.0
441 1752 my($self, $dt) = @_;
442 1752 my($utc_rdn, $utc_sod) = $dt->utc_rd_values;
443 1752 ▶ $utc_sod = 86399 if $utc_sod >= 86400;
Branch: 1 branch
BranchTrueFalseTotal
if $utc_sod >= 86400 15 1737 1752
444 1752 my $type = $self->_type_for_rdn_sod($utc_rdn, $utc_sod);
445 1752 ▶ if(is_string($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand $type) 33 1719 1752
446 33 croak "time @{[_present_rdn_sod($utc_rdn, $utc_sod)]}Z ".
447 "is not represented ".
448 33 "in the @{[$self->{name}]} timezone ".
449 "due to $type";
450 }
451 1719 return $type;
452 }
453  
454 =item $tz->offset_for_datetime(DT)
455  
456 I<DT> must be a L<DateTime>-compatible object (specifically, it must
457 implement the C<utc_rd_values> method). Returns the offset from UT that
458 is in effect at the instant represented by I<DT>, in seconds.
459  
460 =cut
461  
462 584 ▶sub offset_for_datetime {
Subroutine offset_for_datetime: called CC=2 SCAR=6.9
Pod documented
463 584 my($self, $dt) = @_;
464 584 my $type = $self->_type_for_datetime($dt);
465 573 ▶ return is_ref($type, "ARRAY") ? $type->[0] :
Branch: 1 branch
BranchTrueFalseTotal
rand $type ? : 555 18 573
466 $type->offset_for_datetime($dt);
467 }
468  
469 =item $tz->is_dst_for_datetime(DT)
470  
471 I<DT> must be a L<DateTime>-compatible object (specifically, it must
472 implement the C<utc_rd_values> method). Returns a truth value indicating
473 whether the timezone's observance at the instant represented by I<DT>
474 is marked as DST. This DST flag is potentially arbitrary, and doesn't
475 affect anything else.
476  
477 =cut
478  
479 584 ▶sub is_dst_for_datetime {
Subroutine is_dst_for_datetime: called CC=2 SCAR=6.9
Pod documented
480 584 my($self, $dt) = @_;
481 584 my $type = $self->_type_for_datetime($dt);
482 573 ▶ return is_ref($type, "ARRAY") ? $type->[1] :
Branch: 1 branch
BranchTrueFalseTotal
rand $type ? : 555 18 573
483 $type->is_dst_for_datetime($dt);
484 }
485  
486 =item $tz->short_name_for_datetime(DT)
487  
488 I<DT> must be a L<DateTime>-compatible object (specifically, it must
489 implement the C<utc_rd_values> method). Returns the abbreviation
490 used to label the time scale at the instant represented by I<DT>.
491 This abbreviation is potentially arbitrary, and does not uniquely identify
492 either the timezone or the offset.
493  
494 =cut
495  
496 584 ▶sub short_name_for_datetime {
Subroutine short_name_for_datetime: called CC=2 SCAR=6.9
Pod documented
497 584 my($self, $dt) = @_;
498 584 my $type = $self->_type_for_datetime($dt);
499 573 ▶ return is_ref($type, "ARRAY") ? $type->[2] :
Branch: 1 branch
BranchTrueFalseTotal
rand $type ? : 555 18 573
500 $type->short_name_for_datetime($dt);
501 }
502  
503 =item $tz->offset_for_local_datetime(DT)
504  
505 I<DT> must be a L<DateTime>-compatible object (specifically, it
506 must implement the C<local_rd_values> method). Takes the local
507 time represented by I<DT> (regardless of what absolute time it also
508 represents), and interprets that as a local time in the timezone of the
509 timezone object (not the timezone used in I<DT>). Returns the offset
510 from UT that is in effect at that local time, in seconds.
511  
512 If the local time given is ambiguous due to a nearby offset change,
513 the numerically lowest offset (usually the standard one) is returned
514 with no warning of the situation. (Equivalently: the latest possible
515 absolute time is indicated.) If the local time given does not exist
516 due to a nearby offset change, the method C<die>s saying so.
517  
518 =cut
519  
520 234 ▶sub _local_to_utc_rdn_sod($$$) {
Subroutine _local_to_utc_rdn_sod: called CC=3 SCAR=11.0
521 234 my($rdn, $sod, $offset) = @_;
522 234 $sod -= $offset;
523 234 ▶ while($sod < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sod < 0) 15 234 249
524 15 $rdn--;
525 15 $sod += 86400;
526 }
527 234 ▶ while($sod >= 86400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sod >= 86400) 32 234 266
528 32 $rdn++;
529 32 $sod -= 86400;
530 }
531 234 return ($rdn, $sod);
532 }
533  
534 85 ▶sub offset_for_local_datetime {
Subroutine offset_for_local_datetime: called CC=10 SCAR=23.0
Pod documented
535 85 my($self, $dt) = @_;
536 85 my($lcl_rdn, $lcl_sod) = $dt->local_rd_values;
537 85 ▶ $lcl_sod = 86399 if $lcl_sod >= 86400;
Branch: 1 branch
BranchTrueFalseTotal
if $lcl_sod >= 86400 0 85 85
538 85 my %seen_error;
539 85 foreach my $offset (@{$self->{offsets}}) {
540 234 my($utc_rdn, $utc_sod) =
541 _local_to_utc_rdn_sod($lcl_rdn, $lcl_sod, $offset);
542 234 my $ttype = $self->_type_for_rdn_sod($utc_rdn, $utc_sod);
543 234 ▶ if(is_string($ttype)) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand $ttype) 31 203 234
544 31 $seen_error{$ttype} = undef;
545 31 next;
546 }
547 my $local_offset = is_ref($ttype, "ARRAY") ? $ttype->[0] :
548 203 ▶ eval { local $SIG{__DIE__};
Branch: 1 branch
BranchTrueFalseTotal
rand $ttype ? : 181 22 203
549 22 $ttype->offset_for_local_datetime($dt);
550 };
551 203 ▶ return $offset
Branch: 1 branch
BranchTrueFalseTotal
if defined $local_offset and $local_offset == $offset 58 145 203
Condition100%3/3
defined $local_offset and $local_offset == $offset
!ll&&!rl&&r
813758
MC/DC100%2/2
defined $local_offset and $local_offset == $offset
defined $local_offset $local_offset == $offset
Truth table100%3/3
A and B
A= defined $local_offsetB= $local_offset == $offset
ABresult
0X0
100
111
552 if defined($local_offset) && $local_offset == $offset;
553 }
554 27 my $error;
555 27 foreach("zone disuse", "missing data") {
556 45 ▶ if(exists $seen_error{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen_error{$_}) 11 34 45
557 11 $error = $_;
558 11 last;
559 }
560 }
561 27 ▶ $error ||= "offset change";
Condition100%2/2
$error ||= "offset change"
l!l
1116
MC/DC100%1/1
$error ||= "offset change"
$error
Truth table100%2/2
A
A= $error
Aresult
11
00
562 27 croak "local time @{[_present_rdn_sod($lcl_rdn, $lcl_sod)]} ".
563 27 "does not exist in the @{[$self->{name}]} timezone ".
564 "due to $error";
565 }
566  
567 =back
568  
569 =head1 SEE ALSO
570  
571 L<DateTime>,
572 L<DateTime::TimeZone>,
573 L<DateTime::TimeZone::Olson>,
574 L<Time::OlsonTZ::Data>,
575 L<Time::OlsonTZ::Download>,
576 L<tzfile(5)>
577  
578 =head1 AUTHOR
579  
580 Andrew Main (Zefram) <zefram@fysh.org>
581  
582 =head1 COPYRIGHT
583  
584 Copyright (C) 2007, 2009, 2010, 2011, 2012, 2013, 2017
585 Andrew Main (Zefram) <zefram@fysh.org>
586  
587 =head1 LICENSE
588  
589 This module is free software; you can redistribute it and/or modify it
590 under the same terms as Perl itself.
591  
592 =cut
593  
594 6 1;
↑ Summary