Debian Codegraph @

lib/Time/Zone.pm

raw
1
2 package Time::Zone;
3
4
5 require 5.006;
6
7 require Exporter;
8 use Carp;
9 use strict;
10 use POSIX qw(tzset tzname);
11
12 our $VERSION = '2.35'; # VERSION: generated
13 # ABSTRACT: miscellaneous timezone manipulations routines
14 our @ISA = qw(Exporter);
15 our @EXPORT = qw(tz2zone tz_local_offset tz_offset tz_name);
16
17 # Parts stolen from code by Paul Foley <[email protected]>
18
19 my %tzn_cache;
20 sub tz2zone (;$$$)
21 {
22 my($TZ, $time, $isdst) = @_;
23
24 $TZ = defined($ENV{'TZ'}) ? ( $ENV{'TZ'} ? $ENV{'TZ'} : 'GMT' ) : ''
25 unless $TZ;
26
27 # Hack to deal with 'PST8PDT' format of TZ
28 # Note that this can't deal with all the esoteric forms, but it
29 # does recognize the most common: [:]STDoff[DST[off][,rule]]
30
31 if (! defined $isdst) {
32 my $j;
33 $time = time() unless $time;
34 ($j, $j, $j, $j, $j, $j, $j, $j, $isdst) = localtime($time);
35 }
36
37 if (defined $tzn_cache{$TZ}->[$isdst]) {
38 return $tzn_cache{$TZ}->[$isdst];
39 }
40
41 # Handle IANA timezone names (e.g., "America/Chicago", "Europe/Paris")
42 if ($TZ =~ m{/}) {
43 my ($std, $dst_name) = _iana_tzname($TZ);
44 $tzn_cache{$TZ} = [ $std, $dst_name ];
45 return $isdst ? $dst_name : $std;
46 }
47
48 if ($TZ =~ /^
49 ( [^:\d+\-,] {3,} )
50 ( [+-] ?
51 \d {1,2}
52 ( : \d {1,2} ) {0,2}
53 )
54 ( [^\d+\-,] {3,} )?
55 /x
56 ) {
57 my $dsttz = defined($4) ? $4 : $1;
58 $TZ = $isdst ? $dsttz : $1;
59 $tzn_cache{$TZ} = [ $1, $dsttz ];
60 } else {
61 $tzn_cache{$TZ} = [ $TZ, $TZ ];
62 }
63 return $TZ;
64 }
65
66 my @tz_local;
67 sub tz_local_offset (;$)
68 {
69 my ($time) = @_;
70
71 $time = time() unless $time;
72 my (@l) = localtime($time);
73 my $isdst = $l[8];
74
75 if (defined($tz_local[$isdst])) {
76 return $tz_local[$isdst];
77 }
78
79 $tz_local[$isdst] = &calc_off($time);
80
81 return $tz_local[$isdst];
82 }
83
84 sub calc_off
85 {
86 my ($time) = @_;
87
88 my (@l) = localtime($time);
89 my (@g) = gmtime($time);
90
91 my $off;
92
93 $off = $l[0] - $g[0]
94 + ($l[1] - $g[1]) * 60
95 + ($l[2] - $g[2]) * 3600;
96
97 # subscript 7 is yday.
98
99 if ($l[7] == $g[7]) {
100 # done
101 } elsif ($l[7] == $g[7] + 1) {
102 $off += 86400;
103 } elsif ($l[7] == $g[7] - 1) {
104 $off -= 86400;
105 } elsif ($l[7] < $g[7]) {
106 # crossed over a year boundary!
107 # localtime is beginning of year, gmt is end
108 # therefore local is ahead
109 $off += 86400;
110 } else {
111 $off -= 86400;
112 }
113
114 return $off;
115 }
116
117 # Helper: temporarily set TZ to an IANA name, run a block, restore original TZ.
118 sub _with_iana_tz (&$) {
119 my ($code, $tz) = @_;
120 my $had_tz = exists $ENV{TZ};
121 my $saved = $ENV{TZ};
122 $ENV{TZ} = $tz;
123 tzset();
124 my @result = $code->();
125 if ($had_tz) { $ENV{TZ} = $saved } else { delete $ENV{TZ} }
126 tzset();
127 return @result;
128 }
129
130 # Return (std_name, dst_name) for an IANA timezone string.
131 sub _iana_tzname {
132 my ($tz) = @_;
133 my ($std, $dst) = _with_iana_tz { tzname() } $tz;
134 $dst = $std unless defined $dst;
135 return ($std, $dst);
136 }
137
138 # Return the UTC offset in seconds for an IANA timezone string at a given time.
139 sub _iana_offset {
140 my ($tz, $time) = @_;
141 $time = time() unless $time;
142 my ($off) = _with_iana_tz { calc_off($time) } $tz;
143 return $off;
144 }
145
146 # constants
147
148 my (%dstZone, %zoneOff, %dstZoneOff, %Zone);
149
150 CONFIG: {
151 my @dstZone = (
152 "ndt" => -2*3600-1800, # Newfoundland Daylight
153 "brst" => -2*3600, # Brazil Summer Time (East Daylight)
154 "adt" => -3*3600, # Atlantic Daylight
155 "edt" => -4*3600, # Eastern Daylight
156 "cdt" => -5*3600, # Central Daylight
157 "mdt" => -6*3600, # Mountain Daylight
158 "pdt" => -7*3600, # Pacific Daylight
159 "akdt" => -8*3600, # Alaska Daylight
160 "ydt" => -8*3600, # Yukon Daylight
161 "hdt" => -9*3600, # Hawaii Daylight
162 "bst" => +1*3600, # British Summer
163 "cest" => +2*3600, # Central European Summer (preferred)
164 "mest" => +2*3600, # Middle European Summer (alias, kept for compat)
165 "metdst" => +2*3600, # Middle European DST
166 "sst" => +2*3600, # Swedish Summer
167 "fst" => +2*3600, # French Summer
168 "eest" => +3*3600, # Eastern European Summer
169 "msd" => +4*3600, # Moscow Daylight (historical; Russia abolished DST permanently in Oct 2014)
170 "wadt" => +8*3600, # West Australian Daylight
171 "kdt" => +10*3600, # Korean Daylight
172 # "cadt" => +10*3600+1800, # Central Australian Daylight
173 "aedt" => +11*3600, # Eastern Australian Daylight
174 "eadt" => +11*3600, # Eastern Australian Daylight
175 "nzd" => +13*3600, # New Zealand Daylight
176 "nzdt" => +13*3600, # New Zealand Daylight
177 );
178
179 my @Zone = (
180 "gmt" => 0, # Greenwich Mean
181 "ut" => 0, # Universal (Coordinated)
182 "utc" => 0,
183 "wet" => 0, # Western European
184 "wat" => -1*3600, # West Africa
185 "at" => -2*3600, # Azores
186 "fnt" => -2*3600, # Brazil Time (Extreme East - Fernando Noronha)
187 "brt" => -3*3600, # Brazil Time (East Standard - Brasilia)
188 # For completeness. BST is also British Summer, and GST is also Guam Standard.
189 # "bst" => -3*3600, # Brazil Standard
190 # "gst" => -3*3600, # Greenland Standard
191 "nft" => -3*3600-1800,# Newfoundland
192 "nst" => -3*3600-1800,# Newfoundland Standard
193 "mnt" => -4*3600, # Brazil Time (West Standard - Manaus)
194 "ewt" => -4*3600, # U.S. Eastern War Time
195 "ast" => -4*3600, # Atlantic Standard
196 "est" => -5*3600, # Eastern Standard
197 "act" => -5*3600, # Brazil Time (Extreme West - Acre)
198 "cst" => -6*3600, # Central Standard
199 "mst" => -7*3600, # Mountain Standard
200 "pst" => -8*3600, # Pacific Standard
201 "akst" => -9*3600, # Alaska Standard
202 "yst" => -9*3600, # Yukon Standard
203 "hst" => -10*3600, # Hawaii Standard
204 "cat" => -10*3600, # Central Alaska
205 "ahst" => -10*3600, # Alaska-Hawaii Standard
206 "nt" => -11*3600, # Nome
207 "idlw" => -12*3600, # International Date Line West
208 "cet" => +1*3600, # Central European
209 "mez" => +1*3600, # Central European (German)
210 "ect" => +1*3600, # Central European (French)
211 "met" => +1*3600, # Middle European
212 "mewt" => +1*3600, # Middle European Winter
213 "swt" => +1*3600, # Swedish Winter
214 "set" => +1*3600, # Seychelles
215 "fwt" => +1*3600, # French Winter
216 "eet" => +2*3600, # Eastern Europe, USSR Zone 1
217 "ukr" => +2*3600, # Ukraine
218 "bt" => +3*3600, # Baghdad, USSR Zone 2
219 "msk" => +3*3600, # Moscow (UTC+3; was UTC+4 in 2011-2014 when Russia used permanent DST, reverted Oct 2014)
220 # "it" => +3*3600+1800,# Iran
221 "zp4" => +4*3600, # USSR Zone 3
222 "zp5" => +5*3600, # USSR Zone 4
223 "ist" => +5*3600+1800,# Indian Standard
224 "zp6" => +6*3600, # USSR Zone 5
225 # For completeness. NST is also Newfoundland Stanard, and SST is also Swedish Summer.
226 # "nst" => +6*3600+1800,# North Sumatra
227 # "sst" => +7*3600, # South Sumatra, USSR Zone 6
228 "ict" => +7*3600, # Indochina
229 # "jt" => +7*3600+1800,# Java (3pm in Cronusland!)
230 "ict" => +7*3600, # Indochina Time
231 "wst" => +8*3600, # West Australian Standard
232 "pht" => +8*3600, # Philippine
233 "hkt" => +8*3600, # Hong Kong
234 "pht" => +8*3600, # Philippine Time
235 "cct" => +8*3600, # China Coast, USSR Zone 7
236 "jst" => +9*3600, # Japan Standard, USSR Zone 8
237 "kst" => +9*3600, # Korean Standard
238 # "cast" => +9*3600+1800,# Central Australian Standard
239 "aest" => +10*3600, # Eastern Australian Standard
240 "east" => +10*3600, # Eastern Australian Standard
241 "gst" => +10*3600, # Guam Standard, USSR Zone 9
242 "nzt" => +12*3600, # New Zealand
243 "nzst" => +12*3600, # New Zealand Standard
244 "idle" => +12*3600, # International Date Line East
245 );
246
247 %Zone = @Zone;
248 %dstZone = @dstZone;
249 %zoneOff = reverse(@Zone);
250 %dstZoneOff = reverse(@dstZone);
251
252 }
253
254 sub tz_offset (;$$)
255 {
256 my ($zone, $time) = @_;
257
258 return &tz_local_offset($time) unless($zone);
259
260 $time = time() unless $time;
261 my(@l) = localtime($time);
262 my $dst = $l[8];
263
264 # Handle IANA timezone names (e.g., "America/Chicago") before lowercasing
265 if ($zone =~ m{/}) {
266 return _iana_offset($zone, $time);
267 }
268
269 $zone = lc $zone;
270
271 if($zone =~ /^(([\-\+])\d\d?)(\d\d)$/) {
272 my $v = $2 . $3;
273 return $1 * 3600 + $v * 60;
274 } elsif (exists $dstZone{$zone} && ($dst || !exists $Zone{$zone})) {
275 return $dstZone{$zone};
276 } elsif(exists $Zone{$zone}) {
277 return $Zone{$zone};
278 }
279 undef;
280 }
281
282 sub tz_name (;$$)
283 {
284 my ($off, $dst) = @_;
285
286 $off = tz_offset()
287 unless(defined $off);
288
289 $dst = (localtime(time))[8]
290 unless(defined $dst);
291
292 if (exists $dstZoneOff{$off} && ($dst || !exists $zoneOff{$off})) {
293 return $dstZoneOff{$off};
294 } elsif (exists $zoneOff{$off}) {
295 return $zoneOff{$off};
296 }
297 # $off is in seconds; format as +HHMM / -HHMM.
298 # Using abs() for the minutes component handles negative fractional-hour
299 # offsets correctly (e.g. -9000s = -2h30m → "-0230", not "-02-30").
300 sprintf("%+03d%02d", int($off / 3600), abs(int($off / 60)) % 60);
301 }
302
303 1;
304
305 __END__
306
307 =pod
308
309 =encoding UTF-8
310
311 =head1 NAME
312
313 Time::Zone - miscellaneous timezone manipulations routines
314
315 =head1 VERSION
316
317 version 2.35
318
319 =head1 SYNOPSIS
320
321 use Time::Zone;
322 print tz2zone();
323 print tz2zone($ENV{'TZ'});
324 print tz2zone($ENV{'TZ'}, time());
325 my $isdst = 0;
326 print tz2zone($ENV{'TZ'}, undef, $isdst);
327 my $offset = tz_local_offset();
328 my $TZ = "EST";
329 $offset = tz_offset($TZ);
330
331 =head1 DESCRIPTION
332
333 This is a collection of miscellaneous timezone manipulation routines.
334
335 C<tz2zone()> parses the TZ environment variable and returns a timezone
336 string suitable for inclusion in L<date(1)>-like output. It optionally takes
337 a timezone string, a time, and a is-dst flag.
338
339 C<tz_local_offset()> determines the offset from GMT time in seconds. It
340 only does the calculation once.
341
342 C<tz_offset()> determines the offset from GMT in seconds of a specified
343 timezone.
344
345 C<tz_name()> determines the name of the timezone based on its offset
346
347 =head1 NAME
348
349 Time::Zone -- miscellaneous timezone manipulations routines
350
351 =head1 AUTHORS
352
353 Graham Barr <[email protected]>
354 David Muir Sharnoff <[email protected]>
355 Paul Foley <[email protected]>
356
357 =head1 AUTHOR
358
359 Graham <[email protected]>
360
361 =head1 COPYRIGHT AND LICENSE
362
363 This software is copyright (c) 2020 by Graham Barr.
364
365 This is free software; you can redistribute it and/or modify it under
366 the same terms as the Perl 5 programming language system itself.
367
368 =cut