Filename | /usr/share/perl5/DateTime/TimeZone.pm |
Statements | Executed 50 statements in 6.37ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 3.91ms | 4.00ms | BEGIN@10 | DateTime::TimeZone::
1 | 1 | 1 | 2.13ms | 27.3ms | BEGIN@12 | DateTime::TimeZone::
1 | 1 | 1 | 1.30ms | 18.1ms | BEGIN@11 | DateTime::TimeZone::
2 | 2 | 1 | 46µs | 86µs | new | DateTime::TimeZone::
1 | 1 | 1 | 18µs | 18µs | BEGIN@5 | DateTime::TimeZone::
1 | 1 | 1 | 14µs | 68µs | BEGIN@15 | DateTime::TimeZone::
1 | 1 | 1 | 9µs | 14µs | BEGIN@8 | DateTime::TimeZone::
1 | 1 | 1 | 8µs | 8µs | BEGIN@13 | DateTime::TimeZone::
1 | 1 | 1 | 7µs | 19µs | BEGIN@7 | DateTime::TimeZone::
1 | 1 | 1 | 6µs | 45µs | BEGIN@17 | DateTime::TimeZone::
1 | 1 | 1 | 6µs | 6µs | BEGIN@14 | DateTime::TimeZone::
1 | 1 | 1 | 6µs | 28µs | BEGIN@25 | DateTime::TimeZone::
1 | 1 | 1 | 6µs | 32µs | BEGIN@18 | DateTime::TimeZone::
1 | 1 | 1 | 6µs | 29µs | BEGIN@24 | DateTime::TimeZone::
1 | 1 | 1 | 6µs | 28µs | BEGIN@21 | DateTime::TimeZone::
1 | 1 | 1 | 6µs | 66µs | BEGIN@23 | DateTime::TimeZone::
1 | 1 | 1 | 6µs | 26µs | BEGIN@26 | DateTime::TimeZone::
1 | 1 | 1 | 5µs | 28µs | BEGIN@22 | DateTime::TimeZone::
1 | 1 | 1 | 5µs | 26µs | BEGIN@27 | DateTime::TimeZone::
2 | 1 | 1 | 3µs | 3µs | CORE:match (opcode) | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | STORABLE_freeze | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | STORABLE_thaw | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | _generate_next_span | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | _generate_spans_until_match | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | _init | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | _keys_for_type | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | _span_as_array | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | _span_for_datetime | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | _spans_binary_search | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | all_names | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | categories | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | category | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | countries | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | has_dst_changes | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | is_dst_for_datetime | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | is_floating | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | is_olson | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | is_utc | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | is_valid_name | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | links | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | max_span | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | name | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | names_in_category | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | names_in_country | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | offset_as_seconds | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | offset_as_string | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | offset_for_datetime | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | offset_for_local_datetime | DateTime::TimeZone::
0 | 0 | 0 | 0s | 0s | short_name_for_datetime | DateTime::TimeZone::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package DateTime::TimeZone; | ||||
2 | # git description: v1.74-3-g53bc6be | ||||
3 | 1 | 800ns | $DateTime::TimeZone::VERSION = '1.75'; | ||
4 | |||||
5 | 2 | 48µs | 1 | 18µs | # spent 18µs within DateTime::TimeZone::BEGIN@5 which was called:
# once (18µs+0s) by C4::Context::BEGIN@106 at line 5 # spent 18µs making 1 call to DateTime::TimeZone::BEGIN@5 |
6 | |||||
7 | 2 | 25µs | 2 | 32µs | # spent 19µs (7+12) within DateTime::TimeZone::BEGIN@7 which was called:
# once (7µs+12µs) by C4::Context::BEGIN@106 at line 7 # spent 19µs making 1 call to DateTime::TimeZone::BEGIN@7
# spent 12µs making 1 call to strict::import |
8 | 2 | 24µs | 2 | 20µs | # spent 14µs (9+5) within DateTime::TimeZone::BEGIN@8 which was called:
# once (9µs+5µs) by C4::Context::BEGIN@106 at line 8 # spent 14µs making 1 call to DateTime::TimeZone::BEGIN@8
# spent 5µs making 1 call to warnings::import |
9 | |||||
10 | 2 | 1.08ms | 1 | 4.00ms | # spent 4.00ms (3.91+91µs) within DateTime::TimeZone::BEGIN@10 which was called:
# once (3.91ms+91µs) by C4::Context::BEGIN@106 at line 10 # spent 4.00ms making 1 call to DateTime::TimeZone::BEGIN@10 |
11 | 2 | 1.14ms | 1 | 18.1ms | # spent 18.1ms (1.30+16.8) within DateTime::TimeZone::BEGIN@11 which was called:
# once (1.30ms+16.8ms) by C4::Context::BEGIN@106 at line 11 # spent 18.1ms making 1 call to DateTime::TimeZone::BEGIN@11 |
12 | 2 | 1.57ms | 1 | 27.3ms | # spent 27.3ms (2.13+25.2) within DateTime::TimeZone::BEGIN@12 which was called:
# once (2.13ms+25.2ms) by C4::Context::BEGIN@106 at line 12 # spent 27.3ms making 1 call to DateTime::TimeZone::BEGIN@12 |
13 | 2 | 25µs | 1 | 8µs | # spent 8µs within DateTime::TimeZone::BEGIN@13 which was called:
# once (8µs+0s) by C4::Context::BEGIN@106 at line 13 # spent 8µs making 1 call to DateTime::TimeZone::BEGIN@13 |
14 | 2 | 32µs | 1 | 6µs | # spent 6µs within DateTime::TimeZone::BEGIN@14 which was called:
# once (6µs+0s) by C4::Context::BEGIN@106 at line 14 # spent 6µs making 1 call to DateTime::TimeZone::BEGIN@14 |
15 | 3 | 66µs | 3 | 123µs | # spent 68µs (14+55) within DateTime::TimeZone::BEGIN@15 which was called:
# once (14µs+55µs) by C4::Context::BEGIN@106 at line 15 # spent 68µs making 1 call to DateTime::TimeZone::BEGIN@15
# spent 44µs making 1 call to Exporter::import
# spent 10µs making 1 call to UNIVERSAL::VERSION |
16 | |||||
17 | 2 | 37µs | 2 | 84µs | # spent 45µs (6+39) within DateTime::TimeZone::BEGIN@17 which was called:
# once (6µs+39µs) by C4::Context::BEGIN@106 at line 17 # spent 45µs making 1 call to DateTime::TimeZone::BEGIN@17
# spent 39µs making 1 call to constant::import |
18 | 2 | 28µs | 2 | 58µs | # spent 32µs (6+26) within DateTime::TimeZone::BEGIN@18 which was called:
# once (6µs+26µs) by C4::Context::BEGIN@106 at line 18 # spent 32µs making 1 call to DateTime::TimeZone::BEGIN@18
# spent 26µs making 1 call to constant::import |
19 | |||||
20 | # the offsets for each span element | ||||
21 | 2 | 24µs | 2 | 51µs | # spent 28µs (6+22) within DateTime::TimeZone::BEGIN@21 which was called:
# once (6µs+22µs) by C4::Context::BEGIN@106 at line 21 # spent 28µs making 1 call to DateTime::TimeZone::BEGIN@21
# spent 22µs making 1 call to constant::import |
22 | 2 | 25µs | 2 | 51µs | # spent 28µs (5+23) within DateTime::TimeZone::BEGIN@22 which was called:
# once (5µs+23µs) by C4::Context::BEGIN@106 at line 22 # spent 28µs making 1 call to DateTime::TimeZone::BEGIN@22
# spent 23µs making 1 call to constant::import |
23 | 2 | 27µs | 2 | 127µs | # spent 66µs (6+61) within DateTime::TimeZone::BEGIN@23 which was called:
# once (6µs+61µs) by C4::Context::BEGIN@106 at line 23 # spent 66µs making 1 call to DateTime::TimeZone::BEGIN@23
# spent 61µs making 1 call to constant::import |
24 | 2 | 24µs | 2 | 51µs | # spent 29µs (6+23) within DateTime::TimeZone::BEGIN@24 which was called:
# once (6µs+23µs) by C4::Context::BEGIN@106 at line 24 # spent 29µs making 1 call to DateTime::TimeZone::BEGIN@24
# spent 23µs making 1 call to constant::import |
25 | 2 | 24µs | 2 | 50µs | # spent 28µs (6+22) within DateTime::TimeZone::BEGIN@25 which was called:
# once (6µs+22µs) by C4::Context::BEGIN@106 at line 25 # spent 28µs making 1 call to DateTime::TimeZone::BEGIN@25
# spent 22µs making 1 call to constant::import |
26 | 2 | 23µs | 2 | 46µs | # spent 26µs (6+20) within DateTime::TimeZone::BEGIN@26 which was called:
# once (6µs+20µs) by C4::Context::BEGIN@106 at line 26 # spent 26µs making 1 call to DateTime::TimeZone::BEGIN@26
# spent 20µs making 1 call to constant::import |
27 | 2 | 2.06ms | 2 | 47µs | # spent 26µs (5+21) within DateTime::TimeZone::BEGIN@27 which was called:
# once (5µs+21µs) by C4::Context::BEGIN@106 at line 27 # spent 26µs making 1 call to DateTime::TimeZone::BEGIN@27
# spent 21µs making 1 call to constant::import |
28 | |||||
29 | 1 | 8µs | my %SpecialName = map { $_ => 1 } | ||
30 | qw( EST MST HST CET EET MET WET EST5EDT CST6CDT MST7MDT PST8PDT ); | ||||
31 | |||||
32 | # spent 86µs (46+39) within DateTime::TimeZone::new which was called 2 times, avg 43µs/call:
# once (30µs+24µs) by C4::Circulation::BEGIN@24 at line 44 of DateTime/Infinite.pm
# once (16µs+15µs) by C4::Circulation::BEGIN@24 at line 69 of DateTime/Infinite.pm | ||||
33 | 2 | 900ns | my $class = shift; | ||
34 | 2 | 34µs | 2 | 18µs | my %p = validate( # spent 18µs making 2 calls to Params::Validate::XS::validate, avg 9µs/call |
35 | @_, | ||||
36 | { name => { type => SCALAR } }, | ||||
37 | ); | ||||
38 | |||||
39 | 2 | 5µs | if ( exists $DateTime::TimeZone::Catalog::LINKS{ $p{name} } ) { | ||
40 | $p{name} = $DateTime::TimeZone::Catalog::LINKS{ $p{name} }; | ||||
41 | } | ||||
42 | elsif ( exists $DateTime::TimeZone::Catalog::LINKS{ uc $p{name} } ) { | ||||
43 | $p{name} = $DateTime::TimeZone::Catalog::LINKS{ uc $p{name} }; | ||||
44 | } | ||||
45 | |||||
46 | 2 | 10µs | 2 | 3µs | unless ( $p{name} =~ m,/, # spent 3µs making 2 calls to DateTime::TimeZone::CORE:match, avg 2µs/call |
47 | || $SpecialName{ $p{name} } ) { | ||||
48 | 2 | 17µs | 2 | 18µs | if ( $p{name} eq 'floating' ) { # spent 18µs making 2 calls to Class::Singleton::instance, avg 9µs/call |
49 | return DateTime::TimeZone::Floating->instance; | ||||
50 | } | ||||
51 | |||||
52 | if ( $p{name} eq 'local' ) { | ||||
53 | return DateTime::TimeZone::Local->TimeZone(); | ||||
54 | } | ||||
55 | |||||
56 | if ( $p{name} eq 'UTC' || $p{name} eq 'Z' ) { | ||||
57 | return DateTime::TimeZone::UTC->instance; | ||||
58 | } | ||||
59 | |||||
60 | return DateTime::TimeZone::OffsetOnly->new( offset => $p{name} ); | ||||
61 | } | ||||
62 | |||||
63 | my $subclass = $p{name}; | ||||
64 | $subclass =~ s/-/_/g; | ||||
65 | $subclass =~ s{/}{::}g; | ||||
66 | my $real_class = "DateTime::TimeZone::$subclass"; | ||||
67 | |||||
68 | die "The timezone '$p{name}' in an invalid name.\n" | ||||
69 | unless $real_class =~ /^\w+(::\w+)*$/; | ||||
70 | |||||
71 | unless ( $real_class->can('instance') ) { | ||||
72 | ($real_class) = $real_class =~ m{\A([a-zA-Z0-9_]+(?:::[a-zA-Z0-9_]+)*)\z}; | ||||
73 | |||||
74 | my $e = do { | ||||
75 | local $@; | ||||
76 | local $SIG{__DIE__}; | ||||
77 | eval "require $real_class"; | ||||
78 | $@; | ||||
79 | }; | ||||
80 | |||||
81 | if ($e) { | ||||
82 | my $regex = join '.', split /::/, $real_class; | ||||
83 | $regex .= '\\.pm'; | ||||
84 | |||||
85 | if ( $e =~ /^Can't locate $regex/i ) { | ||||
86 | die | ||||
87 | "The timezone '$p{name}' could not be loaded, or is an invalid name.\n"; | ||||
88 | } | ||||
89 | else { | ||||
90 | die $e; | ||||
91 | } | ||||
92 | } | ||||
93 | } | ||||
94 | |||||
95 | my $zone = $real_class->instance( name => $p{name}, is_olson => 1 ); | ||||
96 | |||||
97 | if ( $zone->is_olson() ) { | ||||
98 | my $object_version | ||||
99 | = $zone->can('olson_version') | ||||
100 | ? $zone->olson_version() | ||||
101 | : 'unknown'; | ||||
102 | my $catalog_version = DateTime::TimeZone::Catalog->OlsonVersion(); | ||||
103 | |||||
104 | if ( $object_version ne $catalog_version ) { | ||||
105 | warn | ||||
106 | "Loaded $real_class, which is from an older version ($object_version) of the Olson database than this installation of DateTime::TimeZone ($catalog_version).\n"; | ||||
107 | } | ||||
108 | } | ||||
109 | |||||
110 | return $zone; | ||||
111 | } | ||||
112 | |||||
113 | sub _init { | ||||
114 | my $class = shift; | ||||
115 | my %p = validate( | ||||
116 | @_, { | ||||
117 | name => { type => SCALAR }, | ||||
118 | spans => { type => ARRAYREF }, | ||||
119 | is_olson => { type => BOOLEAN, default => 0 }, | ||||
120 | }, | ||||
121 | ); | ||||
122 | |||||
123 | my $self = bless { | ||||
124 | name => $p{name}, | ||||
125 | spans => $p{spans}, | ||||
126 | is_olson => $p{is_olson}, | ||||
127 | }, $class; | ||||
128 | |||||
129 | foreach my $k (qw( last_offset last_observance rules max_year )) { | ||||
130 | my $m = "_$k"; | ||||
131 | $self->{$k} = $self->$m() if $self->can($m); | ||||
132 | } | ||||
133 | |||||
134 | return $self; | ||||
135 | } | ||||
136 | |||||
137 | sub is_olson { $_[0]->{is_olson} } | ||||
138 | |||||
139 | sub is_dst_for_datetime { | ||||
140 | my $self = shift; | ||||
141 | |||||
142 | my $span = $self->_span_for_datetime( 'utc', $_[0] ); | ||||
143 | |||||
144 | return $span->[IS_DST]; | ||||
145 | } | ||||
146 | |||||
147 | sub offset_for_datetime { | ||||
148 | my $self = shift; | ||||
149 | |||||
150 | my $span = $self->_span_for_datetime( 'utc', $_[0] ); | ||||
151 | |||||
152 | return $span->[OFFSET]; | ||||
153 | } | ||||
154 | |||||
155 | sub offset_for_local_datetime { | ||||
156 | my $self = shift; | ||||
157 | |||||
158 | my $span = $self->_span_for_datetime( 'local', $_[0] ); | ||||
159 | |||||
160 | return $span->[OFFSET]; | ||||
161 | } | ||||
162 | |||||
163 | sub short_name_for_datetime { | ||||
164 | my $self = shift; | ||||
165 | |||||
166 | my $span = $self->_span_for_datetime( 'utc', $_[0] ); | ||||
167 | |||||
168 | return $span->[SHORT_NAME]; | ||||
169 | } | ||||
170 | |||||
171 | sub _span_for_datetime { | ||||
172 | my $self = shift; | ||||
173 | my $type = shift; | ||||
174 | my $dt = shift; | ||||
175 | |||||
176 | my $method = $type . '_rd_as_seconds'; | ||||
177 | |||||
178 | my $end = $type eq 'utc' ? UTC_END : LOCAL_END; | ||||
179 | |||||
180 | my $span; | ||||
181 | my $seconds = $dt->$method(); | ||||
182 | if ( $seconds < $self->max_span->[$end] ) { | ||||
183 | $span = $self->_spans_binary_search( $type, $seconds ); | ||||
184 | } | ||||
185 | else { | ||||
186 | my $until_year = $dt->utc_year + 1; | ||||
187 | $span = $self->_generate_spans_until_match( $until_year, $seconds, | ||||
188 | $type ); | ||||
189 | } | ||||
190 | |||||
191 | # This means someone gave a local time that doesn't exist | ||||
192 | # (like during a transition into savings time) | ||||
193 | unless ( defined $span ) { | ||||
194 | my $err = 'Invalid local time for date'; | ||||
195 | $err .= ' ' . $dt->iso8601 if $type eq 'utc'; | ||||
196 | $err .= " in time zone: " . $self->name; | ||||
197 | $err .= "\n"; | ||||
198 | |||||
199 | die $err; | ||||
200 | } | ||||
201 | |||||
202 | return $span; | ||||
203 | } | ||||
204 | |||||
205 | sub _spans_binary_search { | ||||
206 | my $self = shift; | ||||
207 | my ( $type, $seconds ) = @_; | ||||
208 | |||||
209 | my ( $start, $end ) = _keys_for_type($type); | ||||
210 | |||||
211 | my $min = 0; | ||||
212 | my $max = scalar @{ $self->{spans} } + 1; | ||||
213 | my $i = int( $max / 2 ); | ||||
214 | |||||
215 | # special case for when there are only 2 spans | ||||
216 | $i++ if $max % 2 && $max != 3; | ||||
217 | |||||
218 | $i = 0 if @{ $self->{spans} } == 1; | ||||
219 | |||||
220 | while (1) { | ||||
221 | my $current = $self->{spans}[$i]; | ||||
222 | |||||
223 | if ( $seconds < $current->[$start] ) { | ||||
224 | $max = $i; | ||||
225 | my $c = int( ( $i - $min ) / 2 ); | ||||
226 | $c ||= 1; | ||||
227 | |||||
228 | $i -= $c; | ||||
229 | |||||
230 | return if $i < $min; | ||||
231 | } | ||||
232 | elsif ( $seconds >= $current->[$end] ) { | ||||
233 | $min = $i; | ||||
234 | my $c = int( ( $max - $i ) / 2 ); | ||||
235 | $c ||= 1; | ||||
236 | |||||
237 | $i += $c; | ||||
238 | |||||
239 | return if $i >= $max; | ||||
240 | } | ||||
241 | else { | ||||
242 | |||||
243 | # Special case for overlapping ranges because of DST and | ||||
244 | # other weirdness (like Alaska's change when bought from | ||||
245 | # Russia by the US). Always prefer latest span. | ||||
246 | if ( $current->[IS_DST] && $type eq 'local' ) { | ||||
247 | |||||
248 | # Asia/Dhaka in 2009j goes into DST without any known | ||||
249 | # end-of-DST date (wtf, Bangladesh). | ||||
250 | return $current if $current->[UTC_END] == INFINITY; | ||||
251 | |||||
252 | my $next = $self->{spans}[ $i + 1 ]; | ||||
253 | |||||
254 | # Sometimes we will get here and the span we're | ||||
255 | # looking at is the last that's been generated so far. | ||||
256 | # We need to try to generate one more or else we run | ||||
257 | # out. | ||||
258 | $next ||= $self->_generate_next_span; | ||||
259 | |||||
260 | die "No next span in $self->{max_year}" unless defined $next; | ||||
261 | |||||
262 | if ( ( !$next->[IS_DST] ) | ||||
263 | && $next->[$start] <= $seconds | ||||
264 | && $seconds <= $next->[$end] ) { | ||||
265 | return $next; | ||||
266 | } | ||||
267 | } | ||||
268 | |||||
269 | return $current; | ||||
270 | } | ||||
271 | } | ||||
272 | } | ||||
273 | |||||
274 | sub _generate_next_span { | ||||
275 | my $self = shift; | ||||
276 | |||||
277 | my $last_idx = $#{ $self->{spans} }; | ||||
278 | |||||
279 | my $max_span = $self->max_span; | ||||
280 | |||||
281 | # Kind of a hack, but AFAIK there are no zones where it takes | ||||
282 | # _more_ than a year for a _future_ time zone change to occur, so | ||||
283 | # by looking two years out we can ensure that we will find at | ||||
284 | # least one more span. Of course, I will no doubt be proved wrong | ||||
285 | # and this will cause errors. | ||||
286 | $self->_generate_spans_until_match( $self->{max_year} + 2, | ||||
287 | $max_span->[UTC_END] + ( 366 * 86400 ), 'utc' ); | ||||
288 | |||||
289 | return $self->{spans}[ $last_idx + 1 ]; | ||||
290 | } | ||||
291 | |||||
292 | sub _generate_spans_until_match { | ||||
293 | my $self = shift; | ||||
294 | my $generate_until_year = shift; | ||||
295 | my $seconds = shift; | ||||
296 | my $type = shift; | ||||
297 | |||||
298 | my @changes; | ||||
299 | my @rules = @{ $self->_rules }; | ||||
300 | foreach my $year ( $self->{max_year} .. $generate_until_year ) { | ||||
301 | for ( my $x = 0; $x < @rules; $x++ ) { | ||||
302 | my $last_offset_from_std; | ||||
303 | |||||
304 | if ( @rules == 2 ) { | ||||
305 | $last_offset_from_std | ||||
306 | = $x | ||||
307 | ? $rules[0]->offset_from_std | ||||
308 | : $rules[1]->offset_from_std; | ||||
309 | } | ||||
310 | elsif ( @rules == 1 ) { | ||||
311 | $last_offset_from_std = $rules[0]->offset_from_std; | ||||
312 | } | ||||
313 | else { | ||||
314 | my $count = scalar @rules; | ||||
315 | die | ||||
316 | "Cannot generate future changes for zone with $count infinite rules\n"; | ||||
317 | } | ||||
318 | |||||
319 | my $rule = $rules[$x]; | ||||
320 | |||||
321 | my $next = $rule->utc_start_datetime_for_year( $year, | ||||
322 | $self->{last_offset}, $last_offset_from_std ); | ||||
323 | |||||
324 | # don't bother with changes we've seen already | ||||
325 | next if $next->utc_rd_as_seconds < $self->max_span->[UTC_END]; | ||||
326 | |||||
327 | push @changes, | ||||
328 | DateTime::TimeZone::OlsonDB::Change->new( | ||||
329 | type => 'rule', | ||||
330 | utc_start_datetime => $next, | ||||
331 | local_start_datetime => $next + DateTime::Duration->new( | ||||
332 | seconds => $self->{last_observance}->total_offset | ||||
333 | + $rule->offset_from_std | ||||
334 | ), | ||||
335 | short_name => sprintf( | ||||
336 | $self->{last_observance}->format, $rule->letter | ||||
337 | ), | ||||
338 | observance => $self->{last_observance}, | ||||
339 | rule => $rule, | ||||
340 | ); | ||||
341 | } | ||||
342 | } | ||||
343 | |||||
344 | $self->{max_year} = $generate_until_year; | ||||
345 | |||||
346 | my @sorted | ||||
347 | = sort { $a->utc_start_datetime <=> $b->utc_start_datetime } @changes; | ||||
348 | |||||
349 | my ( $start, $end ) = _keys_for_type($type); | ||||
350 | |||||
351 | my $match; | ||||
352 | for ( my $x = 1; $x < @sorted; $x++ ) { | ||||
353 | my $last_total_offset | ||||
354 | = $x == 1 | ||||
355 | ? $self->max_span->[OFFSET] | ||||
356 | : $sorted[ $x - 2 ]->total_offset; | ||||
357 | |||||
358 | my $span = DateTime::TimeZone::OlsonDB::Change::two_changes_as_span( | ||||
359 | @sorted[ $x - 1, $x ], $last_total_offset ); | ||||
360 | |||||
361 | $span = _span_as_array($span); | ||||
362 | |||||
363 | push @{ $self->{spans} }, $span; | ||||
364 | |||||
365 | $match = $span | ||||
366 | if $seconds >= $span->[$start] && $seconds < $span->[$end]; | ||||
367 | } | ||||
368 | |||||
369 | return $match; | ||||
370 | } | ||||
371 | |||||
372 | sub max_span { $_[0]->{spans}[-1] } | ||||
373 | |||||
374 | sub _keys_for_type { | ||||
375 | $_[0] eq 'utc' ? ( UTC_START, UTC_END ) : ( LOCAL_START, LOCAL_END ); | ||||
376 | } | ||||
377 | |||||
378 | sub _span_as_array { | ||||
379 | [ | ||||
380 | @{ $_[0] }{ | ||||
381 | qw( utc_start utc_end local_start local_end offset is_dst short_name ) | ||||
382 | } | ||||
383 | ]; | ||||
384 | } | ||||
385 | |||||
386 | sub is_floating {0} | ||||
387 | |||||
388 | sub is_utc {0} | ||||
389 | |||||
390 | sub has_dst_changes {0} | ||||
391 | |||||
392 | sub name { $_[0]->{name} } | ||||
393 | sub category { ( split /\//, $_[0]->{name}, 2 )[0] } | ||||
394 | |||||
395 | sub is_valid_name { | ||||
396 | my $tz; | ||||
397 | { | ||||
398 | local $@; | ||||
399 | local $SIG{__DIE__}; | ||||
400 | $tz = eval { $_[0]->new( name => $_[1] ) }; | ||||
401 | } | ||||
402 | |||||
403 | return $tz && $tz->isa('DateTime::TimeZone') ? 1 : 0; | ||||
404 | } | ||||
405 | |||||
406 | sub STORABLE_freeze { | ||||
407 | my $self = shift; | ||||
408 | |||||
409 | return $self->name; | ||||
410 | } | ||||
411 | |||||
412 | sub STORABLE_thaw { | ||||
413 | my $self = shift; | ||||
414 | my $cloning = shift; | ||||
415 | my $serialized = shift; | ||||
416 | |||||
417 | my $class = ref $self || $self; | ||||
418 | |||||
419 | my $obj; | ||||
420 | if ( $class->isa(__PACKAGE__) ) { | ||||
421 | $obj = __PACKAGE__->new( name => $serialized ); | ||||
422 | } | ||||
423 | else { | ||||
424 | $obj = $class->new( name => $serialized ); | ||||
425 | } | ||||
426 | |||||
427 | %$self = %$obj; | ||||
428 | |||||
429 | return $self; | ||||
430 | } | ||||
431 | |||||
432 | # | ||||
433 | # Functions | ||||
434 | # | ||||
435 | sub offset_as_seconds { | ||||
436 | { | ||||
437 | local $@; | ||||
438 | local $SIG{__DIE__}; | ||||
439 | shift if eval { $_[0]->isa('DateTime::TimeZone') }; | ||||
440 | } | ||||
441 | |||||
442 | my $offset = shift; | ||||
443 | |||||
444 | return undef unless defined $offset; | ||||
445 | |||||
446 | return 0 if $offset eq '0'; | ||||
447 | |||||
448 | my ( $sign, $hours, $minutes, $seconds ); | ||||
449 | if ( $offset =~ /^([\+\-])?(\d\d?):(\d\d)(?::(\d\d))?$/ ) { | ||||
450 | ( $sign, $hours, $minutes, $seconds ) = ( $1, $2, $3, $4 ); | ||||
451 | } | ||||
452 | elsif ( $offset =~ /^([\+\-])?(\d\d)(\d\d)(\d\d)?$/ ) { | ||||
453 | ( $sign, $hours, $minutes, $seconds ) = ( $1, $2, $3, $4 ); | ||||
454 | } | ||||
455 | else { | ||||
456 | return undef; | ||||
457 | } | ||||
458 | |||||
459 | $sign = '+' unless defined $sign; | ||||
460 | return undef unless $hours >= 0 && $hours <= 99; | ||||
461 | return undef unless $minutes >= 0 && $minutes <= 59; | ||||
462 | return undef | ||||
463 | unless !defined($seconds) || ( $seconds >= 0 && $seconds <= 59 ); | ||||
464 | |||||
465 | my $total = $hours * 3600 + $minutes * 60; | ||||
466 | $total += $seconds if $seconds; | ||||
467 | $total *= -1 if $sign eq '-'; | ||||
468 | |||||
469 | return $total; | ||||
470 | } | ||||
471 | |||||
472 | sub offset_as_string { | ||||
473 | { | ||||
474 | local $@; | ||||
475 | local $SIG{__DIE__}; | ||||
476 | shift if eval { $_[0]->isa('DateTime::TimeZone') }; | ||||
477 | } | ||||
478 | |||||
479 | my $offset = shift; | ||||
480 | |||||
481 | return undef unless defined $offset; | ||||
482 | return undef unless $offset >= -359999 && $offset <= 359999; | ||||
483 | |||||
484 | my $sign = $offset < 0 ? '-' : '+'; | ||||
485 | |||||
486 | $offset = abs($offset); | ||||
487 | |||||
488 | my $hours = int( $offset / 3600 ); | ||||
489 | $offset %= 3600; | ||||
490 | my $mins = int( $offset / 60 ); | ||||
491 | $offset %= 60; | ||||
492 | my $secs = int($offset); | ||||
493 | |||||
494 | return ( | ||||
495 | $secs | ||||
496 | ? sprintf( '%s%02d%02d%02d', $sign, $hours, $mins, $secs ) | ||||
497 | : sprintf( '%s%02d%02d', $sign, $hours, $mins ) | ||||
498 | ); | ||||
499 | } | ||||
500 | |||||
501 | # These methods all operate on data contained in the DateTime/TimeZone/Catalog.pm file. | ||||
502 | |||||
503 | sub all_names { | ||||
504 | return | ||||
505 | wantarray | ||||
506 | ? @DateTime::TimeZone::Catalog::ALL | ||||
507 | : [@DateTime::TimeZone::Catalog::ALL]; | ||||
508 | } | ||||
509 | |||||
510 | sub categories { | ||||
511 | return wantarray | ||||
512 | ? @DateTime::TimeZone::Catalog::CATEGORY_NAMES | ||||
513 | : [@DateTime::TimeZone::Catalog::CATEGORY_NAMES]; | ||||
514 | } | ||||
515 | |||||
516 | sub links { | ||||
517 | return | ||||
518 | wantarray | ||||
519 | ? %DateTime::TimeZone::Catalog::LINKS | ||||
520 | : {%DateTime::TimeZone::Catalog::LINKS}; | ||||
521 | } | ||||
522 | |||||
523 | sub names_in_category { | ||||
524 | shift if $_[0]->isa('DateTime::TimeZone'); | ||||
525 | return unless exists $DateTime::TimeZone::Catalog::CATEGORIES{ $_[0] }; | ||||
526 | |||||
527 | return wantarray | ||||
528 | ? @{ $DateTime::TimeZone::Catalog::CATEGORIES{ $_[0] } } | ||||
529 | : $DateTime::TimeZone::Catalog::CATEGORIES{ $_[0] }; | ||||
530 | } | ||||
531 | |||||
532 | sub countries { | ||||
533 | wantarray | ||||
534 | ? ( sort keys %DateTime::TimeZone::Catalog::ZONES_BY_COUNTRY ) | ||||
535 | : [ sort keys %DateTime::TimeZone::Catalog::ZONES_BY_COUNTRY ]; | ||||
536 | } | ||||
537 | |||||
538 | sub names_in_country { | ||||
539 | shift if $_[0]->isa('DateTime::TimeZone'); | ||||
540 | |||||
541 | return | ||||
542 | unless | ||||
543 | exists $DateTime::TimeZone::Catalog::ZONES_BY_COUNTRY{ lc $_[0] }; | ||||
544 | |||||
545 | return | ||||
546 | wantarray | ||||
547 | ? @{ $DateTime::TimeZone::Catalog::ZONES_BY_COUNTRY{ lc $_[0] } } | ||||
548 | : $DateTime::TimeZone::Catalog::ZONES_BY_COUNTRY{ lc $_[0] }; | ||||
549 | } | ||||
550 | |||||
551 | 1 | 4µs | 1; | ||
552 | |||||
553 | # ABSTRACT: Time zone object base class and factory | ||||
554 | |||||
555 | __END__ | ||||
# spent 3µs within DateTime::TimeZone::CORE:match which was called 2 times, avg 2µs/call:
# 2 times (3µs+0s) by DateTime::TimeZone::new at line 46, avg 2µs/call |