| 1 |
package Date::Manip::Date; |
| 2 |
# Copyright (c) 1995-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 |
@ISA = ('Date::Manip::Obj'); |
| 16 |
|
| 17 |
require 5.010000; |
| 18 |
use warnings; |
| 19 |
use strict; |
| 20 |
use integer; |
| 21 |
use utf8; |
| 22 |
use IO::File; |
| 23 |
use Storable qw(dclone); |
| 24 |
use Carp; |
| 25 |
#use re 'debug'; |
| 26 |
|
| 27 |
use Date::Manip::Base; |
| 28 |
use Date::Manip::TZ; |
| 29 |
|
| 30 |
our $VERSION; |
| 31 |
$VERSION='6.99'; |
| 32 |
END { undef $VERSION; } |
| 33 |
|
| 34 |
######################################################################## |
| 35 |
# BASE METHODS |
| 36 |
######################################################################## |
| 37 |
|
| 38 |
sub is_date { |
| 39 |
return 1; |
| 40 |
} |
| 41 |
|
| 42 |
# Call this every time a new date is put in to make sure everything is |
| 43 |
# correctly initialized. |
| 44 |
# |
| 45 |
sub _init { |
| 46 |
my($self) = @_; |
| 47 |
|
| 48 |
$$self{'err'} = ''; |
| 49 |
|
| 50 |
$$self{'data'} = |
| 51 |
{ |
| 52 |
'set' => 0, # 1 if the date has been set |
| 53 |
# 2 if the date is in the process of being set |
| 54 |
|
| 55 |
# The date as input |
| 56 |
'in' => '', # the string that was parsed (if any) |
| 57 |
'zin' => '', # the timezone that was parsed (if any) |
| 58 |
|
| 59 |
# The date in the parsed timezone |
| 60 |
'date' => [], # the parsed date split |
| 61 |
'def' => [0,0,0,0,0,0], |
| 62 |
# 1 for each field that came from |
| 63 |
# defaults rather than parsed |
| 64 |
# '' for an implied field |
| 65 |
'tz' => '', # the timezone of the date |
| 66 |
'isdst' => '', # 1 if the date is in DST. |
| 67 |
'offset' => [], # The offset from GMT |
| 68 |
'abb' => '', # The timezone abbreviation. |
| 69 |
'f' => {}, # fields used in printing a date |
| 70 |
|
| 71 |
# The date in GMT |
| 72 |
'gmt' => [], # the date converted to GMT |
| 73 |
|
| 74 |
# The date in local timezone |
| 75 |
'loc' => [], # the date converted to local timezone |
| 76 |
}; |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
sub _init_args { |
| 81 |
my($self) = @_; |
| 82 |
|
| 83 |
my @args = @{ $$self{'args'} }; |
| 84 |
$self->parse(@args); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
sub input { |
| 89 |
my($self) = @_; |
| 90 |
return $$self{'data'}{'in'}; |
| 91 |
} |
| 92 |
|
| 93 |
######################################################################## |
| 94 |
# DATE PARSING |
| 95 |
######################################################################## |
| 96 |
|
| •97 |
# The longest string the parsers will look at. The time matching |
| 98 |
# regexp is applied unanchored, so the cost of failing to match grows |
| 99 |
# with the square of the length of an interior whitespace run. Real |
| 100 |
# date strings are well under 100 characters. |
| 101 |
|
| 102 |
our $MAXLENGTH = 256; |
| 103 |
|
| 104 |
sub parse { |
| 105 |
my($self,$instring,@opts) = @_; |
| 106 |
$self->_init(); |
| 107 |
my $noupdate = 0; |
| 108 |
|
| 109 |
if (! $instring) { |
| 110 |
$$self{'err'} = '[parse] Empty date string'; |
| 111 |
return 1; |
| 112 |
} |
| 113 |
|
| •114 |
if (length($instring) > $MAXLENGTH) { |
| 115 |
$$self{'err'} = '[parse] Date string too long'; |
| 116 |
return 1; |
| 117 |
} |
| 118 |
|
| 119 |
my %opts = map { $_,1 } @opts; |
| 120 |
|
| 121 |
my $dmt = $$self{'tz'}; |
| 122 |
my $dmb = $$dmt{'base'}; |
| 123 |
delete $$self{'data'}{'default_time'}; |
| 124 |
|
| 125 |
my($done,$y,$m,$d,$h,$mn,$s,$tzstring,$zone,$abb,$off,$dow,$got_time, |
| 126 |
$default_time,$firsterr); |
| 127 |
|
| 128 |
ENCODING: |
| 129 |
foreach my $string ($dmb->_encoding($instring)) { |
| 130 |
$got_time = 0; |
| 131 |
$default_time = 0; |
| 132 |
|
| 133 |
# Put parse in a simple loop for an easy exit. |
| 134 |
PARSE: |
| 135 |
{ |
| 136 |
my(@tmp,$tmp); |
| 137 |
$$self{'err'} = ''; |
| 138 |
|
| 139 |
################### |
| 140 |
# Handle some special language-specific rules |
| 141 |
|
| 142 |
# Some languages add a trailing period in some places. For example, |
| 143 |
# the default German date format (running the system date command) |
| 144 |
# produces: Mo 3. Jan 11:00:00 EST 2022 |
| 145 |
# The '3.' needs to have the period stripped. |
| 146 |
|
| 147 |
if ($self->_parse_rule('remove_trailing_period')) { |
| 148 |
$string =~ s/\.\s/ /g; |
| 149 |
$string =~ s/\.$//; |
| 150 |
} |
| 151 |
|
| 152 |
# Some languages add parenthese. For example, the default date |
| 153 |
# output in russian in some cases puts the timezone in parentheses. |
| 154 |
|
| 155 |
if ($self->_parse_rule('remove_parens')) { |
| 156 |
$string =~ s/\(//g; |
| 157 |
$string =~ s/\)//; |
| 158 |
} |
| 159 |
|
| 160 |
my $words = $self->_parse_rule('strip_word'); |
| 161 |
if ($words) { |
| 162 |
foreach my $w (@$words) { |
| 163 |
$string =~ s/(?:^|\s)\Q$w\E(?:\s|$)/ /; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
################### |
| 168 |
|
| 169 |
# Check the standard date format |
| 170 |
|
| 171 |
$tmp = $dmb->split('date',$string); |
| 172 |
if (defined($tmp)) { |
| 173 |
($y,$m,$d,$h,$mn,$s) = @$tmp; |
| 174 |
$got_time = 1; |
| 175 |
last PARSE; |
| 176 |
} |
| 177 |
|
| 178 |
# Parse ISO 8601 dates now (which may have a timezone). |
| 179 |
|
| 180 |
if (! exists $opts{'noiso8601'}) { |
| 181 |
($done,@tmp) = $self->_parse_datetime_iso8601($string,\$noupdate); |
| 182 |
if ($done) { |
| 183 |
($y,$m,$d,$h,$mn,$s,$tzstring,$zone,$abb,$off) = @tmp; |
| 184 |
$got_time = 1; |
| 185 |
last PARSE; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
# There's lots of ways that commas may be included. Remove |
| 190 |
# them (unless it's preceded and followed by a digit in |
| 191 |
# which case it's probably a fractional separator). |
| 192 |
|
| 193 |
$string =~ s/(?<!\d),/ /g; |
| 194 |
$string =~ s/,(?!\d)/ /g; |
| 195 |
|
| 196 |
# Some special full date/time formats ('now', 'epoch') |
| 197 |
|
| 198 |
if (! exists $opts{'nospecial'}) { |
| 199 |
($done,@tmp) = $self->_parse_datetime_other($string,\$noupdate); |
| 200 |
if ($done) { |
| 201 |
($y,$m,$d,$h,$mn,$s,$tzstring,$zone,$abb,$off) = @tmp; |
| 202 |
$got_time = 1; |
| 203 |
last PARSE; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
# Parse (and remove) the time (and an immediately following timezone). |
| 208 |
|
| 209 |
($got_time,@tmp) = $self->_parse_time('parse',$string,\$noupdate,%opts); |
| 210 |
if ($got_time) { |
| 211 |
($string,$h,$mn,$s,$tzstring,$zone,$abb,$off) = @tmp; |
| 212 |
} |
| 213 |
|
| 214 |
if (! $string) { |
| 215 |
($y,$m,$d) = $self->_def_date($y,$m,$d,\$noupdate); |
| 216 |
last; |
| 217 |
} |
| 218 |
|
| 219 |
# Parse (and remove) the day of week. Also, handle the simple DoW |
| 220 |
# formats. |
| 221 |
|
| 222 |
if (! exists $opts{'nodow'}) { |
| 223 |
($done,@tmp) = $self->_parse_dow($string,\$noupdate); |
| 224 |
if (@tmp) { |
| 225 |
if ($done) { |
| 226 |
($y,$m,$d) = @tmp; |
| 227 |
$default_time = 1; |
| 228 |
last PARSE; |
| 229 |
} else { |
| 230 |
($string,$dow) = @tmp; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
$dow = 0 if (! $dow); |
| 235 |
|
| 236 |
# At this point, the string might contain the following dates: |
| 237 |
# |
| 238 |
# OTHER |
| 239 |
# OTHER ZONE / ZONE OTHER |
| 240 |
# DELTA |
| 241 |
# DELTA ZONE / ZONE DELTA |
| 242 |
# HOLIDAY |
| 243 |
# HOLIDAY ZONE / ZONE HOLIDAY |
| 244 |
# |
| 245 |
# ZONE is only allowed if it wasn't parsed with the time |
| 246 |
|
| 247 |
# Unfortunately, there are some conflicts between zones and |
| 248 |
# some other formats, so try parsing the entire string as a date. |
| 249 |
|
| 250 |
(@tmp) = $self->_parse_date($string,$dow,\$noupdate,%opts); |
| 251 |
if (@tmp) { |
| 252 |
my $dow2; |
| 253 |
($y,$m,$d,$dow2) = @tmp; |
| 254 |
if ($dow2 && $dow && $dow != $dow2) { |
| 255 |
$$self{'err'} = '[parse] Day of week invalid'; |
| 256 |
last PARSE; |
| 257 |
} |
| 258 |
$dow = $dow2 if ($dow2); |
| 259 |
$default_time = 1; |
| 260 |
last PARSE; |
| 261 |
} |
| 262 |
|
| 263 |
# Parse any timezone |
| 264 |
|
| 265 |
if (! $tzstring) { |
| 266 |
($string,@tmp) = $self->_parse_tz($string,\$noupdate); |
| 267 |
($tzstring,$zone,$abb,$off) = @tmp if (@tmp); |
| 268 |
last PARSE if (! $string); |
| 269 |
} |
| 270 |
|
| 271 |
# Try the remainder of the string as a date. |
| 272 |
|
| 273 |
if ($tzstring) { |
| 274 |
(@tmp) = $self->_parse_date($string,$dow,\$noupdate,%opts); |
| 275 |
if (@tmp) { |
| 276 |
($y,$m,$d,$dow) = @tmp; |
| 277 |
$default_time = 1; |
| 278 |
last PARSE; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
# Parse deltas |
| 283 |
# |
| 284 |
# Occasionally, a delta is entered for a date (which is |
| 285 |
# interpreted as the date relative to now). There can be some |
| 286 |
# confusion between a date and a delta, but the most |
| 287 |
# important conflicts are the ISO 8601 dates (many of which |
| 288 |
# could be interpreted as a delta), but those have already |
| 289 |
# been taken care of. |
| 290 |
# |
| 291 |
# We may have already gotten the time: |
| 292 |
# 3 days ago at midnight UTC |
| 293 |
# (we already stripped off the 'at midnight UTC' above). |
| 294 |
# |
| 295 |
# We also need to handle the sitution of a delta and a timezone. |
| 296 |
# in 2 hours EST |
| 297 |
# in 2 days EST |
| 298 |
# but only if no time was entered. |
| 299 |
|
| 300 |
if (! exists $opts{'nodelta'}) { |
| 301 |
|
| 302 |
($done,@tmp) = |
| 303 |
$self->_parse_delta($string,$dow,$got_time,$h,$mn,$s,\$noupdate); |
| 304 |
if (@tmp) { |
| 305 |
($y,$m,$d,$h,$mn,$s) = @tmp; |
| 306 |
$got_time = 1; |
| 307 |
$dow = ''; |
| 308 |
} |
| 309 |
last PARSE if ($done); |
| 310 |
|
| 311 |
# We'll also check the original string to see if it's a valid |
| 312 |
# delta since some deltas may have interpreted part of it as |
| 313 |
# a time or timezone. |
| 314 |
|
| 315 |
($done,@tmp) = |
| 316 |
$self->_parse_delta($instring,$dow,$got_time,$h,$mn,$s,\$noupdate); |
| 317 |
if (@tmp) { |
| 318 |
($y,$m,$d,$h,$mn,$s) = @tmp; |
| 319 |
$got_time = 1; |
| 320 |
$dow = ''; |
| 321 |
($tzstring,$zone,$abb,$off) = (); |
| 322 |
} |
| 323 |
last PARSE if ($done); |
| 324 |
} |
| 325 |
|
| 326 |
# Parse holidays |
| 327 |
|
| 328 |
unless (exists $opts{'noholidays'}) { |
| 329 |
($done,@tmp) = |
| 330 |
$self->_parse_holidays($string,\$noupdate); |
| 331 |
if (@tmp) { |
| 332 |
($y,$m,$d) = @tmp; |
| 333 |
} |
| 334 |
last PARSE if ($done); |
| 335 |
} |
| 336 |
|
| 337 |
$$self{'err'} = '[parse] Invalid date string'; |
| 338 |
last PARSE; |
| 339 |
} |
| 340 |
|
| 341 |
# We got an error parsing this encoding of the string. It could |
| 342 |
# be that it is a genuine error, or it may be that we simply |
| 343 |
# need to try a different encoding. If ALL encodings fail, we'll |
| 344 |
# return the error from the first one. |
| 345 |
|
| 346 |
if ($$self{'err'}) { |
| 347 |
if (! $firsterr) { |
| 348 |
$firsterr = $$self{'err'}; |
| 349 |
} |
| 350 |
next ENCODING; |
| 351 |
} |
| 352 |
|
| 353 |
# If we didn't get an error, this is the string to use. |
| 354 |
|
| 355 |
last ENCODING; |
| 356 |
} |
| 357 |
|
| 358 |
if ($$self{'err'}) { |
| 359 |
$$self{'err'} = $firsterr; |
| 360 |
return 1; |
| 361 |
} |
| 362 |
|
| 363 |
# Make sure that a time is set |
| 364 |
|
| 365 |
if (! $got_time) { |
| 366 |
if ($default_time) { |
| 367 |
if (exists $$self{'data'}{'default_time'}) { |
| 368 |
($h,$mn,$s) = @{ $$self{'data'}{'default_time'} }; |
| 369 |
delete $$self{'data'}{'default_time'}; |
| 370 |
} elsif ($dmb->_config('defaulttime') eq 'midnight') { |
| 371 |
($h,$mn,$s) = (0,0,0); |
| 372 |
} else { |
| 373 |
($h,$mn,$s) = $dmt->_now('time',$noupdate); |
| 374 |
$noupdate = 1; |
| 375 |
} |
| 376 |
$got_time = 1; |
| 377 |
} else { |
| 378 |
($h,$mn,$s) = $self->_def_time(undef,undef,undef,\$noupdate); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
$$self{'data'}{'set'} = 2; |
| 383 |
return $self->_parse_check('parse',$instring, |
| 384 |
$y,$m,$d,$h,$mn,$s,$dow,$tzstring,$zone,$abb,$off); |
| 385 |
} |
| 386 |
|
| 387 |
sub parse_time { |
| 388 |
my($self,$string,@opts) = @_; |
| 389 |
my %opts = map { $_,1 } @opts; |
| 390 |
my $noupdate = 0; |
| 391 |
|
| 392 |
if (! $string) { |
| 393 |
$$self{'err'} = '[parse_time] Empty time string'; |
| 394 |
return 1; |
| 395 |
} |
| 396 |
|
| •397 |
if (length($string) > $MAXLENGTH) { |
| 398 |
$$self{'err'} = '[parse_time] Time string too long'; |
| 399 |
return 1; |
| 400 |
} |
| 401 |
|
| 402 |
my($y,$m,$d,$h,$mn,$s); |
| 403 |
|
| 404 |
if ($$self{'err'}) { |
| 405 |
$self->_init(); |
| 406 |
} |
| 407 |
if ($$self{'data'}{'set'}) { |
| 408 |
($y,$m,$d,$h,$mn,$s) = @{ $$self{'data'}{'date'} }; |
| 409 |
} else { |
| 410 |
my $dmt = $$self{'tz'}; |
| 411 |
($y,$m,$d,$h,$mn,$s) = $dmt->_now('now',$noupdate); |
| 412 |
$noupdate = 1; |
| 413 |
} |
| 414 |
my($tzstring,$zone,$abb,$off); |
| 415 |
|
| 416 |
($h,$mn,$s,$tzstring,$zone,$abb,$off) = |
| 417 |
$self->_parse_time('parse_time',$string,\$noupdate,%opts); |
| 418 |
|
| 419 |
return 1 if ($$self{'err'}); |
| 420 |
|
| 421 |
$$self{'data'}{'set'} = 2; |
| 422 |
return $self->_parse_check('parse_time','', |
| 423 |
$y,$m,$d,$h,$mn,$s,'',$tzstring,$zone,$abb,$off); |
| 424 |
} |
| 425 |
|
| 426 |
sub parse_date { |
| 427 |
my($self,$string,@opts) = @_; |
| 428 |
my %opts = map { $_,1 } @opts; |
| 429 |
my $noupdate = 0; |
| 430 |
|
| 431 |
if (! $string) { |
| 432 |
$$self{'err'} = '[parse_date] Empty date string'; |
| 433 |
return 1; |
| 434 |
} |
| 435 |
|
| 436 |
my $dmt = $$self{'tz'}; |
| 437 |
my $dmb = $$dmt{'base'}; |
| 438 |
my($y,$m,$d,$h,$mn,$s); |
| 439 |
|
| 440 |
if ($$self{'err'}) { |
| 441 |
$self->_init(); |
| 442 |
} |
| 443 |
if ($$self{'data'}{'set'}) { |
| 444 |
($y,$m,$d,$h,$mn,$s) = @{ $$self{'data'}{'date'} }; |
| 445 |
} else { |
| 446 |
($h,$mn,$s) = (0,0,0); |
| 447 |
} |
| 448 |
|
| 449 |
# Put parse in a simple loop for an easy exit. |
| 450 |
my($done,@tmp,$dow); |
| 451 |
PARSE: |
| 452 |
{ |
| 453 |
|
| 454 |
# Parse ISO 8601 dates now |
| 455 |
|
| 456 |
unless (exists $opts{'noiso8601'}) { |
| 457 |
($done,@tmp) = $self->_parse_date_iso8601($string,\$noupdate); |
| 458 |
if ($done) { |
| 459 |
($y,$m,$d) = @tmp; |
| 460 |
last PARSE; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
(@tmp) = $self->_parse_date($string,undef,\$noupdate,%opts); |
| 465 |
if (@tmp) { |
| 466 |
($y,$m,$d,$dow) = @tmp; |
| 467 |
last PARSE; |
| 468 |
} |
| 469 |
|
| 470 |
$$self{'err'} = '[parse_date] Invalid date string'; |
| 471 |
return 1; |
| 472 |
} |
| 473 |
|
| 474 |
return 1 if ($$self{'err'}); |
| 475 |
|
| 476 |
$y = $dmt->_fix_year($y); |
| 477 |
|
| 478 |
$$self{'data'}{'set'} = 2; |
| 479 |
return $self->_parse_check('parse_date','',$y,$m,$d,$h,$mn,$s,$dow); |
| 480 |
} |
| 481 |
|
| 482 |
sub _parse_date { |
| 483 |
my($self,$string,$dow,$noupdate,%opts) = @_; |
| 484 |
|
| 485 |
# There's lots of ways that commas may be included. Remove |
| 486 |
# them. |
| 487 |
# |
| 488 |
# Also remove some words we should ignore. |
| 489 |
|
| 490 |
$string =~ s/,/ /g; |
| 491 |
|
| 492 |
my $dmt = $$self{'tz'}; |
| 493 |
my $dmb = $$dmt{'base'}; |
| 494 |
my $ign = (exists $$dmb{'data'}{'rx'}{'other'}{'ignore'} ? |
| 495 |
$$dmb{'data'}{'rx'}{'other'}{'ignore'} : |
| 496 |
$self->_other_rx('ignore')); |
| 497 |
$string =~ s/$ign/ /g; |
| 498 |
my $of = $+{'of'}; |
| 499 |
|
| 500 |
$string =~ s/\s*$//; |
| 501 |
return () if (! $string); |
| 502 |
|
| 503 |
my($done,$y,$m,$d,@tmp); |
| 504 |
|
| 505 |
# Put parse in a simple loop for an easy exit. |
| 506 |
PARSE: |
| 507 |
{ |
| 508 |
|
| 509 |
# Parse (and remove) the day of week. Also, handle the simple DoW |
| 510 |
# formats. |
| 511 |
|
| 512 |
unless (exists $opts{'nodow'}) { |
| 513 |
if (! defined($dow)) { |
| 514 |
($done,@tmp) = $self->_parse_dow($string,$noupdate); |
| 515 |
if (@tmp) { |
| 516 |
if ($done) { |
| 517 |
($y,$m,$d) = @tmp; |
| 518 |
last PARSE; |
| 519 |
} else { |
| 520 |
($string,$dow) = @tmp; |
| 521 |
} |
| 522 |
} |
| 523 |
$dow = 0 if (! $dow); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
# Parse common dates |
| 528 |
|
| 529 |
unless (exists $opts{'nocommon'}) { |
| 530 |
(@tmp) = $self->_parse_date_common($string,$noupdate); |
| 531 |
if (@tmp) { |
| 532 |
($y,$m,$d) = @tmp; |
| 533 |
last PARSE; |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
# Parse less common dates |
| 538 |
|
| 539 |
unless (exists $opts{'noother'}) { |
| 540 |
(@tmp) = $self->_parse_date_other($string,$dow,$of,$noupdate); |
| 541 |
if (@tmp) { |
| 542 |
($y,$m,$d,$dow) = @tmp; |
| 543 |
last PARSE; |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
# Parse truncated dates |
| 548 |
|
| 549 |
if (! $dow && ! $of) { |
| 550 |
(@tmp) = $self->_parse_date_truncated($string,$noupdate); |
| 551 |
if (@tmp) { |
| 552 |
($y,$m,$d,$dow) = @tmp; |
| 553 |
last PARSE; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
return (); |
| 558 |
} |
| 559 |
|
| 560 |
return($y,$m,$d,$dow); |
| 561 |
} |
| 562 |
|
| 563 |
sub parse_format { |
| 564 |
my($self,$format,$string) = @_; |
| 565 |
$self->_init(); |
| 566 |
my $noupdate = 0; |
| 567 |
|
| 568 |
if (! $string) { |
| 569 |
$$self{'err'} = '[parse_format] Empty date string'; |
| 570 |
return 1; |
| 571 |
} |
| 572 |
|
| 573 |
my $dmt = $$self{'tz'}; |
| 574 |
my $dmb = $$dmt{'base'}; |
| 575 |
|
| 576 |
my($err,$re) = $self->_format_regexp($format); |
| 577 |
return $err if ($err); |
| 578 |
return 1 if ($string !~ $re); |
| 579 |
|
| 580 |
my($y,$m,$d,$h,$mn,$s, |
| 581 |
$mon_name,$mon_abb,$dow_name,$dow_abb,$dow_char,$dow_num, |
| 582 |
$doy,$nth,$ampm,$epochs,$epocho, |
| 583 |
$tzstring,$off,$abb,$zone, |
| 584 |
$g,$w,$l,$u) = |
| 585 |
@+{qw(y m d h mn s |
| 586 |
mon_name mon_abb dow_name dow_abb dow_char dow_num doy |
| 587 |
nth ampm epochs epocho tzstring off abb zone g w l u)}; |
| 588 |
|
| 589 |
while (1) { |
| 590 |
# Get y/m/d/h/mn/s from: |
| 591 |
# $epochs,$epocho |
| 592 |
|
| 593 |
if (defined($epochs)) { |
| 594 |
($y,$m,$d,$h,$mn,$s) = @{ $dmb->secs_since_1970($epochs) }; |
| 595 |
my $z; |
| 596 |
if ($zone) { |
| 597 |
$z = $dmt->_zone($zone); |
| 598 |
return 'Invalid zone' if (! $z); |
| 599 |
} elsif ($abb || $off) { |
| 600 |
my $offset = $dmb->_delta_convert('offset',$off); |
| 601 |
$z = $dmt->__zone([],$offset,'',$abb,''); |
| 602 |
if (! $z) { |
| 603 |
$z = $dmt->__zone([],$offset,$abb,'',''); |
| 604 |
} |
| 605 |
return 'Invalid zone' if (! $z); |
| 606 |
} else { |
| 607 |
$z = $dmt->_now('tz',$noupdate); |
| 608 |
$noupdate = 1; |
| 609 |
} |
| 610 |
my($err,$date) = $dmt->convert_from_gmt([$y,$m,$d,$h,$mn,$s],$z); |
| 611 |
($y,$m,$d,$h,$mn,$s) = @$date; |
| 612 |
last; |
| 613 |
} |
| 614 |
|
| 615 |
if (defined($epocho)) { |
| 616 |
($y,$m,$d,$h,$mn,$s) = @{ $dmb->secs_since_1970($epocho) }; |
| 617 |
last; |
| 618 |
} |
| 619 |
|
| 620 |
# Get y/m/d from: |
| 621 |
# $y,$m,$d, |
| 622 |
# $mon_name,$mon_abb |
| 623 |
# $doy,$nth |
| 624 |
# $g/$w,$l/$u |
| 625 |
|
| 626 |
if ($mon_name) { |
| 627 |
$m = $$dmb{'data'}{'wordmatch'}{'month_name'}{lc($mon_name)}; |
| 628 |
} elsif ($mon_abb) { |
| 629 |
$m = $$dmb{'data'}{'wordmatch'}{'month_abb'}{lc($mon_abb)}; |
| 630 |
} |
| 631 |
|
| 632 |
if ($nth) { |
| 633 |
$d = $$dmb{'data'}{'wordmatch'}{'nth'}{lc($nth)}; |
| 634 |
} |
| 635 |
|
| 636 |
if ($doy) { |
| 637 |
$y = $dmt->_now('y',$noupdate) if (! $y); |
| 638 |
$noupdate = 1; |
| 639 |
($y,$m,$d) = @{ $dmb->day_of_year($y,$doy) }; |
| 640 |
|
| 641 |
} elsif ($g) { |
| 642 |
$y = $dmt->_now('y',$noupdate) if (! $y); |
| 643 |
$noupdate = 1; |
| 644 |
|
| 645 |
my $currfirst = $dmb->_config('firstday'); |
| 646 |
$dmb->config('firstday',1); |
| 647 |
($y,$m,$d) = @{ $dmb->week_of_year($g,$w) }; |
| 648 |
$dmb->config('firstday',$currfirst); |
| 649 |
|
| 650 |
} elsif ($l) { |
| 651 |
$y = $dmt->_now('y',$noupdate) if (! $y); |
| 652 |
$noupdate = 1; |
| 653 |
|
| 654 |
my $currfirst = $dmb->_config('firstday'); |
| 655 |
$dmb->config('firstday',7); |
| 656 |
($y,$m,$d) = @{ $dmb->week_of_year($l,$u) }; |
| 657 |
$dmb->config('firstday',$currfirst); |
| 658 |
|
| 659 |
} elsif ($m) { |
| 660 |
($y,$m,$d) = $self->_def_date($y,$m,$d,\$noupdate); |
| 661 |
} |
| 662 |
|
| 663 |
# Get h/mn/s from: |
| 664 |
# $h,$mn,$s,$ampm |
| 665 |
|
| 666 |
if (defined($h)) { |
| 667 |
($h,$mn,$s) = $self->_def_time($h,$mn,$s,\$noupdate); |
| 668 |
} |
| 669 |
|
| 670 |
if ($ampm) { |
| 671 |
if ($$dmb{'data'}{'wordmatch'}{'ampm'}{lc($ampm)} == 2) { |
| 672 |
# pm times |
| 673 |
$h+=12 unless ($h==12); |
| 674 |
} else { |
| 675 |
# am times |
| 676 |
$h=0 if ($h==12); |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
# Get dow from: |
| 681 |
# $dow_name,$dow_abb,$dow_char,$dow_num |
| 682 |
|
| 683 |
if ($dow_name) { |
| 684 |
$dow_num = $$dmb{'data'}{'wordmatch'}{'day_name'}{lc($dow_name)}; |
| 685 |
} elsif ($dow_abb) { |
| 686 |
$dow_num = $$dmb{'data'}{'wordmatch'}{'day_abb'}{lc($dow_abb)}; |
| 687 |
} elsif ($dow_char) { |
| 688 |
$dow_num = $$dmb{'data'}{'wordmatch'}{'day_char'}{lc($dow_char)}; |
| 689 |
} |
| 690 |
|
| 691 |
last; |
| 692 |
} |
| 693 |
|
| 694 |
if (! $m) { |
| 695 |
($y,$m,$d) = $dmt->_now('now',$noupdate); |
| 696 |
$noupdate = 1; |
| 697 |
} |
| 698 |
if (! defined($h)) { |
| 699 |
($h,$mn,$s) = (0,0,0); |
| 700 |
} |
| 701 |
|
| 702 |
$$self{'data'}{'set'} = 2; |
| 703 |
$err = $self->_parse_check('parse_format',$string, |
| 704 |
$y,$m,$d,$h,$mn,$s,$dow_num, |
| 705 |
$tzstring,$zone,$abb,$off); |
| 706 |
|
| 707 |
if (wantarray) { |
| 708 |
my %tmp = %{ dclone(\%+) }; |
| 709 |
return ($err,%tmp); |
| 710 |
} |
| 711 |
return $err; |
| 712 |
} |
| 713 |
|
| 714 |
BEGIN { |
| 715 |
my %y_form = map { $_,1 } qw( Y y s o G L ); |
| 716 |
my %m_form = map { $_,1 } qw( m f b h B j s o W U ); |
| 717 |
my %d_form = map { $_,1 } qw( j d e E s o W U ); |
| 718 |
my %h_form = map { $_,1 } qw( H I k i s o ); |
| 719 |
my %mn_form = map { $_,1 } qw( M s o ); |
| 720 |
my %s_form = map { $_,1 } qw( S s o ); |
| 721 |
|
| 722 |
my %dow_form = map { $_,1 } qw( v a A w ); |
| 723 |
my %am_form = map { $_,1 } qw( p s o ); |
| 724 |
my %z_form = map { $_,1 } qw( Z z N ); |
| 725 |
my %mon_form = map { $_,1 } qw( b h B ); |
| 726 |
my %day_form = map { $_,1 } qw( v a A ); |
| 727 |
|
| 728 |
sub _format_regexp { |
| 729 |
my($self,$format) = @_; |
| 730 |
my $dmt = $$self{'tz'}; |
| 731 |
my $dmb = $$dmt{'base'}; |
| 732 |
|
| 733 |
if (exists $$dmb{'data'}{'format'}{$format}) { |
| 734 |
return @{ $$dmb{'data'}{'format'}{$format} }; |
| 735 |
} |
| 736 |
|
| 737 |
my $re; |
| 738 |
my $err; |
| 739 |
my($y,$m,$d,$h,$mn,$s) = (0,0,0,0,0,0); |
| 740 |
my($dow,$ampm,$zone,$G,$W,$L,$U) = (0,0,0,0,0,0,0); |
| 741 |
|
| 742 |
while ($format) { |
| 743 |
last if ($format eq '%'); |
| 744 |
|
| 745 |
if ($format =~ s/^([^%]+)//) { |
| 746 |
$re .= $1; |
| 747 |
next; |
| 748 |
} |
| 749 |
|
| 750 |
$format =~ s/^%(.)//; |
| 751 |
my $f = $1; |
| 752 |
|
| 753 |
if (exists $y_form{$f}) { |
| 754 |
if ($y) { |
| 755 |
$err = 'Year specified multiple times'; |
| 756 |
last; |
| 757 |
} |
| 758 |
$y = 1; |
| 759 |
} |
| 760 |
|
| 761 |
if (exists $m_form{$f}) { |
| 762 |
if ($m) { |
| 763 |
$err = 'Month specified multiple times'; |
| 764 |
last; |
| 765 |
} |
| 766 |
$m = 1; |
| 767 |
} |
| 768 |
|
| 769 |
if (exists $d_form{$f}) { |
| 770 |
if ($d) { |
| 771 |
$err = 'Day specified multiple times'; |
| 772 |
last; |
| 773 |
} |
| 774 |
$d = 1; |
| 775 |
} |
| 776 |
|
| 777 |
if (exists $h_form{$f}) { |
| 778 |
if ($h) { |
| 779 |
$err = 'Hour specified multiple times'; |
| 780 |
last; |
| 781 |
} |
| 782 |
$h = 1; |
| 783 |
} |
| 784 |
|
| 785 |
if (exists $mn_form{$f}) { |
| 786 |
if ($mn) { |
| 787 |
$err = 'Minutes specified multiple times'; |
| 788 |
last; |
| 789 |
} |
| 790 |
$mn = 1; |
| 791 |
} |
| 792 |
|
| 793 |
if (exists $s_form{$f}) { |
| 794 |
if ($s) { |
| 795 |
$err = 'Seconds specified multiple times'; |
| 796 |
last; |
| 797 |
} |
| 798 |
$s = 1; |
| 799 |
} |
| 800 |
|
| 801 |
if (exists $dow_form{$f}) { |
| 802 |
if ($dow) { |
| 803 |
$err = 'Day-of-week specified multiple times'; |
| 804 |
last; |
| 805 |
} |
| 806 |
$dow = 1; |
| 807 |
} |
| 808 |
|
| 809 |
if (exists $am_form{$f}) { |
| 810 |
if ($ampm) { |
| 811 |
$err = 'AM/PM specified multiple times'; |
| 812 |
last; |
| 813 |
} |
| 814 |
$ampm = 1; |
| 815 |
} |
| 816 |
|
| 817 |
if (exists $z_form{$f}) { |
| 818 |
if ($zone) { |
| 819 |
$err = 'Zone specified multiple times'; |
| 820 |
last; |
| 821 |
} |
| 822 |
$zone = 1; |
| 823 |
} |
| 824 |
|
| 825 |
if ($f eq 'G') { |
| 826 |
if ($G) { |
| 827 |
$err = 'G specified multiple times'; |
| 828 |
last; |
| 829 |
} |
| 830 |
$G = 1; |
| 831 |
|
| 832 |
} elsif ($f eq 'W') { |
| 833 |
if ($W) { |
| 834 |
$err = 'W specified multiple times'; |
| 835 |
last; |
| 836 |
} |
| 837 |
$W = 1; |
| 838 |
|
| 839 |
} elsif ($f eq 'L') { |
| 840 |
if ($L) { |
| 841 |
$err = 'L specified multiple times'; |
| 842 |
last; |
| 843 |
} |
| 844 |
$L = 1; |
| 845 |
|
| 846 |
} elsif ($f eq 'U') { |
| 847 |
if ($U) { |
| 848 |
$err = 'U specified multiple times'; |
| 849 |
last; |
| 850 |
} |
| 851 |
$U = 1; |
| 852 |
} |
| 853 |
|
| 854 |
### |
| 855 |
|
| 856 |
if ($f eq 'Y') { |
| •857 |
$re .= '(?<y>[0-9]{4})'; |
| 858 |
|
| 859 |
} elsif ($f eq 'y') { |
| •860 |
$re .= '(?<y>[0-9]{2})'; |
| 861 |
|
| 862 |
} elsif ($f eq 'm') { |
| •863 |
$re .= '(?<m>[0-9]{2})'; |
| 864 |
|
| 865 |
} elsif ($f eq 'f') { |
| •866 |
$re .= '(?:(?<m>[0-9]{2})| ?(?<m>[0-9]))'; |
| 867 |
|
| 868 |
} elsif (exists $mon_form{$f}) { |
| 869 |
my $abb = $$dmb{'data'}{'rx'}{'month_abb'}[0]; |
| 870 |
my $nam = $$dmb{'data'}{'rx'}{'month_name'}[0]; |
| 871 |
$re .= "(?:(?<mon_name>$nam)|(?<mon_abb>$abb))"; |
| 872 |
|
| 873 |
} elsif ($f eq 'j') { |
| •874 |
$re .= '(?<doy>[0-9]{3})'; |
| 875 |
|
| 876 |
} elsif ($f eq 'd') { |
| •877 |
$re .= '(?<d>[0-9]{2})'; |
| 878 |
|
| 879 |
} elsif ($f eq 'e') { |
| •880 |
$re .= '(?:(?<d>[0-9]{2})| ?(?<d>[0-9]))'; |
| 881 |
|
| 882 |
} elsif (exists $day_form{$f}) { |
| 883 |
my $abb = $$dmb{'data'}{'rx'}{'day_abb'}[0]; |
| 884 |
my $name = $$dmb{'data'}{'rx'}{'day_name'}[0]; |
| 885 |
my $char = $$dmb{'data'}{'rx'}{'day_char'}[0]; |
| 886 |
$re .= "(?:(?<dow_name>$name)|(?<dow_abb>$abb)|(?<dow_char>$char))"; |
| 887 |
|
| 888 |
} elsif ($f eq 'w') { |
| 889 |
$re .= '(?<dow_num>[1-7])'; |
| 890 |
|
| 891 |
} elsif ($f eq 'E') { |
| 892 |
my $nth = $$dmb{'data'}{'rx'}{'nth'}[0]; |
| 893 |
$re .= "(?<nth>$nth)" |
| 894 |
|
| 895 |
} elsif ($f eq 'H' || $f eq 'I') { |
| •896 |
$re .= '(?<h>[0-9]{2})'; |
| 897 |
|
| 898 |
} elsif ($f eq 'k' || $f eq 'i') { |
| •899 |
$re .= '(?:(?<h>[0-9]{2})| ?(?<h>[0-9]))'; |
| 900 |
|
| 901 |
} elsif ($f eq 'p') { |
| 902 |
my $ampm = $$dmb{data}{rx}{ampm}[0]; |
| 903 |
$re .= "(?<ampm>$ampm)"; |
| 904 |
|
| 905 |
} elsif ($f eq 'M') { |
| •906 |
$re .= '(?<mn>[0-9]{2})'; |
| 907 |
|
| 908 |
} elsif ($f eq 'S') { |
| •909 |
$re .= '(?<s>[0-9]{2})'; |
| 910 |
|
| 911 |
} elsif (exists $z_form{$f}) { |
| 912 |
$re .= $dmt->_zrx('zrx'); |
| 913 |
|
| 914 |
} elsif ($f eq 's') { |
| •915 |
$re .= '(?<epochs>[0-9]+)'; |
| 916 |
|
| 917 |
} elsif ($f eq 'o') { |
| •918 |
$re .= '(?<epocho>[0-9]+)'; |
| 919 |
|
| 920 |
} elsif ($f eq 'G') { |
| •921 |
$re .= '(?<g>[0-9]{4})'; |
| 922 |
|
| 923 |
} elsif ($f eq 'W') { |
| •924 |
$re .= '(?<w>[0-9]{2})'; |
| 925 |
|
| 926 |
} elsif ($f eq 'L') { |
| •927 |
$re .= '(?<l>[0-9]{4})'; |
| 928 |
|
| 929 |
} elsif ($f eq 'U') { |
| •930 |
$re .= '(?<u>[0-9]{2})'; |
| 931 |
|
| 932 |
} elsif ($f eq 'c') { |
| 933 |
$format = '%a %b %e %H:%M:%S %Y' . $format; |
| 934 |
|
| 935 |
} elsif ($f eq 'C' || $f eq 'u') { |
| 936 |
$format = '%a %b %e %H:%M:%S %Z %Y' . $format; |
| 937 |
|
| 938 |
} elsif ($f eq 'g') { |
| 939 |
$format = '%a, %d %b %Y %H:%M:%S %Z' . $format; |
| 940 |
|
| 941 |
} elsif ($f eq 'D') { |
| 942 |
$format = '%m/%d/%y' . $format; |
| 943 |
|
| 944 |
} elsif ($f eq 'r') { |
| 945 |
$format = '%I:%M:%S %p' . $format; |
| 946 |
|
| 947 |
} elsif ($f eq 'R') { |
| 948 |
$format = '%H:%M' . $format; |
| 949 |
|
| 950 |
} elsif ($f eq 'T' || $f eq 'X') { |
| 951 |
$format = '%H:%M:%S' . $format; |
| 952 |
|
| 953 |
} elsif ($f eq 'V') { |
| 954 |
$format = '%m%d%H%M%y' . $format; |
| 955 |
|
| 956 |
} elsif ($f eq 'Q') { |
| 957 |
$format = '%Y%m%d' . $format; |
| 958 |
|
| 959 |
} elsif ($f eq 'q') { |
| 960 |
$format = '%Y%m%d%H%M%S' . $format; |
| 961 |
|
| 962 |
} elsif ($f eq 'P') { |
| 963 |
$format = '%Y%m%d%H:%M:%S' . $format; |
| 964 |
|
| 965 |
} elsif ($f eq 'O') { |
| 966 |
$format = '%Y\\-%m\\-%dT%H:%M:%S' . $format; |
| 967 |
|
| 968 |
} elsif ($f eq 'F') { |
| 969 |
$format = '%A, %B %e, %Y' . $format; |
| 970 |
|
| 971 |
} elsif ($f eq 'K') { |
| 972 |
$format = '%Y-%j' . $format; |
| 973 |
|
| 974 |
} elsif ($f eq 'J') { |
| 975 |
$format = '%G-W%W-%w' . $format; |
| 976 |
|
| 977 |
} elsif ($f eq 'x') { |
| 978 |
if ($dmb->_config('dateformat') eq 'US') { |
| 979 |
$format = '%m/%d/%y' . $format; |
| 980 |
} else { |
| 981 |
$format = '%d/%m/%y' . $format; |
| 982 |
} |
| 983 |
|
| 984 |
} elsif ($f eq 't') { |
| 985 |
$re .= "\t"; |
| 986 |
|
| 987 |
} elsif ($f eq '%') { |
| 988 |
$re .= '%'; |
| 989 |
|
| 990 |
} elsif ($f eq '+') { |
| 991 |
$re .= '\\+'; |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
if ($m != $d) { |
| 996 |
$err = 'Date not fully specified'; |
| 997 |
} elsif ( ($h || $mn || $s) && (! $h || ! $mn) ) { |
| 998 |
$err = 'Time not fully specified'; |
| 999 |
} elsif ($ampm && ! $h) { |
| 1000 |
$err = 'Time not fully specified'; |
| 1001 |
} elsif ($G != $W) { |
| 1002 |
$err = 'G/W must both be specified'; |
| 1003 |
} elsif ($L != $U) { |
| 1004 |
$err = 'L/U must both be specified'; |
| 1005 |
} |
| 1006 |
|
| 1007 |
if ($err) { |
| 1008 |
$$dmb{'data'}{'format'}{$format} = [$err]; |
| 1009 |
return ($err); |
| 1010 |
} |
| 1011 |
|
| 1012 |
$$dmb{'data'}{'format'}{$format} = [0, qr/$re/i]; |
| 1013 |
return @{ $$dmb{'data'}{'format'}{$format} }; |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
# This returns 1 if a given rule is set in the language _special_rules. |
| 1018 |
# |
| 1019 |
sub _parse_rule { |
| 1020 |
my($self,$rule) = @_; |
| 1021 |
|
| 1022 |
my $dmt = $$self{'tz'}; |
| 1023 |
my $dmb = $$dmt{'base'}; |
| 1024 |
|
| 1025 |
if (exists $$dmb{'data'}{'lang'}{'_special_rules'} && |
| 1026 |
exists $$dmb{'data'}{'lang'}{'_special_rules'}{$rule}) { |
| 1027 |
return $$dmb{'data'}{'lang'}{'_special_rules'}{$rule}; |
| 1028 |
} |
| 1029 |
return 0; |
| 1030 |
} |
| 1031 |
|
| 1032 |
######################################################################## |
| 1033 |
# DATE FORMATS |
| 1034 |
######################################################################## |
| 1035 |
|
| 1036 |
sub _parse_check { |
| 1037 |
my($self,$caller,$instring, |
| 1038 |
$y,$m,$d,$h,$mn,$s,$dow,$tzstring,$zone,$abb,$off) = @_; |
| 1039 |
my $dmt = $$self{'tz'}; |
| 1040 |
my $dmb = $$dmt{'base'}; |
| 1041 |
|
| 1042 |
# Check day_of_week for validity BEFORE converting 24:00:00 to the |
| 1043 |
# next day |
| 1044 |
|
| 1045 |
if ($dow) { |
| 1046 |
my $tmp = $dmb->day_of_week([$y,$m,$d]); |
| 1047 |
if ($tmp != $dow) { |
| 1048 |
$$self{'err'} = "[$caller] Day of week invalid"; |
| 1049 |
return 1; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
# Handle 24:00:00 times. |
| 1054 |
|
| 1055 |
if ($h == 24) { |
| 1056 |
($h,$mn,$s) = (0,0,0); |
| 1057 |
($y,$m,$d) = @{ $dmb->calc_date_days([$y,$m,$d],1) }; |
| 1058 |
} |
| 1059 |
|
| 1060 |
if (! $dmb->check([$y,$m,$d,$h,$mn,$s])) { |
| 1061 |
$$self{'err'} = "[$caller] Invalid date"; |
| 1062 |
return 1; |
| 1063 |
} |
| 1064 |
my $date = [$y+0,$m+0,$d+0,$h+0,$mn+0,$s+0]; |
| 1065 |
|
| 1066 |
# |
| 1067 |
# We need to check that the date is valid in a timezone. The |
| 1068 |
# timezone may be referred to with $zone, $abb, or $off, and |
| 1069 |
# unfortunately, $abb MAY be the name of an abbrevation OR a |
| 1070 |
# zone in a few cases. |
| 1071 |
# |
| 1072 |
|
| 1073 |
my $zonename; |
| 1074 |
my $abbrev = (defined $abb ? lc($abb) : ''); |
| 1075 |
my $offset = (defined $off ? $dmb->_delta_convert('offset',$off) : ''); |
| 1076 |
my @tmp; |
| 1077 |
|
| 1078 |
if (defined($zone)) { |
| 1079 |
$zonename = $dmt->_zone($zone); |
| 1080 |
if ($zonename) { |
| 1081 |
@tmp = $self->__parse_check($date,$zonename,$off,$abb); |
| 1082 |
} |
| 1083 |
|
| 1084 |
} elsif (defined($abb) || defined($off)) { |
| 1085 |
|
| 1086 |
$zonename = $dmt->__zone($date,$offset,'',$abbrev,''); |
| 1087 |
if ($zonename) { |
| 1088 |
@tmp = $self->__parse_check($date,$zonename,$off,$abb); |
| 1089 |
} |
| 1090 |
|
| 1091 |
if (! @tmp && defined($abb)) { |
| 1092 |
my $tmp = $dmt->_zone($abb); |
| 1093 |
if ($tmp) { |
| 1094 |
$zonename = $tmp; |
| 1095 |
@tmp = $self->__parse_check($date,$zonename,$off,undef); |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
} else { |
| 1100 |
$zonename = $dmt->_now('tz'); |
| 1101 |
if ($zonename) { |
| 1102 |
@tmp = $self->__parse_check($date,$zonename,$off,$abb); |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
if (! $zonename) { |
| 1107 |
if (defined($zone)) { |
| 1108 |
$$self{'err'} = "[$caller] Unable to determine timezone: $zone"; |
| 1109 |
} else { |
| 1110 |
$$self{'err'} = "[$caller] Unable to determine timezone"; |
| 1111 |
} |
| 1112 |
return 1; |
| 1113 |
} |
| 1114 |
|
| 1115 |
if (! @tmp) { |
| 1116 |
$$self{'err'} = "[$caller] Invalid date in timezone"; |
| 1117 |
return 1; |
| 1118 |
} |
| 1119 |
|
| 1120 |
# Store the date |
| 1121 |
|
| 1122 |
my($a,$o,$isdst) = @tmp; |
| 1123 |
|
| 1124 |
$self->set('zdate',$zonename,$date,$isdst); |
| 1125 |
return 1 if ($$self{'err'}); |
| 1126 |
|
| 1127 |
$$self{'data'}{'in'} = $instring; |
| 1128 |
$$self{'data'}{'zin'} = $zone if (defined($zone)); |
| 1129 |
|
| 1130 |
return 0; |
| 1131 |
} |
| 1132 |
|
| 1133 |
sub __parse_check { |
| 1134 |
my($self,$date,$zonename,$off,$abb) = @_; |
| 1135 |
my $dmt = $$self{'tz'}; |
| 1136 |
my $dmb = $$dmt{'base'}; |
| 1137 |
|
| 1138 |
if (defined ($off)) { |
| 1139 |
$off = $dmb->split('offset',$off); |
| 1140 |
} |
| 1141 |
|
| 1142 |
foreach my $isdst (0,1) { |
| 1143 |
my $per = $dmt->date_period($date,$zonename,1,$isdst); |
| 1144 |
next if (! $per); |
| 1145 |
my $a = $$per[4]; |
| 1146 |
my $o = $$per[3]; |
| 1147 |
|
| 1148 |
# If $abb is defined, it must match. |
| 1149 |
next if (defined $abb && lc($a) ne lc($abb)); |
| 1150 |
|
| 1151 |
# If $off is defined, it must match. |
| 1152 |
if (defined ($off)) { |
| 1153 |
next if ($$off[0] != $$o[0] || |
| 1154 |
$$off[1] != $$o[1] || |
| 1155 |
$$off[2] != $$o[2]); |
| 1156 |
} |
| 1157 |
|
| 1158 |
return ($a,$o,$isdst); |
| 1159 |
} |
| 1160 |
return (); |
| 1161 |
} |
| 1162 |
|
| 1163 |
# Set up the regular expressions for ISO 8601 parsing. Returns the |
| 1164 |
# requested regexp. $rx can be: |
| 1165 |
# cdate : regular expression for a complete date |
| 1166 |
# tdate : regular expression for a truncated date |
| 1167 |
# ctime : regular expression for a complete time |
| 1168 |
# ttime : regular expression for a truncated time |
| 1169 |
# date : regular expression for a date only |
| 1170 |
# time : regular expression for a time only |
| 1171 |
# UNDEF : regular expression for a valid date and/or time |
| 1172 |
# |
| 1173 |
# Date matches are: |
| 1174 |
# y m d doy w dow yod c |
| 1175 |
# Time matches are: |
| 1176 |
# h h24 mn s fh fm |
| 1177 |
# |
| 1178 |
sub _iso8601_rx { |
| 1179 |
my($self,$rx) = @_; |
| 1180 |
my $dmt = $$self{'tz'}; |
| 1181 |
my $dmb = $$dmt{'base'}; |
| 1182 |
|
| 1183 |
return $$dmb{'data'}{'rx'}{'iso'}{$rx} |
| 1184 |
if (exists $$dmb{'data'}{'rx'}{'iso'}{$rx}); |
| 1185 |
|
| 1186 |
if ($rx eq 'cdate' || $rx eq 'tdate') { |
| 1187 |
|
| •1188 |
my $y4 = '(?<y>[0-9]{4})'; |
| 1189 |
my $y2 = '(?<y>[0-9]{2})'; |
| 1190 |
my $m = '(?<m>0[1-9]|1[0-2])'; |
| 1191 |
my $d = '(?<d>0[1-9]|[12][0-9]|3[01])'; |
| 1192 |
my $doy = '(?<doy>00[1-9]|0[1-9][0-9]|[1-2][0-9][0-9]|3[0-5][0-9]|36[0-6])'; |
| 1193 |
my $w = '(?<w>0[1-9]|[1-4][0-9]|5[0-3])'; |
| 1194 |
my $dow = '(?<dow>[1-7])'; |
| •1195 |
my $yod = '(?<yod>[0-9])'; |
| 1196 |
my $cc = '(?<c>[0-9]{2})'; |
| 1197 |
|
| 1198 |
my @cdaterx = |
| 1199 |
( |
| 1200 |
"${y4}${m}${d}", # CCYYMMDD |
| 1201 |
"${y4}\\-${m}\\-${d}", # CCYY-MM-DD |
| 1202 |
"\\-${y2}${m}${d}", # -YYMMDD |
| 1203 |
"\\-${y2}\\-${m}\\-${d}", # -YY-MM-DD |
| 1204 |
"\\-?${y2}${m}${d}", # YYMMDD |
| 1205 |
"\\-?${y2}\\-${m}\\-${d}", # YY-MM-DD |
| 1206 |
"\\-\\-${m}\\-?${d}", # --MM-DD --MMDD |
| 1207 |
"\\-\\-\\-${d}", # ---DD |
| 1208 |
|
| 1209 |
"${y4}\\-?${doy}", # CCYY-DoY CCYYDoY |
| 1210 |
"\\-?${y2}\\-?${doy}", # YY-DoY -YY-DoY |
| 1211 |
# YYDoY -YYDoY |
| 1212 |
"\\-${doy}", # -DoY |
| 1213 |
|
| 1214 |
"${y4}W${w}${dow}", # CCYYWwwD |
| 1215 |
"${y4}\\-W${w}\\-${dow}", # CCYY-Www-D |
| 1216 |
"\\-?${y2}W${w}${dow}", # YYWwwD -YYWwwD |
| 1217 |
"\\-?${y2}\\-W${w}\\-${dow}", # YY-Www-D -YY-Www-D |
| 1218 |
|
| 1219 |
"\\-?${yod}W${w}${dow}", # YWwwD -YWwwD |
| 1220 |
"\\-?${yod}\\-W${w}\\-${dow}", # Y-Www-D -Y-Www-D |
| 1221 |
"\\-W${w}\\-?${dow}", # -Www-D -WwwD |
| 1222 |
"\\-W\\-${dow}", # -W-D |
| 1223 |
"\\-\\-\\-${dow}", # ---D |
| 1224 |
); |
| 1225 |
my $cdaterx = join('|',@cdaterx); |
| 1226 |
$cdaterx = qr/(?:$cdaterx)/i; |
| 1227 |
|
| 1228 |
my @tdaterx = |
| 1229 |
( |
| 1230 |
"${y4}\\-${m}", # CCYY-MM |
| 1231 |
"${y4}", # CCYY |
| 1232 |
"\\-${y2}\\-?${m}", # -YY-MM -YYMM |
| 1233 |
"\\-${y2}", # -YY |
| 1234 |
"\\-\\-${m}", # --MM |
| 1235 |
|
| 1236 |
"${y4}\\-?W${w}", # CCYYWww CCYY-Www |
| 1237 |
"\\-?${y2}\\-?W${w}", # YY-Www YYWww |
| 1238 |
# -YY-Www -YYWww |
| 1239 |
"\\-?W${w}", # -Www Www |
| 1240 |
|
| 1241 |
"${cc}", # CC |
| 1242 |
); |
| 1243 |
my $tdaterx = join('|',@tdaterx); |
| 1244 |
$tdaterx = qr/(?:$tdaterx)/i; |
| 1245 |
|
| 1246 |
$$dmb{'data'}{'rx'}{'iso'}{'cdate'} = $cdaterx; |
| 1247 |
$$dmb{'data'}{'rx'}{'iso'}{'tdate'} = $tdaterx; |
| 1248 |
|
| 1249 |
} elsif ($rx eq 'ctime' || $rx eq 'ttime') { |
| 1250 |
|
| 1251 |
my $hh = '(?<h>[0-1][0-9]|2[0-3])'; |
| 1252 |
my $mn = '(?<mn>[0-5][0-9])'; |
| 1253 |
my $ss = '(?<s>[0-5][0-9])'; |
| 1254 |
my $h24a = '(?<h24>24(?::00){0,2})'; |
| 1255 |
my $h24b = '(?<h24>24(?:00){0,2})'; |
| 1256 |
my $h = '(?<h>[0-9])'; |
| 1257 |
|
| •1258 |
my $fh = '(?:[\.,](?<fh>[0-9]*))'; # fractional hours (keep) |
| 1259 |
my $fm = '(?:[\.,](?<fm>[0-9]*))'; # fractional seconds (keep) |
| 1260 |
my $fs = '(?:[\.,][0-9]*)'; # fractional hours (discard) |
| 1261 |
|
| 1262 |
my $zrx = $dmt->_zrx('zrx'); |
| 1263 |
|
| 1264 |
my @ctimerx = |
| 1265 |
( |
| 1266 |
"${hh}${mn}${ss}${fs}?", # HHMNSS[,S+] |
| 1267 |
"${hh}:${mn}:${ss}${fs}?", # HH:MN:SS[,S+] |
| 1268 |
"${hh}:?${mn}${fm}", # HH:MN,M+ HHMN,M+ |
| 1269 |
"${hh}${fh}", # HH,H+ |
| 1270 |
"\\-${mn}:?${ss}${fs}?", # -MN:SS[,S+] -MNSS[,S+] |
| 1271 |
"\\-${mn}${fm}", # -MN,M+ |
| 1272 |
"\\-\\-${ss}${fs}?", # --SS[,S+] |
| 1273 |
"${hh}:?${mn}", # HH:MN HHMN |
| 1274 |
"${h24a}", # 24:00:00 24:00 24 |
| 1275 |
"${h24b}", # 240000 2400 |
| 1276 |
"${h}:${mn}:${ss}${fs}?", # H:MN:SS[,S+] |
| 1277 |
"${h}:${mn}${fm}", # H:MN,M+ |
| 1278 |
); |
| 1279 |
my $ctimerx = join('|',@ctimerx); |
| 1280 |
$ctimerx = qr/(?:$ctimerx)(?:\s*$zrx)?/; |
| 1281 |
|
| 1282 |
my @ttimerx = |
| 1283 |
( |
| 1284 |
"${hh}", # HH |
| 1285 |
"\\-${mn}", # -MN |
| 1286 |
); |
| 1287 |
my $ttimerx = join('|',@ttimerx); |
| 1288 |
$ttimerx = qr/(?:$ttimerx)/; |
| 1289 |
|
| 1290 |
$$dmb{'data'}{'rx'}{'iso'}{'ctime'} = $ctimerx; |
| 1291 |
$$dmb{'data'}{'rx'}{'iso'}{'ttime'} = $ttimerx; |
| 1292 |
|
| 1293 |
} elsif ($rx eq 'date') { |
| 1294 |
|
| 1295 |
my $cdaterx = $self->_iso8601_rx('cdate'); |
| 1296 |
my $tdaterx = $self->_iso8601_rx('tdate'); |
| 1297 |
$$dmb{'data'}{'rx'}{'iso'}{'date'} = qr/(?:$cdaterx|$tdaterx)/; |
| 1298 |
|
| 1299 |
} elsif ($rx eq 'time') { |
| 1300 |
|
| 1301 |
my $ctimerx = $self->_iso8601_rx('ctime'); |
| 1302 |
my $ttimerx = $self->_iso8601_rx('ttime'); |
| 1303 |
$$dmb{'data'}{'rx'}{'iso'}{'time'} = qr/(?:$ctimerx|$ttimerx)/; |
| 1304 |
|
| 1305 |
} elsif ($rx eq 'fulldate') { |
| 1306 |
|
| 1307 |
# A parseable string contains: |
| 1308 |
# a complete date and complete time |
| 1309 |
# a complete date and truncated time |
| 1310 |
# a truncated date |
| 1311 |
# a complete time |
| 1312 |
# a truncated time |
| 1313 |
|
| 1314 |
# If the string contains both a time and date, they may be adjacent |
| 1315 |
# or separated by: |
| 1316 |
# whitespace |
| 1317 |
# T (which must be followed by a number) |
| 1318 |
# a dash |
| 1319 |
|
| 1320 |
my $cdaterx = $self->_iso8601_rx('cdate'); |
| 1321 |
my $tdaterx = $self->_iso8601_rx('tdate'); |
| 1322 |
my $ctimerx = $self->_iso8601_rx('ctime'); |
| 1323 |
my $ttimerx = $self->_iso8601_rx('ttime'); |
| 1324 |
|
| 1325 |
my $sep = qr/(?:T|\-|\s*)/i; |
| 1326 |
|
| 1327 |
my $daterx = qr/^\s*(?: $cdaterx(?:$sep(?:$ctimerx|$ttimerx))? | |
| 1328 |
$tdaterx | |
| 1329 |
$ctimerx | |
| 1330 |
$ttimerx |
| 1331 |
)\s*$/x; |
| 1332 |
|
| 1333 |
$$dmb{'data'}{'rx'}{'iso'}{'fulldate'} = $daterx; |
| 1334 |
} |
| 1335 |
|
| 1336 |
return $$dmb{'data'}{'rx'}{'iso'}{$rx}; |
| 1337 |
} |
| 1338 |
|
| 1339 |
sub _parse_datetime_iso8601 { |
| 1340 |
my($self,$string,$noupdate) = @_; |
| 1341 |
my $dmt = $$self{'tz'}; |
| 1342 |
my $dmb = $$dmt{'base'}; |
| 1343 |
my $daterx = $self->_iso8601_rx('fulldate'); |
| 1344 |
|
| 1345 |
my($y,$m,$d,$h,$mn,$s,$tzstring,$zone,$abb,$off); |
| 1346 |
my($doy,$dow,$yod,$c,$w,$fh,$fm,$h24); |
| 1347 |
|
| 1348 |
if ($string =~ $daterx) { |
| 1349 |
($y,$m,$d,$h,$mn,$s,$doy,$dow,$yod,$c,$w,$fh,$fm,$h24, |
| 1350 |
$tzstring,$zone,$abb,$off) = |
| 1351 |
@+{qw(y m d h mn s doy dow yod c w fh fm h24 tzstring zone abb off)}; |
| 1352 |
|
| 1353 |
if (defined $w || defined $dow) { |
| 1354 |
($y,$m,$d) = $self->_def_date_dow($y,$w,$dow,$noupdate); |
| 1355 |
} elsif (defined $doy) { |
| 1356 |
($y,$m,$d) = $self->_def_date_doy($y,$doy,$noupdate); |
| 1357 |
} else { |
| 1358 |
$y = $c . '00' if (defined $c); |
| 1359 |
($y,$m,$d) = $self->_def_date($y,$m,$d,$noupdate); |
| 1360 |
} |
| 1361 |
|
| 1362 |
($h,$mn,$s) = $self->_time($h,$mn,$s,$fh,$fm,$h24,undef,$noupdate); |
| 1363 |
} else { |
| 1364 |
return (0); |
| 1365 |
} |
| 1366 |
|
| 1367 |
return (1,$y,$m,$d,$h,$mn,$s,$tzstring,$zone,$abb,$off); |
| 1368 |
} |
| 1369 |
|
| 1370 |
sub _parse_date_iso8601 { |
| 1371 |
my($self,$string,$noupdate) = @_; |
| 1372 |
my $dmt = $$self{'tz'}; |
| 1373 |
my $dmb = $$dmt{'base'}; |
| 1374 |
my $daterx = $self->_iso8601_rx('date'); |
| 1375 |
|
| 1376 |
my($y,$m,$d); |
| 1377 |
my($doy,$dow,$yod,$c,$w); |
| 1378 |
|
| 1379 |
if ($string =~ /^$daterx$/) { |
| 1380 |
($y,$m,$d,$doy,$dow,$yod,$c,$w) = |
| 1381 |
@+{qw(y m d doy dow yod c w)}; |
| 1382 |
|
| 1383 |
if (defined $w || defined $dow) { |
| 1384 |
($y,$m,$d) = $self->_def_date_dow($y,$w,$dow,$noupdate); |
| 1385 |
} elsif (defined $doy) { |
| 1386 |
($y,$m,$d) = $self->_def_date_doy($y,$doy,$noupdate); |
| 1387 |
} else { |
| 1388 |
$y = $c . '00' if (defined $c); |
| 1389 |
($y,$m,$d) = $self->_def_date($y,$m,$d,$noupdate); |
| 1390 |
} |
| 1391 |
} else { |
| 1392 |
return (0); |
| 1393 |
} |
| 1394 |
|
| 1395 |
return (1,$y,$m,$d); |
| 1396 |
} |
| 1397 |
|
| 1398 |
# Handle all of the time fields. |
| 1399 |
# |
| 1400 |
no integer; |
| 1401 |
sub _time { |
| 1402 |
my($self,$h,$mn,$s,$fh,$fm,$h24,$ampm,$noupdate) = @_; |
| 1403 |
|
| 1404 |
if (defined($ampm) && $ampm) { |
| 1405 |
my $dmt = $$self{'tz'}; |
| 1406 |
my $dmb = $$dmt{'base'}; |
| 1407 |
if ($$dmb{'data'}{'wordmatch'}{'ampm'}{lc($ampm)} == 2) { |
| 1408 |
# pm times |
| 1409 |
$h+=12 unless ($h==12); |
| 1410 |
} else { |
| 1411 |
# am times |
| 1412 |
$h=0 if ($h==12); |
| 1413 |
} |
| 1414 |
} |
| 1415 |
|
| 1416 |
if (defined $h24) { |
| 1417 |
return(24,0,0); |
| 1418 |
} elsif (defined $fh && $fh ne "") { |
| 1419 |
$fh = "0.$fh"; |
| 1420 |
$s = int($fh * 3600); |
| 1421 |
$mn = int($s/60); |
| 1422 |
$s -= $mn*60; |
| 1423 |
} elsif (defined $fm && $fm ne "") { |
| 1424 |
$fm = "0.$fm"; |
| 1425 |
$s = int($fm*60); |
| 1426 |
} |
| 1427 |
($h,$mn,$s) = $self->_def_time($h,$mn,$s,$noupdate); |
| 1428 |
return($h,$mn,$s); |
| 1429 |
} |
| 1430 |
use integer; |
| 1431 |
|
| 1432 |
# Set up the regular expressions for other date and time formats. Returns the |
| 1433 |
# requested regexp. |
| 1434 |
# |
| 1435 |
sub _other_rx { |
| 1436 |
my($self,$rx) = @_; |
| 1437 |
my $dmt = $$self{'tz'}; |
| 1438 |
my $dmb = $$dmt{'base'}; |
| 1439 |
$rx = '_' if (! defined $rx); |
| 1440 |
|
| 1441 |
if ($rx eq 'time') { |
| 1442 |
|
| 1443 |
my $h24 = '(?<h>2[0-3]|1[0-9]|0?[0-9])'; # 0-23 00-23 |
| 1444 |
my $h12 = '(?<h>1[0-2]|0?[1-9])'; # 1-12 01-12 |
| 1445 |
my $mn = '(?<mn>[0-5][0-9])'; # 00-59 |
| 1446 |
my $ss = '(?<s>[0-5][0-9])'; # 00-59 |
| 1447 |
|
| 1448 |
# how to express fractions |
| 1449 |
|
| 1450 |
my($f1,$f2,$sepfr); |
| 1451 |
if (exists $$dmb{'data'}{'rx'}{'sepfr'} && |
| 1452 |
$$dmb{'data'}{'rx'}{'sepfr'}) { |
| 1453 |
$sepfr = $$dmb{'data'}{'rx'}{'sepfr'}; |
| 1454 |
} else { |
| 1455 |
$sepfr = ''; |
| 1456 |
} |
| 1457 |
|
| 1458 |
if ($sepfr) { |
| 1459 |
$f1 = "(?:[.,]|$sepfr)"; |
| 1460 |
$f2 = "(?:[.,:]|$sepfr)"; |
| 1461 |
} else { |
| 1462 |
$f1 = "[.,]"; |
| 1463 |
$f2 = "[.,:]"; |
| 1464 |
} |
| •1465 |
my $fh = "(?:$f1(?<fh>[0-9]*))"; # fractional hours (keep) |
| 1466 |
my $fm = "(?:$f1(?<fm>[0-9]*))"; # fractional minutes (keep) |
| 1467 |
my $fs = "(?:${f2}[0-9]*)"; # fractional seconds |
| 1468 |
|
| 1469 |
# AM/PM |
| 1470 |
|
| 1471 |
my($ampm); |
| 1472 |
if (exists $$dmb{'data'}{'rx'}{'ampm'}) { |
| 1473 |
$ampm = "(?:\\s*(?<ampm>$$dmb{data}{rx}{ampm}[0]))"; |
| 1474 |
} |
| 1475 |
|
| 1476 |
# H:MN and MN:S separators |
| 1477 |
|
| 1478 |
my @hm = ("\Q:\E"); |
| 1479 |
my @ms = ("\Q:\E"); |
| 1480 |
if ($dmb->_config('periodtimesep')) { |
| 1481 |
push(@hm,"\Q.\E"); |
| 1482 |
push(@ms,"\Q.\E"); |
| 1483 |
} |
| 1484 |
if (exists $$dmb{'data'}{'rx'}{'sephm'} && |
| 1485 |
defined $$dmb{'data'}{'rx'}{'sephm'} && |
| 1486 |
exists $$dmb{'data'}{'rx'}{'sepms'} && |
| 1487 |
defined $$dmb{'data'}{'rx'}{'sepms'}) { |
| 1488 |
push(@hm,@{ $$dmb{'data'}{'rx'}{'sephm'} }); |
| 1489 |
push(@ms,@{ $$dmb{'data'}{'rx'}{'sepms'} }); |
| 1490 |
} |
| 1491 |
|
| 1492 |
# How to express the time |
| 1493 |
# matches = (H, FH, MN, FMN, S, AM, TZSTRING, ZONE, ABB, OFF, ABB) |
| 1494 |
|
| 1495 |
my @timerx; |
| 1496 |
|
| 1497 |
for (my $i=0; $i<=$#hm; $i++) { |
| 1498 |
my $hm = $hm[$i]; |
| 1499 |
my $ms = $ms[$i]; |
| 1500 |
push(@timerx, |
| 1501 |
"${h12}$hm${mn}$ms${ss}${fs}?${ampm}?", # H12:MN:SS[,S+] [AM] |
| 1502 |
) if ($ampm); |
| 1503 |
|
| 1504 |
push(@timerx, |
| 1505 |
"${h24}$hm${mn}$ms${ss}${fs}?", # H24:MN:SS[,S+] |
| 1506 |
"(?<h>24)$hm(?<mn>00)$ms(?<s>00)", # 24:00:00 |
| 1507 |
); |
| 1508 |
} |
| 1509 |
for (my $i=0; $i<=$#hm; $i++) { |
| 1510 |
my $hm = $hm[$i]; |
| 1511 |
my $ms = $ms[$i]; |
| 1512 |
push(@timerx, |
| 1513 |
"${h12}$hm${mn}${fm}${ampm}?", # H12:MN,M+ [AM] |
| 1514 |
) if ($ampm); |
| 1515 |
push(@timerx, |
| 1516 |
"${h24}$hm${mn}${fm}", # H24:MN,M+ |
| 1517 |
); |
| 1518 |
} |
| 1519 |
for (my $i=0; $i<=$#hm; $i++) { |
| 1520 |
my $hm = $hm[$i]; |
| 1521 |
my $ms = $ms[$i]; |
| 1522 |
push(@timerx, |
| 1523 |
"${h12}$hm${mn}${ampm}?", # H12:MN [AM] |
| 1524 |
) if ($ampm); |
| 1525 |
push(@timerx, |
| 1526 |
"${h24}$hm${mn}", # H24:MN |
| 1527 |
"(?<h>24)$hm(?<mn>00)", # 24:00 |
| 1528 |
); |
| 1529 |
} |
| 1530 |
|
| 1531 |
push(@timerx, |
| 1532 |
"${h12}${fh}${ampm}", # H12,H+ AM |
| 1533 |
"${h12}${ampm}", # H12 AM |
| 1534 |
) if ($ampm); |
| 1535 |
push(@timerx, |
| 1536 |
"${h24}${fh}", # H24,H+ |
| 1537 |
); |
| 1538 |
|
| 1539 |
my $timerx = join('|',@timerx); |
| 1540 |
my $zrx = $dmt->_zrx('zrx'); |
| 1541 |
my $at = $$dmb{'data'}{'rx'}{'at'}; |
| 1542 |
my $atrx = qr/(?:^|\s+)(?:$at)\s+/; |
| 1543 |
$timerx = qr/(?:$atrx|^|\s+)(?:$timerx)(?:\s*$zrx)?(?:\s+|$)/i; |
| 1544 |
|
| 1545 |
$$dmb{'data'}{'rx'}{'other'}{$rx} = $timerx; |
| 1546 |
|
| 1547 |
} elsif ($rx eq 'common_1') { |
| 1548 |
|
| 1549 |
# These are of the format M/D/Y |
| 1550 |
|
| 1551 |
# Do NOT replace <m> and <d> with a regular expression to |
| 1552 |
# match 1-12 since the DateFormat config may reverse the two. |
| •1553 |
my $y4 = '(?<y>[0-9]{4})'; |
| 1554 |
my $y2 = '(?<y>[0-9]{2})'; |
| 1555 |
my $m = '(?<m>[0-9]{1,2})'; |
| 1556 |
my $d = '(?<d>[0-9]{1,2})'; |
| 1557 |
my $sep = '(?<sep>[\s\.\/\-])'; |
| 1558 |
|
| 1559 |
my @daterx = |
| 1560 |
( |
| 1561 |
"${m}${sep}${d}\\k<sep>$y4", # M/D/YYYY |
| 1562 |
"${m}${sep}${d}\\k<sep>$y2", # M/D/YY |
| 1563 |
"${m}${sep}${d}", # M/D |
| 1564 |
); |
| 1565 |
my $daterx = join('|',@daterx); |
| 1566 |
|
| 1567 |
$daterx = qr/^\s*(?:$daterx)\s*$/; |
| 1568 |
$$dmb{'data'}{'rx'}{'other'}{$rx} = $daterx; |
| 1569 |
|
| 1570 |
} elsif ($rx eq 'common_2') { |
| 1571 |
|
| 1572 |
my $abb = $$dmb{'data'}{'rx'}{'month_abb'}[0]; |
| 1573 |
my $nam = $$dmb{'data'}{'rx'}{'month_name'}[0]; |
| 1574 |
|
| •1575 |
my $y4 = '(?<y>[0-9]{4})'; |
| 1576 |
my $y2 = '(?<y>[0-9]{2})'; |
| 1577 |
my $m = '(?<m>[0-9]{1,2})'; |
| 1578 |
my $d = '(?<d>[0-9]{1,2})'; |
| 1579 |
my $dd = '(?<d>[0-9]{2})'; |
| 1580 |
my $mmm = "(?:(?<mmm>$abb)|(?<month>$nam))"; |
| 1581 |
my $sep = '(?<sep>[\s\.\/\-])'; |
| 1582 |
|
| 1583 |
my $format_mmmyyyy = $dmb->_config('format_mmmyyyy'); |
| 1584 |
|
| 1585 |
my @daterx = (); |
| 1586 |
push(@daterx, |
| 1587 |
"${y4}${sep}${m}\\k<sep>$d", # YYYY/M/D |
| 1588 |
"${mmm}\\s*${dd}\\s*${y4}", # mmmDDYYYY |
| 1589 |
); |
| 1590 |
push(@daterx, |
| 1591 |
"${mmm}\\s*${dd}\\s*${y2}", # mmmDDYY |
| 1592 |
) if (! $format_mmmyyyy); |
| 1593 |
push(@daterx, |
| 1594 |
"${mmm}\\s*${d}", # mmmD |
| 1595 |
"${d}\\s*${mmm}\\s*${y4}", # DmmmYYYY |
| 1596 |
"${d}\\s*${mmm}\\s*${y2}", # DmmmYY |
| 1597 |
"${d}\\s*${mmm}", # Dmmm |
| 1598 |
"${y4}\\s*${mmm}\\s*${d}", # YYYYmmmD |
| 1599 |
|
| 1600 |
"${mmm}${sep}${d}\\k<sep>${y4}", # mmm/D/YYYY |
| 1601 |
"${mmm}${sep}${d}\\k<sep>${y2}", # mmm/D/YY |
| 1602 |
"${mmm}${sep}${d}", # mmm/D |
| 1603 |
"${d}${sep}${mmm}\\k<sep>${y4}", # D/mmm/YYYY |
| 1604 |
"${d}${sep}${mmm}\\k<sep>${y2}", # D/mmm/YY |
| 1605 |
"${d}${sep}${mmm}", # D/mmm |
| 1606 |
"${y4}${sep}${mmm}\\k<sep>${d}", # YYYY/mmm/D |
| 1607 |
|
| 1608 |
"${mmm}${sep}?${d}\\s+${y2}", # mmmD YY mmm/D YY |
| 1609 |
"${mmm}${sep}?${d}\\s+${y4}", # mmmD YYYY mmm/D YYYY |
| 1610 |
"${d}${sep}?${mmm}\\s+${y2}", # Dmmm YY D/mmm YY |
| 1611 |
"${d}${sep}?${mmm}\\s+${y4}", # Dmmm YYYY D/mmm YYYY |
| 1612 |
|
| 1613 |
"${y2}\\s+${mmm}${sep}?${d}", # YY mmmD YY mmm/D |
| 1614 |
"${y4}\\s+${mmm}${sep}?${d}", # YYYY mmmD YYYY mmm/D |
| 1615 |
"${y2}\\s+${d}${sep}?${mmm}", # YY Dmmm YY D/mmm |
| 1616 |
"${y4}\\s+${d}${sep}?${mmm}", # YYYY Dmmm YYYY D/mmm |
| 1617 |
|
| 1618 |
"${y4}:${m}:${d}", # YYYY:MM:DD |
| 1619 |
); |
| 1620 |
my $daterx = join('|',@daterx); |
| 1621 |
|
| 1622 |
$daterx = qr/^\s*(?:$daterx)\s*$/i; |
| 1623 |
$$dmb{'data'}{'rx'}{'other'}{$rx} = $daterx; |
| 1624 |
|
| 1625 |
} elsif ($rx eq 'truncated') { |
| 1626 |
|
| 1627 |
my $abb = $$dmb{'data'}{'rx'}{'month_abb'}[0]; |
| 1628 |
my $nam = $$dmb{'data'}{'rx'}{'month_name'}[0]; |
| 1629 |
|
| •1630 |
my $y4 = '(?<y>[0-9]{4})'; |
| 1631 |
my $mmm = "(?:(?<mmm>$abb)|(?<month>$nam))"; |
| 1632 |
my $sep = '(?<sep>[\s\.\/\-])'; |
| 1633 |
|
| 1634 |
my $format_mmmyyyy = $dmb->_config('format_mmmyyyy'); |
| 1635 |
|
| 1636 |
my @daterx = (); |
| 1637 |
push(@daterx, |
| 1638 |
"${mmm}\\s*${y4}", # mmmYYYY |
| 1639 |
"${y4}\\s*${mmm}", # YYYYmmm |
| 1640 |
|
| 1641 |
"${y4}${sep}${mmm}", # YYYY/mmm |
| 1642 |
"${mmm}${sep}${y4}", # mmm/YYYY |
| 1643 |
) if ($format_mmmyyyy); |
| 1644 |
|
| 1645 |
if (@daterx) { |
| 1646 |
my $daterx = join('|',@daterx); |
| 1647 |
$daterx = qr/^\s*(?:$daterx)\s*$/i; |
| 1648 |
$$dmb{'data'}{'rx'}{'other'}{$rx} = $daterx; |
| 1649 |
} else { |
| 1650 |
$$dmb{'data'}{'rx'}{'other'}{$rx} = ''; |
| 1651 |
} |
| 1652 |
|
| 1653 |
} elsif ($rx eq 'dow') { |
| 1654 |
|
| 1655 |
my $day_abb = $$dmb{'data'}{'rx'}{'day_abb'}[0]; |
| 1656 |
my $day_name = $$dmb{'data'}{'rx'}{'day_name'}[0]; |
| 1657 |
|
| 1658 |
my $on = $$dmb{'data'}{'rx'}{'on'}; |
| 1659 |
my $onrx = qr/(?:^|\s+)(?:$on)\s+/; |
| 1660 |
my $dowrx = qr/(?:$onrx|^|\s+)(?<dow>$day_name|$day_abb)($|\s+)/i; |
| 1661 |
|
| 1662 |
$$dmb{'data'}{'rx'}{'other'}{$rx} = $dowrx; |
| 1663 |
|
| 1664 |
} elsif ($rx eq 'ignore') { |
| 1665 |
|
| 1666 |
my $of = $$dmb{'data'}{'rx'}{'of'}; |
| 1667 |
|
| 1668 |
my $ignrx = qr/(?:^|\s+)(?<of>$of)(\s+|$)/; |
| 1669 |
$$dmb{'data'}{'rx'}{'other'}{$rx} = $ignrx; |
| 1670 |
|
| 1671 |
} elsif ($rx eq 'miscdatetime') { |
| 1672 |
|
| 1673 |
my $special = $$dmb{'data'}{'rx'}{'offset_time'}[0]; |
| 1674 |
|
| 1675 |
$special = "(?<special>$special)"; |
| •1676 |
my $secs = "(?<epoch>[-+]?[0-9]+)"; |
| 1677 |
my $abb = $$dmb{'data'}{'rx'}{'month_abb'}[0]; |
| 1678 |
my $mmm = "(?<mmm>$abb)"; |
| •1679 |
my $y4 = '(?<y>[0-9]{4})'; |
| 1680 |
my $dd = '(?<d>[0-9]{2})'; |
| 1681 |
my $h24 = '(?<h>2[0-3]|[01][0-9])'; # 00-23 |
| 1682 |
my $mn = '(?<mn>[0-5][0-9])'; # 00-59 |
| 1683 |
my $ss = '(?<s>[0-5][0-9])'; # 00-59 |
| 1684 |
my $offrx = $dmt->_zrx('offrx'); |
| 1685 |
my $zrx = $dmt->_zrx('zrx'); |
| 1686 |
|
| 1687 |
my @daterx = |
| 1688 |
( |
| 1689 |
"${special}", # now |
| 1690 |
"${special}\\s+${zrx}", # now EDT |
| 1691 |
|
| 1692 |
"epoch\\s+$secs", # epoch SECS |
| 1693 |
"epoch\\s+$secs\\s+${zrx}", # epoch SECS EDT |
| 1694 |
|
| 1695 |
"${dd}\\/${mmm}\\/${y4}:${h24}:${mn}:${ss}\\s*${offrx}", |
| 1696 |
# Common log format: 10/Oct/2000:13:55:36 -0700 |
| 1697 |
); |
| 1698 |
my $daterx = join('|',@daterx); |
| 1699 |
|
| 1700 |
$daterx = qr/^\s*(?:$daterx)\s*$/i; |
| 1701 |
$$dmb{'data'}{'rx'}{'other'}{$rx} = $daterx; |
| 1702 |
|
| 1703 |
} elsif ($rx eq 'misc') { |
| 1704 |
|
| 1705 |
my $abb = $$dmb{'data'}{'rx'}{'month_abb'}[0]; |
| 1706 |
my $nam = $$dmb{'data'}{'rx'}{'month_name'}[0]; |
| 1707 |
my $next = $$dmb{'data'}{'rx'}{'nextprev'}[0]; |
| 1708 |
my $last = $$dmb{'data'}{'rx'}{'last'}; |
| 1709 |
my $yf = $$dmb{data}{rx}{fields}[1]; |
| 1710 |
my $mf = $$dmb{data}{rx}{fields}[2]; |
| 1711 |
my $wf = $$dmb{data}{rx}{fields}[3]; |
| 1712 |
my $df = $$dmb{data}{rx}{fields}[4]; |
| 1713 |
my $nth = $$dmb{'data'}{'rx'}{'nth'}[0]; |
| 1714 |
my $nth_wom = $$dmb{'data'}{'rx'}{'nth_wom'}[0]; |
| 1715 |
my $special = $$dmb{'data'}{'rx'}{'offset_date'}[0]; |
| 1716 |
|
| •1717 |
my $y = '(?:(?<y>[0-9]{4})|(?<y>[0-9]{2}))'; |
| 1718 |
my $mmm = "(?:(?<mmm>$abb)|(?<month>$nam))"; |
| 1719 |
$next = "(?<next>$next)"; |
| 1720 |
$last = "(?<last>$last)"; |
| 1721 |
$yf = "(?<field_y>$yf)"; |
| 1722 |
$mf = "(?<field_m>$mf)"; |
| 1723 |
$wf = "(?<field_w>$wf)"; |
| 1724 |
$df = "(?<field_d>$df)"; |
| 1725 |
my $fld = "(?:$yf|$mf|$wf)"; |
| 1726 |
$nth = "(?<nth>$nth)"; |
| 1727 |
$nth_wom = "(?<nth>$nth_wom)"; |
| 1728 |
$special = "(?<special>$special)"; |
| 1729 |
|
| 1730 |
my @daterx = |
| 1731 |
( |
| 1732 |
"${mmm}\\s+${nth}\\s*$y?", # Dec 1st [1970] |
| 1733 |
"${nth}\\s+${mmm}\\s*$y?", # 1st Dec [1970] |
| 1734 |
"$y\\s+${mmm}\\s+${nth}", # 1970 Dec 1st |
| 1735 |
"$y\\s+${nth}\\s+${mmm}", # 1970 1st Dec |
| 1736 |
|
| 1737 |
"${next}\\s+${fld}", # next year, next month, next week |
| 1738 |
"${next}", # next friday |
| 1739 |
|
| 1740 |
"${last}\\s+${mmm}\\s*$y?", # last friday in october 95 |
| 1741 |
"${last}\\s+${df}\\s+${mmm}\\s*$y?", |
| 1742 |
# last day in october 95 |
| 1743 |
"${last}\\s*$y?", # last friday in 95 |
| 1744 |
|
| 1745 |
"${nth_wom}\\s+${mmm}\\s*$y?", # nth DoW in MMM [YYYY] |
| 1746 |
"${nth}\\s*$y?", # nth DoW in [YYYY] |
| 1747 |
|
| 1748 |
"${nth}\\s+$df\\s+${mmm}\\s*$y?", |
| 1749 |
# nth day in MMM [YYYY] |
| 1750 |
|
| 1751 |
"${nth}\\s+${wf}\\s*$y?", # DoW Nth week [YYYY] |
| •1752 |
"${wf}\\s+(?<n>[0-9]+)\\s*$y?", # DoW week N [YYYY] |
| 1753 |
|
| 1754 |
"${special}", # today, tomorrow |
| 1755 |
"${special}\\s+${wf}", # today week |
| 1756 |
# British: same as 1 week from today |
| 1757 |
|
| 1758 |
"${nth}", # nth |
| 1759 |
|
| 1760 |
"${wf}", # monday week |
| 1761 |
# British: same as 'in 1 week on monday' |
| 1762 |
); |
| 1763 |
my $daterx = join('|',@daterx); |
| 1764 |
|
| 1765 |
$daterx = qr/^\s*(?:$daterx)\s*$/i; |
| 1766 |
$$dmb{'data'}{'rx'}{'other'}{$rx} = $daterx; |
| 1767 |
|
| 1768 |
} |
| 1769 |
|
| 1770 |
return $$dmb{'data'}{'rx'}{'other'}{$rx}; |
| 1771 |
} |
| 1772 |
|
| 1773 |
sub _parse_time { |
| 1774 |
my($self,$caller,$string,$noupdate,%opts) = @_; |
| 1775 |
my $dmt = $$self{'tz'}; |
| 1776 |
my $dmb = $$dmt{'base'}; |
| 1777 |
|
| 1778 |
my($timerx,$h,$mn,$s,$fh,$fm,$h24,$ampm,$tzstring,$zone,$abb,$off); |
| 1779 |
my $got_time = 0; |
| 1780 |
|
| 1781 |
# Check for ISO 8601 time |
| 1782 |
# |
| 1783 |
# This is only called via. parse_time (parse_date uses a regexp |
| 1784 |
# that matches a full ISO 8601 date/time instead of parsing them |
| 1785 |
# separately. Since some ISO 8601 times are a substring of non-ISO |
| 1786 |
# 8601 times (i.e. 12:30 is a substring of '12:30 PM'), we need to |
| 1787 |
# match entire strings here. |
| 1788 |
|
| 1789 |
if ($caller eq 'parse_time') { |
| 1790 |
$timerx = (exists $$dmb{'data'}{'rx'}{'iso'}{'time'} ? |
| 1791 |
$$dmb{'data'}{'rx'}{'iso'}{'time'} : |
| 1792 |
$self->_iso8601_rx('time')); |
| 1793 |
|
| 1794 |
if (! exists $opts{'noiso8601'}) { |
| 1795 |
if ($string =~ s/^\s*$timerx\s*$//) { |
| 1796 |
($h,$fh,$mn,$fm,$s,$ampm,$tzstring,$zone,$abb,$off) = |
| 1797 |
@+{qw(h fh mn fm s ampm tzstring zone abb off)}; |
| 1798 |
|
| 1799 |
($h,$mn,$s) = $self->_def_time($h,$mn,$s,$noupdate); |
| 1800 |
$h24 = 1 if ($h == 24 && $mn == 0 && $s == 0); |
| 1801 |
$string =~ s/\s*$//; |
| 1802 |
$got_time = 1; |
| 1803 |
} |
| 1804 |
} |
| 1805 |
} |
| 1806 |
|
| 1807 |
# Make time substitutions (i.e. noon => 12:00:00) |
| 1808 |
|
| 1809 |
if (! $got_time && |
| 1810 |
! exists $opts{'noother'}) { |
| 1811 |
my @rx = @{ $$dmb{'data'}{'rx'}{'times'} }; |
| 1812 |
shift(@rx); |
| 1813 |
foreach my $rx (@rx) { |
| 1814 |
if ($string =~ $rx) { |
| 1815 |
my $repl = $$dmb{'data'}{'wordmatch'}{'times'}{lc($1)}; |
| 1816 |
$string =~ s/$rx/$repl/g; |
| 1817 |
} |
| 1818 |
} |
| 1819 |
} |
| 1820 |
|
| 1821 |
# Check to see if there is a time in the string |
| 1822 |
|
| 1823 |
if (! $got_time) { |
| 1824 |
$timerx = (exists $$dmb{'data'}{'rx'}{'other'}{'time'} ? |
| 1825 |
$$dmb{'data'}{'rx'}{'other'}{'time'} : |
| 1826 |
$self->_other_rx('time')); |
| 1827 |
|
| 1828 |
if ($string =~ s/$timerx/ /) { |
| 1829 |
($h,$fh,$mn,$fm,$s,$ampm,$tzstring,$zone,$abb,$off) = |
| 1830 |
@+{qw(h fh mn fm s ampm tzstring zone abb off)}; |
| 1831 |
|
| 1832 |
($h,$mn,$s) = $self->_def_time($h,$mn,$s,$noupdate); |
| 1833 |
$h24 = 1 if ($h == 24 && $mn == 0 && $s == 0); |
| 1834 |
$string =~ s/\s*$//; |
| 1835 |
$got_time = 1; |
| 1836 |
} |
| 1837 |
} |
| 1838 |
|
| 1839 |
# If we called this from $date->parse() |
| 1840 |
# returns the string and a list of time components |
| 1841 |
|
| 1842 |
if ($caller eq 'parse') { |
| 1843 |
if ($got_time) { |
| 1844 |
($h,$mn,$s) = $self->_time($h,$mn,$s,$fh,$fm,$h24,$ampm,$noupdate); |
| 1845 |
return ($got_time,$string,$h,$mn,$s,$tzstring,$zone,$abb,$off); |
| 1846 |
} else { |
| 1847 |
return (0); |
| 1848 |
} |
| 1849 |
} |
| 1850 |
|
| 1851 |
# If we called this from $date->parse_time() |
| 1852 |
|
| 1853 |
if (! $got_time || $string) { |
| 1854 |
$$self{'err'} = "[$caller] Invalid time string"; |
| 1855 |
return (); |
| 1856 |
} |
| 1857 |
|
| 1858 |
($h,$mn,$s) = $self->_time($h,$mn,$s,$fh,$fm,$h24,$ampm,$noupdate); |
| 1859 |
return ($h,$mn,$s,$tzstring,$zone,$abb,$off); |
| 1860 |
} |
| 1861 |
|
| 1862 |
# Parse common dates |
| 1863 |
sub _parse_date_common { |
| 1864 |
my($self,$string,$noupdate) = @_; |
| 1865 |
my $dmt = $$self{'tz'}; |
| 1866 |
my $dmb = $$dmt{'base'}; |
| 1867 |
|
| 1868 |
# Since we want whitespace to be used as a separator, turn all |
| 1869 |
# whitespace into single spaces. This is necessary since the |
| 1870 |
# regexps do backreferences to make sure that separators are |
| 1871 |
# not mixed. |
| 1872 |
$string =~ s/\s+/ /g; |
| 1873 |
|
| 1874 |
my $daterx = (exists $$dmb{'data'}{'rx'}{'other'}{'common_1'} ? |
| 1875 |
$$dmb{'data'}{'rx'}{'other'}{'common_1'} : |
| 1876 |
$self->_other_rx('common_1')); |
| 1877 |
|
| 1878 |
if ($string =~ $daterx) { |
| 1879 |
my($y,$m,$d) = @+{qw(y m d)}; |
| 1880 |
|
| 1881 |
if ($dmb->_config('dateformat') ne 'US') { |
| 1882 |
($m,$d) = ($d,$m); |
| 1883 |
} |
| 1884 |
|
| 1885 |
($y,$m,$d) = $self->_def_date($y,$m,$d,$noupdate); |
| 1886 |
return($y,$m,$d); |
| 1887 |
} |
| 1888 |
|
| 1889 |
$daterx = (exists $$dmb{'data'}{'rx'}{'other'}{'common_2'} ? |
| 1890 |
$$dmb{'data'}{'rx'}{'other'}{'common_2'} : |
| 1891 |
$self->_other_rx('common_2')); |
| 1892 |
|
| 1893 |
if ($string =~ $daterx) { |
| 1894 |
my($y,$m,$d,$mmm,$month) = @+{qw(y m d mmm month)}; |
| 1895 |
|
| 1896 |
if ($mmm) { |
| 1897 |
$m = $$dmb{'data'}{'wordmatch'}{'month_abb'}{lc($mmm)}; |
| 1898 |
} elsif ($month) { |
| 1899 |
$m = $$dmb{'data'}{'wordmatch'}{'month_name'}{lc($month)}; |
| 1900 |
} |
| 1901 |
|
| 1902 |
($y,$m,$d) = $self->_def_date($y,$m,$d,$noupdate); |
| 1903 |
return($y,$m,$d); |
| 1904 |
} |
| 1905 |
|
| 1906 |
return (); |
| 1907 |
} |
| 1908 |
|
| 1909 |
# Parse truncated dates |
| 1910 |
sub _parse_date_truncated { |
| 1911 |
my($self,$string,$noupdate) = @_; |
| 1912 |
my $dmt = $$self{'tz'}; |
| 1913 |
my $dmb = $$dmt{'base'}; |
| 1914 |
|
| 1915 |
my $daterx = (exists $$dmb{'data'}{'rx'}{'other'}{'truncated'} ? |
| 1916 |
$$dmb{'data'}{'rx'}{'other'}{'truncated'} : |
| 1917 |
$self->_other_rx('truncated')); |
| 1918 |
|
| 1919 |
return () if (! $daterx); |
| 1920 |
|
| 1921 |
# Since we want whitespace to be used as a separator, turn all |
| 1922 |
# whitespace into single spaces. This is necessary since the |
| 1923 |
# regexps do backreferences to make sure that separators are |
| 1924 |
# not mixed. |
| 1925 |
$string =~ s/\s+/ /g; |
| 1926 |
|
| 1927 |
if ($string =~ $daterx) { |
| 1928 |
my($y,$mmm,$month) = @+{qw(y mmm month)}; |
| 1929 |
|
| 1930 |
my ($m,$d); |
| 1931 |
if ($mmm) { |
| 1932 |
$m = $$dmb{'data'}{'wordmatch'}{'month_abb'}{lc($mmm)}; |
| 1933 |
} elsif ($month) { |
| 1934 |
$m = $$dmb{'data'}{'wordmatch'}{'month_name'}{lc($month)}; |
| 1935 |
} |
| 1936 |
|
| 1937 |
# Handle all of the mmmYYYY formats |
| 1938 |
|
| 1939 |
if ($y && $m) { |
| 1940 |
|
| 1941 |
my $format_mmmyyyy = $dmb->_config('format_mmmyyyy'); |
| 1942 |
if ($format_mmmyyyy eq 'first') { |
| 1943 |
$d=1; |
| 1944 |
$$self{'data'}{'default_time'} = [0,0,0]; |
| 1945 |
} else { |
| 1946 |
$d=$dmb->days_in_month($y,$m); |
| 1947 |
$$self{'data'}{'default_time'} = [23,59,59]; |
| 1948 |
} |
| 1949 |
|
| 1950 |
$$self{'data'}{'def'}[0] = ''; |
| 1951 |
$$self{'data'}{'def'}[1] = ''; |
| 1952 |
$$self{'data'}{'def'}[2] = 1; |
| 1953 |
return($y,$m,$d); |
| 1954 |
} |
| 1955 |
} |
| 1956 |
|
| 1957 |
return (); |
| 1958 |
} |
| 1959 |
|
| 1960 |
sub _parse_tz { |
| 1961 |
my($self,$string,$noupdate) = @_; |
| 1962 |
my $dmt = $$self{'tz'}; |
| 1963 |
my($tzstring,$zone,$abb,$off); |
| 1964 |
|
| 1965 |
my $rx = $dmt->_zrx('zrx'); |
| 1966 |
if ($string =~ s/(?:^|\s)$rx(?:$|\s)/ /) { |
| 1967 |
($tzstring,$zone,$abb,$off) = @+{qw(tzstring zone abb off)}; |
| 1968 |
return($string,$tzstring,$zone,$abb,$off); |
| 1969 |
} |
| 1970 |
return($string); |
| 1971 |
} |
| 1972 |
|
| 1973 |
sub _parse_dow { |
| 1974 |
my($self,$string,$noupdate) = @_; |
| 1975 |
my $dmt = $$self{'tz'}; |
| 1976 |
my $dmb = $$dmt{'base'}; |
| 1977 |
my($y,$m,$d,$dow); |
| 1978 |
|
| 1979 |
# Remove the day of week |
| 1980 |
|
| 1981 |
my $rx = (exists $$dmb{'data'}{'rx'}{'other'}{'dow'} ? |
| 1982 |
$$dmb{'data'}{'rx'}{'other'}{'dow'} : |
| 1983 |
$self->_other_rx('dow')); |
| 1984 |
if ($string =~ s/$rx/ /) { |
| 1985 |
$dow = $+{'dow'}; |
| 1986 |
$dow = lc($dow); |
| 1987 |
|
| 1988 |
$dow = $$dmb{'data'}{'wordmatch'}{'day_abb'}{$dow} |
| 1989 |
if (exists $$dmb{'data'}{'wordmatch'}{'day_abb'}{$dow}); |
| 1990 |
$dow = $$dmb{'data'}{'wordmatch'}{'day_name'}{$dow} |
| 1991 |
if (exists $$dmb{'data'}{'wordmatch'}{'day_name'}{$dow}); |
| 1992 |
} else { |
| 1993 |
return (0); |
| 1994 |
} |
| 1995 |
|
| 1996 |
$string =~ s/\s*$//; |
| 1997 |
$string =~ s/^\s*//; |
| 1998 |
|
| 1999 |
return (0,$string,$dow) if ($string); |
| 2000 |
|
| 2001 |
# Handle the simple DoW format |
| 2002 |
|
| 2003 |
($y,$m,$d) = $self->_def_date($y,$m,$d,$noupdate); |
| 2004 |
|
| 2005 |
my($w,$dow1); |
| 2006 |
|
| 2007 |
($y,$w) = $dmb->week_of_year([$y,$m,$d]); # week of year |
| 2008 |
($y,$m,$d) = @{ $dmb->week_of_year($y,$w) }; # first day |
| 2009 |
$dow1 = $dmb->day_of_week([$y,$m,$d]); # DoW of first day |
| 2010 |
$dow1 -= 7 if ($dow1 > $dow); |
| 2011 |
($y,$m,$d) = @{ $dmb->calc_date_days([$y,$m,$d],$dow-$dow1) }; |
| 2012 |
|
| 2013 |
return(1,$y,$m,$d); |
| 2014 |
} |
| 2015 |
|
| 2016 |
sub _parse_holidays { |
| 2017 |
my($self,$string,$noupdate) = @_; |
| 2018 |
my $dmt = $$self{'tz'}; |
| 2019 |
my $dmb = $$dmt{'base'}; |
| 2020 |
my($y,$m,$d); |
| 2021 |
|
| 2022 |
if (! exists $$dmb{'data'}{'rx'}{'holidays'}) { |
| 2023 |
return (0); |
| 2024 |
} |
| 2025 |
|
| 2026 |
$string =~ s/\s*$//; |
| 2027 |
$string =~ s/^\s*//; |
| 2028 |
|
| 2029 |
my $rx = $$dmb{'data'}{'rx'}{'holidays'}; |
| 2030 |
if ($string =~ $rx) { |
| 2031 |
my $hol; |
| 2032 |
($y,$hol) = @+{qw(y holiday)}; |
| 2033 |
$y = $dmt->_now('y',$noupdate) if (! $y); |
| 2034 |
$y += 0; |
| 2035 |
|
| 2036 |
$self->_holidays($y-1); |
| 2037 |
$self->_holidays($y); |
| 2038 |
$self->_holidays($y+1); |
| 2039 |
return (0) if (! exists $$dmb{'data'}{'holidays'}{'yhols'}{$y+0}{$hol}); |
| 2040 |
my ($y,$m,$d) = @{ $$dmb{'data'}{'holidays'}{'yhols'}{$y+0}{$hol} }; |
| 2041 |
return(1,$y,$m,$d); |
| 2042 |
} |
| 2043 |
|
| 2044 |
return (0); |
| 2045 |
} |
| 2046 |
|
| 2047 |
no integer; |
| 2048 |
sub _parse_delta { |
| 2049 |
my($self,$string,$dow,$got_time,$h,$mn,$s,$noupdate) = @_; |
| 2050 |
my $dmt = $$self{'tz'}; |
| 2051 |
my $dmb = $$dmt{'base'}; |
| 2052 |
my($y,$m,$d); |
| 2053 |
|
| 2054 |
my $delta = $self->new_delta(); |
| 2055 |
my $err = $delta->parse($string); |
| 2056 |
my $tz = $dmt->_now('tz'); |
| 2057 |
my $isdst = $dmt->_now('isdst'); |
| 2058 |
|
| 2059 |
if (! $err) { |
| 2060 |
my($dy,$dm,$dw,$dd,$dh,$dmn,$ds) = @{ $$delta{'data'}{'delta'} }; |
| 2061 |
|
| 2062 |
# We can't handle a delta longer than 10000 years |
| 2063 |
if (abs($dy) > 10000 || |
| 2064 |
abs($dm) > 120000 || # 10000*12 |
| 2065 |
abs($dw) > 530000 || # 10000*53 |
| 2066 |
abs($dd) > 3660000 || # 10000*366 |
| 2067 |
abs($dh) > 87840000 || # 10000*366*24 |
| 2068 |
abs($dmn) > 5270400000 || # 10000*366*24*60 |
| 2069 |
abs($ds) > 316224000000) { # 10000*366*24*60*60 |
| 2070 |
$$self{'err'} = '[parse] Delta too large'; |
| 2071 |
return (1); |
| 2072 |
} |
| 2073 |
|
| 2074 |
if ($got_time && |
| 2075 |
($dh != 0 || $dmn != 0 || $ds != 0)) { |
| 2076 |
$$self{'err'} = '[parse] Two times entered or implied'; |
| 2077 |
return (1); |
| 2078 |
} |
| 2079 |
|
| 2080 |
if ($got_time) { |
| 2081 |
($y,$m,$d) = $self->_def_date($y,$m,$d,$noupdate); |
| 2082 |
} else { |
| 2083 |
($y,$m,$d,$h,$mn,$s) = $dmt->_now('now',$$noupdate); |
| 2084 |
$$noupdate = 1; |
| 2085 |
} |
| 2086 |
|
| 2087 |
my $business = ($$delta{'data'}{'mode'} eq 'business' ? 1 : 0); |
| 2088 |
|
| 2089 |
my($date2,$offset,$abbrev); |
| 2090 |
($err,$date2,$offset,$isdst,$abbrev) = |
| 2091 |
$self->__calc_date_delta([$y,$m,$d,$h,$mn,$s], |
| 2092 |
[$dy,$dm,$dw,$dd,$dh,$dmn,$ds], |
| 2093 |
0,$business,$tz,$isdst); |
| 2094 |
($y,$m,$d,$h,$mn,$s) = @$date2; |
| 2095 |
|
| 2096 |
if ($dow) { |
| 2097 |
if ($dd != 0 || $dh != 0 || $dmn != 0 || $ds != 0) { |
| 2098 |
$$self{'err'} = '[parse] Day of week not allowed'; |
| 2099 |
return (1); |
| 2100 |
} |
| 2101 |
|
| 2102 |
my($w,$dow1); |
| 2103 |
|
| 2104 |
($y,$w) = $dmb->week_of_year([$y,$m,$d]); # week of year |
| 2105 |
($y,$m,$d) = @{ $dmb->week_of_year($y,$w) }; # first day |
| 2106 |
$dow1 = $dmb->day_of_week([$y,$m,$d]); # DoW of first day |
| 2107 |
$dow1 -= 7 if ($dow1 > $dow); |
| 2108 |
($y,$m,$d) = @{ $dmb->calc_date_days([$y,$m,$d],$dow-$dow1) }; |
| 2109 |
} |
| 2110 |
|
| 2111 |
return (1,$y,$m,$d,$h,$mn,$s); |
| 2112 |
} |
| 2113 |
|
| 2114 |
return (0); |
| 2115 |
} |
| 2116 |
use integer; |
| 2117 |
|
| 2118 |
sub _parse_datetime_other { |
| 2119 |
my($self,$string,$noupdate) = @_; |
| 2120 |
my $dmt = $$self{'tz'}; |
| 2121 |
my $dmb = $$dmt{'base'}; |
| 2122 |
|
| 2123 |
my $rx = (exists $$dmb{'data'}{'rx'}{'other'}{'miscdatetime'} ? |
| 2124 |
$$dmb{'data'}{'rx'}{'other'}{'miscdatetime'} : |
| 2125 |
$self->_other_rx('miscdatetime')); |
| 2126 |
|
| 2127 |
if ($string =~ $rx) { |
| 2128 |
my ($special,$epoch,$y,$mmm,$d,$h,$mn,$s,$tzstring,$zone,$abb,$off) = |
| 2129 |
@+{qw(special epoch y mmm d h mn s tzstring zone abb off)}; |
| 2130 |
|
| 2131 |
if (defined($special)) { |
| 2132 |
my $delta = $$dmb{'data'}{'wordmatch'}{'offset_time'}{lc($special)}; |
| 2133 |
my @delta = @{ $dmb->split('delta',$delta) }; |
| 2134 |
my @date = $dmt->_now('now',$$noupdate); |
| 2135 |
my $tz = $dmt->_now('tz'); |
| 2136 |
my $isdst = $dmt->_now('isdst'); |
| 2137 |
$$noupdate = 1; |
| 2138 |
|
| 2139 |
my($err,$date2,$offset,$abbrev); |
| 2140 |
($err,$date2,$offset,$isdst,$abbrev) = |
| 2141 |
$self->__calc_date_delta([@date],[@delta],0,0,$tz,$isdst); |
| 2142 |
|
| 2143 |
if ($tzstring) { |
| 2144 |
|
| 2145 |
$date2 = [] if (! defined $date2); |
| 2146 |
my $offset = (defined $off ? $dmb->_delta_convert('offset',$off) : ''); |
| 2147 |
$zone = (defined $zone ? lc($zone) : ''); |
| 2148 |
my $abbrev = (defined $abb ? lc($abb) : ''); |
| 2149 |
|
| 2150 |
# In some cases, a valid abbreviation is also a valid timezone |
| 2151 |
my $tmp = $dmt->__zone($date2,$offset,$zone,$abbrev,''); |
| 2152 |
if (! $tmp && $abbrev && ! $zone) { |
| 2153 |
$abbrev = $dmt->_zone($abbrev); |
| 2154 |
$tmp = $dmt->__zone($date2,$offset,$abbrev,'','') if ($abbrev); |
| 2155 |
} |
| 2156 |
$zone = $tmp; |
| 2157 |
|
| 2158 |
return (0) if (! $zone); |
| 2159 |
|
| 2160 |
my(@tmp) = $dmt->_convert('_parse_datetime_other',$date2,$tz,$zone); |
| 2161 |
$date2 = $tmp[1]; |
| 2162 |
} |
| 2163 |
|
| 2164 |
@date = @$date2; |
| 2165 |
|
| 2166 |
return (1,@date,$tzstring,$zone,$abb,$off); |
| 2167 |
|
| 2168 |
} elsif (defined($epoch)) { |
| 2169 |
my $date = [1970,1,1,0,0,0]; |
| 2170 |
my @delta = (0,0,$epoch); |
| 2171 |
$date = $dmb->calc_date_time($date,\@delta); |
| 2172 |
my($err); |
| 2173 |
if ($tzstring) { |
| 2174 |
|
| 2175 |
my $offset = (defined $off ? $dmb->_delta_convert('offset',$off) : ''); |
| 2176 |
$zone = (defined $zone ? lc($zone) : ''); |
| 2177 |
my $abbrev = (defined $abb ? lc($abb) : ''); |
| 2178 |
|
| 2179 |
# In some cases, a valid abbreviation is also a valid timezone |
| 2180 |
my $tmp = $dmt->__zone($date,$offset,$zone,$abbrev,''); |
| 2181 |
if (! $tmp && $abbrev && ! $zone) { |
| 2182 |
$abbrev = $dmt->_zone($abbrev); |
| 2183 |
$tmp = $dmt->__zone($date,$offset,$abbrev,'','') if ($abbrev); |
| 2184 |
} |
| 2185 |
$zone = $tmp; |
| 2186 |
|
| 2187 |
return (0) if (! $zone); |
| 2188 |
|
| 2189 |
($err,$date) = $dmt->convert_from_gmt($date,$zone); |
| 2190 |
} else { |
| 2191 |
($err,$date) = $dmt->convert_from_gmt($date); |
| 2192 |
} |
| 2193 |
return (1,@$date,$tzstring,$zone,$abb,$off); |
| 2194 |
|
| 2195 |
} elsif (defined($y)) { |
| 2196 |
my $m = $$dmb{'data'}{'wordmatch'}{'month_abb'}{lc($mmm)}; |
| 2197 |
return (1,$y,$m,$d,$h,$mn,$s,$tzstring,$zone,$abb,$off); |
| 2198 |
} |
| 2199 |
} |
| 2200 |
|
| 2201 |
return (0); |
| 2202 |
} |
| 2203 |
|
| 2204 |
sub _parse_date_other { |
| 2205 |
my($self,$string,$dow,$of,$noupdate) = @_; |
| 2206 |
my $dmt = $$self{'tz'}; |
| 2207 |
my $dmb = $$dmt{'base'}; |
| 2208 |
my($y,$m,$d,$h,$mn,$s); |
| 2209 |
|
| 2210 |
my $rx = (exists $$dmb{'data'}{'rx'}{'other'}{'misc'} ? |
| 2211 |
$$dmb{'data'}{'rx'}{'other'}{'misc'} : |
| 2212 |
$self->_other_rx('misc')); |
| 2213 |
|
| 2214 |
my($mmm,$month,$nextprev,$last,$field_y,$field_m,$field_w,$field_d,$nth); |
| 2215 |
my($special,$got_m,$n,$got_y); |
| 2216 |
|
| 2217 |
if ($string =~ $rx) { |
| 2218 |
($y,$mmm,$month,$nextprev,$last,$field_y,$field_m,$field_w,$field_d,$nth, |
| 2219 |
$special,$n) = |
| 2220 |
@+{qw(y mmm month next last field_y field_m field_w field_d |
| 2221 |
nth special n)}; |
| 2222 |
|
| 2223 |
if (defined($y)) { |
| 2224 |
$y = $dmt->_fix_year($y); |
| 2225 |
$got_y = 1; |
| 2226 |
return () if (! $y); |
| 2227 |
} else { |
| 2228 |
$y = $dmt->_now('y',$$noupdate); |
| 2229 |
$$noupdate = 1; |
| 2230 |
$got_y = 0; |
| 2231 |
$$self{'data'}{'def'}[0] = ''; |
| 2232 |
} |
| 2233 |
|
| 2234 |
if (defined($mmm)) { |
| 2235 |
$m = $$dmb{'data'}{'wordmatch'}{'month_abb'}{lc($mmm)}; |
| 2236 |
$got_m = 1; |
| 2237 |
} elsif ($month) { |
| 2238 |
$m = $$dmb{'data'}{'wordmatch'}{'month_name'}{lc($month)}; |
| 2239 |
$got_m = 1; |
| 2240 |
} |
| 2241 |
|
| 2242 |
if ($nth) { |
| 2243 |
$nth = $$dmb{'data'}{'wordmatch'}{'nth'}{lc($nth)}; |
| 2244 |
} |
| 2245 |
|
| 2246 |
if ($got_m && $nth && ! $dow) { |
| 2247 |
# Dec 1st 1970 |
| 2248 |
# 1st Dec 1970 |
| 2249 |
# 1970 Dec 1st |
| 2250 |
# 1970 1st Dec |
| 2251 |
|
| 2252 |
$d = $nth; |
| 2253 |
|
| 2254 |
} elsif ($nextprev) { |
| 2255 |
|
| 2256 |
my $next = 0; |
| 2257 |
my $sign = -1; |
| 2258 |
if ($$dmb{'data'}{'wordmatch'}{'nextprev'}{lc($nextprev)} == 1) { |
| 2259 |
$next = 1; |
| 2260 |
$sign = 1; |
| 2261 |
} |
| 2262 |
|
| 2263 |
if ($field_y || $field_m || $field_w) { |
| 2264 |
# next/prev year/month/week |
| 2265 |
|
| 2266 |
my(@delta); |
| 2267 |
if ($field_y) { |
| 2268 |
@delta = ($sign*1,0,0,0,0,0,0); |
| 2269 |
} elsif ($field_m) { |
| 2270 |
@delta = (0,$sign*1,0,0,0,0,0); |
| 2271 |
} else { |
| 2272 |
@delta = (0,0,$sign*1,0,0,0,0); |
| 2273 |
} |
| 2274 |
|
| 2275 |
my @now = $dmt->_now('now',$$noupdate); |
| 2276 |
my $tz = $dmt->_now('tz'); |
| 2277 |
my $isdst = $dmt->_now('isdst'); |
| 2278 |
$$noupdate = 1; |
| 2279 |
|
| 2280 |
my($err,$offset,$abbrev,$date2); |
| 2281 |
($err,$date2,$offset,$isdst,$abbrev) = |
| 2282 |
$self->__calc_date_delta([@now],[@delta],0,0,$tz,$isdst); |
| 2283 |
($y,$m,$d,$h,$mn,$s) = @$date2; |
| 2284 |
|
| 2285 |
} elsif ($dow) { |
| 2286 |
# next/prev friday |
| 2287 |
|
| 2288 |
my @now = $dmt->_now('now',$$noupdate); |
| 2289 |
$$noupdate = 1; |
| 2290 |
($y,$m,$d,$h,$mn,$s) = @{ $self->__next_prev(\@now,$next,$dow,0) }; |
| 2291 |
$dow = 0; |
| 2292 |
|
| 2293 |
} else { |
| 2294 |
return (); |
| 2295 |
} |
| 2296 |
|
| 2297 |
} elsif ($last) { |
| 2298 |
|
| 2299 |
if ($field_d && $got_m) { |
| 2300 |
# last day in october 95 |
| 2301 |
|
| 2302 |
$d = $dmb->days_in_month($y,$m); |
| 2303 |
|
| 2304 |
} elsif ($dow && $got_m) { |
| 2305 |
# last friday in october 95 |
| 2306 |
|
| 2307 |
$d = $dmb->days_in_month($y,$m); |
| 2308 |
($y,$m,$d,$h,$mn,$s) = |
| 2309 |
@{ $self->__next_prev([$y,$m,$d,0,0,0],0,$dow,1) }; |
| 2310 |
$dow = 0; |
| 2311 |
|
| 2312 |
} elsif ($dow) { |
| 2313 |
# last friday in 95 |
| 2314 |
|
| 2315 |
($y,$m,$d,$h,$mn,$s) = |
| 2316 |
@{ $self->__next_prev([$y,12,31,0,0,0],0,$dow,0) }; |
| 2317 |
|
| 2318 |
} else { |
| 2319 |
return (); |
| 2320 |
} |
| 2321 |
|
| 2322 |
} elsif ($nth && $dow && ! $field_w) { |
| 2323 |
|
| 2324 |
if ($got_m) { |
| 2325 |
if ($of) { |
| 2326 |
# nth DoW of MMM [YYYY] |
| 2327 |
return () if ($nth > 5); |
| 2328 |
|
| 2329 |
$d = 1; |
| 2330 |
($y,$m,$d,$h,$mn,$s) = |
| 2331 |
@{ $self->__next_prev([$y,$m,1,0,0,0],1,$dow,1) }; |
| 2332 |
my $m2 = $m; |
| 2333 |
($y,$m2,$d) = @{ $dmb->calc_date_days([$y,$m,$d],7*($nth-1)) } |
| 2334 |
if ($nth > 1); |
| 2335 |
return () if (! $m2 || $m2 != $m); |
| 2336 |
|
| 2337 |
} else { |
| 2338 |
# DoW, nth MMM [YYYY] (i.e. Sunday, 9th Dec 2008) |
| 2339 |
$d = $nth; |
| 2340 |
} |
| 2341 |
|
| 2342 |
} else { |
| 2343 |
# nth DoW [in YYYY] |
| 2344 |
|
| 2345 |
($y,$m,$d,$h,$mn,$s) = @{ $self->__next_prev([$y,1,1,0,0,0],1,$dow,1) }; |
| 2346 |
($y,$m,$d) = @{ $dmb->calc_date_days([$y,$m,$d],7*($nth-1)) } |
| 2347 |
if ($nth > 1); |
| 2348 |
} |
| 2349 |
|
| 2350 |
} elsif ($field_w && $dow) { |
| 2351 |
|
| 2352 |
if (defined($n) || $nth) { |
| 2353 |
# sunday week 22 in 1996 |
| 2354 |
# sunday 22nd week in 1996 |
| 2355 |
|
| 2356 |
$n = $nth if ($nth); |
| 2357 |
return () if (! $n); |
| 2358 |
($y,$m,$d) = @{ $dmb->week_of_year($y,$n) }; |
| 2359 |
($y,$m,$d) = @{ $self->__next_prev([$y,$m,$d,0,0,0],1,$dow,1) }; |
| 2360 |
|
| 2361 |
} else { |
| 2362 |
# DoW week |
| 2363 |
|
| 2364 |
($y,$m,$d) = $dmt->_now('now',$$noupdate); |
| 2365 |
$$noupdate = 1; |
| 2366 |
my $tmp = $dmb->_config('firstday'); |
| 2367 |
($y,$m,$d) = @{ $self->__next_prev([$y,$m,$d,0,0,0],1,$tmp,0) }; |
| 2368 |
($y,$m,$d) = @{ $self->__next_prev([$y,$m,$d,0,0,0],1,$dow,1) }; |
| 2369 |
} |
| 2370 |
|
| 2371 |
} elsif ($nth && ! $got_y) { |
| 2372 |
# 'in one week' makes it here too so return nothing in that case so it |
| 2373 |
# drops through to the deltas. |
| 2374 |
return () if ($field_d || $field_w || $field_m || $field_y); |
| 2375 |
($y,$m,$d) = $dmt->_now('now',$$noupdate); |
| 2376 |
$$noupdate = 1; |
| 2377 |
$d = $nth; |
| 2378 |
|
| 2379 |
} elsif ($special) { |
| 2380 |
|
| 2381 |
my $delta = $$dmb{'data'}{'wordmatch'}{'offset_date'}{lc($special)}; |
| 2382 |
my @delta = @{ $dmb->split('delta',$delta) }; |
| 2383 |
($y,$m,$d) = $dmt->_now('now',$$noupdate); |
| 2384 |
my $tz = $dmt->_now('tz'); |
| 2385 |
my $isdst = $dmt->_now('isdst'); |
| 2386 |
$$noupdate = 1; |
| 2387 |
my($err,$offset,$abbrev,$date2); |
| 2388 |
($err,$date2,$offset,$isdst,$abbrev) = |
| 2389 |
$self->__calc_date_delta([$y,$m,$d,0,0,0],[@delta],0,0,$tz,$isdst); |
| 2390 |
($y,$m,$d) = @$date2; |
| 2391 |
|
| 2392 |
if ($field_w) { |
| 2393 |
($y,$m,$d) = @{ $dmb->calc_date_days([$y,$m,$d],7) }; |
| 2394 |
} |
| 2395 |
} |
| 2396 |
|
| 2397 |
} else { |
| 2398 |
return (); |
| 2399 |
} |
| 2400 |
|
| 2401 |
return($y,$m,$d,$dow); |
| 2402 |
} |
| 2403 |
|
| 2404 |
# Supply defaults for missing values (Y/M/D) |
| 2405 |
sub _def_date { |
| 2406 |
my($self,$y,$m,$d,$noupdate) = @_; |
| 2407 |
$y = '' if (! defined $y); |
| 2408 |
$m = '' if (! defined $m); |
| 2409 |
$d = '' if (! defined $d); |
| 2410 |
my $defined = 0; |
| 2411 |
my $dmt = $$self{'tz'}; |
| 2412 |
my $dmb = $$dmt{'base'}; |
| 2413 |
|
| 2414 |
# If year was not specified, defaults to current year. |
| 2415 |
# |
| 2416 |
# We'll also fix the year (turn 2-digit into 4-digit). |
| 2417 |
|
| 2418 |
if ($y eq '') { |
| 2419 |
$y = $dmt->_now('y',$$noupdate); |
| 2420 |
$$noupdate = 1; |
| 2421 |
$$self{'data'}{'def'}[0] = ''; |
| 2422 |
} else { |
| 2423 |
$y = $dmt->_fix_year($y); |
| 2424 |
$defined = 1; |
| 2425 |
} |
| 2426 |
|
| 2427 |
# If the month was not specifed, but the year was, a default of |
| 2428 |
# 01 is supplied (this is a truncated date). |
| 2429 |
# |
| 2430 |
# If neither was specified, month defaults to the current month. |
| 2431 |
|
| 2432 |
if ($m ne '') { |
| 2433 |
$defined = 1; |
| 2434 |
} elsif ($defined) { |
| 2435 |
$m = 1; |
| 2436 |
$$self{'data'}{'def'}[1] = 1; |
| 2437 |
} else { |
| 2438 |
$m = $dmt->_now('m',$$noupdate); |
| 2439 |
$$noupdate = 1; |
| 2440 |
$$self{'data'}{'def'}[1] = ''; |
| 2441 |
} |
| 2442 |
|
| 2443 |
# If the day was not specified, but the year or month was, a default |
| 2444 |
# of 01 is supplied (this is a truncated date). |
| 2445 |
# |
| 2446 |
# If none were specified, it default to the current day. |
| 2447 |
|
| 2448 |
if ($d ne '') { |
| 2449 |
$defined = 1; |
| 2450 |
} elsif ($defined) { |
| 2451 |
$d = 1; |
| 2452 |
$$self{'data'}{'def'}[2] = 1; |
| 2453 |
} else { |
| 2454 |
$d = $dmt->_now('d',$$noupdate); |
| 2455 |
$$noupdate = 1; |
| 2456 |
$$self{'data'}{'def'}[2] = ''; |
| 2457 |
} |
| 2458 |
|
| 2459 |
return($y,$m,$d); |
| 2460 |
} |
| 2461 |
|
| 2462 |
# Supply defaults for missing values (Y/DoY) |
| 2463 |
sub _def_date_doy { |
| 2464 |
my($self,$y,$doy,$noupdate) = @_; |
| 2465 |
$y = '' if (! defined $y); |
| 2466 |
my $dmt = $$self{'tz'}; |
| 2467 |
my $dmb = $$dmt{'base'}; |
| 2468 |
|
| 2469 |
# If year was not specified, defaults to current year. |
| 2470 |
# |
| 2471 |
# We'll also fix the year (turn 2-digit into 4-digit). |
| 2472 |
|
| 2473 |
if ($y eq '') { |
| 2474 |
$y = $dmt->_now('y',$$noupdate); |
| 2475 |
$$noupdate = 1; |
| 2476 |
$$self{'data'}{'def'}[0] = ''; |
| 2477 |
} else { |
| 2478 |
$y = $dmt->_fix_year($y); |
| 2479 |
} |
| 2480 |
|
| 2481 |
# DoY must be specified. |
| 2482 |
|
| 2483 |
my($m,$d); |
| 2484 |
my $ymd = $dmb->day_of_year($y,$doy); |
| 2485 |
|
| 2486 |
return @$ymd; |
| 2487 |
} |
| 2488 |
|
| 2489 |
# Supply defaults for missing values (YY/Www/D) and (Y/Www/D) |
| 2490 |
sub _def_date_dow { |
| 2491 |
my($self,$y,$w,$dow,$noupdate) = @_; |
| 2492 |
$y = '' if (! defined $y); |
| 2493 |
$w = '' if (! defined $w); |
| 2494 |
$dow = '' if (! defined $dow); |
| 2495 |
my $dmt = $$self{'tz'}; |
| 2496 |
my $dmb = $$dmt{'base'}; |
| 2497 |
|
| 2498 |
# If year was not specified, defaults to current year. |
| 2499 |
# |
| 2500 |
# If it was specified and is a single digit, it is the |
| 2501 |
# year in the current decade. |
| 2502 |
# |
| 2503 |
# We'll also fix the year (turn 2-digit into 4-digit). |
| 2504 |
|
| 2505 |
if ($y ne '') { |
| 2506 |
if (length($y) == 1) { |
| 2507 |
my $tmp = $dmt->_now('y',$$noupdate); |
| 2508 |
$tmp =~ s/.$/$y/; |
| 2509 |
$y = $tmp; |
| 2510 |
$$noupdate = 1; |
| 2511 |
|
| 2512 |
} else { |
| 2513 |
$y = $dmt->_fix_year($y); |
| 2514 |
|
| 2515 |
} |
| 2516 |
|
| 2517 |
} else { |
| 2518 |
$y = $dmt->_now('y',$$noupdate); |
| 2519 |
$$noupdate = 1; |
| 2520 |
$$self{'data'}{'def'}[0] = ''; |
| 2521 |
} |
| 2522 |
|
| 2523 |
# If week was not specified, it defaults to the current |
| 2524 |
# week. Get the first day of the week. |
| 2525 |
|
| 2526 |
my($m,$d); |
| 2527 |
if ($w ne '') { |
| 2528 |
($y,$m,$d) = @{ $dmb->week_of_year($y,$w) }; |
| 2529 |
} else { |
| 2530 |
my($nowy,$nowm,$nowd) = $dmt->_now('now',$$noupdate); |
| 2531 |
$$noupdate = 1; |
| 2532 |
my $noww; |
| 2533 |
($nowy,$noww) = $dmb->week_of_year([$nowy,$nowm,$nowd]); |
| 2534 |
($y,$m,$d) = @{ $dmb->week_of_year($nowy,$noww) }; |
| 2535 |
} |
| 2536 |
|
| 2537 |
# Handle the DoW |
| 2538 |
|
| 2539 |
if ($dow eq '') { |
| 2540 |
$dow = 1; |
| 2541 |
} |
| 2542 |
my $n = $dmb->days_in_month($y,$m); |
| 2543 |
$d += ($dow-1); |
| 2544 |
if ($d > $n) { |
| 2545 |
$m++; |
| 2546 |
if ($m==13) { |
| 2547 |
$y++; |
| 2548 |
$m = 1; |
| 2549 |
} |
| 2550 |
$d = $d-$n; |
| 2551 |
} |
| 2552 |
|
| 2553 |
return($y,$m,$d); |
| 2554 |
} |
| 2555 |
|
| 2556 |
# Supply defaults for missing values (HH:MN:SS) |
| 2557 |
sub _def_time { |
| 2558 |
my($self,$h,$m,$s,$noupdate) = @_; |
| 2559 |
$h = '' if (! defined $h); |
| 2560 |
$m = '' if (! defined $m); |
| 2561 |
$s = '' if (! defined $s); |
| 2562 |
my $defined = 0; |
| 2563 |
my $dmt = $$self{'tz'}; |
| 2564 |
my $dmb = $$dmt{'base'}; |
| 2565 |
|
| 2566 |
# If no time was specified, defaults to 00:00:00. |
| 2567 |
|
| 2568 |
if ($h eq '' && |
| 2569 |
$m eq '' && |
| 2570 |
$s eq '') { |
| 2571 |
$$self{'data'}{'def'}[3] = 1; |
| 2572 |
$$self{'data'}{'def'}[4] = 1; |
| 2573 |
$$self{'data'}{'def'}[5] = 1; |
| 2574 |
return(0,0,0); |
| 2575 |
} |
| 2576 |
|
| 2577 |
# If hour was not specified, defaults to current hour. |
| 2578 |
|
| 2579 |
if ($h ne '') { |
| 2580 |
$defined = 1; |
| 2581 |
} else { |
| 2582 |
$h = $dmt->_now('h',$$noupdate); |
| 2583 |
$$noupdate = 1; |
| 2584 |
$$self{'data'}{'def'}[3] = ''; |
| 2585 |
} |
| 2586 |
|
| 2587 |
# If the minute was not specifed, but the hour was, a default of |
| 2588 |
# 00 is supplied (this is a truncated time). |
| 2589 |
# |
| 2590 |
# If neither was specified, minute defaults to the current minute. |
| 2591 |
|
| 2592 |
if ($m ne '') { |
| 2593 |
$defined = 1; |
| 2594 |
} elsif ($defined) { |
| 2595 |
$m = 0; |
| 2596 |
$$self{'data'}{'def'}[4] = 1; |
| 2597 |
} else { |
| 2598 |
$m = $dmt->_now('mn',$$noupdate); |
| 2599 |
$$noupdate = 1; |
| 2600 |
$$self{'data'}{'def'}[4] = ''; |
| 2601 |
} |
| 2602 |
|
| 2603 |
# If the second was not specified (either the hour or the minute were), |
| 2604 |
# a default of 00 is supplied (this is a truncated time). |
| 2605 |
|
| 2606 |
if ($s eq '') { |
| 2607 |
$s = 0; |
| 2608 |
$$self{'data'}{'def'}[5] = 1; |
| 2609 |
} |
| 2610 |
|
| 2611 |
return($h,$m,$s); |
| 2612 |
} |
| 2613 |
|
| 2614 |
######################################################################## |
| 2615 |
# OTHER DATE METHODS |
| 2616 |
######################################################################## |
| 2617 |
|
| 2618 |
# Gets the date in the parsed timezone (if $type = ''), local timezone |
| 2619 |
# (if $type = 'local') or GMT timezone (if $type = 'gmt'). |
| 2620 |
# |
| 2621 |
# Gets the string value in scalar context, the split value in list |
| 2622 |
# context. |
| 2623 |
# |
| 2624 |
sub value { |
| 2625 |
my($self,$type) = @_; |
| 2626 |
my $dmt = $$self{'tz'}; |
| 2627 |
my $dmb = $$dmt{'base'}; |
| 2628 |
my $date; |
| 2629 |
|
| 2630 |
while (1) { |
| 2631 |
if (! $$self{'data'}{'set'}) { |
| 2632 |
$$self{'err'} = '[value] Object does not contain a date'; |
| 2633 |
last; |
| 2634 |
} |
| 2635 |
|
| 2636 |
$type = '' if (! $type); |
| 2637 |
|
| 2638 |
if ($type eq 'gmt') { |
| 2639 |
|
| 2640 |
if (! @{ $$self{'data'}{'gmt'} }) { |
| 2641 |
my $zone = $$self{'data'}{'tz'}; |
| 2642 |
my $date = $$self{'data'}{'date'}; |
| 2643 |
|
| 2644 |
if ($zone eq 'Etc/GMT') { |
| 2645 |
$$self{'data'}{'gmt'} = $date; |
| 2646 |
|
| 2647 |
} else { |
| 2648 |
my $isdst = $$self{'data'}{'isdst'}; |
| 2649 |
my($err,$d) = $dmt->convert_to_gmt($date,$zone,$isdst); |
| 2650 |
if ($err) { |
| 2651 |
$$self{'err'} = '[value] Unable to convert date to GMT'; |
| 2652 |
last; |
| 2653 |
} |
| 2654 |
$$self{'data'}{'gmt'} = $d; |
| 2655 |
} |
| 2656 |
} |
| 2657 |
$date = $$self{'data'}{'gmt'}; |
| 2658 |
|
| 2659 |
} elsif ($type eq 'local') { |
| 2660 |
|
| 2661 |
if (! @{ $$self{'data'}{'loc'} }) { |
| 2662 |
my $zone = $$self{'data'}{'tz'}; |
| 2663 |
$date = $$self{'data'}{'date'}; |
| 2664 |
my $local = $dmt->_now('tz',1); |
| 2665 |
|
| 2666 |
if ($zone eq $local) { |
| 2667 |
$$self{'data'}{'loc'} = $date; |
| 2668 |
|
| 2669 |
} else { |
| 2670 |
my $isdst = $$self{'data'}{'isdst'}; |
| 2671 |
my($err,$d) = $dmt->convert_to_local($date,$zone,$isdst); |
| 2672 |
if ($err) { |
| 2673 |
$$self{'err'} = '[value] Unable to convert date to localtime'; |
| 2674 |
last; |
| 2675 |
} |
| 2676 |
$$self{'data'}{'loc'} = $d; |
| 2677 |
} |
| 2678 |
} |
| 2679 |
$date = $$self{'data'}{'loc'}; |
| 2680 |
|
| 2681 |
} else { |
| 2682 |
|
| 2683 |
$date = $$self{'data'}{'date'}; |
| 2684 |
|
| 2685 |
} |
| 2686 |
|
| 2687 |
last; |
| 2688 |
} |
| 2689 |
|
| 2690 |
if ($$self{'err'}) { |
| 2691 |
if (wantarray) { |
| 2692 |
return (); |
| 2693 |
} else { |
| 2694 |
return ''; |
| 2695 |
} |
| 2696 |
} |
| 2697 |
|
| 2698 |
if (wantarray) { |
| 2699 |
return @$date; |
| 2700 |
} else { |
| 2701 |
return $dmb->join('date',$date); |
| 2702 |
} |
| 2703 |
} |
| 2704 |
|
| 2705 |
sub cmp { |
| 2706 |
my($self,$date) = @_; |
| 2707 |
if ($$self{'err'} || ! $$self{'data'}{'set'}) { |
| 2708 |
carp "WARNING: [cmp] Arguments must be valid dates: date1"; |
| 2709 |
return undef; |
| 2710 |
} |
| 2711 |
|
| 2712 |
if (! (ref($date) eq 'Date::Manip::Date')) { |
| 2713 |
carp "WARNING: [cmp] Argument must be a Date::Manip::Date object"; |
| 2714 |
return undef; |
| 2715 |
} |
| 2716 |
if ($$date{'err'} || ! $$date{'data'}{'set'}) { |
| 2717 |
carp "WARNING: [cmp] Arguments must be valid dates: date2"; |
| 2718 |
return undef; |
| 2719 |
} |
| 2720 |
|
| 2721 |
my($d1,$d2); |
| 2722 |
if ($$self{'data'}{'tz'} eq $$date{'data'}{'tz'}) { |
| 2723 |
$d1 = $self->value(); |
| 2724 |
$d2 = $date->value(); |
| 2725 |
} else { |
| 2726 |
$d1 = $self->value('gmt'); |
| 2727 |
$d2 = $date->value('gmt'); |
| 2728 |
} |
| 2729 |
|
| 2730 |
return ($d1 cmp $d2); |
| 2731 |
} |
| 2732 |
|
| 2733 |
BEGIN { |
| 2734 |
my %field = qw(y 0 m 1 d 2 h 3 mn 4 s 5); |
| 2735 |
|
| 2736 |
sub set { |
| 2737 |
my($self,$field,@val) = @_; |
| 2738 |
$field = lc($field); |
| 2739 |
my $dmt = $$self{'tz'}; |
| 2740 |
my $dmb = $$dmt{'base'}; |
| 2741 |
|
| 2742 |
# Make sure $self includes a valid date (unless the entire date is |
| 2743 |
# being set, in which case it doesn't matter). |
| 2744 |
|
| 2745 |
my $date = []; |
| 2746 |
my(@def,$tz,$isdst); |
| 2747 |
|
| 2748 |
if ($field eq 'zdate') { |
| 2749 |
# If {data}{set} = 2, we want to preserve the defaults. Also, we've |
| 2750 |
# already initialized. |
| 2751 |
# |
| 2752 |
# It is only set in the parse routines which means that this was |
| 2753 |
# called via _parse_check. |
| 2754 |
|
| 2755 |
$self->_init() if ($$self{'data'}{'set'} != 2); |
| 2756 |
@def = @{ $$self{'data'}{'def'} }; |
| 2757 |
|
| 2758 |
} elsif ($field eq 'date') { |
| 2759 |
if ($$self{'data'}{'set'} && ! $$self{'err'}) { |
| 2760 |
$tz = $$self{'data'}{'tz'}; |
| 2761 |
} else { |
| 2762 |
$tz = $dmt->_now('tz',1); |
| 2763 |
} |
| 2764 |
$self->_init(); |
| 2765 |
@def = @{ $$self{'data'}{'def'} }; |
| 2766 |
|
| 2767 |
} else { |
| 2768 |
return 1 if ($$self{'err'} || ! $$self{'data'}{'set'}); |
| 2769 |
$date = $$self{'data'}{'date'}; |
| 2770 |
$tz = $$self{'data'}{'tz'}; |
| 2771 |
$isdst = $$self{'data'}{'isdst'}; |
| 2772 |
@def = @{ $$self{'data'}{'def'} }; |
| 2773 |
$self->_init(); |
| 2774 |
} |
| 2775 |
|
| 2776 |
# Check the arguments |
| 2777 |
|
| 2778 |
my($err,$new_tz,$new_date,$new_time); |
| 2779 |
|
| 2780 |
if ($field eq 'date') { |
| 2781 |
|
| 2782 |
if ($#val == 0) { |
| 2783 |
# date,DATE |
| 2784 |
$new_date = $val[0]; |
| 2785 |
} elsif ($#val == 1) { |
| 2786 |
# date,DATE,ISDST |
| 2787 |
($new_date,$isdst) = @val; |
| 2788 |
} else { |
| 2789 |
$err = 1; |
| 2790 |
} |
| 2791 |
for (my $i=0; $i<=5; $i++) { |
| 2792 |
$def[$i] = 0 if ($def[$i]); |
| 2793 |
} |
| 2794 |
|
| 2795 |
} elsif ($field eq 'time') { |
| 2796 |
|
| 2797 |
if ($#val == 0) { |
| 2798 |
# time,TIME |
| 2799 |
$new_time = $val[0]; |
| 2800 |
} elsif ($#val == 1) { |
| 2801 |
# time,TIME,ISDST |
| 2802 |
($new_time,$isdst) = @val; |
| 2803 |
} else { |
| 2804 |
$err = 1; |
| 2805 |
} |
| 2806 |
$def[3] = 0 if ($def[3]); |
| 2807 |
$def[4] = 0 if ($def[4]); |
| 2808 |
$def[5] = 0 if ($def[5]); |
| 2809 |
|
| 2810 |
} elsif ($field eq 'zdate') { |
| 2811 |
|
| 2812 |
if ($#val == 0) { |
| 2813 |
# zdate,DATE |
| 2814 |
$new_date = $val[0]; |
| 2815 |
} elsif ($#val == 1 && ($val[1] eq '0' || $val[1] eq '1')) { |
| 2816 |
# zdate,DATE,ISDST |
| 2817 |
($new_date,$isdst) = @val; |
| 2818 |
} elsif ($#val == 1) { |
| 2819 |
# zdate,ZONE,DATE |
| 2820 |
($new_tz,$new_date) = @val; |
| 2821 |
} elsif ($#val == 2) { |
| 2822 |
# zdate,ZONE,DATE,ISDST |
| 2823 |
($new_tz,$new_date,$isdst) = @val; |
| 2824 |
} else { |
| 2825 |
$err = 1; |
| 2826 |
} |
| 2827 |
if ($$self{'data'}{'set'} != 2) { |
| 2828 |
for (my $i=0; $i<=5; $i++) { |
| 2829 |
$def[$i] = 0 if ($def[$i]); |
| 2830 |
} |
| 2831 |
} |
| 2832 |
$tz = $dmt->_now('tz',1) if (! $new_tz); |
| 2833 |
|
| 2834 |
} elsif ($field eq 'zone') { |
| 2835 |
|
| 2836 |
if ($#val == -1) { |
| 2837 |
# zone |
| 2838 |
} elsif ($#val == 0 && ($val[0] eq '0' || $val[0] eq '1')) { |
| 2839 |
# zone,ISDST |
| 2840 |
$isdst = $val[0]; |
| 2841 |
} elsif ($#val == 0) { |
| 2842 |
# zone,ZONE |
| 2843 |
$new_tz = $val[0]; |
| 2844 |
} elsif ($#val == 1) { |
| 2845 |
# zone,ZONE,ISDST |
| 2846 |
($new_tz,$isdst) = @val; |
| 2847 |
} else { |
| 2848 |
$err = 1; |
| 2849 |
} |
| 2850 |
$tz = $dmt->_now('tz',1) if (! $new_tz); |
| 2851 |
|
| 2852 |
} elsif (exists $field{$field}) { |
| 2853 |
|
| 2854 |
my $i = $field{$field}; |
| 2855 |
my $val; |
| 2856 |
if ($#val == 0) { |
| 2857 |
$val = $val[0]; |
| 2858 |
} elsif ($#val == 1) { |
| 2859 |
($val,$isdst) = @val; |
| 2860 |
} else { |
| 2861 |
$err = 1; |
| 2862 |
} |
| 2863 |
|
| 2864 |
$$date[$i] = $val; |
| 2865 |
$def[$i] = 0 if ($def[$i]); |
| 2866 |
|
| 2867 |
} else { |
| 2868 |
|
| 2869 |
$err = 2; |
| 2870 |
|
| 2871 |
} |
| 2872 |
|
| 2873 |
if ($err) { |
| 2874 |
if ($err == 1) { |
| 2875 |
$$self{'err'} = '[set] Invalid arguments'; |
| 2876 |
} else { |
| 2877 |
$$self{'err'} = '[set] Invalid field'; |
| 2878 |
} |
| 2879 |
return 1; |
| 2880 |
} |
| 2881 |
|
| 2882 |
# Handle the arguments (it can be a zone or an offset) |
| 2883 |
|
| 2884 |
if ($new_tz) { |
| 2885 |
my $tmp = $dmt->_zone($new_tz); |
| 2886 |
if ($tmp) { |
| 2887 |
# A zone/alias |
| 2888 |
$tz = $tmp; |
| 2889 |
|
| 2890 |
} else { |
| 2891 |
# An offset |
| 2892 |
|
| 2893 |
my $dstflag = ''; |
| 2894 |
$dstflag = ($isdst ? 'dstonly' : 'stdonly') if (defined $isdst); |
| 2895 |
|
| 2896 |
$tz = $dmb->__zone($date,lc($new_tz),'',$dstflag); |
| 2897 |
|
| 2898 |
if (! $tz) { |
| 2899 |
$$self{'err'} = "[set] Invalid timezone argument: $new_tz"; |
| 2900 |
return 1; |
| 2901 |
} |
| 2902 |
} |
| 2903 |
} |
| 2904 |
|
| 2905 |
if ($new_date) { |
| 2906 |
if ($dmb->check($new_date)) { |
| 2907 |
$date = $new_date; |
| 2908 |
} else { |
| 2909 |
$$self{'err'} = '[set] Invalid date argument'; |
| 2910 |
return 1; |
| 2911 |
} |
| 2912 |
} |
| 2913 |
|
| 2914 |
if ($new_time) { |
| 2915 |
if ($dmb->check_time($new_time)) { |
| 2916 |
$$date[3] = $$new_time[0]; |
| 2917 |
$$date[4] = $$new_time[1]; |
| 2918 |
$$date[5] = $$new_time[2]; |
| 2919 |
} else { |
| 2920 |
$$self{'err'} = '[set] Invalid time argument'; |
| 2921 |
return 1; |
| 2922 |
} |
| 2923 |
} |
| 2924 |
|
| 2925 |
# Check the date/timezone combination |
| 2926 |
|
| 2927 |
my($abb,$off); |
| 2928 |
if ($tz eq 'etc/gmt') { |
| 2929 |
$abb = 'GMT'; |
| 2930 |
$off = [0,0,0]; |
| 2931 |
$isdst = 0; |
| 2932 |
} else { |
| 2933 |
my $per = $dmt->date_period($date,$tz,1,$isdst); |
| 2934 |
if (! $per) { |
| 2935 |
$$self{'err'} = '[set] Invalid date/timezone'; |
| 2936 |
return 1; |
| 2937 |
} |
| 2938 |
$isdst = $$per[5]; |
| 2939 |
$abb = $$per[4]; |
| 2940 |
$off = $$per[3]; |
| 2941 |
} |
| 2942 |
|
| 2943 |
# Set the information |
| 2944 |
|
| 2945 |
$$self{'data'}{'set'} = 1; |
| 2946 |
$$self{'data'}{'date'} = $date; |
| 2947 |
$$self{'data'}{'tz'} = $tz; |
| 2948 |
$$self{'data'}{'isdst'} = $isdst; |
| 2949 |
$$self{'data'}{'offset'}= $off; |
| 2950 |
$$self{'data'}{'abb'} = $abb; |
| 2951 |
$$self{'data'}{'def'} = [ @def ]; |
| 2952 |
|
| 2953 |
return 0; |
| 2954 |
} |
| 2955 |
} |
| 2956 |
|
| 2957 |
######################################################################## |
| 2958 |
# NEXT/PREV METHODS |
| 2959 |
|
| 2960 |
sub prev { |
| 2961 |
my($self,@args) = @_; |
| 2962 |
return 1 if ($$self{'err'} || ! $$self{'data'}{'set'}); |
| 2963 |
my $date = $$self{'data'}{'date'}; |
| 2964 |
|
| 2965 |
$date = $self->__next_prev($date,0,@args); |
| 2966 |
|
| 2967 |
return 1 if (! defined($date)); |
| 2968 |
$self->set('date',$date); |
| 2969 |
return 0; |
| 2970 |
} |
| 2971 |
|
| 2972 |
sub next { |
| 2973 |
my($self,@args) = @_; |
| 2974 |
return 1 if ($$self{'err'} || ! $$self{'data'}{'set'}); |
| 2975 |
my $date = $$self{'data'}{'date'}; |
| 2976 |
|
| 2977 |
$date = $self->__next_prev($date,1,@args); |
| 2978 |
|
| 2979 |
return 1 if (! defined($date)); |
| 2980 |
$self->set('date',$date); |
| 2981 |
return 0; |
| 2982 |
} |
| 2983 |
|
| 2984 |
sub __next_prev { |
| 2985 |
my($self,$date,$next,$dow,$curr,$time) = @_; |
| 2986 |
|
| 2987 |
my ($caller,$sign,$prev); |
| 2988 |
if ($next) { |
| 2989 |
$caller = 'next'; |
| 2990 |
$sign = 1; |
| 2991 |
$prev = 0; |
| 2992 |
} else { |
| 2993 |
$caller = 'prev'; |
| 2994 |
$sign = -1; |
| 2995 |
$prev = 1; |
| 2996 |
} |
| 2997 |
|
| 2998 |
my $dmt = $$self{'tz'}; |
| 2999 |
my $dmb = $$dmt{'base'}; |
| 3000 |
my $orig = [ @$date ]; |
| 3001 |
|
| 3002 |
# Check the time (if any) |
| 3003 |
|
| 3004 |
if (defined($time)) { |
| 3005 |
if ($dow) { |
| 3006 |
# $time will refer to a full [H,MN,S] |
| 3007 |
my($err,$h,$mn,$s) = $dmb->_hms_fields({ 'out' => 'list' },$time); |
| 3008 |
if ($err) { |
| 3009 |
$$self{'err'} = "[$caller] invalid time argument"; |
| 3010 |
return undef; |
| 3011 |
} |
| 3012 |
$time = [$h,$mn,$s]; |
| 3013 |
} else { |
| 3014 |
# $time may have leading undefs |
| 3015 |
my @tmp = @$time; |
| 3016 |
if ($#tmp != 2) { |
| 3017 |
$$self{'err'} = "[$caller] invalid time argument"; |
| 3018 |
return undef; |
| 3019 |
} |
| 3020 |
my($h,$mn,$s) = @$time; |
| 3021 |
if (defined($h)) { |
| 3022 |
$mn = 0 if (! defined($mn)); |
| 3023 |
$s = 0 if (! defined($s)); |
| 3024 |
} elsif (defined($mn)) { |
| 3025 |
$s = 0 if (! defined($s)); |
| 3026 |
} else { |
| 3027 |
$s = 0 if (! defined($s)); |
| 3028 |
} |
| 3029 |
$time = [$h,$mn,$s]; |
| 3030 |
} |
| 3031 |
} |
| 3032 |
|
| 3033 |
# Find the next DoW |
| 3034 |
|
| 3035 |
if ($dow) { |
| 3036 |
|
| 3037 |
if (! $dmb->_is_int($dow,1,7)) { |
| 3038 |
$$self{'err'} = "[$caller] Invalid DOW: $dow"; |
| 3039 |
return undef; |
| 3040 |
} |
| 3041 |
|
| 3042 |
# Find the next/previous occurrence of DoW |
| 3043 |
|
| 3044 |
my $curr_dow = $dmb->day_of_week($date); |
| 3045 |
my $adjust = 0; |
| 3046 |
|
| 3047 |
if ($dow == $curr_dow) { |
| 3048 |
$adjust = 1 if ($curr == 0); |
| 3049 |
|
| 3050 |
} else { |
| 3051 |
my $num; |
| 3052 |
if ($next) { |
| 3053 |
# force $dow to be more than $curr_dow |
| 3054 |
$dow += 7 if ($dow<$curr_dow); |
| 3055 |
$num = $dow - $curr_dow; |
| 3056 |
} else { |
| 3057 |
# force $dow to be less than $curr_dow |
| 3058 |
$dow -= 7 if ($dow>$curr_dow); |
| 3059 |
$num = $curr_dow - $dow; |
| 3060 |
$num *= -1; |
| 3061 |
} |
| 3062 |
|
| 3063 |
# Add/subtract $num days |
| 3064 |
$date = $dmb->calc_date_days($date,$num); |
| 3065 |
} |
| 3066 |
|
| 3067 |
if (defined($time)) { |
| 3068 |
my ($y,$m,$d,$h,$mn,$s) = @$date; |
| 3069 |
($h,$mn,$s) = @$time; |
| 3070 |
$date = [$y,$m,$d,$h,$mn,$s]; |
| 3071 |
} |
| 3072 |
|
| 3073 |
my $cmp = $dmb->cmp($orig,$date); |
| 3074 |
$adjust = 1 if ($curr == 2 && $cmp != -1*$sign); |
| 3075 |
|
| 3076 |
if ($adjust) { |
| 3077 |
# Add/subtract 1 week |
| 3078 |
$date = $dmb->calc_date_days($date,$sign*7); |
| 3079 |
} |
| 3080 |
|
| 3081 |
return $date; |
| 3082 |
} |
| 3083 |
|
| 3084 |
# Find the next Time |
| 3085 |
|
| 3086 |
if (defined($time)) { |
| 3087 |
|
| 3088 |
my ($h,$mn,$s) = @$time; |
| 3089 |
my $orig = [ @$date ]; |
| 3090 |
|
| 3091 |
my $cmp; |
| 3092 |
if (defined $h) { |
| 3093 |
# Find next/prev HH:MN:SS |
| 3094 |
|
| 3095 |
@$date[3..5] = @$time; |
| 3096 |
$cmp = $dmb->cmp($orig,$date); |
| 3097 |
if ($cmp == -1) { |
| 3098 |
if ($prev) { |
| 3099 |
$date = $dmb->calc_date_days($date,-1); |
| 3100 |
} |
| 3101 |
} elsif ($cmp == 1) { |
| 3102 |
if ($next) { |
| 3103 |
$date = $dmb->calc_date_days($date,1); |
| 3104 |
} |
| 3105 |
} else { |
| 3106 |
if (! $curr) { |
| 3107 |
$date = $dmb->calc_date_days($date,$sign); |
| 3108 |
} |
| 3109 |
} |
| 3110 |
|
| 3111 |
} elsif (defined $mn) { |
| 3112 |
# Find next/prev MN:SS |
| 3113 |
|
| 3114 |
@$date[4..5] = @$time[1..2]; |
| 3115 |
|
| 3116 |
$cmp = $dmb->cmp($orig,$date); |
| 3117 |
if ($cmp == -1) { |
| 3118 |
if ($prev) { |
| 3119 |
$date = $dmb->calc_date_time($date,[-1,0,0]); |
| 3120 |
} |
| 3121 |
} elsif ($cmp == 1) { |
| 3122 |
if ($next) { |
| 3123 |
$date = $dmb->calc_date_time($date,[1,0,0]); |
| 3124 |
} |
| 3125 |
} else { |
| 3126 |
if (! $curr) { |
| 3127 |
$date = $dmb->calc_date_time($date,[$sign,0,0]); |
| 3128 |
} |
| 3129 |
} |
| 3130 |
|
| 3131 |
} else { |
| 3132 |
# Find next/prev SS |
| 3133 |
|
| 3134 |
$$date[5] = $$time[2]; |
| 3135 |
|
| 3136 |
$cmp = $dmb->cmp($orig,$date); |
| 3137 |
if ($cmp == -1) { |
| 3138 |
if ($prev) { |
| 3139 |
$date = $dmb->calc_date_time($date,[0,-1,0]); |
| 3140 |
} |
| 3141 |
} elsif ($cmp == 1) { |
| 3142 |
if ($next) { |
| 3143 |
$date = $dmb->calc_date_time($date,[0,1,0]); |
| 3144 |
} |
| 3145 |
} else { |
| 3146 |
if (! $curr) { |
| 3147 |
$date = $dmb->calc_date_time($date,[0,$sign,0]); |
| 3148 |
} |
| 3149 |
} |
| 3150 |
} |
| 3151 |
|
| 3152 |
return $date; |
| 3153 |
} |
| 3154 |
|
| 3155 |
$$self{'err'} = "[$caller] Either DoW or time (or both) required"; |
| 3156 |
return undef; |
| 3157 |
} |
| 3158 |
|
| 3159 |
######################################################################## |
| 3160 |
# CALC METHOD |
| 3161 |
|
| 3162 |
sub calc { |
| 3163 |
my($self,$obj,@args) = @_; |
| 3164 |
|
| 3165 |
if (ref($obj) eq 'Date::Manip::Date') { |
| 3166 |
return $self->_calc_date_date($obj,@args); |
| 3167 |
|
| 3168 |
} elsif (ref($obj) eq 'Date::Manip::Delta') { |
| 3169 |
return $self->_calc_date_delta($obj,@args); |
| 3170 |
|
| 3171 |
} else { |
| 3172 |
return undef; |
| 3173 |
} |
| 3174 |
} |
| 3175 |
|
| 3176 |
sub _calc_date_date { |
| 3177 |
my($self,$date,@args) = @_; |
| 3178 |
my $ret = $self->new_delta(); |
| 3179 |
|
| 3180 |
if ($$self{'err'} || ! $$self{'data'}{'set'}) { |
| 3181 |
$$ret{'err'} = '[calc] First object invalid (date)'; |
| 3182 |
return $ret; |
| 3183 |
} |
| 3184 |
|
| 3185 |
if ($$date{'err'} || ! $$date{'data'}{'set'}) { |
| 3186 |
$$ret{'err'} = '[calc] Second object invalid (date)'; |
| 3187 |
return $ret; |
| 3188 |
} |
| 3189 |
|
| 3190 |
# Handle subtract/mode arguments |
| 3191 |
|
| 3192 |
my($subtract,$mode); |
| 3193 |
|
| 3194 |
if ($#args == -1) { |
| 3195 |
($subtract,$mode) = (0,''); |
| 3196 |
} elsif ($#args == 0) { |
| 3197 |
if ($args[0] eq '0' || $args[0] eq '1') { |
| 3198 |
($subtract,$mode) = ($args[0],''); |
| 3199 |
} else { |
| 3200 |
($subtract,$mode) = (0,$args[0]); |
| 3201 |
} |
| 3202 |
|
| 3203 |
} elsif ($#args == 1) { |
| 3204 |
($subtract,$mode) = @args; |
| 3205 |
} else { |
| 3206 |
$$ret{'err'} = '[calc] Invalid arguments'; |
| 3207 |
return $ret; |
| 3208 |
} |
| 3209 |
$mode = 'exact' if (! $mode); |
| 3210 |
|
| 3211 |
if ($mode !~ /^(business|bsemi|bapprox|approx|semi|exact)$/i) { |
| 3212 |
$$ret{'err'} = '[calc] Invalid mode argument'; |
| 3213 |
return $ret; |
| 3214 |
} |
| 3215 |
|
| 3216 |
# if business mode |
| 3217 |
# dates must be in the same timezone |
| 3218 |
# use dates in that zone |
| 3219 |
# |
| 3220 |
# otherwise if both dates are in the same timezone && approx/semi mode |
| 3221 |
# use the dates in that zone |
| 3222 |
# |
| 3223 |
# otherwise |
| 3224 |
# convert to gmt |
| 3225 |
# use those dates |
| 3226 |
|
| 3227 |
my($date1,$date2,$tz1,$isdst1,$tz2,$isdst2); |
| 3228 |
if ($mode eq 'business' || $mode eq 'bapprox' || $mode eq 'bsemi') { |
| 3229 |
if ($$self{'data'}{'tz'} eq $$date{'data'}{'tz'}) { |
| 3230 |
$date1 = [ $self->value() ]; |
| 3231 |
$date2 = [ $date->value() ]; |
| 3232 |
$tz1 = $$self{'data'}{'tz'}; |
| 3233 |
$tz2 = $tz1; |
| 3234 |
$isdst1 = $$self{'data'}{'isdst'}; |
| 3235 |
$isdst2 = $$date{'data'}{'isdst'}; |
| 3236 |
} else { |
| 3237 |
$$ret{'err'} = '[calc] Dates must be in the same timezone for ' . |
| 3238 |
'business mode calculations'; |
| 3239 |
return $ret; |
| 3240 |
} |
| 3241 |
|
| 3242 |
} elsif (($mode eq 'approx' || $mode eq 'semi') && |
| 3243 |
$$self{'data'}{'tz'} eq $$date{'data'}{'tz'}) { |
| 3244 |
$date1 = [ $self->value() ]; |
| 3245 |
$date2 = [ $date->value() ]; |
| 3246 |
$tz1 = $$self{'data'}{'tz'}; |
| 3247 |
$tz2 = $tz1; |
| 3248 |
$isdst1 = $$self{'data'}{'isdst'}; |
| 3249 |
$isdst2 = $$date{'data'}{'isdst'}; |
| 3250 |
|
| 3251 |
} else { |
| 3252 |
$date1 = [ $self->value('gmt') ]; |
| 3253 |
$date2 = [ $date->value('gmt') ]; |
| 3254 |
$tz1 = 'GMT'; |
| 3255 |
$tz2 = $tz1; |
| 3256 |
$isdst1 = 0; |
| 3257 |
$isdst2 = 0; |
| 3258 |
} |
| 3259 |
|
| 3260 |
# Do the calculation |
| 3261 |
|
| 3262 |
my(@delta); |
| 3263 |
if ($subtract) { |
| 3264 |
if ($mode eq 'business' || $mode eq 'exact' || $subtract == 2) { |
| 3265 |
@delta = @{ $self->__calc_date_date($mode,$date2,$tz2,$isdst2, |
| 3266 |
$date1,$tz1,$isdst1) }; |
| 3267 |
} else { |
| 3268 |
@delta = @{ $self->__calc_date_date($mode,$date1,$tz1,$isdst1, |
| 3269 |
$date2,$tz2,$isdst2) }; |
| 3270 |
@delta = map { -1*$_ } @delta; |
| 3271 |
} |
| 3272 |
} else { |
| 3273 |
@delta = @{ $self->__calc_date_date($mode,$date1,$tz1,$isdst1, |
| 3274 |
$date2,$tz2,$isdst2) }; |
| 3275 |
} |
| 3276 |
|
| 3277 |
# Save the delta |
| 3278 |
|
| 3279 |
if ($mode eq 'business' || $mode eq 'bapprox' || $mode eq 'bsemi') { |
| 3280 |
$ret->set('business',\@delta); |
| 3281 |
} else { |
| 3282 |
$ret->set('delta',\@delta); |
| 3283 |
} |
| 3284 |
return $ret; |
| 3285 |
} |
| 3286 |
|
| 3287 |
sub __calc_date_date { |
| 3288 |
my($self,$mode,$date1,$tz1,$isdst1,$date2,$tz2,$isdst2) = @_; |
| 3289 |
my $dmt = $$self{'tz'}; |
| 3290 |
my $dmb = $$dmt{'base'}; |
| 3291 |
|
| 3292 |
my($dy,$dm,$dw,$dd,$dh,$dmn,$ds) = (0,0,0,0,0,0,0); |
| 3293 |
|
| 3294 |
if ($mode eq 'approx' || $mode eq 'bapprox') { |
| 3295 |
my($y1,$m1,$d1,$h1,$mn1,$s1) = @$date1; |
| 3296 |
my($y2,$m2,$d2,$h2,$mn2,$s2) = @$date2; |
| 3297 |
$dy = $y2-$y1; |
| 3298 |
$dm = $m2-$m1; |
| 3299 |
|
| 3300 |
if ($dy || $dm) { |
| 3301 |
# If $d1 is greater than the number of days allowed in the |
| 3302 |
# month $y2/$m2, set it equal to the number of days. In other |
| 3303 |
# words: |
| 3304 |
# Jan 31 2006 to Feb 28 2008 = 2 years 1 month |
| 3305 |
# |
| 3306 |
my $dim = $dmb->days_in_month($y2,$m2); |
| 3307 |
$d1 = $dim if ($d1 > $dim); |
| 3308 |
|
| 3309 |
$date1 = [$y2,$m2,$d1,$h1,$mn1,$s1]; |
| 3310 |
} |
| 3311 |
} |
| 3312 |
|
| 3313 |
if ($mode eq 'semi' || $mode eq 'approx') { |
| 3314 |
|
| 3315 |
# Calculate the number of weeks/days apart (temporarily ignoring |
| 3316 |
# DST effects). |
| 3317 |
|
| 3318 |
$dd = $dmb->days_since_1BC($date2) - |
| 3319 |
$dmb->days_since_1BC($date1); |
| 3320 |
$dw = int($dd/7); |
| 3321 |
$dd -= $dw*7; |
| 3322 |
|
| 3323 |
# Adding $dd to $date1 gives: ($y2,$m2,$d2, $h1,$mn1,$s1) |
| 3324 |
# Make sure this is valid (taking into account DST effects). |
| 3325 |
# If it isn't, make it valid. |
| 3326 |
|
| 3327 |
if ($dw || $dd) { |
| 3328 |
my($y1,$m1,$d1,$h1,$mn1,$s1) = @$date1; |
| 3329 |
my($y2,$m2,$d2,$h2,$mn2,$s2) = @$date2; |
| 3330 |
$date1 = [$y2,$m2,$d2,$h1,$mn1,$s1]; |
| 3331 |
} |
| 3332 |
if ($dy || $dm || $dw || $dd) { |
| 3333 |
my $force = ( ($dw > 0 || $dd > 0) ? 1 : -1 ); |
| 3334 |
my($off,$isdst,$abb); |
| 3335 |
($date1,$off,$isdst,$abb) = |
| 3336 |
$self->_calc_date_check_dst($date1,$tz2,$isdst2,$force); |
| 3337 |
} |
| 3338 |
} |
| 3339 |
|
| 3340 |
if ($mode eq 'bsemi' || $mode eq 'bapprox') { |
| 3341 |
# Calculate the number of weeks. Ignore the days |
| 3342 |
# part. Also, since there are no DST effects, we don't |
| 3343 |
# have to check for validity. |
| 3344 |
|
| 3345 |
$dd = $dmb->days_since_1BC($date2) - |
| 3346 |
$dmb->days_since_1BC($date1); |
| 3347 |
$dw = int($dd/7); |
| 3348 |
$dd = 0; |
| 3349 |
$date1 = $dmb->calc_date_days($date1,$dw*7); |
| 3350 |
} |
| 3351 |
|
| 3352 |
if ($mode eq 'exact' || $mode eq 'semi' || $mode eq 'approx') { |
| 3353 |
my $sec1 = $dmb->secs_since_1970($date1); |
| 3354 |
my $sec2 = $dmb->secs_since_1970($date2); |
| 3355 |
$ds = $sec2 - $sec1; |
| 3356 |
|
| 3357 |
{ |
| 3358 |
no integer; |
| 3359 |
$dh = int($ds/3600); |
| 3360 |
$ds -= $dh*3600; |
| 3361 |
} |
| 3362 |
$dmn = int($ds/60); |
| 3363 |
$ds -= $dmn*60; |
| 3364 |
} |
| 3365 |
|
| 3366 |
if ($mode eq 'business' || $mode eq 'bsemi' || $mode eq 'bapprox') { |
| 3367 |
|
| 3368 |
# Make sure both are work days |
| 3369 |
|
| 3370 |
$date1 = $self->__nextprev_business_day(0,0,1,$date1); |
| 3371 |
$date2 = $self->__nextprev_business_day(0,0,1,$date2); |
| 3372 |
|
| 3373 |
my($y1,$m1,$d1,$h1,$mn1,$s1) = @$date1; |
| 3374 |
my($y2,$m2,$d2,$h2,$mn2,$s2) = @$date2; |
| 3375 |
|
| 3376 |
# Find out which direction we need to move $date1 to get to $date2 |
| 3377 |
|
| 3378 |
my $dir = 0; |
| 3379 |
if ($y1 < $y2) { |
| 3380 |
$dir = 1; |
| 3381 |
} elsif ($y1 > $y2) { |
| 3382 |
$dir = -1; |
| 3383 |
} elsif ($m1 < $m2) { |
| 3384 |
$dir = 1; |
| 3385 |
} elsif ($m1 > $m2) { |
| 3386 |
$dir = -1; |
| 3387 |
} elsif ($d1 < $d2) { |
| 3388 |
$dir = 1; |
| 3389 |
} elsif ($d1 > $d2) { |
| 3390 |
$dir = -1; |
| 3391 |
} |
| 3392 |
|
| 3393 |
# Now do the day part (to get to the same day) |
| 3394 |
|
| 3395 |
$dd = 0; |
| 3396 |
while ($dir) { |
| 3397 |
($y1,$m1,$d1) = @{ $dmb->calc_date_days([$y1,$m1,$d1],$dir) }; |
| 3398 |
$dd += $dir if ($self->__is_business_day([$y1,$m1,$d1,0,0,0],0)); |
| 3399 |
$dir = 0 if ($y1 == $y2 && $m1 == $m2 && $d1 == $d2); |
| 3400 |
} |
| 3401 |
|
| 3402 |
# Both dates are now on a business day, and during business |
| 3403 |
# hours, so do the hr/min/sec part trivially |
| 3404 |
|
| 3405 |
$dh = $h2-$h1; |
| 3406 |
$dmn = $mn2-$mn1; |
| 3407 |
$ds = $s2-$s1; |
| 3408 |
} |
| 3409 |
|
| 3410 |
return [ $dy,$dm,$dw,$dd,$dh,$dmn,$ds ]; |
| 3411 |
} |
| 3412 |
|
| 3413 |
no integer; |
| 3414 |
sub _calc_date_delta { |
| 3415 |
my($self,$delta,$subtract) = @_; |
| 3416 |
my $ret = $self->new_date(); |
| 3417 |
|
| 3418 |
if ($$self{'err'} || ! $$self{'data'}{'set'}) { |
| 3419 |
$$ret{'err'} = '[calc] Date object invalid'; |
| 3420 |
return $ret; |
| 3421 |
} |
| 3422 |
|
| 3423 |
if ($$delta{'err'}) { |
| 3424 |
$$ret{'err'} = '[calc] Delta object invalid'; |
| 3425 |
return $ret; |
| 3426 |
} |
| 3427 |
|
| 3428 |
# Get the date/delta fields |
| 3429 |
|
| 3430 |
$subtract = 0 if (! $subtract); |
| 3431 |
my @delta = @{ $$delta{'data'}{'delta'} }; |
| 3432 |
my @date = @{ $$self{'data'}{'date'} }; |
| 3433 |
my $business = ($$delta{'data'}{'mode'} eq 'business' ? 1 : 0); |
| 3434 |
my $tz = $$self{'data'}{'tz'}; |
| 3435 |
my $isdst = $$self{'data'}{'isdst'}; |
| 3436 |
|
| 3437 |
# We can't handle a delta longer than 10000 years |
| 3438 |
my($dy,$dm,$dw,$dd,$dh,$dmn,$ds) = @delta; |
| 3439 |
if (abs($dy) > 10000 || |
| 3440 |
abs($dm) > 120000 || # 10000*12 |
| 3441 |
abs($dw) > 530000 || # 10000*53 |
| 3442 |
abs($dd) > 3660000 || # 10000*366 |
| 3443 |
abs($dh) > 87840000 || # 10000*366*24 |
| 3444 |
abs($dmn) > 5270400000 || # 10000*366*24*60 |
| 3445 |
abs($ds) > 316224000000) { # 10000*366*24*60*60 |
| 3446 |
$$ret{'err'} = '[calc] Delta too large'; |
| 3447 |
return $ret; |
| 3448 |
} |
| 3449 |
|
| 3450 |
my($err,$date2,$offset,$abbrev); |
| 3451 |
($err,$date2,$offset,$isdst,$abbrev) = |
| 3452 |
$self->__calc_date_delta([@date],[@delta],$subtract,$business,$tz,$isdst); |
| 3453 |
|
| 3454 |
if (ref($date2) eq 'ARRAY' && ($$date2[0]<0 || $$date2[0]>9999)) { |
| 3455 |
$$ret{'err'} = '[calc] Delta produces date outside valid range'; |
| 3456 |
} elsif ($err) { |
| 3457 |
$$ret{'err'} = '[calc] Unable to perform calculation'; |
| 3458 |
} else { |
| 3459 |
$$ret{'data'}{'set'} = 1; |
| 3460 |
$$ret{'data'}{'date'} = $date2; |
| 3461 |
$$ret{'data'}{'tz'} = $tz; |
| 3462 |
$$ret{'data'}{'isdst'} = $isdst; |
| 3463 |
$$ret{'data'}{'offset'}= $offset; |
| 3464 |
$$ret{'data'}{'abb'} = $abbrev; |
| 3465 |
} |
| 3466 |
return $ret; |
| 3467 |
} |
| 3468 |
use integer; |
| 3469 |
|
| 3470 |
sub __calc_date_delta { |
| 3471 |
my($self,$date,$delta,$subtract,$business,$tz,$isdst) = @_; |
| 3472 |
|
| 3473 |
my ($dy,$dm,$dw,$dd,$dh,$dmn,$ds) = @$delta; |
| 3474 |
my @date = @$date; |
| 3475 |
|
| 3476 |
my ($err,$date2,$offset,$abbrev); |
| 3477 |
|
| 3478 |
# In business mode, daylight saving time is ignored, so days are |
| 3479 |
# of a constant, known length, so they'll be done in the exact |
| 3480 |
# function. Otherwise, they'll be done in the approximate function. |
| 3481 |
# |
| 3482 |
# Also in business mode, if $subtract = 2, then the starting date |
| 3483 |
# must be a business date or an error occurs. |
| 3484 |
|
| 3485 |
my($dd_exact,$dd_approx); |
| 3486 |
if ($business) { |
| 3487 |
$dd_exact = $dd; |
| 3488 |
$dd_approx = 0; |
| 3489 |
|
| 3490 |
if ($subtract == 2 && ! $self->__is_business_day($date,1)) { |
| 3491 |
return (1); |
| 3492 |
} |
| 3493 |
|
| 3494 |
} else { |
| 3495 |
$dd_exact = 0; |
| 3496 |
$dd_approx = $dd; |
| 3497 |
} |
| 3498 |
|
| 3499 |
if ($subtract == 2 && ($dy || $dm || $dw || $dd_approx)) { |
| 3500 |
# For subtract=2: |
| 3501 |
# DATE = RET + DELTA |
| 3502 |
# |
| 3503 |
# The delta consisists of an approximate part (which is added first) |
| 3504 |
# and an exact part (added second): |
| 3505 |
# DATE = RET + DELTA(approx) + DELTA(exact) |
| 3506 |
# DATE = RET' + DELTA(exact) |
| 3507 |
# where RET' = RET + DELTA(approx) |
| 3508 |
# |
| 3509 |
# For an exact delta, subtract==2 and subtract==1 are equivalent, |
| 3510 |
# so this can be written: |
| 3511 |
# DATE - DELTA(exact) = RET' |
| 3512 |
# |
| 3513 |
# So the inverse subtract only needs to include the approximate |
| 3514 |
# portion of the delta. |
| 3515 |
|
| 3516 |
($err,$date2,$offset,$isdst,$abbrev) = |
| 3517 |
$self->__calc_date_delta_exact([@date],[-1*$dd_exact,-1*$dh,-1*$dmn,-1*$ds], |
| 3518 |
$business,$tz,$isdst); |
| 3519 |
|
| 3520 |
($err,$date2,$offset,$isdst,$abbrev) = |
| 3521 |
$self->__calc_date_delta_inverse($date2,[$dy,$dm,$dw,$dd_approx], |
| 3522 |
$business,$tz,$isdst) |
| 3523 |
if (! $err); |
| 3524 |
|
| 3525 |
} else { |
| 3526 |
# We'll add the approximate part, followed by the exact part. |
| 3527 |
# After the approximate part, we need to make sure we're on |
| 3528 |
# a valid business day in business mode. |
| 3529 |
|
| 3530 |
($dy,$dm,$dw,$dd_exact,$dd_approx,$dh,$dmn,$ds) = |
| 3531 |
map { -1*$_ } ($dy,$dm,$dw,$dd_exact,$dd_approx,$dh,$dmn,$ds) |
| 3532 |
if ($subtract); |
| 3533 |
@$date2 = @date; |
| 3534 |
|
| 3535 |
if ($dy || $dm || $dw || $dd) { |
| 3536 |
($err,$date2,$offset,$isdst,$abbrev) = |
| 3537 |
$self->__calc_date_delta_approx($date2,[$dy,$dm,$dw,$dd_approx], |
| 3538 |
$business,$tz,$isdst); |
| 3539 |
} elsif ($business) { |
| 3540 |
$date2 = $self->__nextprev_business_day(0,0,1,$date2); |
| 3541 |
} |
| 3542 |
|
| 3543 |
($err,$date2,$offset,$isdst,$abbrev) = |
| 3544 |
$self->__calc_date_delta_exact($date2,[$dd_exact,$dh,$dmn,$ds], |
| 3545 |
$business,$tz,$isdst) |
| 3546 |
if (! $err && ($dd_exact || $dh || $dmn || $ds)); |
| 3547 |
} |
| 3548 |
|
| 3549 |
return($err,$date2,$offset,$isdst,$abbrev); |
| 3550 |
} |
| 3551 |
|
| 3552 |
# Do the inverse part of a calculation. |
| 3553 |
# |
| 3554 |
# $delta = [$dy,$dm,$dw,$dd] |
| 3555 |
# |
| 3556 |
sub __calc_date_delta_inverse { |
| 3557 |
my($self,$date,$delta,$business,$tz,$isdst) = @_; |
| 3558 |
my $dmt = $$self{'tz'}; |
| 3559 |
my $dmb = $$dmt{'base'}; |
| 3560 |
my @date2; |
| 3561 |
|
| 3562 |
# Given: DATE1, DELTA |
| 3563 |
# Find: DATE2 |
| 3564 |
# where DATE2 + DELTA = DATE1 |
| 3565 |
# |
| 3566 |
# Start with: |
| 3567 |
# DATE2 = DATE1 - DELTA |
| 3568 |
# |
| 3569 |
# if (DATE2+DELTA < DATE1) |
| 3570 |
# while (1) |
| 3571 |
# DATE2 = DATE2 + 1 day |
| 3572 |
# if DATE2+DELTA < DATE1 |
| 3573 |
# next |
| 3574 |
# elsif DATE2+DELTA > DATE1 |
| 3575 |
# return ERROR |
| 3576 |
# else |
| 3577 |
# return DATE2 |
| 3578 |
# done |
| 3579 |
# |
| 3580 |
# elsif (DATE2+DELTA > DATE1) |
| 3581 |
# while (1) |
| 3582 |
# DATE2 = DATE2 - 1 day |
| 3583 |
# if DATE2+DELTA > DATE1 |
| 3584 |
# next |
| 3585 |
# elsif DATE2+DELTA < DATE1 |
| 3586 |
# return ERROR |
| 3587 |
# else |
| 3588 |
# return DATE2 |
| 3589 |
# done |
| 3590 |
# |
| 3591 |
# else |
| 3592 |
# return DATE2 |
| 3593 |
|
| 3594 |
if ($business) { |
| 3595 |
|
| 3596 |
my $date1 = $date; |
| 3597 |
my ($err,$date2,$off,$isd,$abb,@del,$tmp,$cmp); |
| 3598 |
@del = map { $_*-1 } @$delta; |
| 3599 |
|
| 3600 |
($err,$date2,$off,$isd,$abb) = |
| 3601 |
$self->__calc_date_delta_approx($date,[@del],$business,$tz,$isdst); |
| 3602 |
|
| 3603 |
($err,$tmp,$off,$isd,$abb) = |
| 3604 |
$self->__calc_date_delta_approx($date2,$delta,$business,$tz,$isdst); |
| 3605 |
|
| 3606 |
$cmp = $self->_cmp_date($tmp,$date1); |
| 3607 |
|
| 3608 |
if ($cmp < 0) { |
| 3609 |
while (1) { |
| 3610 |
$date2 = $self->__nextprev_business_day(0,1,0,$date2); |
| 3611 |
($err,$tmp,$off,$isd,$abb) = |
| 3612 |
$self->__calc_date_delta_approx($date2,$delta,$business,$tz,$isdst); |
| 3613 |
$cmp = $self->_cmp_date($tmp,$date1); |
| 3614 |
if ($cmp < 0) { |
| 3615 |
next; |
| 3616 |
} elsif ($cmp > 0) { |
| 3617 |
return (1); |
| 3618 |
} else { |
| 3619 |
last; |
| 3620 |
} |
| 3621 |
} |
| 3622 |
|
| 3623 |
} elsif ($cmp > 0) { |
| 3624 |
while (1) { |
| 3625 |
$date2 = $self->__nextprev_business_day(1,1,0,$date2); |
| 3626 |
($err,$tmp,$off,$isd,$abb) = |
| 3627 |
$self->__calc_date_delta_approx($date2,$delta,$business,$tz,$isdst); |
| 3628 |
$cmp = $self->_cmp_date($tmp,$date1); |
| 3629 |
if ($cmp > 0) { |
| 3630 |
next; |
| 3631 |
} elsif ($cmp < 0) { |
| 3632 |
return (1); |
| 3633 |
} else { |
| 3634 |
last; |
| 3635 |
} |
| 3636 |
} |
| 3637 |
} |
| 3638 |
|
| 3639 |
@date2 = @$date2; |
| 3640 |
|
| 3641 |
} else { |
| 3642 |
|
| 3643 |
my @tmp = @$date[0..2]; # [y,m,d] |
| 3644 |
my @hms = @$date[3..5]; # [h,m,s] |
| 3645 |
my $date1 = [@tmp]; |
| 3646 |
|
| 3647 |
my $date2 = $dmb->_calc_date_ymwd($date1,$delta,1); |
| 3648 |
my $tmp = $dmb->_calc_date_ymwd($date2,$delta); |
| 3649 |
my $cmp = $self->_cmp_date($tmp,$date1); |
| 3650 |
|
| 3651 |
if ($cmp < 0) { |
| 3652 |
while (1) { |
| 3653 |
$date2 = $dmb->calc_date_days($date2,1); |
| 3654 |
$tmp = $dmb->_calc_date_ymwd($date2,$delta); |
| 3655 |
$cmp = $self->_cmp_date($tmp,$date1); |
| 3656 |
if ($cmp < 0) { |
| 3657 |
next; |
| 3658 |
} elsif ($cmp > 0) { |
| 3659 |
return (1); |
| 3660 |
} else { |
| 3661 |
last; |
| 3662 |
} |
| 3663 |
} |
| 3664 |
|
| 3665 |
} elsif ($cmp > 0) { |
| 3666 |
while (1) { |
| 3667 |
$date2 = $dmb->calc_date_days($date2,-1); |
| 3668 |
$tmp = $dmb->_calc_date_ymwd($date2,$delta); |
| 3669 |
$cmp = $self->_cmp_date($tmp,$date1); |
| 3670 |
if ($cmp > 0) { |
| 3671 |
next; |
| 3672 |
} elsif ($cmp < 0) { |
| 3673 |
return (1); |
| 3674 |
} else { |
| 3675 |
last; |
| 3676 |
} |
| 3677 |
} |
| 3678 |
} |
| 3679 |
|
| 3680 |
@date2 = (@$date2,@hms); |
| 3681 |
} |
| 3682 |
|
| 3683 |
# Make sure DATE2 is valid (within DST constraints) and |
| 3684 |
# return it. |
| 3685 |
|
| 3686 |
my($date2,$abb,$off,$err); |
| 3687 |
($date2,$off,$isdst,$abb) = $self->_calc_date_check_dst([@date2],$tz,$isdst,0); |
| 3688 |
|
| 3689 |
return (1) if (! defined($date2)); |
| 3690 |
return (0,$date2,$off,$isdst,$abb); |
| 3691 |
} |
| 3692 |
|
| 3693 |
sub _cmp_date { |
| 3694 |
my($self,$date0,$date1) = @_; |
| 3695 |
return ($$date0[0] <=> $$date1[0] || |
| 3696 |
$$date0[1] <=> $$date1[1] || |
| 3697 |
$$date0[2] <=> $$date1[2]); |
| 3698 |
} |
| 3699 |
|
| 3700 |
# Do the approximate part of a calculation. |
| 3701 |
# |
| 3702 |
sub __calc_date_delta_approx { |
| 3703 |
my($self,$date,$delta,$business,$tz,$isdst) = @_; |
| 3704 |
|
| 3705 |
my $dmt = $$self{'tz'}; |
| 3706 |
my $dmb = $$dmt{'base'}; |
| 3707 |
my($y,$m,$d,$h,$mn,$s) = @$date; |
| 3708 |
my($dy,$dm,$dw,$dd) = @$delta; |
| 3709 |
|
| 3710 |
# |
| 3711 |
# Do the year/month part. |
| 3712 |
# |
| 3713 |
# If we are past the last day of a month, move the date back to |
| 3714 |
# the last day of the month. i.e. Jan 31 + 1 month = Feb 28. |
| 3715 |
# |
| 3716 |
|
| 3717 |
$y += $dy if ($dy); |
| 3718 |
$dmb->_mod_add(-12,$dm,\$m,\$y) # -12 means 1-12 instead of 0-11 |
| 3719 |
if ($dm); |
| 3720 |
|
| 3721 |
my $dim = $dmb->days_in_month($y,$m); |
| 3722 |
$d = $dim if ($d > $dim); |
| 3723 |
|
| 3724 |
# |
| 3725 |
# Do the week part. |
| 3726 |
# |
| 3727 |
# The week is treated as 7 days for both business and non-business |
| 3728 |
# calculations. |
| 3729 |
# |
| 3730 |
# In a business calculation, make sure we're on a business date. |
| 3731 |
# |
| 3732 |
|
| 3733 |
if ($business) { |
| 3734 |
($y,$m,$d) = @{ $dmb->calc_date_days([$y,$m,$d],$dw*7) } if ($dw); |
| 3735 |
($y,$m,$d,$h,$mn,$s) = |
| 3736 |
@{ $self->__nextprev_business_day(0,0,1,[$y,$m,$d,$h,$mn,$s]) }; |
| 3737 |
} else { |
| 3738 |
$dd += $dw*7; |
| 3739 |
} |
| 3740 |
|
| 3741 |
# |
| 3742 |
# Now do the day part. $dd is always 0 in business calculations. |
| 3743 |
# |
| 3744 |
|
| 3745 |
if ($dd) { |
| 3746 |
($y,$m,$d) = @{ $dmb->calc_date_days([$y,$m,$d],$dd) }; |
| 3747 |
} |
| 3748 |
|
| 3749 |
# |
| 3750 |
# At this point, we need to make sure that we're a valid date |
| 3751 |
# (within the constraints of DST). |
| 3752 |
# |
| 3753 |
# If it is not valid in this offset, try the other one. If neither |
| 3754 |
# works, then we want the the date to be 24 hours later than the |
| 3755 |
# previous day at this time (if $dd > 0) or 24 hours earlier than |
| 3756 |
# the next day at this time (if $dd < 0). We'll use the 24 hour |
| 3757 |
# definition even for business days, but then we'll double check |
| 3758 |
# that the resulting date is a business date. |
| 3759 |
# |
| 3760 |
|
| 3761 |
my $force = ( ($dd > 0 || $dw > 0 || $dm > 0 || $dy > 0) ? 1 : -1 ); |
| 3762 |
my($off,$abb); |
| 3763 |
($date,$off,$isdst,$abb) = |
| 3764 |
$self->_calc_date_check_dst([$y,$m,$d,$h,$mn,$s],$tz,$isdst,$force); |
| 3765 |
return (0,$date,$off,$isdst,$abb); |
| 3766 |
} |
| 3767 |
|
| 3768 |
# Do the exact part of a calculation. |
| 3769 |
# |
| 3770 |
sub __calc_date_delta_exact { |
| 3771 |
my($self,$date,$delta,$business,$tz,$isdst) = @_; |
| 3772 |
my $dmt = $$self{'tz'}; |
| 3773 |
my $dmb = $$dmt{'base'}; |
| 3774 |
|
| 3775 |
if ($business) { |
| 3776 |
|
| 3777 |
# Simplify hours/minutes/seconds where the day length is defined |
| 3778 |
# by the start/end of the business day. |
| 3779 |
|
| 3780 |
my ($dd,$dh,$dmn,$ds) = @$delta; |
| 3781 |
my ($y,$m,$d,$h,$mn,$s)= @$date; |
| 3782 |
my ($hbeg,$mbeg,$sbeg) = @{ $$dmb{'data'}{'calc'}{'workdaybeg'} }; |
| 3783 |
my ($hend,$mend,$send) = @{ $$dmb{'data'}{'calc'}{'workdayend'} }; |
| 3784 |
my $bdlen = $$dmb{'data'}{'len'}{'bdlength'}; |
| 3785 |
|
| 3786 |
no integer; |
| 3787 |
my $tmp; |
| 3788 |
$ds += $dh*3600 + $dmn*60; |
| 3789 |
$tmp = int($ds/$bdlen); |
| 3790 |
$dd += $tmp; |
| 3791 |
$ds -= $tmp*$bdlen; |
| 3792 |
$dh = int($ds/3600); |
| 3793 |
$ds -= $dh*3600; |
| 3794 |
$dmn = int($ds/60); |
| 3795 |
$ds -= $dmn*60; |
| 3796 |
use integer; |
| 3797 |
|
| 3798 |
if ($dd) { |
| 3799 |
my $prev = 0; |
| 3800 |
if ($dd < 1) { |
| 3801 |
$prev = 1; |
| 3802 |
$dd *= -1; |
| 3803 |
} |
| 3804 |
|
| 3805 |
($y,$m,$d,$h,$mn,$s) = |
| 3806 |
@{ $self->__nextprev_business_day($prev,$dd,0,[$y,$m,$d,$h,$mn,$s]) }; |
| 3807 |
} |
| 3808 |
|
| 3809 |
# At this point, we're adding less than a day for the |
| 3810 |
# hours/minutes/seconds part AND we know that the current |
| 3811 |
# day is during business hours. |
| 3812 |
# |
| 3813 |
# We'll add them (without affecting days... we'll need to |
| 3814 |
# test things by hand to make sure we should or shouldn't |
| 3815 |
# do that. |
| 3816 |
|
| 3817 |
$dmb->_mod_add(60,$ds,\$s,\$mn); |
| 3818 |
$dmb->_mod_add(60,$dmn,\$mn,\$h); |
| 3819 |
$h += $dh; |
| 3820 |
# Note: it's possible that $h > 23 at this point or $h < 0 |
| 3821 |
|
| 3822 |
if ($h > $hend || |
| 3823 |
($h == $hend && $mn > $mend) || |
| 3824 |
($h == $hend && $mn == $mend && $s > $send) || |
| 3825 |
($h == $hend && $mn == $mend && $s == $send)) { |
| 3826 |
|
| 3827 |
# We've gone past the end of the business day. |
| 3828 |
|
| 3829 |
my $t2 = $dmb->calc_time_time([$h,$mn,$s],[$hend,$mend,$send],1); |
| 3830 |
|
| 3831 |
while (1) { |
| 3832 |
($y,$m,$d) = @{ $dmb->calc_date_days([$y,$m,$d],1) }; |
| 3833 |
last if ($self->__is_business_day([$y,$m,$d,$h,$mn,$s])); |
| 3834 |
} |
| 3835 |
|
| 3836 |
($h,$mn,$s) = @{ $dmb->calc_time_time([$hbeg,$mbeg,$sbeg],$t2) }; |
| 3837 |
|
| 3838 |
} elsif ($h < $hbeg || |
| 3839 |
($h == $hbeg && $mn < $mbeg) || |
| 3840 |
($h == $hbeg && $mn == $mbeg && $s < $sbeg)) { |
| 3841 |
|
| 3842 |
# We've gone back past the start of the business day. |
| 3843 |
|
| 3844 |
my $t2 = $dmb->calc_time_time([$hbeg,$mbeg,$sbeg],[$h,$mn,$s],1); |
| 3845 |
|
| 3846 |
while (1) { |
| 3847 |
($y,$m,$d) = @{ $dmb->calc_date_days([$y,$m,$d],-1) }; |
| 3848 |
last if ($self->__is_business_day([$y,$m,$d,$h,$mn,$s])); |
| 3849 |
} |
| 3850 |
|
| 3851 |
($h,$mn,$s) = @{ $dmb->calc_time_time([$hend,$mend,$send],$t2,1) }; |
| 3852 |
} |
| 3853 |
|
| 3854 |
# Now make sure that the date is valid within DST constraints. |
| 3855 |
|
| 3856 |
my $force = ( ($dd > 0 || $dh > 0 || $dmn > 0 || $ds > 0) ? 1 : -1 ); |
| 3857 |
my($off,$abb); |
| 3858 |
($date,$off,$isdst,$abb) = |
| 3859 |
$self->_calc_date_check_dst([$y,$m,$d,$h,$mn,$s],$tz,$isdst,$force); |
| 3860 |
return (0,$date,$off,$isdst,$abb); |
| 3861 |
|
| 3862 |
} else { |
| 3863 |
|
| 3864 |
# Convert to GTM |
| 3865 |
# Do the calculation |
| 3866 |
# Convert back |
| 3867 |
|
| 3868 |
my ($dd,$dh,$dm,$ds) = @$delta; # $dd is always 0 |
| 3869 |
my $del = [$dh,$dm,$ds]; |
| 3870 |
my ($err,$offset,$abbrev); |
| 3871 |
|
| 3872 |
($err,$date,$offset,$isdst,$abbrev) = |
| 3873 |
$dmt->_convert('__calc_date_delta_exact',$date,$tz,'GMT',$isdst); |
| 3874 |
|
| 3875 |
$date = $dmb->calc_date_time($date,$del,0); |
| 3876 |
return($err,$date,$offset,$isdst,$abbrev) |
| 3877 |
if ($$date[0] < 0 || $$date[0] > 9999); |
| 3878 |
|
| 3879 |
($err,$date,$offset,$isdst,$abbrev) = |
| 3880 |
$dmt->_convert('__calc_date_delta_exact',$date,'GMT',$tz,$isdst); |
| 3881 |
|
| 3882 |
return($err,$date,$offset,$isdst,$abbrev); |
| 3883 |
} |
| 3884 |
} |
| 3885 |
|
| 3886 |
# This checks to see which time (STD or DST) a date is in. It checks |
| 3887 |
# $isdst first, and the other value (1-$isdst) second. |
| 3888 |
# |
| 3889 |
# If the date is found in either time, it is returned. |
| 3890 |
# |
| 3891 |
# If the date is NOT found, then we got here by adding/subtracting 1 day |
| 3892 |
# from a different value, and we've obtained an invalid value. In this |
| 3893 |
# case, if $force = 0, then return nothing. |
| 3894 |
# |
| 3895 |
# If $force = 1, then go to the previous day and add 24 hours. If force |
| 3896 |
# is -1, then go to the next day and subtract 24 hours. |
| 3897 |
# |
| 3898 |
# Returns: |
| 3899 |
# ($date,$off,$isdst,$abb) |
| 3900 |
# or |
| 3901 |
# (undef) |
| 3902 |
# |
| 3903 |
sub _calc_date_check_dst { |
| 3904 |
my($self,$date,$tz,$isdst,$force) = @_; |
| 3905 |
my $dmt = $$self{'tz'}; |
| 3906 |
my $dmb = $$dmt{'base'}; |
| 3907 |
my($abb,$off,$err); |
| 3908 |
|
| 3909 |
# Try the date as is in both ISDST and 1-ISDST times |
| 3910 |
|
| 3911 |
my $per = $dmt->date_period($date,$tz,1,$isdst); |
| 3912 |
if ($per) { |
| 3913 |
$abb = $$per[4]; |
| 3914 |
$off = $$per[3]; |
| 3915 |
return($date,$off,$isdst,$abb); |
| 3916 |
} |
| 3917 |
|
| 3918 |
$per = $dmt->date_period($date,$tz,1,1-$isdst); |
| 3919 |
if ($per) { |
| 3920 |
$isdst = 1-$isdst; |
| 3921 |
$abb = $$per[4]; |
| 3922 |
$off = $$per[3]; |
| 3923 |
return($date,$off,$isdst,$abb); |
| 3924 |
} |
| 3925 |
|
| 3926 |
# If we made it here, the date is invalid in this timezone. |
| 3927 |
# Either return undef, or add/subtract a day from the date |
| 3928 |
# and find out what time period we're in (all we care about |
| 3929 |
# is the ISDST value). |
| 3930 |
|
| 3931 |
if (! $force) { |
| 3932 |
return(undef); |
| 3933 |
} |
| 3934 |
|
| 3935 |
my($dd); |
| 3936 |
if ($force > 0) { |
| 3937 |
$date = $dmb->calc_date_days($date,-1); |
| 3938 |
$dd = 1; |
| 3939 |
} else { |
| 3940 |
$date = $dmb->calc_date_days($date,+1); |
| 3941 |
$dd = -1; |
| 3942 |
} |
| 3943 |
|
| 3944 |
$per = $dmt->date_period($date,$tz,1,$isdst); |
| 3945 |
$isdst = (1-$isdst) if (! $per); |
| 3946 |
|
| 3947 |
# Now, convert it to GMT, add/subtract 24 hours, and convert |
| 3948 |
# it back. |
| 3949 |
|
| 3950 |
($err,$date,$off,$isdst,$abb) = $dmt->convert_to_gmt($date,$tz,$isdst); |
| 3951 |
$date = $dmb->calc_date_days($date,$dd); |
| 3952 |
($err,$date,$off,$isdst,$abb) = $dmt->convert_from_gmt($date,$tz); |
| 3953 |
|
| 3954 |
return($date,$off,$isdst,$abb); |
| 3955 |
} |
| 3956 |
|
| 3957 |
######################################################################## |
| 3958 |
# MISC METHODS |
| 3959 |
|
| 3960 |
sub secs_since_1970_GMT { |
| 3961 |
my($self,$secs) = @_; |
| 3962 |
|
| 3963 |
my $dmt = $$self{'tz'}; |
| 3964 |
my $dmb = $$dmt{'base'}; |
| 3965 |
|
| 3966 |
if (defined $secs) { |
| 3967 |
my $date = $dmb->secs_since_1970($secs); |
| 3968 |
my $err; |
| 3969 |
($err,$date) = $dmt->convert_from_gmt($date); |
| 3970 |
return 1 if ($err); |
| 3971 |
$self->set('date',$date); |
| 3972 |
return 0; |
| 3973 |
} |
| 3974 |
|
| 3975 |
if ($$self{'err'} || ! $$self{'data'}{'set'}) { |
| 3976 |
carp "WARNING: [secs_since_1970_GMT] Object must contain a valid date"; |
| 3977 |
return undef; |
| 3978 |
} |
| 3979 |
|
| 3980 |
my @date = $self->value('gmt'); |
| 3981 |
$secs = $dmb->secs_since_1970(\@date); |
| 3982 |
return $secs; |
| 3983 |
} |
| 3984 |
|
| 3985 |
sub week_of_year { |
| 3986 |
my($self,$first) = @_; |
| 3987 |
if ($$self{'err'} || ! $$self{'data'}{'set'}) { |
| 3988 |
carp "WARNING: [week_of_year] Object must contain a valid date"; |
| 3989 |
return undef; |
| 3990 |
} |
| 3991 |
|
| 3992 |
my $dmt = $$self{'tz'}; |
| 3993 |
my $dmb = $$dmt{'base'}; |
| 3994 |
my $date = $$self{'data'}{'date'}; |
| 3995 |
my ($y,$m,$d) = @$date; |
| 3996 |
|
| 3997 |
my $currfirst = $dmb->_config('firstday'); |
| 3998 |
$dmb->config('firstday',$first); |
| 3999 |
my($yy,$wk) = $dmb->week_of_year([$y,$m,$d]); |
| 4000 |
$dmb->config('firstday',$currfirst); |
| 4001 |
|
| 4002 |
return 53 if ($yy > $y); |
| 4003 |
return 0 if ($yy < $y); |
| 4004 |
return $wk; |
| 4005 |
} |
| 4006 |
|
| 4007 |
sub complete { |
| 4008 |
my($self,$field) = @_; |
| 4009 |
if ($$self{'err'} || ! $$self{'data'}{'set'}) { |
| 4010 |
carp "WARNING: [complete] Object must contain a valid date"; |
| 4011 |
return undef; |
| 4012 |
} |
| 4013 |
|
| 4014 |
if (! $field) { |
| 4015 |
return 1 if (! $$self{'data'}{'def'}[1] && |
| 4016 |
! $$self{'data'}{'def'}[2] && |
| 4017 |
! $$self{'data'}{'def'}[3] && |
| 4018 |
! $$self{'data'}{'def'}[4] && |
| 4019 |
! $$self{'data'}{'def'}[5]); |
| 4020 |
return 0; |
| 4021 |
} |
| 4022 |
|
| 4023 |
if ($field eq 'm') { |
| 4024 |
return 1 if (! $$self{'data'}{'def'}[1]); |
| 4025 |
} |
| 4026 |
|
| 4027 |
if ($field eq 'd') { |
| 4028 |
return 1 if (! $$self{'data'}{'def'}[2]); |
| 4029 |
} |
| 4030 |
|
| 4031 |
if ($field eq 'h') { |
| 4032 |
return 1 if (! $$self{'data'}{'def'}[3]); |
| 4033 |
} |
| 4034 |
|
| 4035 |
if ($field eq 'mn') { |
| 4036 |
return 1 if (! $$self{'data'}{'def'}[4]); |
| 4037 |
} |
| 4038 |
|
| 4039 |
if ($field eq 's') { |
| 4040 |
return 1 if (! $$self{'data'}{'def'}[5]); |
| 4041 |
} |
| 4042 |
return 0; |
| 4043 |
} |
| 4044 |
|
| 4045 |
sub convert { |
| 4046 |
my($self,$zone) = @_; |
| 4047 |
if ($$self{'err'} || ! $$self{'data'}{'set'}) { |
| 4048 |
carp "WARNING: [convert] Object must contain a valid date"; |
| 4049 |
return 1; |
| 4050 |
} |
| 4051 |
my $dmt = $$self{'tz'}; |
| 4052 |
my $dmb = $$dmt{'base'}; |
| 4053 |
|
| 4054 |
my $zonename = $dmt->_zone($zone); |
| 4055 |
|
| 4056 |
if (! $zonename) { |
| 4057 |
$$self{'err'} = "[convert] Unable to determine timezone: $zone"; |
| 4058 |
return 1; |
| 4059 |
} |
| 4060 |
|
| 4061 |
my $date0 = $$self{'data'}{'date'}; |
| 4062 |
my $zone0 = $$self{'data'}{'tz'}; |
| 4063 |
my $isdst0 = $$self{'data'}{'isdst'}; |
| 4064 |
|
| 4065 |
my($err,$date,$off,$isdst,$abb) = $dmt->convert($date0,$zone0,$zonename,$isdst0); |
| 4066 |
|
| 4067 |
if ($err) { |
| 4068 |
$$self{'err'} = '[convert] Unable to convert date to new timezone'; |
| 4069 |
return 1; |
| 4070 |
} |
| 4071 |
|
| 4072 |
$self->_init(); |
| 4073 |
$$self{'data'}{'date'} = $date; |
| 4074 |
$$self{'data'}{'tz'} = $zonename; |
| 4075 |
$$self{'data'}{'isdst'} = $isdst; |
| 4076 |
$$self{'data'}{'offset'} = $off; |
| 4077 |
$$self{'data'}{'abb'} = $abb; |
| 4078 |
$$self{'data'}{'set'} = 1; |
| 4079 |
|
| 4080 |
return 0; |
| 4081 |
} |
| 4082 |
|
| 4083 |
######################################################################## |
| 4084 |
# BUSINESS DAY METHODS |
| 4085 |
|
| 4086 |
sub is_business_day { |
| 4087 |
my($self,$checktime) = @_; |
| 4088 |
if ($$self{'err'} || ! $$self{'data'}{'set'}) { |
| 4089 |
carp "WARNING: [is_business_day] Object must contain a valid date"; |
| 4090 |
return undef; |
| 4091 |
} |
| 4092 |
my $date = $$self{'data'}{'date'}; |
| 4093 |
return $self->__is_business_day($date,$checktime); |
| 4094 |
} |
| 4095 |
|
| 4096 |
sub __is_business_day { |
| 4097 |
my($self,$date,$checktime) = @_; |
| 4098 |
my($y,$m,$d,$h,$mn,$s) = @$date; |
| 4099 |
|
| 4100 |
my $dmt = $$self{'tz'}; |
| 4101 |
my $dmb = $$dmt{'base'}; |
| 4102 |
|
| 4103 |
# Return 0 if it's a weekend. |
| 4104 |
|
| 4105 |
my $dow = $dmb->day_of_week([$y,$m,$d]); |
| 4106 |
return 0 if ($dow < $dmb->_config('workweekbeg') || |
| 4107 |
$dow > $dmb->_config('workweekend')); |
| 4108 |
|
| 4109 |
# Return 0 if it's not during work hours (and we're checking |
| 4110 |
# for that). |
| 4111 |
|
| 4112 |
if ($checktime && |
| 4113 |
! $dmb->_config('workday24hr')) { |
| 4114 |
my $t = $dmb->join('hms',[$h,$mn,$s]); |
| 4115 |
my $t0 = $dmb->join('hms',$$dmb{'data'}{'calc'}{'workdaybeg'}); |
| 4116 |
my $t1 = $dmb->join('hms',$$dmb{'data'}{'calc'}{'workdayend'}); |
| 4117 |
return 0 if ($t lt $t0 || $t gt $t1); |
| 4118 |
} |
| 4119 |
|
| 4120 |
# Check for holidays |
| 4121 |
|
| 4122 |
if (! $$dmb{'data'}{'init_holidays'}) { |
| 4123 |
$self->_holidays($y-1); |
| 4124 |
$self->_holidays($y); |
| 4125 |
$self->_holidays($y+1); |
| 4126 |
} |
| 4127 |
|
| 4128 |
return 0 if (exists $$dmb{'data'}{'holidays'}{'dates'} && |
| 4129 |
exists $$dmb{'data'}{'holidays'}{'dates'}{$y+0} && |
| 4130 |
exists $$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m+0} && |
| 4131 |
exists $$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m+0}{$d+0}); |
| 4132 |
|
| 4133 |
return 1; |
| 4134 |
} |
| 4135 |
|
| 4136 |
sub list_holidays { |
| 4137 |
my($self,$y) = @_; |
| 4138 |
my $dmt = $$self{'tz'}; |
| 4139 |
my $dmb = $$dmt{'base'}; |
| 4140 |
|
| 4141 |
$y = $$self{'data'}{'date'}[0] if (! $y && $$self{'data'}{'set'} == 1); |
| 4142 |
$y = $dmt->_now('y',1) if (! $y); |
| 4143 |
$self->_holidays($y-1); |
| 4144 |
$self->_holidays($y); |
| 4145 |
$self->_holidays($y+1); |
| 4146 |
|
| 4147 |
my @ret; |
| 4148 |
my @m = sort { $a <=> $b } keys %{ $$dmb{'data'}{'holidays'}{'dates'}{$y+0} }; |
| 4149 |
foreach my $m (@m) { |
| 4150 |
my @d = sort { $a <=> $b } keys %{ $$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m} }; |
| 4151 |
foreach my $d (@d) { |
| 4152 |
my $hol = $self->new_date(); |
| 4153 |
$hol->set('date',[$y,$m,$d,0,0,0]); |
| 4154 |
push(@ret,$hol); |
| 4155 |
} |
| 4156 |
} |
| 4157 |
|
| 4158 |
return @ret; |
| 4159 |
} |
| 4160 |
|
| 4161 |
sub holiday { |
| 4162 |
my($self) = @_; |
| 4163 |
if ($$self{'err'} || ! $$self{'data'}{'set'}) { |
| 4164 |
carp "WARNING: [holiday] Object must contain a valid date"; |
| 4165 |
return undef; |
| 4166 |
} |
| 4167 |
my $dmt = $$self{'tz'}; |
| 4168 |
my $dmb = $$dmt{'base'}; |
| 4169 |
|
| 4170 |
my($y,$m,$d) = @{ $$self{'data'}{'date'} }; |
| 4171 |
$self->_holidays($y-1); |
| 4172 |
$self->_holidays($y); |
| 4173 |
$self->_holidays($y+1); |
| 4174 |
|
| 4175 |
if (exists $$dmb{'data'}{'holidays'}{'dates'}{$y+0} && |
| 4176 |
exists $$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m+0} && |
| 4177 |
exists $$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m+0}{$d+0}) { |
| 4178 |
my @tmp = @{ $$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m+0}{$d+0} }; |
| 4179 |
|
| 4180 |
foreach my $tmp (@tmp) { |
| 4181 |
$tmp = '' if ($tmp =~ /DMunnamed/); |
| 4182 |
} |
| 4183 |
|
| 4184 |
if (wantarray) { |
| 4185 |
return () if (! @tmp); |
| 4186 |
return @tmp; |
| 4187 |
} else { |
| 4188 |
return '' if (! @tmp); |
| 4189 |
return $tmp[0]; |
| 4190 |
} |
| 4191 |
} |
| 4192 |
return undef; |
| 4193 |
} |
| 4194 |
|
| 4195 |
sub next_business_day { |
| 4196 |
my($self,$off,$checktime) = @_; |
| 4197 |
if ($$self{'err'} || ! $$self{'data'}{'set'}) { |
| 4198 |
carp "WARNING: [next_business_day] Object must contain a valid date"; |
| 4199 |
return undef; |
| 4200 |
} |
| 4201 |
my $date = $$self{'data'}{'date'}; |
| 4202 |
|
| 4203 |
$date = $self->__nextprev_business_day(0,$off,$checktime,$date); |
| 4204 |
$self->set('date',$date); |
| 4205 |
return; |
| 4206 |
} |
| 4207 |
|
| 4208 |
sub prev_business_day { |
| 4209 |
my($self,$off,$checktime) = @_; |
| 4210 |
if ($$self{'err'} || ! $$self{'data'}{'set'}) { |
| 4211 |
carp "WARNING: [prev_business_day] Object must contain a valid date"; |
| 4212 |
return undef; |
| 4213 |
} |
| 4214 |
my $date = $$self{'data'}{'date'}; |
| 4215 |
|
| 4216 |
$date = $self->__nextprev_business_day(1,$off,$checktime,$date); |
| 4217 |
$self->set('date',$date); |
| 4218 |
return; |
| 4219 |
} |
| 4220 |
|
| 4221 |
sub __nextprev_business_day { |
| 4222 |
my($self,$prev,$off,$checktime,$date) = @_; |
| 4223 |
my($y,$m,$d,$h,$mn,$s) = @$date; |
| 4224 |
|
| 4225 |
my $dmt = $$self{'tz'}; |
| 4226 |
my $dmb = $$dmt{'base'}; |
| 4227 |
|
| 4228 |
# Get day 0 |
| 4229 |
|
| 4230 |
while (! $self->__is_business_day([$y,$m,$d,$h,$mn,$s],$checktime)) { |
| 4231 |
if ($checktime) { |
| 4232 |
($y,$m,$d,$h,$mn,$s) = |
| 4233 |
@{ $self->__next_prev([$y,$m,$d,$h,$mn,$s],1,undef,0, |
| 4234 |
$$dmb{'data'}{'calc'}{'workdaybeg'}) }; |
| 4235 |
} else { |
| 4236 |
# Move forward 1 day |
| 4237 |
($y,$m,$d) = @{ $dmb->calc_date_days([$y,$m,$d],1) }; |
| 4238 |
} |
| 4239 |
} |
| 4240 |
|
| 4241 |
# Move $off days into the future/past |
| 4242 |
|
| 4243 |
while ($off > 0) { |
| 4244 |
while (1) { |
| 4245 |
if ($prev) { |
| 4246 |
# Move backward 1 day |
| 4247 |
($y,$m,$d) = @{ $dmb->calc_date_days([$y,$m,$d],-1) }; |
| 4248 |
} else { |
| 4249 |
# Move forward 1 day |
| 4250 |
($y,$m,$d) = @{ $dmb->calc_date_days([$y,$m,$d],1) }; |
| 4251 |
} |
| 4252 |
last if ($self->__is_business_day([$y,$m,$d,$h,$mn,$s])); |
| 4253 |
} |
| 4254 |
$off--; |
| 4255 |
} |
| 4256 |
|
| 4257 |
return [$y,$m,$d,$h,$mn,$s]; |
| 4258 |
} |
| 4259 |
|
| 4260 |
sub nearest_business_day { |
| 4261 |
my($self,$tomorrow) = @_; |
| 4262 |
if ($$self{'err'} || ! $$self{'data'}{'set'}) { |
| 4263 |
carp "WARNING: [nearest_business_day] Object must contain a valid date"; |
| 4264 |
return undef; |
| 4265 |
} |
| 4266 |
|
| 4267 |
my $date = $$self{'data'}{'date'}; |
| 4268 |
$date = $self->__nearest_business_day($tomorrow,$date); |
| 4269 |
|
| 4270 |
# If @date is empty, the date is a business day and doesn't need |
| 4271 |
# to be changed. |
| 4272 |
|
| 4273 |
return if (! defined($date)); |
| 4274 |
|
| 4275 |
$self->set('date',$date); |
| 4276 |
return; |
| 4277 |
} |
| 4278 |
|
| 4279 |
sub __nearest_business_day { |
| 4280 |
my($self,$tomorrow,$date) = @_; |
| 4281 |
|
| 4282 |
# We're done if this is a business day |
| 4283 |
return undef if ($self->__is_business_day($date,0)); |
| 4284 |
|
| 4285 |
my $dmt = $$self{'tz'}; |
| 4286 |
my $dmb = $$dmt{'base'}; |
| 4287 |
|
| 4288 |
$tomorrow = $dmb->_config('tomorrowfirst') if (! defined $tomorrow); |
| 4289 |
|
| 4290 |
my($a1,$a2); |
| 4291 |
if ($tomorrow) { |
| 4292 |
($a1,$a2) = (1,-1); |
| 4293 |
} else { |
| 4294 |
($a1,$a2) = (-1,1); |
| 4295 |
} |
| 4296 |
|
| 4297 |
my ($y,$m,$d,$h,$mn,$s) = @$date; |
| 4298 |
my ($y1,$m1,$d1) = ($y,$m,$d); |
| 4299 |
my ($y2,$m2,$d2) = ($y,$m,$d); |
| 4300 |
|
| 4301 |
while (1) { |
| 4302 |
($y1,$m1,$d1) = @{ $dmb->calc_date_days([$y1,$m1,$d1],$a1) }; |
| 4303 |
if ($self->__is_business_day([$y1,$m1,$d1,$h,$mn,$s],0)) { |
| 4304 |
($y,$m,$d) = ($y1,$m1,$d1); |
| 4305 |
last; |
| 4306 |
} |
| 4307 |
($y2,$m2,$d2) = @{ $dmb->calc_date_days([$y2,$m2,$d2],$a2) }; |
| 4308 |
if ($self->__is_business_day([$y2,$m2,$d2,$h,$mn,$s],0)) { |
| 4309 |
($y,$m,$d) = ($y2,$m2,$d2); |
| 4310 |
last; |
| 4311 |
} |
| 4312 |
} |
| 4313 |
|
| 4314 |
return [$y,$m,$d,$h,$mn,$s]; |
| 4315 |
} |
| 4316 |
|
| 4317 |
# We need to create all the objects which will be used to determine holidays. |
| 4318 |
# By doing this once only, a lot of time is saved. |
| 4319 |
# |
| 4320 |
sub _holiday_objs { |
| 4321 |
my($self) = @_; |
| 4322 |
my $dmt = $$self{'tz'}; |
| 4323 |
my $dmb = $$dmt{'base'}; |
| 4324 |
|
| 4325 |
$$dmb{'data'}{'holidays'}{'init'} = 1; |
| 4326 |
|
| 4327 |
# Go through all of the strings from the config file. |
| 4328 |
# |
| 4329 |
my (@str) = @{ $$dmb{'data'}{'sections'}{'holidays'} }; |
| 4330 |
$$dmb{'data'}{'holidays'}{'defs'} = []; |
| 4331 |
|
| 4332 |
# Keep track of the holiday names |
| 4333 |
my $unnamed = 0; |
| 4334 |
|
| 4335 |
LINE: |
| 4336 |
while (@str) { |
| 4337 |
my($string) = shift(@str); |
| 4338 |
my($name) = shift(@str); |
| 4339 |
if (! $name) { |
| 4340 |
$unnamed++; |
| 4341 |
$name = "DMunnamed $unnamed"; |
| 4342 |
} |
| 4343 |
|
| 4344 |
# If $string is a parse_date string AND it contains a year, we'll |
| 4345 |
# store the date as a holiday, but not store the holiday description |
| 4346 |
# so it never needs to be re-parsed. |
| 4347 |
|
| 4348 |
my $date = $self->new_date(); |
| 4349 |
my $err = $date->parse_date($string); |
| 4350 |
|
| 4351 |
if (! $err) { |
| 4352 |
my($y,$m,$d) = @{ $$date{'data'}{'date'} }; |
| 4353 |
|
| 4354 |
if ($$date{'data'}{'def'}[0] eq '') { |
| 4355 |
# Lines of the form: Jun 12 |
| 4356 |
# |
| 4357 |
# We will NOT cache this holiday because we want to only |
| 4358 |
# cache holidays from lines like 'Jun 12 1972' during this |
| 4359 |
# phase so we find conflicts. |
| 4360 |
|
| 4361 |
push(@{ $$dmb{'data'}{'holidays'}{'defs'} },$name,$string); |
| 4362 |
|
| 4363 |
} else { |
| 4364 |
# Lines of the form: Jun 12 1972 |
| 4365 |
# |
| 4366 |
# We'll cache these to make sure we don't have two lines: |
| 4367 |
# Jun 12 1972 = Some Holiday |
| 4368 |
# Jun 13 1972 = Some Holiday |
| 4369 |
|
| 4370 |
if (exists $$dmb{'data'}{'holidays'}{'hols'}{$name}{$y+0}) { |
| 4371 |
carp "WARNING: Holiday defined twice for one year: $name [$y]"; |
| 4372 |
next LINE; |
| 4373 |
} |
| 4374 |
|
| 4375 |
$$dmb{'data'}{'holidays'}{'yhols'}{$y+0}{$name} = [$y,$m,$d]; |
| 4376 |
$$dmb{'data'}{'holidays'}{'hols'}{$name}{$y+0} = [$y,$m,$d]; |
| 4377 |
|
| 4378 |
if (exists $$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m+0}{$d+0}) { |
| 4379 |
push @{ $$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m+0}{$d+0} },$name; |
| 4380 |
} else { |
| 4381 |
$$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m+0}{$d+0} = [ $name ]; |
| 4382 |
} |
| 4383 |
} |
| 4384 |
next LINE; |
| 4385 |
} |
| 4386 |
$date->err(1); |
| 4387 |
|
| 4388 |
# If $string is a recurrence, we'll create a Recur object (which we |
| 4389 |
# only have to do once) and store it. |
| 4390 |
|
| 4391 |
my $recur = $self->new_recur(); |
| 4392 |
$err = $recur->parse($string); |
| 4393 |
if (! $err) { |
| 4394 |
push(@{ $$dmb{'data'}{'holidays'}{'defs'} },$name,$recur); |
| 4395 |
next LINE; |
| 4396 |
} |
| 4397 |
$recur->err(1); |
| 4398 |
|
| 4399 |
carp "WARNING: invalid holiday description: $string"; |
| 4400 |
} |
| 4401 |
return; |
| 4402 |
} |
| 4403 |
|
| 4404 |
# Make sure that holidays are done for a given year. |
| 4405 |
# |
| 4406 |
sub _holidays { |
| 4407 |
my($self,$year) = @_; |
| 4408 |
|
| 4409 |
my $dmt = $$self{'tz'}; |
| 4410 |
my $dmb = $$dmt{'base'}; |
| 4411 |
|
| 4412 |
return if ($$dmb{'data'}{'holidays'}{'ydone'}{$year+0}); |
| 4413 |
$self->_holiday_objs() if (! $$dmb{'data'}{'holidays'}{'init'}); |
| 4414 |
|
| 4415 |
# Parse the year |
| 4416 |
|
| 4417 |
# Get the objects and set them to use the new year. Also, get the |
| 4418 |
# range for recurrences. |
| 4419 |
|
| 4420 |
my @hol = @{ $$dmb{'data'}{'holidays'}{'defs'} }; |
| 4421 |
|
| 4422 |
my $beg = "$year-01-01-00:00:00"; |
| 4423 |
my $end = "$year-12-31-23:59:59"; |
| 4424 |
|
| 4425 |
# Get the date for each holiday. |
| 4426 |
|
| 4427 |
$$dmb{'data'}{'init_holidays'} = 1; |
| 4428 |
$$dmb{'data'}{'tmpnow'} = [$year,1,1,0,0,0]; |
| 4429 |
|
| 4430 |
HOLIDAY: |
| 4431 |
while (@hol) { |
| 4432 |
|
| 4433 |
my $name = shift(@hol); |
| 4434 |
my $obj = shift(@hol); |
| 4435 |
|
| 4436 |
# Each holiday only gets defined once per year |
| 4437 |
next if (exists $$dmb{'data'}{'holidays'}{'hols'}{$name}{$year+0}); |
| 4438 |
|
| 4439 |
if (ref($obj)) { |
| 4440 |
# It's a recurrence |
| 4441 |
|
| 4442 |
# We have to initialize the recurrence as it may contain idates |
| 4443 |
# and dates outside of this range that are not correct. |
| 4444 |
|
| 4445 |
$obj->_init_dates(); |
| 4446 |
|
| 4447 |
# If the recurrence has a date range built in, we won't override it. |
| 4448 |
# Otherwise, we'll only look for dates in this year. |
| 4449 |
|
| 4450 |
my @dates; |
| 4451 |
if ($obj->start() && $obj->end()) { |
| 4452 |
@dates = $obj->dates(); |
| 4453 |
} else { |
| 4454 |
@dates = $obj->dates($beg,$end,1); |
| 4455 |
} |
| 4456 |
|
| 4457 |
foreach my $date (@dates) { |
| 4458 |
my($y,$m,$d) = @{ $$date{'data'}{'date'} }; |
| 4459 |
|
| 4460 |
$$dmb{'data'}{'holidays'}{'yhols'}{$year+0}{$name} = [$y,$m,$d]; |
| 4461 |
$$dmb{'data'}{'holidays'}{'hols'}{$name}{$year+0} = [$y,$m,$d]; |
| 4462 |
|
| 4463 |
if (exists $$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m+0}{$d+0}) { |
| 4464 |
push @{ $$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m+0}{$d+0} },$name; |
| 4465 |
} else { |
| 4466 |
$$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m+0}{$d+0} = [$name]; |
| 4467 |
} |
| 4468 |
} |
| 4469 |
|
| 4470 |
} else { |
| 4471 |
my $date = $self->new_date(); |
| 4472 |
$date->parse_date($obj); |
| 4473 |
my($y,$m,$d) = @{ $$date{'data'}{'date'} }; |
| 4474 |
|
| 4475 |
$$dmb{'data'}{'holidays'}{'yhols'}{$year+0}{$name} = [$y,$m,$d]; |
| 4476 |
$$dmb{'data'}{'holidays'}{'hols'}{$name}{$year+0} = [$y,$m,$d]; |
| 4477 |
|
| 4478 |
if (exists $$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m+0}{$d+0}) { |
| 4479 |
push @{ $$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m+0}{$d+0} },$name; |
| 4480 |
} else { |
| 4481 |
$$dmb{'data'}{'holidays'}{'dates'}{$y+0}{$m+0}{$d+0} = [$name]; |
| 4482 |
} |
| 4483 |
} |
| 4484 |
} |
| 4485 |
|
| 4486 |
$$dmb{'data'}{'init_holidays'} = 0; |
| 4487 |
$$dmb{'data'}{'tmpnow'} = []; |
| 4488 |
$$dmb{'data'}{'holidays'}{'ydone'}{$year+0} = 1; |
| 4489 |
return; |
| 4490 |
} |
| 4491 |
|
| 4492 |
######################################################################## |
| 4493 |
# PRINTF METHOD |
| 4494 |
|
| 4495 |
BEGIN { |
| 4496 |
my %pad_0 = map { $_,1 } qw ( Y m d H M S I j G W L U ); |
| 4497 |
my %pad_0_pos = map { $_,1 } qw ( Y m d H M S I j G W L U V g ); |
| 4498 |
|
| 4499 |
my %pad_sp = map { $_,1 } qw ( y f e k i ); |
| 4500 |
my %pad_sp_pos = map { $_,1 } qw ( y f e k i l ); |
| 4501 |
|
| 4502 |
my %hr = map { $_,1 } qw ( H k I i ); |
| 4503 |
my %hr_pos = map { $_,1 } qw ( H k I i l ); |
| 4504 |
|
| 4505 |
my %dow = map { $_,1 } qw ( v a A w ); |
| 4506 |
my %dow_pos = map { $_,1 } qw ( v a A w u ); |
| 4507 |
|
| 4508 |
my %num = map { $_,1 } qw ( Y m d H M S y f e k I i j G W L U ); |
| 4509 |
my %num_pos = map { $_,1 } qw ( Y m d H M S y f e k I i j G W L U g V C l u ); |
| 4510 |
|
| 4511 |
sub printf { |
| 4512 |
my($self,@in) = @_; |
| 4513 |
if ($$self{'err'} || ! $$self{'data'}{'set'}) { |
| 4514 |
carp "WARNING: [printf] Object must contain a valid date"; |
| 4515 |
return undef; |
| 4516 |
} |
| 4517 |
|
| 4518 |
my $dmt = $$self{'tz'}; |
| 4519 |
my $dmb = $$dmt{'base'}; |
| 4520 |
|
| 4521 |
my $posix = $dmb->_config('use_posix_printf'); |
| 4522 |
|
| 4523 |
my($y,$m,$d,$h,$mn,$s) = @{ $$self{'data'}{'date'} }; |
| 4524 |
|
| 4525 |
my(@out); |
| 4526 |
foreach my $in (@in) { |
| 4527 |
my $out = ''; |
| 4528 |
while ($in) { |
| 4529 |
last if ($in eq '%'); |
| 4530 |
|
| 4531 |
# Everything up to the first '%' |
| 4532 |
|
| 4533 |
if ($in =~ s/^([^%]+)//) { |
| 4534 |
$out .= $1; |
| 4535 |
next; |
| 4536 |
} |
| 4537 |
|
| 4538 |
# Extended formats: %<...> |
| 4539 |
|
| 4540 |
if ($in =~ s/^%<([^>]+)>//) { |
| 4541 |
my $f = $1; |
| 4542 |
my $val; |
| 4543 |
|
| 4544 |
if ($f =~ /^a=([1-7])$/) { |
| 4545 |
$val = $$dmb{'data'}{'wordlist'}{'day_abb'}[$1-1]; |
| 4546 |
|
| 4547 |
} elsif ($f =~ /^v=([1-7])$/) { |
| 4548 |
$val = $$dmb{'data'}{'wordlist'}{'day_char'}[$1-1]; |
| 4549 |
|
| 4550 |
} elsif ($f =~ /^A=([1-7])$/) { |
| 4551 |
$val = $$dmb{'data'}{'wordlist'}{'day_name'}[$1-1]; |
| 4552 |
|
| 4553 |
} elsif ($f =~ /^p=([1-2])$/) { |
| 4554 |
$val = $$dmb{'data'}{'wordlist'}{'ampm'}[$1-1]; |
| 4555 |
|
| 4556 |
} elsif ($f =~ /^b=(0?[1-9]|1[0-2])$/) { |
| 4557 |
$val = $$dmb{'data'}{'wordlist'}{'month_abb'}[$1-1]; |
| 4558 |
|
| 4559 |
} elsif ($f =~ /^B=(0?[1-9]|1[0-2])$/) { |
| 4560 |
$val = $$dmb{'data'}{'wordlist'}{'month_name'}[$1-1]; |
| 4561 |
|
| 4562 |
} elsif ($f =~ /^E=(0?[1-9]|[1-4][0-9]|5[0-3])$/) { |
| 4563 |
$val = $$dmb{'data'}{'wordlist'}{'nth'}[$1-1]; |
| 4564 |
|
| 4565 |
} else { |
| 4566 |
$val = '%<' . $1 . '>'; |
| 4567 |
} |
| 4568 |
$out .= $val; |
| 4569 |
next; |
| 4570 |
} |
| 4571 |
|
| 4572 |
# Normals one-character formats |
| 4573 |
|
| 4574 |
$in =~ s/^%(.)//s; |
| 4575 |
my $f = $1; |
| 4576 |
|
| 4577 |
if (exists $$self{'data'}{'f'}{$f}) { |
| 4578 |
$out .= $$self{'data'}{'f'}{$f}; |
| 4579 |
next; |
| 4580 |
} |
| 4581 |
|
| 4582 |
my ($val,$pad,$len,$dow); |
| 4583 |
|
| 4584 |
if ((! $posix && exists $pad_0{$f}) || |
| 4585 |
($posix && exists $pad_0_pos{$f})) { |
| 4586 |
$pad = '0'; |
| 4587 |
} |
| 4588 |
|
| 4589 |
if ((! $posix && exists $pad_sp{$f}) || |
| 4590 |
($posix && exists $pad_sp_pos{$f})) { |
| 4591 |
$pad = ' '; |
| 4592 |
} |
| 4593 |
|
| 4594 |
# Year/week |
| 4595 |
|
| 4596 |
if ($f eq 'G' || |
| 4597 |
$f eq 'W' || |
| 4598 |
$f eq 'L' || |
| 4599 |
$f eq 'U' || |
| 4600 |
($f eq 'g' && $posix) || |
| 4601 |
($f eq 'V' && $posix) |
| 4602 |
) { |
| 4603 |
|
| 4604 |
my $week1ofyear; |
| 4605 |
my $firstday; |
| 4606 |
if ($f eq 'L' || $f eq 'U') { |
| 4607 |
$firstday = 7; |
| 4608 |
} else { |
| 4609 |
$firstday = 1; |
| 4610 |
} |
| 4611 |
|
| 4612 |
if ($posix && ($f eq 'G' || |
| 4613 |
$f eq 'g' || |
| 4614 |
$f eq 'V' || |
| 4615 |
$f eq 'L')) { |
| 4616 |
$week1ofyear = 'jan4'; |
| 4617 |
} elsif ($posix && ($f eq 'W')) { |
| 4618 |
$week1ofyear = 1; |
| 4619 |
} elsif ($posix && ($f eq 'U')) { |
| 4620 |
$week1ofyear = 7; |
| 4621 |
} else { |
| 4622 |
$week1ofyear = $dmb->_config('week1ofyear'); |
| 4623 |
} |
| 4624 |
|
| 4625 |
my($yy,$ww) = $dmb->_week_of_year($firstday,$week1ofyear,[$y,$m,$d]); |
| 4626 |
if ($f eq 'G' || |
| 4627 |
$f eq 'L') { |
| 4628 |
$val = $yy; |
| 4629 |
$len = 4; |
| 4630 |
|
| 4631 |
} elsif ($f eq 'g') { |
| 4632 |
$yy =~ /^..(..)/; |
| 4633 |
$val = $1; |
| 4634 |
$len = 2; |
| 4635 |
|
| 4636 |
} else { |
| 4637 |
$val = $ww; |
| 4638 |
$len = 2; |
| 4639 |
} |
| 4640 |
} |
| 4641 |
|
| 4642 |
if ($f eq 'Y' || $f eq 'y') { |
| 4643 |
$val = $y; |
| 4644 |
$len = 4; |
| 4645 |
} |
| 4646 |
|
| 4647 |
if ($f eq 'C' && $posix) { |
| 4648 |
$y =~ /^(..)/; |
| 4649 |
$val = $1; |
| 4650 |
$len = 2; |
| 4651 |
} |
| 4652 |
|
| 4653 |
if ($f eq 'm' || $f eq 'f') { |
| 4654 |
$val = $m; |
| 4655 |
$len = 2; |
| 4656 |
} |
| 4657 |
|
| 4658 |
if ($f eq 'd' || $f eq 'e') { |
| 4659 |
$val = $d; |
| 4660 |
$len = 2; |
| 4661 |
} |
| 4662 |
|
| 4663 |
if ($f eq 'j') { |
| 4664 |
$val = $dmb->day_of_year([$y,$m,$d]); |
| 4665 |
$len = 3; |
| 4666 |
} |
| 4667 |
|
| 4668 |
|
| 4669 |
if ((! $posix && exists $hr{$f}) || |
| 4670 |
( $posix && exists $hr_pos{$f})) { |
| 4671 |
$val = $h; |
| 4672 |
if ($f eq 'I' || $f eq 'i' || $f eq 'l') { |
| 4673 |
$val -= 12 if ($val > 12); |
| 4674 |
$val = 12 if ($val == 0); |
| 4675 |
} |
| 4676 |
$len = 2; |
| 4677 |
} |
| 4678 |
|
| 4679 |
if ($f eq 'M') { |
| 4680 |
$val = $mn; |
| 4681 |
$len = 2; |
| 4682 |
} |
| 4683 |
|
| 4684 |
if ($f eq 'S') { |
| 4685 |
$val = $s; |
| 4686 |
$len = 2; |
| 4687 |
} |
| 4688 |
|
| 4689 |
if ((! $posix && exists $dow{$f}) || |
| 4690 |
($posix && exists $dow_pos{$f})) { |
| 4691 |
$dow = $dmb->day_of_week([$y,$m,$d]); |
| 4692 |
$dow = 7 if ($f eq 'u' && $dow == 0); |
| 4693 |
$val = $dow; |
| 4694 |
$len = 1; |
| 4695 |
} |
| 4696 |
|
| 4697 |
### |
| 4698 |
|
| 4699 |
if ( (! $posix && exists $num{$f}) || |
| 4700 |
($posix && exists $num_pos{$f})) { |
| 4701 |
while (length($val) < $len) { |
| 4702 |
$val = "$pad$val"; |
| 4703 |
} |
| 4704 |
|
| 4705 |
$val = substr($val,2,2) if ($f eq 'y'); |
| 4706 |
|
| 4707 |
} elsif ($f eq 'b' || $f eq 'h') { |
| 4708 |
$val = $$dmb{'data'}{'wordlist'}{'month_abb'}[$m-1]; |
| 4709 |
|
| 4710 |
} elsif ($f eq 'B') { |
| 4711 |
$val = $$dmb{'data'}{'wordlist'}{'month_name'}[$m-1]; |
| 4712 |
|
| 4713 |
} elsif ($f eq 'v') { |
| 4714 |
$val = $$dmb{'data'}{'wordlist'}{'day_char'}[$dow-1]; |
| 4715 |
|
| 4716 |
} elsif ($f eq 'a') { |
| 4717 |
$val = $$dmb{'data'}{'wordlist'}{'day_abb'}[$dow-1]; |
| 4718 |
|
| 4719 |
} elsif ($f eq 'A') { |
| 4720 |
$val = $$dmb{'data'}{'wordlist'}{'day_name'}[$dow-1]; |
| 4721 |
|
| 4722 |
} elsif ($f eq 'w') { |
| 4723 |
$val = $dow; |
| 4724 |
|
| 4725 |
} elsif ($f eq 'p' || ($f eq 'P' && $posix)) { |
| 4726 |
my $i = ($h >= 12 ? 1 : 0); |
| 4727 |
$val = $$dmb{'data'}{'wordlist'}{'ampm'}[$i]; |
| 4728 |
$val = lc($val) if ($f eq 'P'); |
| 4729 |
|
| 4730 |
} elsif ($f eq 'Z') { |
| 4731 |
$val = $$self{'data'}{'abb'}; |
| 4732 |
|
| 4733 |
} elsif ($f eq 'N') { |
| 4734 |
my $off = $$self{'data'}{'offset'}; |
| 4735 |
$val = $dmb->join('offset',$off); |
| 4736 |
|
| 4737 |
} elsif ($f eq 'z') { |
| 4738 |
my $off = $$self{'data'}{'offset'}; |
| 4739 |
$val = $dmb->join('offset',$off); |
| 4740 |
$val =~ s/://g; |
| 4741 |
$val =~ s/00$//; |
| 4742 |
|
| 4743 |
} elsif ($f eq 'E') { |
| 4744 |
$val = $$dmb{'data'}{'wordlist'}{'nth_dom'}[$d-1]; |
| 4745 |
|
| 4746 |
} elsif ($f eq 's') { |
| 4747 |
$val = $self->secs_since_1970_GMT(); |
| 4748 |
|
| 4749 |
} elsif ($f eq 'o') { |
| 4750 |
my $date2 = $self->new_date(); |
| 4751 |
$date2->parse('1970-01-01 00:00:00'); |
| 4752 |
my $delta = $date2->calc($self); |
| 4753 |
$val = $delta->printf('%sys'); |
| 4754 |
|
| 4755 |
} elsif ($f eq 'l' && ! $posix) { |
| 4756 |
my $d0 = $self->new_date(); |
| 4757 |
my $d1 = $self->new_date(); |
| 4758 |
$d0->parse('-0:6:0:0:0:0:0'); # 6 months ago |
| 4759 |
$d1->parse('+0:6:0:0:0:0:0'); # in 6 months |
| 4760 |
$d0 = $d0->value(); |
| 4761 |
$d1 = $d1->value(); |
| 4762 |
my $date = $self->value(); |
| 4763 |
if ($date lt $d0 || $date ge $d1) { |
| 4764 |
$in = '%b %e %Y' . $in; |
| 4765 |
} else { |
| 4766 |
$in = '%b %e %H:%M' . $in; |
| 4767 |
} |
| 4768 |
$val = ''; |
| 4769 |
|
| 4770 |
} elsif ($f eq 'c') { |
| 4771 |
$in = '%a %b %e %H:%M:%S %Y' . $in; |
| 4772 |
$val = ''; |
| 4773 |
|
| 4774 |
} elsif (($f eq 'C' && ! $posix) || $f eq 'u') { |
| 4775 |
$in = '%a %b %e %H:%M:%S %Z %Y' . $in; |
| 4776 |
$val = ''; |
| 4777 |
|
| 4778 |
} elsif ($f eq 'g' && ! $posix) { |
| 4779 |
$in = '%a, %d %b %Y %H:%M:%S %Z' . $in; |
| 4780 |
$val = ''; |
| 4781 |
|
| 4782 |
} elsif ($f eq 'D') { |
| 4783 |
$in = '%m/%d/%y' . $in; |
| 4784 |
$val = ''; |
| 4785 |
|
| 4786 |
} elsif ($f eq 'r') { |
| 4787 |
$in = '%I:%M:%S %p' . $in; |
| 4788 |
$val = ''; |
| 4789 |
|
| 4790 |
} elsif ($f eq 'R') { |
| 4791 |
$in = '%H:%M' . $in; |
| 4792 |
$val = ''; |
| 4793 |
|
| 4794 |
} elsif ($f eq 'T' || $f eq 'X') { |
| 4795 |
$in = '%H:%M:%S' . $in; |
| 4796 |
$val = ''; |
| 4797 |
|
| 4798 |
} elsif ($f eq 'V' && ! $posix) { |
| 4799 |
$in = '%m%d%H%M%y' . $in; |
| 4800 |
$val = ''; |
| 4801 |
|
| 4802 |
} elsif ($f eq 'Q') { |
| 4803 |
$in = '%Y%m%d' . $in; |
| 4804 |
$val = ''; |
| 4805 |
|
| 4806 |
} elsif ($f eq 'q') { |
| 4807 |
$in = '%Y%m%d%H%M%S' . $in; |
| 4808 |
$val = ''; |
| 4809 |
|
| 4810 |
} elsif ($f eq 'P' && ! $posix) { |
| 4811 |
$in = '%Y%m%d%H:%M:%S' . $in; |
| 4812 |
$val = ''; |
| 4813 |
|
| 4814 |
} elsif ($f eq 'O') { |
| 4815 |
$in = '%Y-%m-%dT%H:%M:%S' . $in; |
| 4816 |
$val = ''; |
| 4817 |
|
| 4818 |
} elsif ($f eq 'F') { |
| 4819 |
if ($posix) { |
| 4820 |
$in = '%Y-%m-%d' . $in; |
| 4821 |
} else { |
| 4822 |
$in = '%A, %B %e, %Y' . $in; |
| 4823 |
} |
| 4824 |
$val = ''; |
| 4825 |
|
| 4826 |
} elsif ($f eq 'K') { |
| 4827 |
$in = '%Y-%j' . $in; |
| 4828 |
$val = ''; |
| 4829 |
|
| 4830 |
} elsif ($f eq 'x') { |
| 4831 |
if ($dmb->_config('dateformat') eq 'US') { |
| 4832 |
$in = '%m/%d/%y' . $in; |
| 4833 |
} else { |
| 4834 |
$in = '%d/%m/%y' . $in; |
| 4835 |
} |
| 4836 |
$val = ''; |
| 4837 |
|
| 4838 |
} elsif ($f eq 'J') { |
| 4839 |
if ($posix) { |
| 4840 |
$in = '%G-W%V-%w' . $in; |
| 4841 |
} else { |
| 4842 |
$in = '%G-W%W-%w' . $in; |
| 4843 |
} |
| 4844 |
$val = ''; |
| 4845 |
|
| 4846 |
} elsif ($f eq 'n') { |
| 4847 |
$val = "\n"; |
| 4848 |
|
| 4849 |
} elsif ($f eq 't') { |
| 4850 |
$val = "\t"; |
| 4851 |
|
| 4852 |
} else { |
| 4853 |
$val = $f; |
| 4854 |
} |
| 4855 |
|
| 4856 |
if ($val ne '') { |
| 4857 |
$$self{'data'}{'f'}{$f} = $val; |
| 4858 |
$out .= $val; |
| 4859 |
} |
| 4860 |
} |
| 4861 |
push(@out,$out); |
| 4862 |
} |
| 4863 |
|
| 4864 |
if (wantarray) { |
| 4865 |
return @out; |
| 4866 |
} elsif (@out == 1) { |
| 4867 |
return $out[0]; |
| 4868 |
} |
| 4869 |
|
| 4870 |
return '' |
| 4871 |
} |
| 4872 |
} |
| 4873 |
|
| 4874 |
######################################################################## |
| 4875 |
# EVENT METHODS |
| 4876 |
|
| 4877 |
sub list_events { |
| 4878 |
my($self,@args) = @_; |
| 4879 |
if ($$self{'err'} || ! $$self{'data'}{'set'}) { |
| 4880 |
carp "WARNING: [list_events] Object must contain a valid date"; |
| 4881 |
return undef; |
| 4882 |
} |
| 4883 |
my $dmt = $$self{'tz'}; |
| 4884 |
my $dmb = $$dmt{'base'}; |
| 4885 |
|
| 4886 |
# Arguments |
| 4887 |
|
| 4888 |
my($date,$day,$format); |
| 4889 |
if (@args && $args[$#args] eq 'dates') { |
| 4890 |
pop(@args); |
| 4891 |
$format = 'dates'; |
| 4892 |
} else { |
| 4893 |
$format = 'std'; |
| 4894 |
} |
| 4895 |
|
| 4896 |
if (@args && $#args==0 && ref($args[0]) eq 'Date::Manip::Date') { |
| 4897 |
$date = $args[0]; |
| 4898 |
} elsif (@args && $#args==0 && $args[0]==0) { |
| 4899 |
$day = 1; |
| 4900 |
} elsif (@args) { |
| 4901 |
carp "ERROR: [list_events] unknown argument list"; |
| 4902 |
return []; |
| 4903 |
} |
| 4904 |
|
| 4905 |
# Get the beginning/end dates we're looking for events in |
| 4906 |
|
| 4907 |
my($beg,$end); |
| 4908 |
if ($date) { |
| 4909 |
$beg = $self; |
| 4910 |
$end = $date; |
| 4911 |
} elsif ($day) { |
| 4912 |
$beg = $self->new_date(); |
| 4913 |
$end = $self->new_date(); |
| 4914 |
my($y,$m,$d) = $self->value(); |
| 4915 |
$beg->set('date',[$y,$m,$d,0,0,0]); |
| 4916 |
$end->set('date',[$y,$m,$d,23,59,59]); |
| 4917 |
} else { |
| 4918 |
$beg = $self; |
| 4919 |
$end = $self; |
| 4920 |
} |
| 4921 |
|
| 4922 |
if ($beg->cmp($end) == 1) { |
| 4923 |
my $tmp = $beg; |
| 4924 |
$beg = $end; |
| 4925 |
$end = $tmp; |
| 4926 |
} |
| 4927 |
|
| 4928 |
# We need to get a list of all events which may apply. |
| 4929 |
|
| 4930 |
my($y0) = $beg->value(); |
| 4931 |
my($y1) = $end->value(); |
| 4932 |
foreach my $y ($y0..$y1) { |
| 4933 |
$self->_events_year($y); |
| 4934 |
} |
| 4935 |
|
| 4936 |
my @events = (); |
| 4937 |
foreach my $i (keys %{ $$dmb{'data'}{'events'} }) { |
| 4938 |
my $event = $$dmb{'data'}{'events'}{$i}; |
| 4939 |
my $type = $$event{'type'}; |
| 4940 |
my $name = $$event{'name'}; |
| 4941 |
|
| 4942 |
if ($type eq 'specified') { |
| 4943 |
my $d0 = $$dmb{'data'}{'events'}{$i}{'beg'}; |
| 4944 |
my $d1 = $$dmb{'data'}{'events'}{$i}{'end'}; |
| 4945 |
push @events,[$d0,$d1,$name]; |
| 4946 |
|
| 4947 |
} elsif ($type eq 'ym' || $type eq 'date') { |
| 4948 |
foreach my $y ($y0..$y1) { |
| 4949 |
if (exists $$dmb{'data'}{'events'}{$i}{$y}) { |
| 4950 |
my($d0,$d1) = @{ $$dmb{'data'}{'events'}{$i}{$y} }; |
| 4951 |
push @events,[$d0,$d1,$name]; |
| 4952 |
} |
| 4953 |
} |
| 4954 |
|
| 4955 |
} elsif ($type eq 'recur') { |
| 4956 |
my $rec = $$dmb{'data'}{'events'}{$i}{'recur'}; |
| 4957 |
my $del = $$dmb{'data'}{'events'}{$i}{'delta'}; |
| 4958 |
my @d = $rec->dates($beg,$end); |
| 4959 |
foreach my $d0 (@d) { |
| 4960 |
my $d1 = $d0->calc($del); |
| 4961 |
push @events,[$d0,$d1,$name]; |
| 4962 |
} |
| 4963 |
} |
| 4964 |
} |
| 4965 |
|
| 4966 |
# Next we need to see which ones apply. |
| 4967 |
|
| 4968 |
my @tmp; |
| 4969 |
foreach my $e (@events) { |
| 4970 |
my($d0,$d1,$name) = @$e; |
| 4971 |
|
| 4972 |
push(@tmp,$e) if ($beg->cmp($d1) != 1 && |
| 4973 |
$end->cmp($d0) != -1); |
| 4974 |
} |
| 4975 |
|
| 4976 |
# Now format them... |
| 4977 |
|
| 4978 |
if ($format eq 'std') { |
| 4979 |
@events = sort { $$a[0]->cmp($$b[0]) || |
| 4980 |
$$a[1]->cmp($$b[1]) || |
| 4981 |
$$a[2] cmp $$b[2] } @tmp; |
| 4982 |
|
| 4983 |
} elsif ($format eq 'dates') { |
| 4984 |
my $p1s = $self->new_delta(); |
| 4985 |
$p1s->parse('+0:0:0:0:0:0:1'); |
| 4986 |
|
| 4987 |
@events = (); |
| 4988 |
my (@tmp2); |
| 4989 |
foreach my $e (@tmp) { |
| 4990 |
my $name = $$e[2]; |
| 4991 |
if ($$e[0]->cmp($beg) == -1) { |
| 4992 |
# Event begins before the start |
| 4993 |
push(@tmp2,[$beg,'+',$name]); |
| 4994 |
} else { |
| 4995 |
push(@tmp2,[$$e[0],'+',$name]); |
| 4996 |
} |
| 4997 |
|
| 4998 |
my $d1 = $$e[1]->calc($p1s); |
| 4999 |
|
| 5000 |
if ($d1->cmp($end) == -1) { |
| 5001 |
# Event ends before the end |
| 5002 |
push(@tmp2,[$d1,'-',$name]); |
| 5003 |
} |
| 5004 |
} |
| 5005 |
|
| 5006 |
return () if (! @tmp2); |
| 5007 |
@tmp2 = sort { $$a[0]->cmp($$b[0]) || |
| 5008 |
$$a[1] cmp $$b[1] || |
| 5009 |
$$a[2] cmp $$b[2] } @tmp2; |
| 5010 |
|
| 5011 |
# @tmp2 is now: |
| 5012 |
# ( [ DATE1, OP1, NAME1 ], [ DATE2, OP2, NAME2 ], ... ) |
| 5013 |
# which is sorted by date. |
| 5014 |
|
| 5015 |
my $d = $tmp2[0]->[0]; |
| 5016 |
|
| 5017 |
if ($beg->cmp($d) != 0) { |
| 5018 |
push(@events,[$beg]); |
| 5019 |
} |
| 5020 |
|
| 5021 |
my %e; |
| 5022 |
while (1) { |
| 5023 |
|
| 5024 |
# If the first element is the same date as we're |
| 5025 |
# currently working with, just perform the operation |
| 5026 |
# and remove it from the list. If the list is not empty, |
| 5027 |
# we'll proceed to the next element. |
| 5028 |
|
| 5029 |
my $d0 = $tmp2[0]->[0]; |
| 5030 |
if ($d->cmp($d0) == 0) { |
| 5031 |
my $e = shift(@tmp2); |
| 5032 |
my $op = $$e[1]; |
| 5033 |
my $n = $$e[2]; |
| 5034 |
if ($op eq '+') { |
| 5035 |
$e{$n} = 1; |
| 5036 |
} else { |
| 5037 |
delete $e{$n}; |
| 5038 |
} |
| 5039 |
|
| 5040 |
next if (@tmp2); |
| 5041 |
} |
| 5042 |
|
| 5043 |
# We need to store the existing %e. |
| 5044 |
|
| 5045 |
my @n = sort keys %e; |
| 5046 |
push(@events,[$d,@n]); |
| 5047 |
|
| 5048 |
# If the list is empty, we're done. Otherwise, we need to |
| 5049 |
# reset the date and continue. |
| 5050 |
|
| 5051 |
last if (! @tmp2); |
| 5052 |
$d = $tmp2[0]->[0]; |
| 5053 |
} |
| 5054 |
} |
| 5055 |
|
| 5056 |
return @events; |
| 5057 |
} |
| 5058 |
|
| 5059 |
# The events of type date and ym are determined on a year-by-year basis |
| 5060 |
# |
| 5061 |
sub _events_year { |
| 5062 |
my($self,$y) = @_; |
| 5063 |
my $dmt = $$self{'tz'}; |
| 5064 |
my $dmb = $$dmt{'base'}; |
| 5065 |
my $tz = $dmt->_now('tz',1); |
| 5066 |
return if (exists $$dmb{'data'}{'eventyears'}{$y}); |
| 5067 |
$self->_event_objs() if (! $$dmb{'data'}{'eventobjs'}); |
| 5068 |
|
| 5069 |
my $d = $self->new_date(); |
| 5070 |
$d->config('forcedate',"${y}-01-01-00:00:00,$tz"); |
| 5071 |
|
| 5072 |
my $hrM1 = $d->new_delta(); |
| 5073 |
$hrM1->set('delta',[0,0,0,0,0,59,59]); |
| 5074 |
|
| 5075 |
my $dayM1 = $d->new_delta(); |
| 5076 |
$dayM1->set('delta',[0,0,0,0,23,59,59]); |
| 5077 |
|
| 5078 |
foreach my $i (keys %{ $$dmb{'data'}{'events'} }) { |
| 5079 |
my $event = $$dmb{'data'}{'events'}{$i}; |
| 5080 |
my $type = $$event{'type'}; |
| 5081 |
|
| 5082 |
if ($type eq 'ym') { |
| 5083 |
my $beg = $$event{'beg'}; |
| 5084 |
my $end = $$event{'end'}; |
| 5085 |
my $d0 = $d->new_date(); |
| 5086 |
$d0->parse_date($beg); |
| 5087 |
$d0->set('time',[0,0,0]); |
| 5088 |
|
| 5089 |
my $d1; |
| 5090 |
if ($end) { |
| 5091 |
$d1 = $d0->new_date(); |
| 5092 |
$d1->parse_date($end); |
| 5093 |
$d1->set('time',[23,59,59]); |
| 5094 |
} else { |
| 5095 |
$d1 = $d0->calc($dayM1); |
| 5096 |
} |
| 5097 |
$$dmb{'data'}{'events'}{$i}{$y} = [ $d0,$d1 ]; |
| 5098 |
|
| 5099 |
} elsif ($type eq 'date') { |
| 5100 |
my $beg = $$event{'beg'}; |
| 5101 |
my $end = $$event{'end'}; |
| 5102 |
my $del = $$event{'delta'}; |
| 5103 |
my $d0 = $d->new_date(); |
| 5104 |
$d0->parse($beg); |
| 5105 |
|
| 5106 |
my $d1; |
| 5107 |
if ($end) { |
| 5108 |
$d1 = $d0->new_date(); |
| 5109 |
$d1->parse($end); |
| 5110 |
} elsif ($del) { |
| 5111 |
$d1 = $d0->calc($del); |
| 5112 |
} else { |
| 5113 |
$d1 = $d0->calc($hrM1); |
| 5114 |
} |
| 5115 |
$$dmb{'data'}{'events'}{$i}{$y} = [ $d0,$d1 ]; |
| 5116 |
} |
| 5117 |
} |
| 5118 |
|
| 5119 |
return; |
| 5120 |
} |
| 5121 |
|
| 5122 |
# This parses the raw event list. It only has to be done once. |
| 5123 |
# |
| 5124 |
sub _event_objs { |
| 5125 |
my($self) = @_; |
| 5126 |
my $dmt = $$self{'tz'}; |
| 5127 |
my $dmb = $$dmt{'base'}; |
| 5128 |
# Only parse once. |
| 5129 |
$$dmb{'data'}{'eventobjs'} = 1; |
| 5130 |
|
| 5131 |
my $hrM1 = $self->new_delta(); |
| 5132 |
$hrM1->set('delta',[0,0,0,0,0,59,59]); |
| 5133 |
|
| 5134 |
my $M1 = $self->new_delta(); |
| 5135 |
$M1->set('delta',[0,0,0,0,0,0,-1]); |
| 5136 |
|
| 5137 |
my @tmp = @{ $$dmb{'data'}{'sections'}{'events'} }; |
| 5138 |
my $i = 0; |
| 5139 |
while (@tmp) { |
| 5140 |
my $string = shift(@tmp); |
| 5141 |
my $name = shift(@tmp); |
| 5142 |
my @event = split(/\s*;\s*/,$string); |
| 5143 |
|
| 5144 |
if ($#event == 0) { |
| 5145 |
|
| 5146 |
# YMD/YM |
| 5147 |
|
| 5148 |
my $d1 = $self->new_date(); |
| 5149 |
my $err = $d1->parse_date($event[0]); |
| 5150 |
if (! $err) { |
| 5151 |
if ($$d1{'data'}{'def'}[0] eq '') { |
| 5152 |
# YM |
| 5153 |
$$dmb{'data'}{'events'}{$i++} = { 'type' => 'ym', |
| 5154 |
'name' => $name, |
| 5155 |
'beg' => $event[0] }; |
| 5156 |
} else { |
| 5157 |
# YMD |
| 5158 |
my $d2 = $d1->new_date(); |
| 5159 |
my ($y,$m,$d) = $d1->value(); |
| 5160 |
$d1->set('time',[0,0,0]); |
| 5161 |
$d2->set('date',[$y,$m,$d,23,59,59]); |
| 5162 |
$$dmb{'data'}{'events'}{$i++} = { 'type' => 'specified', |
| 5163 |
'name' => $name, |
| 5164 |
'beg' => $d1, |
| 5165 |
'end' => $d2 }; |
| 5166 |
} |
| 5167 |
next; |
| 5168 |
} |
| 5169 |
|
| 5170 |
# Date |
| 5171 |
|
| 5172 |
$err = $d1->parse($event[0]); |
| 5173 |
if (! $err) { |
| 5174 |
if ($$d1{'data'}{'def'}[0] eq '') { |
| 5175 |
# Date (no year) |
| 5176 |
$$dmb{'data'}{'events'}{$i++} = { 'type' => 'date', |
| 5177 |
'name' => $name, |
| 5178 |
'beg' => $event[0], |
| 5179 |
'delta' => $hrM1 |
| 5180 |
}; |
| 5181 |
} else { |
| 5182 |
# Date (year) |
| 5183 |
my $d2 = $d1->calc($hrM1); |
| 5184 |
$$dmb{'data'}{'events'}{$i++} = { 'type' => 'specified', |
| 5185 |
'name' => $name, |
| 5186 |
'beg' => $d1, |
| 5187 |
'end' => $d2 |
| 5188 |
}; |
| 5189 |
} |
| 5190 |
next; |
| 5191 |
} |
| 5192 |
|
| 5193 |
# Recur |
| 5194 |
|
| 5195 |
my $r = $self->new_recur(); |
| 5196 |
$err = $r->parse($event[0]); |
| 5197 |
if ($err) { |
| 5198 |
carp "ERROR: invalid event definition (must be Date, YMD, YM, or Recur)\n" |
| 5199 |
. " $string\n"; |
| 5200 |
next; |
| 5201 |
} |
| 5202 |
|
| 5203 |
my @d = $r->dates(); |
| 5204 |
if (@d) { |
| 5205 |
foreach my $d (@d) { |
| 5206 |
my $d2 = $d->calc($hrM1); |
| 5207 |
$$dmb{'data'}{'events'}{$i++} = { 'type' => 'specified', |
| 5208 |
'name' => $name, |
| 5209 |
'beg' => $d1, |
| 5210 |
'end' => $d2 |
| 5211 |
}; |
| 5212 |
} |
| 5213 |
} else { |
| 5214 |
$$dmb{'data'}{'events'}{$i++} = { 'type' => 'recur', |
| 5215 |
'name' => $name, |
| 5216 |
'recur' => $r, |
| 5217 |
'delta' => $hrM1 |
| 5218 |
}; |
| 5219 |
} |
| 5220 |
|
| 5221 |
} elsif ($#event == 1) { |
| 5222 |
my($o1,$o2) = @event; |
| 5223 |
|
| 5224 |
# YMD;YMD |
| 5225 |
# YM;YM |
| 5226 |
|
| 5227 |
my $d1 = $self->new_date(); |
| 5228 |
my $err = $d1->parse_date($o1); |
| 5229 |
if (! $err) { |
| 5230 |
my $d2 = $self->new_date(); |
| 5231 |
$err = $d2->parse_date($o2); |
| 5232 |
if ($err) { |
| 5233 |
carp "ERROR: invalid event definition (must be YMD;YMD or YM;YM)\n" |
| 5234 |
. " $string\n"; |
| 5235 |
next; |
| 5236 |
} elsif ($$d1{'data'}{'def'}[0] ne $$d2{'data'}{'def'}[0]) { |
| 5237 |
carp "ERROR: invalid event definition (YMD;YM or YM;YMD not allowed)\n" |
| 5238 |
. " $string\n"; |
| 5239 |
next; |
| 5240 |
} |
| 5241 |
|
| 5242 |
if ($$d1{'data'}{'def'}[0] eq '') { |
| 5243 |
# YM;YM |
| 5244 |
$$dmb{'data'}{'events'}{$i++} = { 'type' => 'ym', |
| 5245 |
'name' => $name, |
| 5246 |
'beg' => $o1, |
| 5247 |
'end' => $o2 |
| 5248 |
}; |
| 5249 |
} else { |
| 5250 |
# YMD;YMD |
| 5251 |
$d1->set('time',[0,0,0]); |
| 5252 |
$d2->set('time',[23,59,59]); |
| 5253 |
$$dmb{'data'}{'events'}{$i++} = { 'type' => 'specified', |
| 5254 |
'name' => $name, |
| 5255 |
'beg' => $d1, |
| 5256 |
'end' => $d2 }; |
| 5257 |
} |
| 5258 |
next; |
| 5259 |
} |
| 5260 |
|
| 5261 |
# Date;Date |
| 5262 |
# Date;Delta |
| 5263 |
|
| 5264 |
$err = $d1->parse($o1); |
| 5265 |
if (! $err) { |
| 5266 |
|
| 5267 |
my $d2 = $self->new_date(); |
| 5268 |
$err = $d2->parse($o2,'nodelta'); |
| 5269 |
|
| 5270 |
if (! $err) { |
| 5271 |
# Date;Date |
| 5272 |
if ($$d1{'data'}{'def'}[0] ne $$d2{'data'}{'def'}[0]) { |
| 5273 |
carp "ERROR: invalid event definition (year must be absent or\n" |
| 5274 |
. " included in both dats in Date;Date)\n" |
| 5275 |
. " $string\n"; |
| 5276 |
next; |
| 5277 |
} |
| 5278 |
|
| 5279 |
if ($$d1{'data'}{'def'}[0] eq '') { |
| 5280 |
# Date (no year) |
| 5281 |
$$dmb{'data'}{'events'}{$i++} = { 'type' => 'date', |
| 5282 |
'name' => $name, |
| 5283 |
'beg' => $o1, |
| 5284 |
'end' => $o2 |
| 5285 |
}; |
| 5286 |
} else { |
| 5287 |
# Date (year) |
| 5288 |
$$dmb{'data'}{'events'}{$i++} = { 'type' => 'specified', |
| 5289 |
'name' => $name, |
| 5290 |
'beg' => $d1, |
| 5291 |
'end' => $d2 |
| 5292 |
}; |
| 5293 |
} |
| 5294 |
next; |
| 5295 |
} |
| 5296 |
|
| 5297 |
# Date;Delta |
| 5298 |
my $del = $self->new_delta(); |
| 5299 |
$err = $del->parse($o2); |
| 5300 |
|
| 5301 |
if ($err) { |
| 5302 |
carp "ERROR: invalid event definition (must be Date;Date or\n" |
| 5303 |
. " Date;Delta) $string\n"; |
| 5304 |
next; |
| 5305 |
} |
| 5306 |
|
| 5307 |
$del = $del->calc($M1); |
| 5308 |
if ($$d1{'data'}{'def'}[0] eq '') { |
| 5309 |
# Date (no year) |
| 5310 |
$$dmb{'data'}{'events'}{$i++} = { 'type' => 'date', |
| 5311 |
'name' => $name, |
| 5312 |
'beg' => $o1, |
| 5313 |
'delta' => $del |
| 5314 |
}; |
| 5315 |
} else { |
| 5316 |
# Date (year) |
| 5317 |
$d2 = $d1->calc($del); |
| 5318 |
$$dmb{'data'}{'events'}{$i++} = { 'type' => 'specified', |
| 5319 |
'name' => $name, |
| 5320 |
'beg' => $d1, |
| 5321 |
'end' => $d2 |
| 5322 |
}; |
| 5323 |
} |
| 5324 |
next; |
| 5325 |
} |
| 5326 |
|
| 5327 |
# Recur;Delta |
| 5328 |
|
| 5329 |
my $r = $self->new_recur(); |
| 5330 |
$err = $r->parse($o1); |
| 5331 |
|
| 5332 |
my $del = $self->new_delta(); |
| 5333 |
if (! $err) { |
| 5334 |
$err = $del->parse($o2); |
| 5335 |
} |
| 5336 |
|
| 5337 |
if ($err) { |
| 5338 |
carp "ERROR: invalid event definition (must be Date;Date, YMD;YMD, " |
| 5339 |
. " YM;YM, Date;Delta, or Recur;Delta)\n" |
| 5340 |
. " $string\n"; |
| 5341 |
next; |
| 5342 |
} |
| 5343 |
|
| 5344 |
$del = $del->calc($M1); |
| 5345 |
my @d = $r->dates(); |
| 5346 |
if (@d) { |
| 5347 |
foreach my $d1 (@d) { |
| 5348 |
my $d2 = $d1->calc($del); |
| 5349 |
$$dmb{'data'}{'events'}{$i++} = { 'type' => 'specified', |
| 5350 |
'name' => $name, |
| 5351 |
'beg' => $d1, |
| 5352 |
'end' => $d2 |
| 5353 |
}; |
| 5354 |
} |
| 5355 |
} else { |
| 5356 |
$$dmb{'data'}{'events'}{$i++} = { 'type' => 'recur', |
| 5357 |
'name' => $name, |
| 5358 |
'recur' => $r, |
| 5359 |
'delta' => $del |
| 5360 |
}; |
| 5361 |
} |
| 5362 |
|
| 5363 |
} else { |
| 5364 |
carp "ERROR: invalid event definition\n" |
| 5365 |
. " $string\n"; |
| 5366 |
next; |
| 5367 |
} |
| 5368 |
} |
| 5369 |
|
| 5370 |
return; |
| 5371 |
} |
| 5372 |
|
| 5373 |
1; |
| 5374 |
# Local Variables: |
| 5375 |
# mode: cperl |
| 5376 |
# indent-tabs-mode: nil |
| 5377 |
# cperl-indent-level: 3 |
| 5378 |
# cperl-continued-statement-offset: 2 |
| 5379 |
# cperl-continued-brace-offset: 0 |
| 5380 |
# cperl-brace-offset: 0 |
| 5381 |
# cperl-brace-imaginary-offset: 0 |
| 5382 |
# cperl-label-offset: 0 |
| 5383 |
# End: |