Debian Codegraph @

lib/DateTime/TimeZone/OlsonDB.pm

raw
1 package DateTime::TimeZone::OlsonDB;
2
3 use strict;
4 use warnings;
5 use namespace::autoclean;
6
7 our $VERSION = '2.69';
8
9 use DateTime::Duration;
10 use DateTime::TimeZone::OlsonDB::Rule;
11 use DateTime::TimeZone::OlsonDB::Zone;
12
13 my $x = 1;
14 our %MONTHS = map { $_ => $x++ } qw( Jan Feb Mar Apr May Jun
15 Jul Aug Sep Oct Nov Dec);
16
17 # 2024b accidentally used "April" instead of "Apr".
18 $MONTHS{April} = $MONTHS{Apr};
19
20 $x = 1;
21 our %DAYS = map { $_ => $x++ } qw( Mon Tue Wed Thu Fri Sat Sun );
22
23 our $PLUS_ONE_DAY_DUR = DateTime::Duration->new( days => 1 );
24 our $MINUS_ONE_DAY_DUR = DateTime::Duration->new( days => -1 );
25
26 sub new {
27 my $class = shift;
28
29 return bless {
30 rules => {},
31 zones => {},
32 links => {},
33 }, $class;
34 }
35
36 sub parse_file {
37 my $self = shift;
38 my $file = shift;
39
40 open my $fh, '<', $file
41 or die "Cannot read $file: $!";
42
43 while (<$fh>) {
44 chomp;
45 $self->_parse_line($_);
46 }
47
48 close $fh or die $!;
49 }
50
51 sub _parse_line {
52 my $self = shift;
53 my $line = shift;
54
55 return if $line =~ /^\s+$/;
56 return if $line =~ /^#/;
57
58 # remove any comments at the end of the line
59 $line =~ s/\s*#.+$//;
60
61 if ( $self->{in_zone} && $line =~ /^[ \t]/ ) {
62 $self->_parse_zone( $line, $self->{in_zone} );
63 return;
64 }
65
66 foreach (qw( Rule Zone Link )) {
67 if ( substr( $line, 0, 4 ) eq $_ ) {
68 my $m = '_parse_' . lc $_;
69 $self->$m($line);
70 }
71 }
72 }
73
74 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
75 sub _parse_rule {
76 my $self = shift;
77 my $rule = shift;
78
79 my @items = split /\s+/, $rule, 10;
80
81 shift @items;
82 my $name = shift @items;
83
84 my %rule;
85 @rule{qw( from to type in on at save letter )} = @items;
86 delete $rule{letter} if $rule{letter} eq '-';
87
88 # As of the 2003a data, there are no rules with a type set
89 delete $rule{type} if $rule{type} eq '-';
90
91 push @{ $self->{rules}{$name} },
92 DateTime::TimeZone::OlsonDB::Rule->new( name => $name, %rule );
93
94 undef $self->{in_zone};
95 }
96
97 sub _parse_zone {
98 my $self = shift;
99 my $zone = shift;
100 my $name = shift;
101
102 my $expect = $name ? 5 : 6;
103 my @items = grep { defined && length } split /\s+/, $zone, $expect;
104
105 my %obs;
106 unless ($name) {
107 shift @items; # remove "Zone"
108 $name = shift @items;
109 }
110
111 @obs{qw( gmtoff rules format until )} = @items;
112
113 if ( $obs{rules} =~ /\d\d?:\d\d/ ) {
114 $obs{offset_from_std} = delete $obs{rules};
115 }
116 else {
117 delete $obs{rules} if $obs{rules} eq '-';
118 }
119
120 delete $obs{until} unless defined $obs{until};
121
122 push @{ $self->{zones}{$name} }, \%obs;
123
124 $self->{in_zone} = $name;
125 }
126
127 sub _parse_link {
128 my $self = shift;
129 my $link = shift;
130
131 my @items = split /\s+/, $link, 3;
132
133 $self->{links}{ $items[2] } = $items[1];
134
135 undef $self->{in_zone};
136 }
137 ## use critic
138
139 sub links { %{ $_[0]->{links} } }
140
141 sub zone_names { keys %{ $_[0]->{zones} } }
142
143 sub zone {
144 my $self = shift;
145 my $name = shift;
146
147 die "Invalid zone name $name"
148 unless exists $self->{zones}{$name};
149
150 return DateTime::TimeZone::OlsonDB::Zone->new(
151 name => $name,
152 observances => $self->{zones}{$name},
153 olson_db => $self,
154 );
155 }
156
157 sub expanded_zone {
158 my $self = shift;
159 my %p = @_;
160
161 $p{expand_to_year} ||= (localtime)[5] + 1910;
162
163 my $zone = $self->zone( $p{name} );
164
165 $zone->expand_observances( $self, $p{expand_to_year} );
166
167 return $zone;
168 }
169
170 sub rules_by_name {
171 my $self = shift;
172 my $name = shift;
173
174 return unless defined $name;
175
176 die "Invalid rule name $name"
177 unless exists $self->{rules}{$name};
178
179 return @{ $self->{rules}{$name} };
180 }
181
182 sub parse_day_spec {
183 my ( $day, $month, $year ) = @_;
184
185 return ( $month, $day ) if $day =~ /^\d+$/;
186
187 if ( $day =~ /^last(\w\w\w)$/ ) {
188 my $dow = $DAYS{$1};
189
190 my $last_day = DateTime->last_day_of_month(
191 year => $year,
192 month => $month,
193 time_zone => 'floating',
194 );
195
196 my $dt = DateTime->new(
197 year => $year,
198 month => $month,
199 day => $last_day->day,
200 time_zone => 'floating',
201 );
202
203 while ( $dt->day_of_week != $dow ) {
204 $dt -= $PLUS_ONE_DAY_DUR;
205 }
206
207 return ( $dt->month, $dt->day );
208 }
209 elsif ( $day =~ /^(\w\w\w)([><])=(\d\d?)$/ ) {
210 my $dow = $DAYS{$1};
211
212 my $dt = DateTime->new(
213 year => $year,
214 month => $month,
215 day => $3,
216 time_zone => 'floating',
217 );
218
219 my $dur = $2 eq '<' ? $MINUS_ONE_DAY_DUR : $PLUS_ONE_DAY_DUR;
220
221 while ( $dt->day_of_week != $dow ) {
222 $dt += $dur;
223 }
224
225 return ( $dt->month, $dt->day );
226 }
227 else {
228 die "Invalid on spec for rule: $day\n";
229 }
230 }
231
232 sub utc_datetime_for_time_spec {
233 my %p = @_;
234
235 # 'w'all - ignore it, because that's the default
236 $p{spec} =~ s/w$//;
237
238 # 'g'reenwich, 'u'tc, or 'z'ulu
239 my $is_utc = $p{spec} =~ s/[guz]$//;
240
241 # 's'tandard time - ignore DS offset
242 my $is_std = $p{spec} =~ s/s$//;
243
244 ## no critic (NamingConventions::ProhibitAmbiguousNames)
245 my ( $hour, $minute, $second ) = split /:/, $p{spec};
246 $minute = 0 unless defined $minute;
247 $second = 0 unless defined $second;
248
249 my $add_day = 0;
250 if ( $hour >= 24 ) {
251 $hour = $hour - 24;
252 $add_day = 1;
253 }
254
255 my $utc;
256 if ($is_utc) {
257 $utc = DateTime->new(
258 year => $p{year},
259 month => $p{month},
260 day => $p{day},
261 hour => $hour,
262 minute => $minute,
263 second => $second,
264 time_zone => 'floating',
265 );
266 }
267 else {
268 my $local = DateTime->new(
269 year => $p{year},
270 month => $p{month},
271 day => $p{day},
272 hour => $hour,
273 minute => $minute,
274 second => $second,
275 time_zone => 'floating',
276 );
277
278 $p{offset_from_std} = 0 if $is_std;
279
280 my $dur = DateTime::Duration->new(
281 seconds => $p{offset_from_utc} + $p{offset_from_std} );
282
283 $utc = $local - $dur;
284 }
285
286 $utc->add( days => 1 ) if $add_day;
287
288 return $utc;
289 }
290
291 1;
292
293 # ABSTRACT: An object to represent an Olson time zone database
294
295 __END__
296
297 =pod
298
299 =encoding UTF-8
300
301 =head1 NAME
302
303 DateTime::TimeZone::OlsonDB - An object to represent an Olson time zone database
304
305 =head1 VERSION
306
307 version 2.69
308
309 =head1 SYNOPSIS
310
311 none yet
312
313 =head1 DESCRIPTION
314
315 This module parses the Olson database time zone definition files and creates
316 various objects representing time zone data.
317
318 Each time zone is broken down into several parts. The first piece is an
319 observance, which is an offset from UTC and an abbreviation. A single zone may
320 contain many observances, reflecting historical changes in that time zone over
321 time. An observance may also refer to a set of rules.
322
323 Rules are named, and may apply to many different zones. For example, the "US"
324 rules apply to most of the time zones in the US, unsurprisingly. Rules are
325 made of an offset from standard time and a definition of when that offset
326 changes. Changes can be a one time thing, or they can recur at regular times
327 through a span of years.
328
329 Each rule may have an associated letter, which is used to generate an
330 abbreviated name for the time zone, along with the offset's abbreviation. For
331 example, if the offset's abbreviation is "C%sT", and the a rule specifies the
332 letter "S", then the abbreviation when that rule is in effect is "CST".
333
334 =head1 USAGE
335
336 Not yet documented. This stuff is a mess.
337
338 =head1 SUPPORT
339
340 Bugs may be submitted at L<https://github.com/houseabsolute/DateTime-TimeZone/issues>.
341
342 =head1 SOURCE
343
344 The source code repository for DateTime-TimeZone can be found at L<https://github.com/houseabsolute/DateTime-TimeZone>.
345
346 =head1 AUTHOR
347
348 Dave Rolsky <[email protected]>
349
350 =head1 COPYRIGHT AND LICENSE
351
352 This software is copyright (c) 2026 by Dave Rolsky.
353
354 This is free software; you can redistribute it and/or modify it under
355 the same terms as the Perl 5 programming language system itself.
356
357 The full text of the license can be found in the
358 F<LICENSE> file included with this distribution.
359
360 =cut