| 1 | package Date::Manip::TZ_Base; |
| 2 | # Copyright (c) 2010-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 | ######################################################################## |
| 8 | |
| 9 | require 5.010000; |
| 10 | use warnings; |
| 11 | use strict; |
| 12 | use IO::File; |
| 13 | use Carp; |
| 14 | |
| 15 | our ($VERSION); |
| 16 | $VERSION='6.99'; |
| 17 | END { undef $VERSION; } |
| 18 | |
| 19 | ######################################################################## |
| 20 | # METHODS |
| 21 | ######################################################################## |
| 22 | |
| 23 | sub _config_var { |
| 24 | my($self,$var,$val) = @_; |
| 25 | $var = lc($var); |
| 26 | |
| 27 | # A simple flag used to force a new configuration, but has |
| 28 | # no other affect. |
| 29 | return if ($var eq 'ignore'); |
| 30 | |
| 31 | my $istz = ref($self) eq 'Date::Manip::TZ'; |
| 32 | |
| 33 | if ($istz && ($var eq 'tz' || |
| 34 | $var eq 'forcedate' || |
| 35 | $var eq 'setdate' || |
| 36 | $var eq 'configfile')) { |
| 37 | if ($var eq 'tz') { |
| 38 | carp "WARNING: the TZ Date::Manip config variable is deprecated\n" . |
| 39 | " and will be removed in version 7.00. Please use\n" . |
| 40 | " the SetDate or ForceDate config variables instead.\n"; |
| 41 | } |
| 42 | return $self->_config_var_tz($var,$val); |
| 43 | } else { |
| 44 | my $base = ($istz ? $$self{'base'} : $self); |
| 45 | return $base->_config_var_base($var,$val); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | # This reads a config file |
| 50 | # |
| 51 | sub _config_file { |
| 52 | my($self,$file) = @_; |
| 53 | |
| 54 | return if (! $file); |
| 55 | |
| 56 | if (! -f $file) { |
| 57 | carp "ERROR: [config_file] file doesn't exist: $file"; |
| 58 | return; |
| 59 | } |
| 60 | if (! -r $file) { |
| 61 | carp "ERROR: [config_file] file not readable: $file"; |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | my $in = new IO::File; |
| 66 | if (! $in->open($file)) { |
| 67 | carp "ERROR: [config_file] unable to open file: $file: $!"; |
| 68 | return; |
| 69 | } |
| 70 | my @in = <$in>; |
| 71 | $in->close(); |
| 72 | |
| 73 | my $sect = 'conf'; |
| 74 | my %sect; |
| 75 | |
| 76 | chomp(@in); |
| 77 | foreach my $line (@in) { |
| 78 | $line =~ s/^\s+//o; |
| 79 | $line =~ s/\s+$//o; |
| 80 | next if (! $line or $line =~ /^\043/o); |
| 81 | |
| 82 | if ($line =~ /^\*/o) { |
| 83 | # New section |
| 84 | $sect = $self->_config_file_section($line); |
| 85 | } else { |
| 86 | $sect{$sect} = 1; |
| 87 | $self->_config_file_var($sect,$line); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | # If we did a holidays section, we need to create a regular |
| 92 | # expression with all of the holiday names. |
| 93 | |
| 94 | my $istz = ref($self) eq 'Date::Manip::TZ'; |
| 95 | my $base = ($istz ? $$self{'base'} : $self); |
| 96 | |
| 97 | if (exists $sect{'holidays'}) { |
| 98 | my @hol = @{ $$base{'data'}{'sections'}{'holidays'} }; |
| 99 | my @nam; |
| 100 | while (@hol) { |
| 101 | my $junk = shift(@hol); |
| 102 | my $hol = shift(@hol); |
| 103 | push(@nam,$hol) if ($hol); |
| 104 | } |
| 105 | |
| 106 | if (@nam) { |
| 107 | @nam = sort _sortByLength(@nam); |
| 108 | my $hol = '(?<holiday>' . join('|',map { "\Q$_\E" } @nam) . ')'; |
| 109 | my $yr = '(?<y>\d\d\d\d|\d\d)'; |
| 110 | |
| 111 | my $rx = "$hol\\s*$yr|" . # Christmas 2009 |
| 112 | "$yr\\s*$hol|" . # 2009 Christmas |
| 113 | "$hol"; # Christmas |
| 114 | |
| 115 | $$base{'data'}{'rx'}{'holidays'} = qr/^(?:$rx)$/i; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | sub _config_file_section { |
| 121 | my($self,$line) = @_; |
| 122 | |
| 123 | my $istz = ref($self) eq 'Date::Manip::TZ'; |
| 124 | my $base = ($istz ? $$self{'base'} : $self); |
| 125 | |
| 126 | $line =~ s/^\*//o; |
| 127 | $line =~ s/\s*$//o; |
| 128 | my $sect = lc($line); |
| 129 | if (! exists $$base{'data'}{'sections'}{$sect}) { |
| 130 | carp "WARNING: [config_file] unknown section created: $sect"; |
| 131 | $base->_section($sect); |
| 132 | } |
| 133 | return $sect; |
| 134 | } |
| 135 | |
| 136 | sub _config_file_var { |
| 137 | my($self,$sect,$line) = @_; |
| 138 | |
| 139 | my $istz = ref($self) eq 'Date::Manip::TZ'; |
| 140 | my $base = ($istz ? $$self{'base'} : $self); |
| 141 | |
| 142 | my($var,$val); |
| 143 | if ($line =~ /^\s*(.*?)\s*=\s*(.*?)\s*$/o) { |
| 144 | ($var,$val) = ($1,$2); |
| 145 | } else { |
| 146 | croak "ERROR: invalid Date::Manip config file line:\n $line\n"; |
| 147 | } |
| 148 | |
| 149 | if ($sect eq 'conf') { |
| 150 | $var = lc($var); |
| 151 | $self->_config($var,$val); |
| 152 | } else { |
| 153 | $base->_section($sect,$var,$val); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | # $val = $self->config(VAR); |
| 158 | # Returns the value of a variable. |
| 159 | # |
| 160 | # $self->config([SECT], VAR, VAL) sets the value of a variable |
| 161 | # Sets the value of a variable. |
| 162 | # |
| 163 | sub _config { |
| 164 | my($self,$var,$val) = @_; |
| 165 | |
| 166 | my $sect = 'conf'; |
| 167 | |
| 168 | # |
| 169 | # $self->_conf(VAR, VAL) sets the value of a variable |
| 170 | # |
| 171 | |
| 172 | $var = lc($var); |
| 173 | if (defined $val) { |
| 174 | return $self->_config_var($var,$val); |
| 175 | } |
| 176 | |
| 177 | # |
| 178 | # $self->_conf(VAR) returns the value of a variable |
| 179 | # |
| 180 | |
| 181 | if (exists $$self{'data'}{'sections'}{$sect}{$var}) { |
| 182 | return $$self{'data'}{'sections'}{$sect}{$var}; |
| 183 | } else { |
| 184 | carp "ERROR: [config] invalid config variable: $var"; |
| 185 | return ''; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | ######################################################################## |
| 190 | |
| 191 | sub _fix_year { |
| 192 | my($self,$y) = @_; |
| 193 | my $istz = ref($self) eq 'Date::Manip::TZ'; |
| 194 | my $base = ($istz ? $self->base() : $self); |
| 195 | |
| 196 | my $method = $base->_config('yytoyyyy'); |
| 197 | |
| 198 | return $y if (length($y)==4); |
| 199 | return undef if (length($y)!=2); |
| 200 | |
| 201 | my $curr_y; |
| 202 | if (ref($self) eq 'Date::Manip::TZ') { |
| 203 | $curr_y = $self->_now('y',1); |
| 204 | } else { |
| 205 | $curr_y = ( localtime(time) )[5]; |
| 206 | $curr_y += 1900; |
| 207 | } |
| 208 | |
| 209 | if ($method eq 'c') { |
| 210 | return substr($curr_y,0,2) . $y; |
| 211 | |
| 212 | } elsif ($method =~ /^c(\d\d)$/) { |
| 213 | return "$1$y"; |
| 214 | |
| 215 | } elsif ($method =~ /^c(\d\d)(\d\d)$/) { |
| 216 | return "$1$y" + ($y<$2 ? 100 : 0); |
| 217 | |
| 218 | } else { |
| 219 | my $y1 = $curr_y - $method; |
| 220 | my $y2 = $y1 + 99; |
| 221 | $y1 =~ /^(\d\d)/; |
| 222 | $y = "$1$y"; |
| 223 | if ($y<$y1) { |
| 224 | $y += 100; |
| 225 | } |
| 226 | if ($y>$y2) { |
| 227 | $y -= 100; |
| 228 | } |
| 229 | return $y; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | ############################################################################### |
| 234 | # Functions for setting the default date/time |
| 235 | |
| 236 | # Many date operations use a default time and/or date to set some |
| 237 | # or all values. This function may be used to set or examine the |
| 238 | # default time. |
| 239 | # |
| 240 | # _now allows you to get the current date and/or time in the |
| 241 | # local timezone. |
| 242 | # |
| 243 | # The function performed depends on $op and are described in the |
| 244 | # following table: |
| 245 | # |
| 246 | # $op function |
| 247 | # ------------------ ---------------------------------- |
| 248 | # undef Returns the current default values |
| 249 | # (y,m,d,h,mn,s) without updating |
| 250 | # the time (it'll update if it has |
| 251 | # never been set). |
| 252 | # |
| 253 | # 'now' Updates now and returns |
| 254 | # (y,m,d,h,mn,s) |
| 255 | # |
| 256 | # 'time' Updates now and Returns (h,mn,s) |
| 257 | # |
| 258 | # 'y' Returns the default value of one |
| 259 | # 'm' of the fields (no update) |
| 260 | # 'd' |
| 261 | # 'h' |
| 262 | # 'mn' |
| 263 | # 's' |
| 264 | # |
| 265 | # 'systz' Returns the system timezone |
| 266 | # |
| 267 | # 'isdst' Returns the 'now' values if set, |
| 268 | # 'tz' or system time values otherwise. |
| 269 | # 'offset' |
| 270 | # 'abb' |
| 271 | # |
| 272 | sub _now { |
| 273 | my($self,$op,$noupdate) = @_; |
| 274 | my $istz = ref($self) eq 'Date::Manip::TZ'; |
| 275 | my $base = ($istz ? $self->base() : $self); |
| 276 | |
| 277 | # Update "NOW" if we're checking 'now', 'time', or the date |
| 278 | # is not set already. |
| 279 | |
| 280 | if (! defined $noupdate) { |
| 281 | if ($op =~ /(?:now|time)/) { |
| 282 | $noupdate = 0; |
| 283 | } else { |
| 284 | $noupdate = 1; |
| 285 | } |
| 286 | } |
| 287 | $noupdate = 0 if (! exists $$base{'data'}{'now'}{'date'}); |
| 288 | $self->_update_now() unless ($noupdate); |
| 289 | |
| 290 | # Now return the value of the operation |
| 291 | |
| 292 | my @tmpnow = @{ $$base{'data'}{'tmpnow'} }; |
| 293 | my @now = (@tmpnow ? @tmpnow : @{ $$base{'data'}{'now'}{'date'} }); |
| 294 | |
| 295 | if ($op eq 'tz') { |
| 296 | if (exists $$base{'data'}{'now'}{'tz'}) { |
| 297 | return $$base{'data'}{'now'}{'tz'}; |
| 298 | } else { |
| 299 | return $$base{'data'}{'now'}{'systz'}; |
| 300 | } |
| 301 | |
| 302 | } elsif ($op eq 'systz') { |
| 303 | return $$base{'data'}{'now'}{'systz'}; |
| 304 | |
| 305 | } elsif ($op eq 'isdst') { |
| 306 | return $$base{'data'}{'now'}{'isdst'}; |
| 307 | |
| 308 | } elsif ($op eq 'offset') { |
| 309 | return @{ $$base{'data'}{'now'}{'offset'} }; |
| 310 | |
| 311 | } elsif ($op eq 'abb') { |
| 312 | return $$base{'data'}{'now'}{'abb'}; |
| 313 | |
| 314 | } elsif ($op eq 'now') { |
| 315 | return @now; |
| 316 | |
| 317 | } elsif ($op eq 'y') { |
| 318 | return $now[0]; |
| 319 | |
| 320 | } elsif ($op eq 'time') { |
| 321 | return @now[3..5]; |
| 322 | |
| 323 | } elsif ($op eq 'm') { |
| 324 | return $now[1]; |
| 325 | |
| 326 | } elsif ($op eq 'd') { |
| 327 | return $now[2]; |
| 328 | |
| 329 | } elsif ($op eq 'h') { |
| 330 | return $now[3]; |
| 331 | |
| 332 | } elsif ($op eq 'mn') { |
| 333 | return $now[4]; |
| 334 | |
| 335 | } elsif ($op eq 's') { |
| 336 | return $now[5]; |
| 337 | |
| 338 | } else { |
| 339 | carp "ERROR: [now] invalid argument list: $op"; |
| 340 | return (); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | sub _update_now { |
| 345 | my($self) = @_; |
| 346 | my $istz = ref($self) eq 'Date::Manip::TZ'; |
| 347 | my $base = ($istz ? $self->base() : $self); |
| 348 | |
| 349 | # If we've called ForceDate, don't change it. |
| 350 | return if ($$base{'data'}{'now'}{'force'}); |
| 351 | |
| 352 | # If we've called SetDate (which will only happen if a |
| 353 | # Date::Manip:TZ object is available), figure out what 'now' is |
| 354 | # based on the number of seconds that have elapsed since it was |
| 355 | # set. This will ONLY happen if TZ has been loaded. |
| 356 | |
| 357 | if ($$base{'data'}{'now'}{'set'}) { |
| 358 | my $date = $$base{'data'}{'now'}{'setdate'}; |
| 359 | my $secs = time - $$base{'data'}{'now'}{'setsecs'}; |
| 360 | |
| 361 | $date = $base->calc_date_time($date,[0,0,$secs]); # 'now' in GMT |
| 362 | my $zone = $self->_now('tz',1); |
| 363 | my ($err,$date2,$offset,$isdst,$abbrev) = $self->convert_from_gmt($date,$zone); |
| 364 | |
| 365 | $$base{'data'}{'now'}{'date'} = $date2; |
| 366 | $$base{'data'}{'now'}{'isdst'} = $isdst; |
| 367 | $$base{'data'}{'now'}{'offset'} = $offset; |
| 368 | $$base{'data'}{'now'}{'abb'} = $abbrev; |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | # Otherwise, we'll use the system time. |
| 373 | |
| 374 | my $time = time; |
| 375 | my($s,$mn,$h,$d,$m,$y,$wday,$yday,$isdst) = localtime($time); |
| 376 | my($s0,$mn0,$h0,$d0,$m0,$y0) = gmtime($time); |
| 377 | |
| 378 | $y += 1900; |
| 379 | $m++; |
| 380 | |
| 381 | $y0 += 1900; |
| 382 | $m0++; |
| 383 | |
| 384 | my $off = $base->calc_date_date([$y,$m,$d,$h,$mn,$s],[$y0,$m0,$d0,$h0,$mn0,$s0],1); |
| 385 | |
| 386 | $$base{'data'}{'now'}{'date'} = [$y,$m,$d,$h,$mn,$s]; |
| 387 | $$base{'data'}{'now'}{'isdst'} = $isdst; |
| 388 | $$base{'data'}{'now'}{'offset'}= $off; |
| 389 | |
| 390 | my $abb = '???'; |
| 391 | if (ref($self) eq 'Date::Manip::TZ') { |
| 392 | my $zone = $self->_now('tz',1); |
| 393 | my $per = $self->date_period([$y,$m,$d,$h,$mn,$s],$zone,1,$isdst); |
| 394 | $abb = $$per[4]; |
| 395 | } |
| 396 | |
| 397 | $$base{'data'}{'now'}{'abb'} = $abb; |
| 398 | |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | ############################################################################### |
| 403 | # This sorts from longest to shortest element |
| 404 | # |
| 405 | no strict 'vars'; |
| 406 | sub _sortByLength { |
| 407 | return (length $b <=> length $a); |
| 408 | } |
| 409 | use strict 'vars'; |
| 410 | |
| 411 | 1; |
| 412 | # Local Variables: |
| 413 | # mode: cperl |
| 414 | # indent-tabs-mode: nil |
| 415 | # cperl-indent-level: 3 |
| 416 | # cperl-continued-statement-offset: 2 |
| 417 | # cperl-continued-brace-offset: 0 |
| 418 | # cperl-brace-offset: 0 |
| 419 | # cperl-brace-imaginary-offset: 0 |
| 420 | # cperl-label-offset: 0 |
| 421 | # End: |