| 1 | package Date::Manip::TZ; |
| 2 | # Copyright (c) 2008-2026 Sullivan Beck. All rights reserved. |
| 3 | # This program is free software; you can redistribute it and/or modify it |
| 4 | # under the same terms as Perl itself. |
| 5 | |
| 6 | ######################################################################## |
| 7 | # Any routine that starts with an underscore (_) is NOT intended for |
| 8 | # public use. They are for internal use in the the Date::Manip |
| 9 | # modules and are subject to change without warning or notice. |
| 10 | # |
| 11 | # ABSOLUTELY NO USER SUPPORT IS OFFERED FOR THESE ROUTINES! |
| 12 | ######################################################################## |
| 13 | |
| 14 | use Date::Manip::Obj; |
| 15 | use Date::Manip::TZ_Base; |
| 16 | @ISA = qw(Date::Manip::Obj Date::Manip::TZ_Base); |
| 17 | |
| 18 | require 5.010000; |
| 19 | use warnings; |
| 20 | use strict; |
| 21 | |
| 22 | use IO::File; |
| 23 | require Date::Manip::Zones; |
| 24 | use Date::Manip::Base; |
| 25 | use Data::Dumper; |
| 26 | use Carp; |
| 27 | |
| 28 | our $VERSION; |
| 29 | $VERSION='6.99'; |
| 30 | END { undef $VERSION; } |
| 31 | |
| 32 | # To get rid of a 'used only once' warnings. |
| 33 | END { |
| 34 | my $tmp = \%Date::Manip::Zones::Module; |
| 35 | $tmp = \%Date::Manip::Zones::ZoneNames; |
| 36 | $tmp = \%Date::Manip::Zones::Alias; |
| 37 | $tmp = \%Date::Manip::Zones::Abbrev; |
| 38 | $tmp = \%Date::Manip::Zones::Offmod; |
| 39 | $tmp = $Date::Manip::Zones::FirstDate; |
| 40 | $tmp = $Date::Manip::Zones::LastDate; |
| 41 | $tmp = $Date::Manip::Zones::LastYear; |
| 42 | $tmp = $Date::Manip::Zones::TzcodeVersion; |
| 43 | $tmp = $Date::Manip::Zones::TzdataVersion; |
| 44 | } |
| 45 | |
| 46 | ######################################################################## |
| 47 | # BASE METHODS |
| 48 | ######################################################################## |
| 49 | |
| 50 | sub _init { |
| 51 | my($self) = @_; |
| 52 | |
| 53 | $$self{'data'} = |
| 54 | { |
| 55 | # These are the variables defined in Date::Manip::Zones |
| 56 | 'Module' => \%Date::Manip::Zones::Module, |
| 57 | 'ZoneNames' => \%Date::Manip::Zones::ZoneNames, |
| 58 | 'Alias' => \%Date::Manip::Zones::Alias, |
| 59 | 'Abbrev' => \%Date::Manip::Zones::Abbrev, |
| 60 | 'Offmod' => \%Date::Manip::Zones::Offmod, |
| 61 | 'FirstDate' => $Date::Manip::Zones::FirstDate, |
| 62 | 'LastDate' => $Date::Manip::Zones::LastDate, |
| 63 | 'LastYear' => $Date::Manip::Zones::LastYear, |
| 64 | |
| 65 | # These override values from Date::Manip::Zones |
| 66 | 'MyAlias' => {}, |
| 67 | 'MyAbbrev' => {}, |
| 68 | 'MyOffsets' => {}, |
| 69 | |
| 70 | # Each timezone/offset module that is loaded goes here |
| 71 | 'Zones' => {}, |
| 72 | 'Offsets' => {}, |
| 73 | |
| 74 | # methods a list of methods used for determining the |
| 75 | # current zone |
| 76 | # path the PATH to set for determining the current |
| 77 | # zone |
| 78 | # dates critical dates on a per/year (UT) basis |
| 79 | # zonerx the regular expression for matching timezone |
| 80 | # names/aliases |
| 81 | # abbrx the regular expression for matching timezone |
| 82 | # abbreviations |
| 83 | # offrx the regular expression for matching a valid |
| 84 | # timezone offset |
| 85 | # zrx the regular expression to match all timezone |
| 86 | # information |
| 87 | 'methods' => [], |
| 88 | 'path' => undef, |
| 89 | 'zonerx' => undef, |
| 90 | 'abbrx' => undef, |
| 91 | 'offrx' => undef, |
| 92 | 'zrx' => undef, |
| 93 | }; |
| 94 | |
| 95 | # OS specific stuff |
| 96 | |
| 97 | my $dmb = $$self{'base'}; |
| 98 | my $os = $dmb->_os(); |
| 99 | |
| 100 | if ($os eq 'Unix') { |
| 101 | $$self{'data'}{'path'} = '/bin:/usr/bin'; |
| 102 | $$self{'data'}{'methods'} = [ |
| 103 | qw(main TZ |
| 104 | env zone TZ |
| 105 | file /etc/TIMEZONE |
| 106 | file /etc/timezone |
| 107 | file /etc/sysconfig/clock |
| 108 | file /etc/default/init |
| 109 | ), |
| 110 | 'command', '/bin/date +%Z', |
| 111 | 'command', '/usr/bin/date +%Z', |
| 112 | 'command', '/usr/local/bin/date +%Z', |
| 113 | qw(cmdfield /bin/date -2 |
| 114 | cmdfield /usr/bin/date -2 |
| 115 | cmdfield /usr/local/bin/date -2 |
| 116 | ), |
| 117 | 'command', '/bin/date +%z', |
| 118 | 'command', '/usr/bin/date +%z', |
| 119 | 'command', '/usr/local/bin/date +%z', |
| 120 | qw( tzdata /etc/localtime /usr/share/zoneinfo |
| 121 | ), |
| 122 | 'gmtoff' |
| 123 | ]; |
| 124 | |
| 125 | } elsif ($os eq 'Windows') { |
| 126 | $$self{'data'}{'methods'} = [ |
| 127 | qw(main TZ |
| 128 | env zone TZ |
| 129 | registry |
| 130 | gmtoff), |
| 131 | ]; |
| 132 | |
| 133 | } elsif ($os eq 'VMS') { |
| 134 | $$self{'data'}{'methods'} = [ |
| 135 | qw(main TZ |
| 136 | env zone TZ |
| 137 | env zone SYS$TIMEZONE_NAME |
| 138 | env zone UCX$TZ |
| 139 | env zone TCPIP$TZ |
| 140 | env zone MULTINET_TIMEZONE |
| 141 | env offset SYS$TIMEZONE_DIFFERENTIAL |
| 142 | gmtoff |
| 143 | ), |
| 144 | ]; |
| 145 | |
| 146 | } else { |
| 147 | $$self{'data'}{'methods'} = [ |
| 148 | qw(main TZ |
| 149 | env zone TZ |
| 150 | gmtoff |
| 151 | ), |
| 152 | ]; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | sub _init_final { |
| 157 | my($self) = @_; |
| 158 | |
| 159 | $self->_set_curr_zone(); |
| 160 | } |
| 161 | |
| 162 | no strict 'refs'; |
| 163 | # This loads data from an offset module |
| 164 | # |
| 165 | sub _offmod { |
| 166 | my($self,$offset) = @_; |
| 167 | return if (exists $$self{'data'}{'Offsets'}{$offset}); |
| 168 | |
| 169 | my $mod = $$self{'data'}{'Offmod'}{$offset}; |
| 170 | eval "require Date::Manip::Offset::${mod}"; |
| 171 | my %off = %{ "Date::Manip::Offset::${mod}::Offset" }; |
| 172 | |
| 173 | $$self{'data'}{'Offsets'}{$offset} = { %off }; |
| 174 | } |
| 175 | |
| 176 | # This loads data from a zone module (takes a lowercase zone) |
| 177 | # |
| 178 | sub _module { |
| 179 | my($self,$zone) = @_; |
| 180 | return if (exists $$self{'data'}{'Zones'}{$zone}{'Loaded'}); |
| 181 | |
| 182 | my $mod = $$self{'data'}{'Module'}{$zone}; |
| 183 | eval "require Date::Manip::TZ::${mod}"; |
| 184 | my %dates = %{ "Date::Manip::TZ::${mod}::Dates" }; |
| 185 | my %last = %{ "Date::Manip::TZ::${mod}::LastRule" }; |
| 186 | $$self{'data'}{'Zones'}{$zone} = |
| 187 | { |
| 188 | 'Dates' => { %dates }, |
| 189 | 'LastRule' => { %last }, |
| 190 | 'Loaded' => 1 |
| 191 | }; |
| 192 | } |
| 193 | use strict 'refs'; |
| 194 | |
| 195 | ######################################################################## |
| 196 | # CHECKING/MODIFYING ZONEINFO DATA |
| 197 | ######################################################################## |
| 198 | |
| 199 | sub _zone { |
| 200 | my($self,$zone) = @_; |
| 201 | $zone = lc($zone); |
| 202 | |
| 203 | if (exists $$self{'data'}{'MyAlias'}{$zone}) { |
| 204 | return $$self{'data'}{'MyAlias'}{$zone}; |
| 205 | } elsif (exists $$self{'data'}{'Alias'}{$zone}) { |
| 206 | return $$self{'data'}{'Alias'}{$zone}; |
| 207 | } else { |
| 208 | return ''; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | sub tzdata { |
| 213 | my($self) = @_; |
| 214 | return $Date::Manip::Zones::TzdataVersion; |
| 215 | } |
| 216 | |
| 217 | sub tzcode { |
| 218 | my($self) = @_; |
| 219 | return $Date::Manip::Zones::TzcodeVersion; |
| 220 | } |
| 221 | |
| 222 | sub define_alias { |
| 223 | my($self,$alias,$zone) = @_; |
| 224 | $alias = lc($alias); |
| 225 | |
| 226 | if ($alias eq 'reset') { |
| 227 | $$self{'data'}{'MyAlias'} = {}; |
| 228 | $$self{'data'}{'zonerx'} = undef; |
| 229 | return 0; |
| 230 | } |
| 231 | if (lc($zone) eq 'reset') { |
| 232 | delete $$self{'data'}{'MyAlias'}{$alias}; |
| 233 | $$self{'data'}{'zonerx'} = undef; |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | $zone = $self->_zone($zone); |
| 238 | |
| 239 | return 1 if (! $zone); |
| 240 | $$self{'data'}{'MyAlias'}{$alias} = $zone; |
| 241 | $$self{'data'}{'zonerx'} = undef; |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | sub define_abbrev { |
| 246 | my($self,$abbrev,@zone) = @_; |
| 247 | $abbrev = lc($abbrev); |
| 248 | |
| 249 | if ($abbrev eq 'reset') { |
| 250 | $$self{'data'}{'MyAbbrev'} = {}; |
| 251 | $$self{'data'}{'abbrx'} = undef; |
| 252 | return 0; |
| 253 | } |
| 254 | if ($#zone == 0 && lc($zone[0]) eq 'reset') { |
| 255 | delete $$self{'data'}{'MyAbbrev'}{$abbrev}; |
| 256 | $$self{'data'}{'abbrx'} = undef; |
| 257 | return (0); |
| 258 | } |
| 259 | |
| 260 | if (! exists $$self{'data'}{'Abbrev'}{$abbrev}) { |
| 261 | return (1); |
| 262 | } |
| 263 | |
| 264 | my (@z,%z); |
| 265 | my %zone = map { $_,1 } @{ $$self{'data'}{'Abbrev'}{$abbrev} }; |
| 266 | foreach my $z (@zone) { |
| 267 | my $zone = $self->_zone($z); |
| 268 | return (2,$z) if (! $zone); |
| 269 | return (3,$z) if (! exists $zone{$zone}); |
| 270 | next if (exists $z{$zone}); |
| 271 | $z{$zone} = 1; |
| 272 | push(@z,$zone); |
| 273 | } |
| 274 | |
| 275 | $$self{'data'}{'MyAbbrev'}{$abbrev} = [ @z ]; |
| 276 | $$self{'data'}{'abbrx'} = undef; |
| 277 | # Some abbreviations are timezone aliases, so get rid of them (if any) |
| 278 | # so this abbreviation won't falsely identify as another timezone. |
| 279 | delete $$self{'data'}{'Alias'}{$abbrev}; |
| 280 | return (); |
| 281 | } |
| 282 | |
| 283 | sub define_offset { |
| 284 | my($self,$offset,@args) = @_; |
| 285 | my $dmb = $$self{'base'}; |
| 286 | |
| 287 | if (lc($offset) eq 'reset') { |
| 288 | $$self{'data'}{'MyOffsets'} = {}; |
| 289 | return (0); |
| 290 | } |
| 291 | if ($#args == 0 && lc($args[0]) eq 'reset') { |
| 292 | delete $$self{'data'}{'MyOffsets'}{$offset}; |
| 293 | return (0); |
| 294 | } |
| 295 | |
| 296 | # Check that $offset is valid. If it is, load the |
| 297 | # appropriate module. |
| 298 | |
| 299 | if (ref($offset)) { |
| 300 | $offset = $dmb->join('offset',$offset); |
| 301 | } else { |
| 302 | $offset = $dmb->_delta_convert('offset',$offset); |
| 303 | } |
| 304 | return (9) if (! $offset); |
| 305 | return (1) if (! exists $$self{'data'}{'Offmod'}{$offset}); |
| 306 | |
| 307 | $self->_offmod($offset); |
| 308 | |
| 309 | # Find out whether we're handling STD, DST, or both. |
| 310 | |
| 311 | my(@isdst) = (0,1); |
| 312 | if ($args[0] =~ /^std|dst|stdonly|dstonly$/i) { |
| 313 | my $tmp = lc(shift(@args)); |
| 314 | if ($tmp eq 'stdonly') { |
| 315 | @isdst = (0); |
| 316 | } elsif ($tmp eq 'dstonly') { |
| 317 | @isdst = (1); |
| 318 | } |
| 319 | } |
| 320 | my @zone = @args; |
| 321 | |
| 322 | if ($#isdst == 0 && |
| 323 | ! exists($$self{'data'}{'Offsets'}{$offset}{$isdst[0]})) { |
| 324 | return (2); |
| 325 | } |
| 326 | |
| 327 | # Check to see that each zone is valid, and contains this offset. |
| 328 | |
| 329 | my %tmp; |
| 330 | foreach my $isdst (0,1) { |
| 331 | next if (! exists $$self{'data'}{'Offsets'}{$offset}{$isdst}); |
| 332 | my @z = @{ $$self{'data'}{'Offsets'}{$offset}{$isdst} }; |
| 333 | $tmp{$isdst} = { map { $_,1 } @z }; |
| 334 | } |
| 335 | |
| 336 | foreach my $z (@zone) { |
| 337 | my $lcz = lc($z); |
| 338 | if (! exists $$self{'data'}{'ZoneNames'}{$lcz}) { |
| 339 | return (3,$z); |
| 340 | } elsif (! exists $tmp{0}{$lcz} && |
| 341 | ! exists $tmp{1}{$lcz}) { |
| 342 | return (4,$z); |
| 343 | } elsif ($#isdst == 0 && |
| 344 | ! exists $tmp{$isdst[0]}{$lcz}) { |
| 345 | return (5,$z); |
| 346 | } |
| 347 | $z = $lcz; |
| 348 | } |
| 349 | |
| 350 | # Set the zones accordingly. |
| 351 | |
| 352 | foreach my $isdst (@isdst) { |
| 353 | my @z; |
| 354 | foreach my $z (@zone) { |
| 355 | push(@z,$z) if (exists $tmp{$isdst}{$z}); |
| 356 | } |
| 357 | $$self{'data'}{'MyOffsets'}{$offset}{$isdst} = [ @z ]; |
| 358 | } |
| 359 | |
| 360 | return (0); |
| 361 | } |
| 362 | |
| 363 | ######################################################################## |
| 364 | # SYSTEM ZONE |
| 365 | ######################################################################## |
| 366 | |
| 367 | sub curr_zone { |
| 368 | my($self,$reset) = @_; |
| 369 | my $dmb = $$self{'base'}; |
| 370 | |
| 371 | if ($reset) { |
| 372 | $self->_set_curr_zone(); |
| 373 | } |
| 374 | |
| 375 | my($ret) = $self->_now('systz',1); |
| 376 | return $$self{'data'}{'ZoneNames'}{$ret} |
| 377 | } |
| 378 | |
| 379 | sub curr_zone_methods { |
| 380 | my($self,@methods) = @_; |
| 381 | |
| 382 | if (${^TAINT}) { |
| 383 | carp "ERROR: [curr_zone_methods] not allowed when taint checking on"; |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | $$self{'data'}{'methods'} = [ @methods ]; |
| 388 | } |
| 389 | |
| 390 | sub _set_curr_zone { |
| 391 | my($self) = @_; |
| 392 | my $dmb = $$self{'base'}; |
| 393 | my $currzone = $self->_get_curr_zone(); |
| 394 | |
| 395 | $$dmb{'data'}{'now'}{'systz'} = $self->_zone($currzone); |
| 396 | } |
| 397 | |
| 398 | # This determines the system timezone using all of the methods |
| 399 | # applicable to the operating system. The first match is used. |
| 400 | # |
| 401 | sub _get_curr_zone { |
| 402 | my($self) = @_; |
| 403 | my $dmb = $$self{'base'}; |
| 404 | |
| 405 | my $t = time; |
| 406 | my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($t); |
| 407 | my $currzone = ''; |
| 408 | my $dstflag = ($isdst ? 'dstonly' : 'stdonly'); |
| 409 | |
| 410 | my (@methods) = @{ $$self{'data'}{'methods'} }; |
| 411 | my $debug = ($ENV{DATE_MANIP_DEBUG} ? 1 : 0); |
| 412 | |
| 413 | defined $$self{'data'}{'path'} |
| 414 | and local $ENV{PATH} = $$self{'data'}{'path'}; |
| 415 | |
| 416 | METHOD: |
| 417 | while (@methods) { |
| 418 | my $method = shift(@methods); |
| 419 | my @zone = (); |
| 420 | |
| 421 | print "*** DEBUG *** METHOD: $method [" if ($debug); |
| 422 | |
| 423 | if ($method eq 'main') { |
| 424 | |
| 425 | if (! @methods) { |
| 426 | print "]\n" if ($debug); |
| 427 | carp "ERROR: [_set_curr_zone] main requires argument"; |
| 428 | return; |
| 429 | } |
| 430 | my $var = shift(@methods); |
| 431 | print "$var] " if ($debug); |
| 432 | no strict "refs"; |
| 433 | my $val = ${ "::$var" }; |
| 434 | use strict "refs"; |
| 435 | if (defined $val) { |
| 436 | push(@zone,$val); |
| 437 | print "$val\n" if ($debug); |
| 438 | } else { |
| 439 | print "undef\n" if ($debug); |
| 440 | } |
| 441 | |
| 442 | } elsif ($method eq 'env') { |
| 443 | if (@methods < 2) { |
| 444 | print "]\n" if ($debug); |
| 445 | carp "ERROR: [_set_curr_zone] env requires 2 argument"; |
| 446 | return; |
| 447 | } |
| 448 | my $type = lc( shift(@methods) ); |
| 449 | print "$type," if ($debug); |
| 450 | |
| 451 | if ($type ne 'zone' && |
| 452 | $type ne 'offset') { |
| 453 | print "?]\n" if ($debug); |
| 454 | carp "ERROR: [_set_curr_zone] env requires 'offset' or 'zone' " . |
| 455 | "as the first argument"; |
| 456 | return; |
| 457 | } |
| 458 | my $var = shift(@methods); |
| 459 | print "$var] " if ($debug); |
| 460 | if (exists $ENV{$var}) { |
| 461 | if ($type eq 'zone') { |
| 462 | push(@zone,$ENV{$var}); |
| 463 | print "$ENV{$var}\n" if ($debug); |
| 464 | } else { |
| 465 | my $off = $ENV{$var}; |
| 466 | print "$ENV{$var} = " if ($debug); |
| 467 | $off = $dmb->_delta_convert('time',"0:0:$off"); |
| 468 | $off = $dmb->_delta_convert('offset',$off); |
| 469 | print "$off\n" if ($debug); |
| 470 | push(@zone,$off); |
| 471 | } |
| 472 | } else { |
| 473 | print "undef\n" if ($debug); |
| 474 | } |
| 475 | |
| 476 | } elsif ($method eq 'file') { |
| 477 | if (! @methods) { |
| 478 | print "]\n" if ($debug); |
| 479 | carp "ERROR: [_set_curr_zone] file requires argument"; |
| 480 | return; |
| 481 | } |
| 482 | my $file = shift(@methods); |
| 483 | print "$file] " if ($debug); |
| 484 | if (! -f $file) { |
| 485 | print "not found\n" if ($debug); |
| 486 | next; |
| 487 | } |
| 488 | |
| 489 | my $in = new IO::File; |
| 490 | $in->open($file) || next; |
| 491 | my $firstline = 1; |
| 492 | |
| 493 | my @z; |
| 494 | while (! $in->eof) { |
| 495 | my $line = <$in>; |
| 496 | chomp($line); |
| 497 | next if ($line =~ /^\s*\043/ || |
| 498 | $line =~ /^\s*$/); |
| 499 | if ($firstline) { |
| 500 | $firstline = 0; |
| 501 | $line =~ s/^\s*//; |
| 502 | $line =~ s/\s*$//; |
| 503 | $line =~ s/["']//g; # " |
| 504 | $line =~ s/\s+/_/g; |
| 505 | @z = ($line); |
| 506 | } |
| 507 | |
| 508 | # We're looking for lines of the form: |
| 509 | # TZ = string |
| 510 | # TIMEZONE = string |
| 511 | # ZONE = string |
| 512 | # Alternately, we may use a 1-line file (ignoring comments and |
| 513 | # whitespace) which contains only the zone name (it may be |
| 514 | # quoted or contain embedded whitespace). |
| 515 | # |
| 516 | # 'string' can be: |
| 517 | # the name of a timezone enclosed in single/double quotes |
| 518 | # with everything after the closing quote ignored (the |
| 519 | # name of the timezone may have spaces instead of underscores) |
| 520 | # |
| 521 | # a space delimited list of tokens, the first of which |
| 522 | # is the time zone |
| 523 | # |
| 524 | # the name of a timezone with underscores replaced by |
| 525 | # spaces and nothing after the timezone |
| 526 | # |
| 527 | # For some reason, RHEL6 desktop version stores timezones as |
| 528 | # America/New York |
| 529 | # instead of |
| 530 | # America/New_York |
| 531 | # which is why we have to handle the space/underscore |
| 532 | # substitution. |
| 533 | |
| 534 | if ($line =~ /^\s*(?:TZ|TIMEZONE|ZONE)\s*=\s*(.*)\s*$/) { |
| 535 | my $val = $1; |
| 536 | @z = (); |
| 537 | last if (! $val); |
| 538 | |
| 539 | if ($val =~ /^(["'])(.*?)\1/) { |
| 540 | my $z = $2; |
| 541 | last if (! $z); |
| 542 | $z =~ s/\s+/_/g; |
| 543 | push(@zone,$z); |
| 544 | |
| 545 | } elsif ($val =~ /\s/) { |
| 546 | $val =~ /^(\S+)/; |
| 547 | push(@zone,$1); |
| 548 | $val =~ s/\s+/_/g; |
| 549 | push(@zone,$val); |
| 550 | |
| 551 | } else { |
| 552 | push(@zone,$val); |
| 553 | } |
| 554 | |
| 555 | last; |
| 556 | } |
| 557 | } |
| 558 | close(IN); |
| 559 | |
| 560 | push(@zone,@z) if (@z); |
| 561 | |
| 562 | if ($debug) { |
| 563 | if (@zone) { |
| 564 | print "@zone\n"; |
| 565 | } else { |
| 566 | print "no result\n"; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | } elsif ($method eq 'tzdata') { |
| 571 | if (@methods < 2) { |
| 572 | print "]\n" if ($debug); |
| 573 | carp "ERROR: [_set_curr_zone] tzdata requires two arguments"; |
| 574 | return; |
| 575 | } |
| 576 | my $file = shift(@methods); |
| 577 | my $dir = shift(@methods); |
| 578 | print "$file $dir" if ($debug); |
| 579 | |
| 580 | my $z; |
| 581 | if (-f $file && -d $dir) { |
| 582 | $z = _get_zoneinfo_zone($file,$dir); |
| 583 | } |
| 584 | if (defined($z)) { |
| 585 | push @zone, $z; |
| 586 | print "] $z\n" if ($debug); |
| 587 | } elsif ($debug) { |
| 588 | print "] no result\n"; |
| 589 | } |
| 590 | |
| 591 | } elsif ($method eq 'command') { |
| 592 | if (! @methods) { |
| 593 | print "]\n" if ($debug); |
| 594 | carp "ERROR: [_set_curr_zone] command requires argument"; |
| 595 | return; |
| 596 | } |
| 597 | my $command = shift(@methods); |
| 598 | print "$command] " if ($debug); |
| 599 | my ($out) = _cmd($command); |
| 600 | push(@zone,$out) if ($out); |
| 601 | |
| 602 | if ($debug) { |
| 603 | if ($out) { |
| 604 | print "$out\n"; |
| 605 | } else { |
| 606 | print "no output\n"; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | } elsif ($method eq 'cmdfield') { |
| 611 | if ($#methods < 1) { |
| 612 | print "]\n" if ($debug); |
| 613 | carp "ERROR: [_set_curr_zone] cmdfield requires 2 arguments"; |
| 614 | return; |
| 615 | } |
| 616 | my $command = shift(@methods); |
| 617 | my $n = shift(@methods); |
| 618 | print "$command,$n]\n" if ($debug); |
| 619 | my ($out) = _cmd($command); |
| 620 | my $val; |
| 621 | |
| 622 | if ($out) { |
| 623 | $out =~ s/^\s*//; |
| 624 | $out =~ s/\s*$//; |
| 625 | my @out = split(/\s+/,$out); |
| 626 | $val = $out[$n] if (defined $out[$n]); |
| 627 | push(@zone,$val); |
| 628 | } |
| 629 | |
| 630 | if ($debug) { |
| 631 | if ($val) { |
| 632 | print "$val\n"; |
| 633 | } else { |
| 634 | print "no result\n"; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | } elsif ($method eq 'gmtoff') { |
| 639 | print "] " if ($debug); |
| 640 | my($secUT,$minUT,$hourUT,$mdayUT,$monUT,$yearUT,$wdayUT,$ydayUT, |
| 641 | $isdstUT) = gmtime($t); |
| 642 | if ($mdayUT>($mday+1)) { |
| 643 | # UT = 28-31 LT = 1 |
| 644 | $mdayUT=0; |
| 645 | } elsif ($mdayUT<($mday-1)) { |
| 646 | # UT = 1 LT = 28-31 |
| 647 | $mday=0; |
| 648 | } |
| 649 | $sec = (($mday*24 + $hour)*60 + $min)*60 + $sec; |
| 650 | $secUT = (($mdayUT*24 + $hourUT)*60 + $minUT)*60 + $secUT; |
| 651 | my $off = $sec-$secUT; |
| 652 | |
| 653 | $off = $dmb->_delta_convert('time',"0:0:$off"); |
| 654 | $off = $dmb->_delta_convert('offset',$off); |
| 655 | push(@zone,$off); |
| 656 | print "$off\n" if ($debug); |
| 657 | |
| 658 | } elsif ($method eq 'registry') { |
| 659 | print "] " if ($debug); |
| 660 | my $z = $self->_windows_registry_val(); |
| 661 | if ($z) { |
| 662 | push(@zone,$z); |
| 663 | print "$z\n" if ($debug); |
| 664 | } else { |
| 665 | print "no result\n" if ($debug); |
| 666 | } |
| 667 | |
| 668 | } else { |
| 669 | print "]\n" if ($debug); |
| 670 | carp "ERROR: [_set_curr_zone] invalid method: $method"; |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | while (@zone) { |
| 675 | my $zone = lc(shift(@zone)); |
| 676 | |
| 677 | # OpenUNIX puts a colon at the start |
| 678 | $zone =~ s/^://; |
| 679 | |
| 680 | # If we got a zone name/alias |
| 681 | $currzone = $self->_zone($zone); |
| 682 | last METHOD if ($currzone); |
| 683 | |
| 684 | # If we got an abbreviation (EST) |
| 685 | if (exists $$self{'data'}{'Abbrev'}{$zone}) { |
| 686 | $currzone = $$self{'data'}{'Abbrev'}{$zone}[0]; |
| 687 | last METHOD; |
| 688 | } |
| 689 | |
| 690 | # If we got an offset |
| 691 | |
| 692 | $currzone = $self->__zone([],'',$zone,'',$dstflag); |
| 693 | last METHOD if ($currzone); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | if (! $currzone) { |
| 698 | carp "ERROR: Date::Manip unable to determine Time Zone. GMT will be used."; |
| 699 | $currzone = 'Etc/GMT'; |
| 700 | } |
| 701 | |
| 702 | return $currzone; |
| 703 | } |
| 704 | |
| 705 | ####################### |
| 706 | # The following section comes from the DateTime-TimeZone module |
| 707 | |
| 708 | { |
| 709 | my $want_content; |
| 710 | my $want_size; |
| 711 | my $zoneinfo; |
| 712 | |
| 713 | sub _get_zoneinfo_zone { |
| 714 | my($localtime,$z) = @_; |
| 715 | $zoneinfo = $z; |
| 716 | |
| 717 | # /etc/localtime should be either a link to a tzdata file in |
| 718 | # /usr/share/zoneinfo or a copy of one of the files there. |
| 719 | |
| 720 | return '' if (! -d $zoneinfo || ! -f $localtime); |
| 721 | |
| 722 | require Cwd; |
| 723 | if (-l $localtime) { |
| 724 | return _zoneinfo_file_name_to_zone( |
| 725 | Cwd::abs_path($localtime), |
| 726 | Cwd::abs_path($zoneinfo), |
| 727 | ); |
| 728 | } |
| 729 | |
| 730 | $want_content = _zoneinfo_file_slurp($localtime); |
| 731 | $want_size = -s $localtime; |
| 732 | |
| 733 | # File::Find can't bail in the middle of a find, and we only want the |
| 734 | # first match, so we'll call it in an eval. |
| 735 | |
| 736 | local $@ = undef; |
| 737 | eval { |
| 738 | require File::Find; |
| 739 | File::Find::find |
| 740 | ({ |
| 741 | wanted => \&_zoneinfo_find_file, |
| 742 | no_chdir => 1, |
| 743 | }, |
| 744 | $zoneinfo, |
| 745 | ); |
| 746 | 1; |
| 747 | } and return; |
| 748 | ref $@ |
| 749 | and return $@->{zone}; |
| 750 | croak $@; |
| 751 | } |
| 752 | |
| 753 | sub _zoneinfo_find_file { |
| 754 | my $zone; |
| 755 | defined($zone = _zoneinfo_file_name_to_zone($File::Find::name, |
| 756 | $zoneinfo)) |
| 757 | and -f $_ |
| 758 | and $want_size == -s _ |
| 759 | and ($want_content eq _zoneinfo_file_slurp($File::Find::name)) |
| 760 | and croak { zone => $zone }; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | sub _zoneinfo_file_name_to_zone { |
| 765 | my($file,$zoneinfo) = @_; |
| 766 | require File::Spec; |
| 767 | my $zone = File::Spec->abs2rel($file,$zoneinfo); |
| 768 | return $zone if (exists $Date::Manip::Zones::ZoneNames{lc($zone)} || |
| 769 | exists $Date::Manip::Zones::Alias{lc($zone)}); |
| 770 | return; |
| 771 | } |
| 772 | |
| 773 | sub _zoneinfo_file_slurp { |
| 774 | my($file) = @_; |
| 775 | open my $fh, '<', $file |
| 776 | or return; |
| 777 | binmode $fh; |
| 778 | local $/ = undef; |
| 779 | return <$fh>; |
| 780 | } |
| 781 | |
| 782 | sub _windows_registry_val { |
| 783 | my($self) = @_; |
| 784 | |
| 785 | require Win32::TieRegistry; |
| 786 | |
| 787 | my $lmachine = new Win32::TieRegistry 'LMachine', |
| 788 | { Access => Win32::TieRegistry::KEY_READ(), |
| 789 | Delimiter => '/' } |
| 790 | or return ''; |
| 791 | |
| 792 | my $tzinfo = $lmachine->Open('SYSTEM/CurrentControlSet/Control/TimeZoneInformation/'); |
| 793 | |
| 794 | # |
| 795 | # Windows Vista, Windows 2008 Server |
| 796 | # |
| 797 | |
| 798 | my $tzkn = $tzinfo->GetValue('TimeZoneKeyName'); |
| 799 | if (defined($tzkn) && $tzkn) { |
| 800 | # For some reason, Vista is tacking on a bunch of stuff at the |
| 801 | # end of the timezone, starting with a chr(0). Strip it off. |
| 802 | |
| 803 | my $c = chr(0); |
| 804 | my $i = index($tzkn,$c); |
| 805 | if ($i != -1) { |
| 806 | $tzkn = substr($tzkn,0,$i); |
| 807 | } |
| 808 | my $z = $self->_zone($tzkn); |
| 809 | return $z if ($z); |
| 810 | } |
| 811 | |
| 812 | # |
| 813 | # Windows NT, Windows 2000, Windows XP, Windows 2003 Server |
| 814 | # |
| 815 | |
| 816 | my $stdnam = $tzinfo->GetValue('StandardName'); |
| 817 | my $z = $self->_zone($stdnam); |
| 818 | return $z if ($z); |
| 819 | |
| 820 | # |
| 821 | # For non-English versions, we have to determine which timezone it |
| 822 | # actually is. |
| 823 | # |
| 824 | |
| 825 | my $atz = $lmachine->Open('SOFTWARE/Microsoft/Windows NT/CurrentVersion/Time Zones/'); |
| 826 | if (! defined($atz) || ! $atz) { |
| 827 | $atz = $lmachine->Open('SOFTWARE/Microsoft/Windows/CurrentVersion/Time Zones/'); |
| 828 | } |
| 829 | |
| 830 | return "" if (! defined($atz) || ! $atz); |
| 831 | |
| 832 | foreach my $z ($atz->SubKeyNames()) { |
| 833 | my $tmp = $atz->Open("$z/"); |
| 834 | my $znam = $tmp->GetValue('Std'); |
| 835 | return $z if ($znam eq $stdnam); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | # End of DateTime-TimeZone section |
| 840 | ####################### |
| 841 | |
| 842 | # We will be testing commands that don't exist on all architectures, |
| 843 | # so disable warnings. |
| 844 | # |
| 845 | no warnings; |
| 846 | sub _cmd { |
| 847 | my($cmd) = @_; |
| 848 | local(*IN); |
| 849 | open(IN,"$cmd |") || return (); |
| 850 | my @out = <IN>; |
| 851 | close(IN); |
| 852 | chomp(@out); |
| 853 | return @out; |
| 854 | } |
| 855 | use warnings; |
| 856 | |
| 857 | ######################################################################## |
| 858 | # DETERMINING A TIMEZONE |
| 859 | ######################################################################## |
| 860 | |
| 861 | sub zone { |
| 862 | my($self,@args) = @_; |
| 863 | my $dmb = $$self{'base'}; |
| 864 | if (! @args) { |
| 865 | my($tz) = $self->_now('tz',1); |
| 866 | return $$self{'data'}{'ZoneNames'}{$tz} |
| 867 | } |
| 868 | |
| 869 | # Parse the arguments |
| 870 | |
| 871 | my($zone,$abbrev,$offset,$dstflag) = ('','','',''); |
| 872 | my $date = []; |
| 873 | my $tmp; |
| 874 | foreach my $arg (@args) { |
| 875 | |
| 876 | if (ref($arg) eq 'ARRAY') { |
| 877 | if ($#$arg == 5) { |
| 878 | # [Y,M,D,H,Mn,S] |
| 879 | return undef if (@$date); |
| 880 | $date = $arg; |
| 881 | |
| 882 | } elsif ($#$arg == 2) { |
| 883 | # [H,Mn,S] |
| 884 | return undef if ($offset); |
| 885 | $offset = $dmb->join('offset',$arg); |
| 886 | return undef if (! $offset); |
| 887 | |
| 888 | } else { |
| 889 | return undef; |
| 890 | } |
| 891 | |
| 892 | } elsif (ref($arg)) { |
| 893 | return undef; |
| 894 | |
| 895 | } else { |
| 896 | $arg = lc($arg); |
| 897 | |
| 898 | if ($arg =~ /^(std|dst|stdonly|dstonly)$/) { |
| 899 | return undef if ($dstflag); |
| 900 | $dstflag = $arg; |
| 901 | |
| 902 | } elsif ($tmp = $self->_zone($arg)) { |
| 903 | return undef if ($zone); |
| 904 | $zone = $tmp; |
| 905 | |
| 906 | } elsif (exists $$self{'data'}{'MyAbbrev'}{$arg} || |
| 907 | exists $$self{'data'}{'Abbrev'}{$arg}) { |
| 908 | return undef if ($abbrev); |
| 909 | $abbrev = $arg; |
| 910 | |
| 911 | } elsif ($tmp = $dmb->split('offset',$arg)) { |
| 912 | return undef if ($offset); |
| 913 | $offset = $dmb->_delta_convert('offset',$arg); |
| 914 | |
| 915 | } elsif ($tmp = $dmb->split('date',$arg)) { |
| 916 | return undef if ($date); |
| 917 | $date = $tmp; |
| 918 | |
| 919 | } else { |
| 920 | return undef; |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | return $self->__zone($date,$offset,$zone,$abbrev,$dstflag); |
| 926 | } |
| 927 | |
| 928 | # $date = [Y,M,D,H,Mn,S] |
| 929 | # $offset = '-HH:Mn:SS' |
| 930 | # $zone = 'us/eastern' (lowercase) |
| 931 | # $abbrev = 'est' (lowercase) |
| 932 | # $dstflag= 'stdonly' (lowercase) |
| 933 | # |
| 934 | sub __zone { |
| 935 | my($self,$date,$offset,$zone,$abbrev,$dstflag) = @_; |
| 936 | my $dmb = $$self{'base'}; |
| 937 | |
| 938 | # |
| 939 | # Determine the zones that match all data. |
| 940 | # |
| 941 | |
| 942 | my @zone; |
| 943 | |
| 944 | while (1) { |
| 945 | |
| 946 | # No information |
| 947 | |
| 948 | if (! $zone && |
| 949 | ! $abbrev && |
| 950 | ! $offset) { |
| 951 | my($z) = $self->_now('tz',1); |
| 952 | @zone = (lc($z)); |
| 953 | } |
| 954 | |
| 955 | # $dstflag |
| 956 | # |
| 957 | # $dstflag is "dst' if |
| 958 | # zone is passed in as an offset |
| 959 | # date is passed in |
| 960 | |
| 961 | $dstflag = "dst" if ($offset && @$date && ! $dstflag); |
| 962 | |
| 963 | my(@isdst); |
| 964 | if ($dstflag eq 'stdonly') { |
| 965 | @isdst = (0); |
| 966 | } elsif ($dstflag eq 'dstonly') { |
| 967 | @isdst = (1); |
| 968 | } elsif ($dstflag eq 'dst') { |
| 969 | @isdst = (1,0); |
| 970 | } else { |
| 971 | @isdst = (0,1); |
| 972 | } |
| 973 | |
| 974 | # We may pass in $zone and not $abbrev when it really should be |
| 975 | # $abbrev. |
| 976 | |
| 977 | if ($zone && ! $abbrev) { |
| 978 | if (exists $$self{'data'}{'Alias'}{$zone}) { |
| 979 | # no change |
| 980 | } elsif (exists $$self{'data'}{'MyAbbrev'}{$zone} || |
| 981 | exists $$self{'data'}{'Abbrev'}{$zone}) { |
| 982 | $abbrev = $zone; |
| 983 | $zone = ''; |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | # $zone |
| 988 | |
| 989 | if ($zone) { |
| 990 | my $z = (exists $$self{'data'}{'Alias'}{$zone} ? |
| 991 | $$self{'data'}{'Alias'}{$zone} : $zone); |
| 992 | @zone = ($z); |
| 993 | } |
| 994 | |
| 995 | # $abbrev |
| 996 | |
| 997 | if ($abbrev) { |
| 998 | my @abbrev_zones; |
| 999 | if (exists $$self{'data'}{'MyAbbrev'}{$abbrev}) { |
| 1000 | @abbrev_zones = @{ $$self{'data'}{'MyAbbrev'}{$abbrev} }; |
| 1001 | } elsif (exists $$self{'data'}{'Abbrev'}{$abbrev}) { |
| 1002 | @abbrev_zones = @{ $$self{'data'}{'Abbrev'}{$abbrev} }; |
| 1003 | } |
| 1004 | |
| 1005 | my @z; |
| 1006 | foreach my $isdst (@isdst) { |
| 1007 | my @tmp = $self->_check_abbrev_isdst($abbrev,$isdst,@abbrev_zones); |
| 1008 | if (@tmp) { |
| 1009 | if (@z) { |
| 1010 | @z = _list_add(\@z,\@tmp); |
| 1011 | } else { |
| 1012 | @z = @tmp; |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | if (@zone) { |
| 1018 | @zone = _list_union(\@z,\@zone); |
| 1019 | } else { |
| 1020 | @zone = @z; |
| 1021 | } |
| 1022 | last if (! @zone); |
| 1023 | } |
| 1024 | |
| 1025 | # $offset |
| 1026 | |
| 1027 | if ($offset) { |
| 1028 | return undef if (! exists $$self{'data'}{'Offmod'}{$offset}); |
| 1029 | $self->_offmod($offset); |
| 1030 | |
| 1031 | my @z; |
| 1032 | foreach my $isdst (@isdst) { |
| 1033 | my $tmp = $$self{'data'}{'MyOffsets'}{$offset}{$isdst} || |
| 1034 | $$self{'data'}{'Offsets'}{$offset}{$isdst}; |
| 1035 | |
| 1036 | my @tmp; |
| 1037 | if ($abbrev) { |
| 1038 | @tmp = $self->_check_offset_abbrev_isdst($offset,$abbrev,$isdst,$tmp); |
| 1039 | } else { |
| 1040 | @tmp = @$tmp if ($tmp); |
| 1041 | } |
| 1042 | |
| 1043 | if (@tmp) { |
| 1044 | if (@z) { |
| 1045 | @z = _list_add(\@z,\@tmp); |
| 1046 | } else { |
| 1047 | @z = @tmp; |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | if (@zone) { |
| 1053 | @zone = _list_union(\@zone,\@z); |
| 1054 | } else { |
| 1055 | @zone = @z; |
| 1056 | } |
| 1057 | last if (! @zone); |
| 1058 | } |
| 1059 | |
| 1060 | # $date |
| 1061 | |
| 1062 | if (@$date) { |
| 1063 | # Get all periods for the year. |
| 1064 | # |
| 1065 | # Test all periods to make sure that $date is between the |
| 1066 | # wallclock times AND matches other criteria. All periods |
| 1067 | # must be tested since the same wallclock time can be in |
| 1068 | # multiple periods. |
| 1069 | |
| 1070 | my @tmp; |
| 1071 | my $isdst = ''; |
| 1072 | $isdst = 0 if ($dstflag eq 'stdonly'); |
| 1073 | $isdst = 1 if ($dstflag eq 'dstonly'); |
| 1074 | |
| 1075 | ZONE: |
| 1076 | foreach my $z (@zone) { |
| 1077 | $self->_module($z) if (! exists $$self{'data'}{'Zones'}{$z}{'Loaded'}); |
| 1078 | my $y = $$date[0]; |
| 1079 | my @periods = $self->_all_periods($z,$y); |
| 1080 | |
| 1081 | foreach my $period (@periods) { |
| 1082 | next if (($abbrev ne '' && lc($abbrev) ne lc($$period[4])) || |
| 1083 | ($offset ne '' && $offset ne $$period[2]) || |
| 1084 | ($isdst ne '' && $isdst ne $$period[5]) || |
| 1085 | $dmb->cmp($date,$$period[1]) == -1 || |
| 1086 | $dmb->cmp($date,$$period[7]) == 1 |
| 1087 | ); |
| 1088 | push(@tmp,$z); |
| 1089 | next ZONE; |
| 1090 | } |
| 1091 | } |
| 1092 | @zone = @tmp; |
| 1093 | last if (! @zone); |
| 1094 | } |
| 1095 | |
| 1096 | last; |
| 1097 | } |
| 1098 | |
| 1099 | # Return the value/list |
| 1100 | |
| 1101 | if (wantarray) { |
| 1102 | my @ret; |
| 1103 | foreach my $z (@zone) { |
| 1104 | push(@ret,$$self{'data'}{'ZoneNames'}{$z}); |
| 1105 | } |
| 1106 | return @ret; |
| 1107 | } |
| 1108 | |
| 1109 | return '' if (! @zone); |
| 1110 | return $$self{'data'}{'ZoneNames'}{$zone[0]} |
| 1111 | } |
| 1112 | |
| 1113 | # This returns a list of all timezones which have the correct |
| 1114 | # abbrev/isdst combination. |
| 1115 | # |
| 1116 | sub _check_abbrev_isdst { |
| 1117 | my($self,$abbrev,$isdst,@zones) = @_; |
| 1118 | |
| 1119 | my @ret; |
| 1120 | ZONE: |
| 1121 | foreach my $zone (@zones) { |
| 1122 | $self->_module($zone) if (! exists $$self{'data'}{'Zones'}{$zone}{'Loaded'}); |
| 1123 | |
| 1124 | foreach my $y (sort keys %{ $$self{'data'}{'Zones'}{$zone}{'Dates'} }) { |
| 1125 | my @periods = @{ $$self{'data'}{'Zones'}{$zone}{'Dates'}{$y} }; |
| 1126 | foreach my $period (@periods) { |
| 1127 | my($dateUT,$dateLT,$off,$offref,$abb,$dst,$endUT,$endLT) = @$period; |
| 1128 | next if (lc($abbrev) ne lc($abb) || |
| 1129 | $isdst != $dst); |
| 1130 | push(@ret,$zone); |
| 1131 | next ZONE; |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | return @ret; |
| 1137 | } |
| 1138 | |
| 1139 | # This returns a list of all timezones which have the correct |
| 1140 | # abbrev/isdst combination. |
| 1141 | # |
| 1142 | sub _check_offset_abbrev_isdst { |
| 1143 | my($self,$offset,$abbrev,$isdst,$zones) = @_; |
| 1144 | |
| 1145 | my @ret; |
| 1146 | ZONE: foreach my $zone (@$zones) { |
| 1147 | $self->_module($zone) if (! exists $$self{'data'}{'Zones'}{$zone}{'Loaded'}); |
| 1148 | |
| 1149 | foreach my $y (sort keys %{ $$self{'data'}{'Zones'}{$zone}{'Dates'} }) { |
| 1150 | my @periods = @{ $$self{'data'}{'Zones'}{$zone}{'Dates'}{$y} }; |
| 1151 | foreach my $period (@periods) { |
| 1152 | my($dateUT,$dateLT,$off,$offref,$abb,$dst,$endUT,$endLT) = @$period; |
| 1153 | next if (lc($abbrev) ne lc($abb) || |
| 1154 | $offset ne $off || |
| 1155 | $isdst != $dst); |
| 1156 | push(@ret,$zone); |
| 1157 | next ZONE; |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | return @ret; |
| 1163 | } |
| 1164 | |
| 1165 | # This finds the elements common to two lists, and preserves the order |
| 1166 | # from the first list. |
| 1167 | # |
| 1168 | sub _list_union { |
| 1169 | my($list1,$list2) = @_; |
| 1170 | my(%list2) = map { $_,1 } @$list2; |
| 1171 | my(@ret); |
| 1172 | foreach my $ele (@$list1) { |
| 1173 | push(@ret,$ele) if (exists $list2{$ele}); |
| 1174 | } |
| 1175 | return @ret; |
| 1176 | } |
| 1177 | |
| 1178 | # This adds elements from the second list to the first list, provided |
| 1179 | # they are not already there. |
| 1180 | # |
| 1181 | sub _list_add { |
| 1182 | my($list1,$list2) = @_; |
| 1183 | my(%list1) = map { $_,1 } @$list1; |
| 1184 | my(@ret) = @$list1; |
| 1185 | foreach my $ele (@$list2) { |
| 1186 | next if (exists $list1{$ele}); |
| 1187 | push(@ret,$ele); |
| 1188 | $list1{$ele} = 1; |
| 1189 | } |
| 1190 | return @ret; |
| 1191 | } |
| 1192 | |
| 1193 | ######################################################################## |
| 1194 | # PERIODS METHODS |
| 1195 | ######################################################################## |
| 1196 | |
| 1197 | sub all_periods { |
| 1198 | my($self,$zone,$year) = @_; |
| 1199 | |
| 1200 | my $z = $self->_zone($zone); |
| 1201 | if (! $z) { |
| 1202 | carp "ERROR: [periods] Invalid zone: $zone"; |
| 1203 | return; |
| 1204 | } |
| 1205 | $zone = $z; |
| 1206 | $self->_module($zone) if (! exists $$self{'data'}{'Zones'}{$zone}{'Loaded'}); |
| 1207 | |
| 1208 | # Run a faster 'dclone' so we don't return the actual data. |
| 1209 | |
| 1210 | my @tmp = $self->_all_periods($zone,$year); |
| 1211 | my @ret; |
| 1212 | foreach my $ele (@tmp) { |
| 1213 | push(@ret, |
| 1214 | [ [ @{$$ele[0]} ],[ @{$$ele[1]} ],$$ele[2],[ @{$$ele[3]} ],$$ele[4], |
| 1215 | $$ele[5], [ @{$$ele[6]} ],[ @{$$ele[7]} ],$$ele[8],$$ele[9], |
| 1216 | $$ele[10],$$ele[11] ]); |
| 1217 | } |
| 1218 | return @ret; |
| 1219 | } |
| 1220 | |
| 1221 | sub _all_periods { |
| 1222 | my($self,$zone,$year) = @_; |
| 1223 | $year += 0; |
| 1224 | |
| 1225 | if (! exists $$self{'data'}{'Zones'}{$zone}{'AllDates'}{$year}) { |
| 1226 | |
| 1227 | # |
| 1228 | # $ym1 is the year prior to $year which contains a rule (which will |
| 1229 | # end in $year or later). $y is $year IF the zone contains rules |
| 1230 | # for this year. |
| 1231 | # |
| 1232 | |
| 1233 | my($ym1,$ym0); |
| 1234 | if ($year > $$self{'data'}{'LastYear'} && |
| 1235 | exists $$self{'data'}{'Zones'}{$zone}{'LastRule'}{'zone'}) { |
| 1236 | $ym1 = $year-1; |
| 1237 | $ym0 = $year; |
| 1238 | |
| 1239 | } else { |
| 1240 | foreach my $y (sort { $a <=> $b } |
| 1241 | keys %{ $$self{'data'}{'Zones'}{$zone}{'Dates'} }) { |
| 1242 | if ($y < $year) { |
| 1243 | $ym1 = $y; |
| 1244 | next; |
| 1245 | } |
| 1246 | $ym0 = $year if ($year == $y); |
| 1247 | last; |
| 1248 | } |
| 1249 | } |
| 1250 | $ym1 = 0 if (! $ym1); |
| 1251 | |
| 1252 | # |
| 1253 | # Get the periods from the prior year. The last one is used (any others |
| 1254 | # are discarded). |
| 1255 | # |
| 1256 | |
| 1257 | my(@periods); |
| 1258 | |
| 1259 | # $ym1 will be 0 in 0001 |
| 1260 | if ($ym1) { |
| 1261 | my @tmp = $self->_periods($zone,$ym1); |
| 1262 | push(@periods,pop(@tmp)) if (@tmp); |
| 1263 | } |
| 1264 | |
| 1265 | # |
| 1266 | # Add on any periods from the current year. |
| 1267 | # |
| 1268 | |
| 1269 | if ($ym0) { |
| 1270 | push(@periods,$self->_periods($zone,$year)); |
| 1271 | } |
| 1272 | |
| 1273 | $$self{'data'}{'Zones'}{$zone}{'AllDates'}{$year} = [ @periods ]; |
| 1274 | } |
| 1275 | |
| 1276 | return @{ $$self{'data'}{'Zones'}{$zone}{'AllDates'}{$year} }; |
| 1277 | } |
| 1278 | |
| 1279 | sub periods { |
| 1280 | my($self,$zone,$year,$year1) = @_; |
| 1281 | |
| 1282 | my $z = $self->_zone($zone); |
| 1283 | if (! $z) { |
| 1284 | carp "ERROR: [periods] Invalid zone: $zone"; |
| 1285 | return; |
| 1286 | } |
| 1287 | $zone = $z; |
| 1288 | $self->_module($zone) if (! exists $$self{'data'}{'Zones'}{$zone}{'Loaded'}); |
| 1289 | |
| 1290 | if (! defined($year1)) { |
| 1291 | return $self->_periods($zone,$year); |
| 1292 | } |
| 1293 | |
| 1294 | $year = 1 if (! defined($year)); |
| 1295 | |
| 1296 | my @ret; |
| 1297 | my $lastyear = $$self{'data'}{'LastYear'}; |
| 1298 | |
| 1299 | if ($year <= $lastyear) { |
| 1300 | foreach my $y (sort { $a <=> $b } |
| 1301 | keys %{ $$self{'data'}{'Zones'}{$zone}{'Dates'} }) { |
| 1302 | last if ($y > $year1 || $y > $lastyear); |
| 1303 | next if ($y < $year); |
| 1304 | push(@ret,$self->_periods($zone,$y)); |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | if ($year1 > $lastyear) { |
| 1309 | $year = $lastyear + 1 if ($year <= $lastyear); |
| 1310 | foreach my $y ($year..$year1) { |
| 1311 | push(@ret,$self->_periods($zone,$y)); |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | return @ret; |
| 1316 | } |
| 1317 | |
| 1318 | sub _periods { |
| 1319 | my($self,$zone,$year) = @_; |
| 1320 | $year += 0; |
| 1321 | |
| 1322 | if (! exists $$self{'data'}{'Zones'}{$zone}{'Dates'}{$year}) { |
| 1323 | |
| 1324 | my @periods = (); |
| 1325 | if ($year > $$self{'data'}{'LastYear'}) { |
| 1326 | # Calculate periods using the LastRule method |
| 1327 | @periods = $self->_lastrule($zone,$year); |
| 1328 | } |
| 1329 | |
| 1330 | $$self{'data'}{'Zones'}{$zone}{'Dates'}{$year} = [ @periods ]; |
| 1331 | } |
| 1332 | |
| 1333 | # A faster 'dclone' so we don't return the actual data |
| 1334 | my @ret; |
| 1335 | foreach my $ele (@{ $$self{'data'}{'Zones'}{$zone}{'Dates'}{$year} }) { |
| 1336 | push(@ret, |
| 1337 | [ [ @{$$ele[0]} ],[ @{$$ele[1]} ],$$ele[2],[ @{$$ele[3]} ],$$ele[4],$$ele[5], |
| 1338 | [ @{$$ele[6]} ],[ @{$$ele[7]} ],$$ele[8],$$ele[9],$$ele[10],$$ele[11] ]); |
| 1339 | } |
| 1340 | return @ret; |
| 1341 | } |
| 1342 | |
| 1343 | sub date_period { |
| 1344 | my($self,$date,$zone,$wallclock,$isdst) = @_; |
| 1345 | $wallclock = 0 if (! $wallclock); |
| 1346 | $isdst = 0 if (! $isdst); |
| 1347 | |
| 1348 | my $z = $self->_zone($zone); |
| 1349 | if (! $z) { |
| 1350 | carp "ERROR: [date_period] Invalid zone: $zone"; |
| 1351 | return; |
| 1352 | } |
| 1353 | $zone = $z; |
| 1354 | $self->_module($zone) if (! exists $$self{'data'}{'Zones'}{$zone}{'Loaded'}); |
| 1355 | |
| 1356 | my $dmb = $$self{'base'}; |
| 1357 | my @date = @$date; |
| 1358 | my $year = $date[0]; |
| 1359 | my $dates= $dmb->_date_fields(@$date); |
| 1360 | return () if ($year < 0 || $year > 9999); |
| 1361 | |
| 1362 | if ($wallclock) { |
| 1363 | # A wallclock date |
| 1364 | |
| 1365 | my @period = $self->_all_periods($zone,$year); |
| 1366 | my $beg = $period[0]->[9]; |
| 1367 | my $end = $period[-1]->[11]; |
| 1368 | if (($dates cmp $beg) == -1) { |
| 1369 | @period = $self->_all_periods($zone,$year-1); |
| 1370 | } elsif (($dates cmp $end) == 1) { |
| 1371 | @period = $self->_all_periods($zone,$year+1); |
| 1372 | } |
| 1373 | |
| 1374 | my(@per); |
| 1375 | foreach my $period (@period) { |
| 1376 | my($begUT,$begLT,$offsetstr,$offset,$abbrev,$dst,$endUT,$endLT, |
| 1377 | $begUTs,$begLTs,$endUTs,$endLTs) = @$period; |
| 1378 | if (($dates cmp $begLTs) != -1 && ($dates cmp $endLTs) != 1) { |
| 1379 | push(@per,$period); |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | if ($#per == -1) { |
| 1384 | return (); |
| 1385 | } elsif ($#per == 0) { |
| 1386 | return $per[0]; |
| 1387 | } elsif ($#per == 1) { |
| 1388 | if ($per[0][5] == $isdst) { |
| 1389 | return $per[0]; |
| 1390 | } else { |
| 1391 | return $per[1]; |
| 1392 | } |
| 1393 | } else { |
| 1394 | carp "ERROR: [date_period] Impossible error"; |
| 1395 | return; |
| 1396 | } |
| 1397 | |
| 1398 | } else { |
| 1399 | # A GMT date |
| 1400 | |
| 1401 | my @period = $self->_all_periods($zone,$year); |
| 1402 | foreach my $period (@period) { |
| 1403 | my($begUT,$begLT,$offsetstr,$offset,$abbrev,$isdst,$endUT,$endLT, |
| 1404 | $begUTs,$begLTs,$endUTs,$endLTs) = @$period; |
| 1405 | if (($dates cmp $begUTs) != -1 && ($dates cmp $endUTs) != 1) { |
| 1406 | return $period; |
| 1407 | } |
| 1408 | } |
| 1409 | carp "ERROR: [date_period] Impossible error"; |
| 1410 | return; |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | # Calculate critical dates from the last rule. If $endonly is passed |
| 1415 | # in, it only calculates the ending of the zone period before the |
| 1416 | # start of the first one. This is necessary so that the last period in |
| 1417 | # one year can find out when it ends (which is determined in the |
| 1418 | # following year). |
| 1419 | # |
| 1420 | # Returns: |
| 1421 | # [begUT, begLT, offsetstr, offset, abb, ISDST, endUT, endLT, |
| 1422 | # begUTstr, begLTstr, endUTstr, endLTstr] |
| 1423 | # for each. |
| 1424 | # |
| 1425 | sub _lastrule { |
| 1426 | my($self,$zone,$year,$endonly) = @_; |
| 1427 | |
| 1428 | # |
| 1429 | # Get the list of rules (actually, the month in which the |
| 1430 | # rule triggers a time change). If there are none, then |
| 1431 | # this zone doesn't have a LAST RULE. |
| 1432 | # |
| 1433 | |
| 1434 | my @mon = (sort keys |
| 1435 | %{ $$self{'data'}{'Zones'}{$zone}{'LastRule'}{'rules'} }); |
| 1436 | return () if (! @mon); |
| 1437 | |
| 1438 | # |
| 1439 | # Analyze each time change. |
| 1440 | # |
| 1441 | |
| 1442 | my @dates = (); |
| 1443 | my $dmb = $$self{'base'}; |
| 1444 | |
| 1445 | my $stdoff = $$self{'data'}{'Zones'}{$zone}{'LastRule'}{'zone'}{'stdoff'}; |
| 1446 | my $dstoff = $$self{'data'}{'Zones'}{$zone}{'LastRule'}{'zone'}{'dstoff'}; |
| 1447 | |
| 1448 | my (@period); |
| 1449 | |
| 1450 | foreach my $mon (@mon) { |
| 1451 | my $flag = |
| 1452 | $$self{'data'}{'Zones'}{$zone}{'LastRule'}{'rules'}{$mon}{'flag'}; |
| 1453 | my $dow = |
| 1454 | $$self{'data'}{'Zones'}{$zone}{'LastRule'}{'rules'}{$mon}{'dow'}; |
| 1455 | my $num = |
| 1456 | $$self{'data'}{'Zones'}{$zone}{'LastRule'}{'rules'}{$mon}{'num'}; |
| 1457 | my $isdst= |
| 1458 | $$self{'data'}{'Zones'}{$zone}{'LastRule'}{'rules'}{$mon}{'isdst'}; |
| 1459 | my $time = |
| 1460 | $$self{'data'}{'Zones'}{$zone}{'LastRule'}{'rules'}{$mon}{'time'}; |
| 1461 | my $type = |
| 1462 | $$self{'data'}{'Zones'}{$zone}{'LastRule'}{'rules'}{$mon}{'type'}; |
| 1463 | my $abb = |
| 1464 | $$self{'data'}{'Zones'}{$zone}{'LastRule'}{'rules'}{$mon}{'abb'}; |
| 1465 | |
| 1466 | # The end of the current period and the beginning of the next |
| 1467 | my($endUT,$endLT,$begUT,$begLT) = |
| 1468 | $dmb->_critical_date($year,$mon,$flag,$num,$dow, |
| 1469 | $isdst,$time,$type,$stdoff,$dstoff); |
| 1470 | return ($endUT,$endLT) if ($endonly); |
| 1471 | |
| 1472 | if (@period) { |
| 1473 | push(@period,$endUT,$endLT); |
| 1474 | push(@dates,[@period]); |
| 1475 | } |
| 1476 | my $offsetstr = ($isdst ? $dstoff : $stdoff); |
| 1477 | my $offset = $dmb->split('offset',$offsetstr); |
| 1478 | |
| 1479 | @period = ($begUT,$begLT,$offsetstr,$offset,$abb,$isdst); |
| 1480 | } |
| 1481 | |
| 1482 | push(@period,$self->_lastrule($zone,$year+1,1)); |
| 1483 | push(@dates,[@period]); |
| 1484 | |
| 1485 | foreach my $period (@dates) { |
| 1486 | my($begUT,$begLT,$offsetstr,$offset,$abbrev,$dst,$endUT,$endLT) = @$period; |
| 1487 | my $begUTstr = $dmb->join("date",$begUT); |
| 1488 | my $begLTstr = $dmb->join("date",$begLT); |
| 1489 | my $endUTstr = $dmb->join("date",$endUT); |
| 1490 | my $endLTstr = $dmb->join("date",$endLT); |
| 1491 | $period = [$begUT,$begLT,$offsetstr,$offset,$abbrev,$dst,$endUT,$endLT, |
| 1492 | $begUTstr,$begLTstr,$endUTstr,$endLTstr]; |
| 1493 | } |
| 1494 | |
| 1495 | return @dates; |
| 1496 | } |
| 1497 | |
| 1498 | ######################################################################## |
| 1499 | # CONVERSION |
| 1500 | ######################################################################## |
| 1501 | |
| 1502 | sub convert { |
| 1503 | my($self,$date,$from,$to,$isdst) = @_; |
| 1504 | $self->_convert('convert',$date,$from,$to,$isdst); |
| 1505 | } |
| 1506 | |
| 1507 | sub convert_to_gmt { |
| 1508 | my($self,$date,@arg) = @_; |
| 1509 | my($err,$from,$isdst) = _convert_args('convert_to_gmt',@arg); |
| 1510 | return (1) if ($err); |
| 1511 | |
| 1512 | my $dmb = $$self{'base'}; |
| 1513 | |
| 1514 | if (! $from) { |
| 1515 | $from = $self->_now('tz',1); |
| 1516 | } |
| 1517 | $self->_convert('convert_to_gmt',$date,$from,'GMT',$isdst); |
| 1518 | } |
| 1519 | |
| 1520 | sub convert_from_gmt { |
| 1521 | my($self,$date,@arg) = @_; |
| 1522 | my($err,$to,$isdst) = _convert_args('convert_from_gmt',@arg); |
| 1523 | return (1) if ($err); |
| 1524 | |
| 1525 | my $dmb = $$self{'base'}; |
| 1526 | |
| 1527 | if (! $to) { |
| 1528 | $to = $self->_now('tz',1); |
| 1529 | } |
| 1530 | $self->_convert('convert_from_gmt',$date,'GMT',$to,$isdst); |
| 1531 | } |
| 1532 | |
| 1533 | sub convert_to_local { |
| 1534 | my($self,$date,@arg) = @_; |
| 1535 | my($err,$from,$isdst) = _convert_args('convert_to_local',@arg); |
| 1536 | return (1) if ($err); |
| 1537 | |
| 1538 | my $dmb = $$self{'base'}; |
| 1539 | |
| 1540 | if (! $from) { |
| 1541 | $from = 'GMT'; |
| 1542 | } |
| 1543 | $self->_convert('convert_to_local',$date,$from,$self->_now('tz',1),$isdst); |
| 1544 | } |
| 1545 | |
| 1546 | sub convert_from_local { |
| 1547 | my($self,$date,@arg) = @_; |
| 1548 | my($err,$to,$isdst) = _convert_args('convert_from_local',@arg); |
| 1549 | return (1) if ($err); |
| 1550 | |
| 1551 | my $dmb = $$self{'base'}; |
| 1552 | |
| 1553 | if (! $to) { |
| 1554 | $to = 'GMT'; |
| 1555 | } |
| 1556 | $self->_convert('convert_from_local',$date,$self->_now('tz',1),$to,$isdst); |
| 1557 | } |
| 1558 | |
| 1559 | sub _convert_args { |
| 1560 | my($caller,@args) = @_; |
| 1561 | |
| 1562 | if ($#args == -1) { |
| 1563 | return (0,'',0); |
| 1564 | } elsif ($#args == 0) { |
| 1565 | if ($args[0] eq '0' || |
| 1566 | $args[0] eq '1') { |
| 1567 | return (0,'',$args[0]); |
| 1568 | } else { |
| 1569 | return (0,$args[0],0); |
| 1570 | } |
| 1571 | } elsif ($#args == 1) { |
| 1572 | return (0,@args); |
| 1573 | } else { |
| 1574 | return (1,'',0); |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | sub _convert { |
| 1579 | my($self,$caller,$date,$from,$to,$isdst) = @_; |
| 1580 | my $dmb = $$self{'base'}; |
| 1581 | |
| 1582 | # Handle $date as a reference and a string |
| 1583 | my (@date); |
| 1584 | if (ref($date)) { |
| 1585 | @date = @$date; |
| 1586 | } else { |
| 1587 | my $split = $dmb->split('date',$date); |
| 1588 | return (4) if (! defined $split); |
| 1589 | @date = @$split; |
| 1590 | $date = [@date]; |
| 1591 | } |
| 1592 | |
| 1593 | if ($from ne $to) { |
| 1594 | my $tmp = $self->_zone($from); |
| 1595 | if (! $tmp) { |
| 1596 | return (2); |
| 1597 | } |
| 1598 | $from = $tmp; |
| 1599 | |
| 1600 | $tmp = $self->_zone($to); |
| 1601 | if (! $tmp) { |
| 1602 | return (3); |
| 1603 | } |
| 1604 | $to = $tmp; |
| 1605 | } |
| 1606 | |
| 1607 | if ($from eq $to) { |
| 1608 | my $per = $self->date_period($date,$from,1,$isdst); |
| 1609 | my $offset = $$per[3]; |
| 1610 | my $abb = $$per[4]; |
| 1611 | return (0,$date,$offset,$isdst,$abb); |
| 1612 | } |
| 1613 | |
| 1614 | # Convert $date from $from to GMT |
| 1615 | |
| 1616 | if ($from ne "Etc/GMT") { |
| 1617 | my $per = $self->date_period($date,$from,1,$isdst); |
| 1618 | if (! $per) { |
| 1619 | return (4); |
| 1620 | } |
| 1621 | my $offset = $$per[3]; |
| 1622 | @date = @{ $dmb->calc_date_time(\@date,$offset,1) }; |
| 1623 | } |
| 1624 | |
| 1625 | # Convert $date from GMT to $to |
| 1626 | |
| 1627 | $isdst = 0; |
| 1628 | my $offset = [0,0,0]; |
| 1629 | my $abb = 'GMT'; |
| 1630 | |
| 1631 | if ($to ne "Etc/GMT") { |
| 1632 | my $per = $self->date_period([@date],$to,0); |
| 1633 | $offset = $$per[3]; |
| 1634 | $isdst = $$per[5]; |
| 1635 | $abb = $$per[4]; |
| 1636 | @date = @{ $dmb->calc_date_time(\@date,$offset) }; |
| 1637 | } |
| 1638 | |
| 1639 | return (0,[@date],$offset,$isdst,$abb); |
| 1640 | } |
| 1641 | |
| 1642 | ######################################################################## |
| 1643 | # REGULAR EXPRESSIONS FOR TIMEZONE INFORMATION |
| 1644 | ######################################################################## |
| 1645 | |
| 1646 | # Returns regular expressions capable of matching timezones. |
| 1647 | # |
| 1648 | # The timezone regular expressions are: |
| 1649 | # namerx : this will match a zone name or alias (America/New_York) |
| 1650 | # abbrx : this will match a zone abbreviation (EDT) |
| 1651 | # zonerx : this will match a zone name or an abbreviation |
| 1652 | # offrx : this will match a pure offset (+0400) |
| 1653 | # offabbrx : this will match an offset with an abbreviation (+0400 WET) |
| 1654 | # offparrx : this will match an offset and abbreviation if parentheses |
| 1655 | # ("+0400 (WET)") |
| 1656 | # zrx : this will match all forms |
| 1657 | # |
| 1658 | # The regular expression will have the following named matches: |
| 1659 | # tzstring : the full string matched |
| 1660 | # zone : the name/alias |
| 1661 | # abb : the zone abbrevation |
| 1662 | # off : the offset |
| 1663 | # |
| 1664 | sub _zrx { |
| 1665 | my($self,$re) = @_; |
| 1666 | return $$self{'data'}{$re} if (defined $$self{'data'}{$re}); |
| 1667 | |
| 1668 | # Zone name |
| 1669 | |
| 1670 | my @zone; |
| 1671 | if (exists $ENV{'DATE_MANIP_DEBUG_ZONES'}) { |
| 1672 | @zone = split(/\s+/,$ENV{'DATE_MANIP_DEBUG_ZONES'}); |
| 1673 | } else { |
| 1674 | @zone = (keys %{ $$self{'data'}{'Alias'} }, |
| 1675 | keys %{ $$self{'data'}{'MyAlias'} }); |
| 1676 | } |
| 1677 | @zone = sort _sortByLength(@zone); |
| 1678 | foreach my $zone (@zone) { |
| 1679 | $zone =~ s/\057/\\057/g; # / |
| 1680 | $zone =~ s/\055/\\055/g; # - |
| 1681 | $zone =~ s/\056/\\056/g; # . |
| 1682 | $zone =~ s/\050/\\050/g; # ( |
| 1683 | $zone =~ s/\051/\\051/g; # ) |
| 1684 | $zone =~ s/\053/\\053/g; # + |
| 1685 | } |
| 1686 | |
| 1687 | my $zone = join('|',@zone); |
| 1688 | $zone = qr/(?<zone>$zone)/i; |
| 1689 | |
| 1690 | # Abbreviation |
| 1691 | |
| 1692 | my @abb; |
| 1693 | if (exists $ENV{'DATE_MANIP_DEBUG_ABBREVS'}) { |
| 1694 | @abb = split(/\s+/,$ENV{'DATE_MANIP_DEBUG_ABBREVS'}); |
| 1695 | } else { |
| 1696 | @abb = (keys %{ $$self{'data'}{'Abbrev'} }, |
| 1697 | keys %{ $$self{'data'}{'MyAbbrev'} }); |
| 1698 | } |
| 1699 | @abb = sort _sortByLength(@abb); |
| 1700 | foreach my $abb (@abb) { |
| 1701 | $abb =~ s/\055/\\055/g; # - |
| 1702 | $abb =~ s/\053/\\053/g; # + |
| 1703 | } |
| 1704 | |
| 1705 | my $abb = join('|',@abb); |
| 1706 | $abb = qr/(?<abb>$abb)/i; |
| 1707 | |
| 1708 | # Offset (+HH, +HHMM, +HH:MM, +HH:MM:SS, +HHMMSS) |
| 1709 | |
| 1710 | my($hr) = qr/(?:[0-1][0-9]|2[0-3])/; # 00 - 23 |
| 1711 | my($mn) = qr/(?:[0-5][0-9])/; # 00 - 59 |
| 1712 | my($ss) = qr/(?:[0-5][0-9])/; # 00 - 59 |
| 1713 | |
| 1714 | my($off) = qr/ (?<off> [+-] (?: $hr:$mn:$ss | |
| 1715 | $hr$mn$ss | |
| 1716 | $hr:?$mn | |
| 1717 | $hr |
| 1718 | ) |
| 1719 | ) /ix; |
| 1720 | |
| 1721 | # Assemble everything |
| 1722 | # |
| 1723 | # A timezone can be any of the following in this order: |
| 1724 | # Offset (ABB) |
| 1725 | # Offset ABB |
| 1726 | # ABB |
| 1727 | # Zone |
| 1728 | # Offset |
| 1729 | # We put ABB before Zone so CET gets parse as the more common abbreviation |
| 1730 | # than the less common zone name. |
| 1731 | |
| 1732 | $$self{'data'}{'namerx'} = qr/(?<tzstring>$zone)/; |
| 1733 | $$self{'data'}{'abbrx'} = qr/(?<tzstring>$abb)/; |
| 1734 | $$self{'data'}{'zonerx'} = qr/(?<tzstring>(?:$abb|$zone))/; |
| 1735 | $$self{'data'}{'offrx'} = qr/(?<tzstring>$off)/; |
| 1736 | $$self{'data'}{'offabbrx'} = qr/(?<tzstring>$off\s+$abb)/; |
| 1737 | $$self{'data'}{'offparrx'} = qr/(?<tzstring>$off\s*\($abb\))/; |
| 1738 | $$self{'data'}{'zrx'} = qr/(?<tzstring>(?:$off\s*\($abb\)|$off\s+$abb|$abb|$zone|$off))/; |
| 1739 | |
| 1740 | return $$self{'data'}{$re}; |
| 1741 | } |
| 1742 | |
| 1743 | # This sorts from longest to shortest element |
| 1744 | # |
| 1745 | no strict 'vars'; |
| 1746 | sub _sortByLength { |
| 1747 | return (length $b <=> length $a); |
| 1748 | } |
| 1749 | use strict 'vars'; |
| 1750 | |
| 1751 | ######################################################################## |
| 1752 | # CONFIG VARS |
| 1753 | ######################################################################## |
| 1754 | |
| 1755 | # This sets a config variable. It also performs all side effects from |
| 1756 | # setting that variable. |
| 1757 | # |
| 1758 | sub _config_var_tz { |
| 1759 | my($self,$var,$val) = @_; |
| 1760 | |
| 1761 | if ($var eq 'tz') { |
| 1762 | my $err = $self->_config_var_setdate("now,$val",0); |
| 1763 | return if ($err); |
| 1764 | $$self{'data'}{'sections'}{'conf'}{'forcedate'} = 0; |
| 1765 | $val = 1; |
| 1766 | |
| 1767 | } elsif ($var eq 'setdate') { |
| 1768 | my $err = $self->_config_var_setdate($val,0); |
| 1769 | return if ($err); |
| 1770 | $$self{'data'}{'sections'}{'conf'}{'forcedate'} = 0; |
| 1771 | $val = 1; |
| 1772 | |
| 1773 | } elsif ($var eq 'forcedate') { |
| 1774 | my $err = $self->_config_var_setdate($val,1); |
| 1775 | return if ($err); |
| 1776 | $$self{'data'}{'sections'}{'conf'}{'setdate'} = 0; |
| 1777 | $val = 1; |
| 1778 | |
| 1779 | } elsif ($var eq 'configfile') { |
| 1780 | $self->_config_file($val); |
| 1781 | return; |
| 1782 | } |
| 1783 | |
| 1784 | my $base = $$self{'base'}; |
| 1785 | $$base{'data'}{'sections'}{'conf'}{$var} = $val; |
| 1786 | return; |
| 1787 | } |
| 1788 | |
| 1789 | sub _config_var_setdate { |
| 1790 | my($self,$val,$force) = @_; |
| 1791 | my $base = $$self{'base'}; |
| 1792 | |
| 1793 | my $dstrx = qr/(?:,\s*(stdonly|dstonly|std|dst))?/i; |
| 1794 | my $zonrx = qr/,\s*(.+)/; |
| 1795 | my $da1rx = qr/(\d\d\d\d)(\d\d)(\d\d)(\d\d):(\d\d):(\d\d)/; |
| 1796 | my $da2rx = qr/(\d\d\d\d)\-(\d\d)\-(\d\d)\-(\d\d):(\d\d):(\d\d)/; |
| 1797 | my $time = time; |
| 1798 | |
| 1799 | my($op,$date,$dstflag,$zone,@date,$offset,$abb); |
| 1800 | |
| 1801 | # |
| 1802 | # Parse the argument |
| 1803 | # |
| 1804 | |
| 1805 | if ($val =~ /^now${dstrx}${zonrx}$/oi) { |
| 1806 | # now,ZONE |
| 1807 | # now,DSTFLAG,ZONE |
| 1808 | # Sets now to the system date/time but sets the timezone to be ZONE |
| 1809 | |
| 1810 | $op = 'nowzone'; |
| 1811 | ($dstflag,$zone) = ($1,$2); |
| 1812 | |
| 1813 | } elsif ($val =~ /^zone${dstrx}${zonrx}$/oi) { |
| 1814 | # zone,ZONE |
| 1815 | # zone,DSTFLAG,ZONE |
| 1816 | # Converts 'now' to the alternate zone |
| 1817 | |
| 1818 | $op = 'zone'; |
| 1819 | ($dstflag,$zone) = ($1,$2); |
| 1820 | |
| 1821 | } elsif ($val =~ /^${da1rx}${dstrx}${zonrx}$/o || |
| 1822 | $val =~ /^${da2rx}${dstrx}${zonrx}$/o) { |
| 1823 | # DATE,ZONE |
| 1824 | # DATE,DSTFLAG,ZONE |
| 1825 | # Sets the date and zone |
| 1826 | |
| 1827 | $op = 'datezone'; |
| 1828 | my($y,$m,$d,$h,$mn,$s); |
| 1829 | ($y,$m,$d,$h,$mn,$s,$dstflag,$zone) = ($1,$2,$3,$4,$5,$6,$7,$8); |
| 1830 | $date = [$y,$m,$d,$h,$mn,$s]; |
| 1831 | |
| 1832 | } elsif ($val =~ /^${da1rx}$/o || |
| 1833 | $val =~ /^${da2rx}$/o) { |
| 1834 | # DATE |
| 1835 | # Sets the date in the system timezone |
| 1836 | |
| 1837 | $op = 'date'; |
| 1838 | my($y,$m,$d,$h,$mn,$s) = ($1,$2,$3,$4,$5,$6); |
| 1839 | $date = [$y,$m,$d,$h,$mn,$s]; |
| 1840 | $zone = $self->_now('systz',1); |
| 1841 | |
| 1842 | } elsif (lc($val) eq 'now') { |
| 1843 | # now |
| 1844 | # Resets everything |
| 1845 | |
| 1846 | my $systz = $$base{'data'}{'now'}{'systz'}; |
| 1847 | $base->_init_now(); |
| 1848 | $$base{'data'}{'now'}{'systz'} = $systz; |
| 1849 | return 0; |
| 1850 | |
| 1851 | } else { |
| 1852 | carp "ERROR: [config_var] invalid SetDate/ForceDate value: $val"; |
| 1853 | return 1; |
| 1854 | } |
| 1855 | |
| 1856 | $dstflag = 'std' if (! $dstflag); |
| 1857 | |
| 1858 | # |
| 1859 | # Get the date we're setting 'now' to |
| 1860 | # |
| 1861 | |
| 1862 | if ($op eq 'nowzone') { |
| 1863 | # Use the system localtime |
| 1864 | |
| 1865 | my($s,$mn,$h,$d,$m,$y) = localtime($time); |
| 1866 | $y += 1900; |
| 1867 | $m++; |
| 1868 | $date = [$y,$m,$d,$h,$mn,$s]; |
| 1869 | |
| 1870 | } elsif ($op eq 'zone') { |
| 1871 | # Use the system GMT time |
| 1872 | |
| 1873 | my($s,$mn,$h,$d,$m,$y) = gmtime($time); |
| 1874 | $y += 1900; |
| 1875 | $m++; |
| 1876 | $date = [$y,$m,$d,$h,$mn,$s]; |
| 1877 | } |
| 1878 | |
| 1879 | # |
| 1880 | # Find out what zone was passed in. It can be an alias or an offset. |
| 1881 | # |
| 1882 | |
| 1883 | if ($zone) { |
| 1884 | my ($err,@args); |
| 1885 | my $dmb = $$self{'base'}; |
| 1886 | $date = [] if (! defined $date); |
| 1887 | $zone = $self->__zone($date,'',lc($zone),'',lc($dstflag)); |
| 1888 | if (! $zone) { |
| 1889 | carp "ERROR: [config_var] invalid zone in SetDate: @args"; |
| 1890 | return 1; |
| 1891 | } |
| 1892 | |
| 1893 | } else { |
| 1894 | $zone = $$base{'data'}{'now'}{'systz'}; |
| 1895 | } |
| 1896 | |
| 1897 | # |
| 1898 | # Handle the zone |
| 1899 | # |
| 1900 | |
| 1901 | my($isdst,@isdst); |
| 1902 | if ($dstflag eq 'std') { |
| 1903 | @isdst = (0,1); |
| 1904 | } elsif ($dstflag eq 'stdonly') { |
| 1905 | @isdst = (0); |
| 1906 | } elsif ($dstflag eq 'dst') { |
| 1907 | @isdst = (1,0); |
| 1908 | } else { |
| 1909 | @isdst = (1); |
| 1910 | } |
| 1911 | |
| 1912 | if ($op eq 'nowzone' || |
| 1913 | $op eq 'datezone' || |
| 1914 | $op eq 'date') { |
| 1915 | |
| 1916 | # Check to make sure that the date can exist in this zone. |
| 1917 | my $per; |
| 1918 | foreach my $dst (@isdst) { |
| 1919 | next if ($per); |
| 1920 | $per = $self->date_period($date,$zone,1,$dst); |
| 1921 | } |
| 1922 | |
| 1923 | if (! $per) { |
| 1924 | carp "ERROR: [config_var] invalid date: SetDate: $date, $zone"; |
| 1925 | return 1; |
| 1926 | } |
| 1927 | $isdst = $$per[5]; |
| 1928 | $abb = $$per[4]; |
| 1929 | $offset = $$per[3]; |
| 1930 | |
| 1931 | } elsif ($op eq 'zone') { |
| 1932 | |
| 1933 | # Convert to that zone |
| 1934 | my($err); |
| 1935 | ($err,$date,$offset,$isdst,$abb) = $self->convert_from_gmt($date,$zone); |
| 1936 | if ($err) { |
| 1937 | carp "ERROR: [config_var] invalid SetDate date/offset values: $date, $zone"; |
| 1938 | return 1; |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | # |
| 1943 | # Set NOW |
| 1944 | # |
| 1945 | |
| 1946 | $$base{'data'}{'now'}{'date'} = $date; |
| 1947 | $$base{'data'}{'now'}{'tz'} = $self->_zone($zone); |
| 1948 | $$base{'data'}{'now'}{'isdst'} = $isdst; |
| 1949 | $$base{'data'}{'now'}{'abb'} = $abb; |
| 1950 | $$base{'data'}{'now'}{'offset'} = $offset; |
| 1951 | |
| 1952 | # |
| 1953 | # Treate SetDate/ForceDate |
| 1954 | # |
| 1955 | |
| 1956 | if ($force) { |
| 1957 | $$base{'data'}{'now'}{'force'} = 1; |
| 1958 | $$base{'data'}{'now'}{'set'} = 0; |
| 1959 | } else { |
| 1960 | $$base{'data'}{'now'}{'force'} = 0; |
| 1961 | $$base{'data'}{'now'}{'set'} = 1; |
| 1962 | $$base{'data'}{'now'}{'setsecs'} = $time; |
| 1963 | my($err,$setdate) = $self->convert_to_gmt($date,$zone); |
| 1964 | $$base{'data'}{'now'}{'setdate'} = $setdate; |
| 1965 | } |
| 1966 | |
| 1967 | return 0; |
| 1968 | } |
| 1969 | |
| 1970 | 1; |
| 1971 | # Local Variables: |
| 1972 | # mode: cperl |
| 1973 | # indent-tabs-mode: nil |
| 1974 | # cperl-indent-level: 3 |
| 1975 | # cperl-continued-statement-offset: 2 |
| 1976 | # cperl-continued-brace-offset: 0 |
| 1977 | # cperl-brace-offset: 0 |
| 1978 | # cperl-brace-imaginary-offset: 0 |
| 1979 | # cperl-label-offset: 0 |
| 1980 | # End: |