| 1 |
package Time::ParseDate; |
| 2 |
|
| 3 |
require 5.008001; |
| 4 |
|
| 5 |
use Carp; |
| 6 |
use Time::Timezone; |
| 7 |
use Time::JulianDay; |
| 8 |
require Exporter; |
| 9 |
@ISA = qw(Exporter); |
| 10 |
@EXPORT = qw(parsedate); |
| 11 |
@EXPORT_OK = qw(pd_raw %mtable %umult %wdays); |
| 12 |
|
| 13 |
use strict; |
| 14 |
#use diagnostics; |
| 15 |
|
| 16 |
# constants |
| 17 |
use vars qw(%mtable %umult %wdays $VERSION); |
| 18 |
|
| 19 |
$VERSION = 2026.0219; |
| 20 |
|
| 21 |
# globals |
| 22 |
use vars qw($debug); |
| 23 |
|
| 24 |
# dynamically-scoped |
| 25 |
use vars qw($parse); |
| 26 |
|
| 27 |
my %mtable; |
| 28 |
my %umult; |
| 29 |
my %wdays; |
| 30 |
my $y2k; |
| 31 |
|
| 32 |
CONFIG: { |
| 33 |
|
| 34 |
%mtable = qw( |
| 35 |
Jan 1 Jan. 1 January 1 |
| 36 |
Feb 2 Feb. 2 February 2 |
| 37 |
Mar 3 Mar. 3 March 3 |
| 38 |
Apr 4 Apr. 4 April 4 |
| 39 |
May 5 |
| 40 |
Jun 6 Jun. 6 June 6 |
| 41 |
Jul 7 Jul. 7 July 7 |
| 42 |
Aug 8 Aug. 8 August 8 |
| 43 |
Sep 9 Sep. 9 September 9 Sept 9 |
| 44 |
Oct 10 Oct. 10 October 10 |
| 45 |
Nov 11 Nov. 11 November 11 |
| 46 |
Dec 12 Dec. 12 December 12 ); |
| 47 |
%umult = qw( |
| 48 |
sec 1 second 1 |
| 49 |
min 60 minute 60 |
| 50 |
hour 3600 |
| 51 |
day 86400 |
| 52 |
week 604800 |
| 53 |
fortnight 1209600); |
| 54 |
%wdays = qw( |
| 55 |
sun 0 sunday 0 |
| 56 |
mon 1 monday 1 |
| 57 |
tue 2 tuesday 2 |
| 58 |
wed 3 wednesday 3 |
| 59 |
thu 4 thursday 4 |
| 60 |
fri 5 friday 5 |
| 61 |
sat 6 saturday 6 |
| 62 |
); |
| 63 |
|
| 64 |
$y2k = 946684800; # turn of the century |
| 65 |
} |
| 66 |
|
| 67 |
my $break = qr{(?:\s+|\Z|\b(?![-:.,/]\d))}; |
| 68 |
|
| 69 |
sub parsedate |
| 70 |
{ |
| 71 |
my ($t, %options) = @_; |
| 72 |
|
| 73 |
my ($y, $m, $d); # year, month - 1..12, day |
| 74 |
my ($H, $M, $S); # hour, minute, second |
| 75 |
my $tz; # timezone |
| 76 |
my $tzo; # timezone offset |
| 77 |
my ($rd, $rs); # relative days, relative seconds |
| 78 |
|
| 79 |
my $rel; # time&|date is relative |
| 80 |
|
| 81 |
my $isspec; |
| 82 |
my $now = defined($options{NOW}) ? $options{NOW} : time; |
| 83 |
my $passes = 0; |
| 84 |
my $uk = defined($options{UK}) ? $options{UK} : 0; |
| 85 |
|
| 86 |
local $parse = ''; # will be dynamically scoped. |
| 87 |
|
| 88 |
if ($t =~ s#^ ([ \d]\d) |
| 89 |
/ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) |
| 90 |
/ (\d\d\d\d) |
| 91 |
: (\d\d) |
| 92 |
: (\d\d) |
| 93 |
: (\d\d) |
| 94 |
(?: |
| 95 |
[ ] |
| 96 |
([-+] \d\d\d\d) |
| 97 |
(?: \("?(?:(?:[A-Z]{1,4}[TCW56])|IDLE)\))? |
| 98 |
)? |
| 99 |
$break |
| 100 |
##xi) { #"emacs |
| 101 |
# [ \d]/Mon/yyyy:hh:mm:ss [-+]\d\d\d\d |
| 102 |
# This is the format for www server logging. |
| 103 |
|
| 104 |
($d, $m, $y, $H, $M, $S, $tzo) = ($1, $mtable{"\u\L$2"}, $3, $4, $5, $6, $7 ? &mkoff($7) : ($tzo || undef)); |
| 105 |
$parse .= " ".__LINE__ if $debug; |
| 106 |
} elsif ($t =~ s#^(\d\d)/(\d\d)/(\d\d)\.(\d\d)\:(\d\d)($break)##) { |
| 107 |
# yy/mm/dd.hh:mm |
| 108 |
# I support this format because it's used by wbak/rbak |
| 109 |
# on Apollo Domain OS. Silly, but historical. |
| 110 |
|
| 111 |
($y, $m, $d, $H, $M, $S) = ($1, $2, $3, $4, $5, 0); |
| 112 |
$parse .= " ".__LINE__ if $debug; |
| 113 |
} else { |
| 114 |
while(1) { |
| 115 |
if (! defined $m and ! defined $rd and ! defined $y |
| 116 |
and ! ($passes == 0 and $options{'TIMEFIRST'})) |
| 117 |
{ |
| 118 |
# no month defined. |
| 119 |
if (&parse_date_only(\$t, \$y, \$m, \$d, $uk)) { |
| 120 |
$parse .= " ".__LINE__ if $debug; |
| 121 |
next; |
| 122 |
} |
| 123 |
} |
| 124 |
if (! defined $H and ! defined $rs) { |
| 125 |
if (&parse_time_only(\$t, \$H, \$M, \$S, |
| 126 |
\$tz, %options)) |
| 127 |
{ |
| 128 |
$parse .= " ".__LINE__ if $debug; |
| 129 |
next; |
| 130 |
} |
| 131 |
} |
| 132 |
next if $passes == 0 and $options{'TIMEFIRST'}; |
| 133 |
if (! defined $y) { |
| 134 |
if (&parse_year_only(\$t, \$y, $now, %options)) { |
| 135 |
$parse .= " ".__LINE__ if $debug; |
| 136 |
next; |
| 137 |
} |
| 138 |
} |
| 139 |
if (! defined $tz and ! defined $tzo and ! defined $rs |
| 140 |
and (defined $m or defined $H)) |
| 141 |
{ |
| 142 |
if (&parse_tz_only(\$t, \$tz, \$tzo)) { |
| 143 |
$parse .= " ".__LINE__ if $debug; |
| 144 |
next; |
| 145 |
} |
| 146 |
} |
| 147 |
if (! defined $H and ! defined $rs) { |
| 148 |
if (&parse_time_offset(\$t, \$rs, %options)) { |
| 149 |
$rel = 1; |
| 150 |
$parse .= " ".__LINE__ if $debug; |
| 151 |
next; |
| 152 |
} |
| 153 |
} |
| 154 |
if (! defined $m and ! defined $rd and ! defined $y) { |
| 155 |
if (&parse_date_offset(\$t, $now, \$y, |
| 156 |
\$m, \$d, \$rd, \$rs, %options)) |
| 157 |
{ |
| 158 |
$rel = 1; |
| 159 |
$parse .= " ".__LINE__ if $debug; |
| 160 |
next; |
| 161 |
} |
| 162 |
} |
| 163 |
if (defined $M or defined $rd) { |
| 164 |
if ($t =~ s/^\s*(?:at|\@|\+)($break)//x) { |
| 165 |
$rel = 1; |
| 166 |
$parse .= " ".__LINE__ if $debug; |
| 167 |
next; |
| 168 |
} |
| 169 |
} |
| 170 |
last; |
| 171 |
} continue { |
| 172 |
$passes++; |
| 173 |
&debug_display($tz, $tzo, $H, $M, $S, $m, $d, $y, $rs, $rd, $rel, $passes, $parse, $t) if $debug; |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
if ($passes == 0) { |
| 178 |
print "nothing matched\n" if $debug; |
| 179 |
return (undef, "no match on time/date") |
| 180 |
if wantarray(); |
| 181 |
return undef; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
&debug_display($tz, $tzo, $H, $M, $S, $m, $d, $y, $rs, $rd, $rel, $passes, $parse, $t) if $debug; |
| 186 |
|
| 187 |
$t =~ s/^\s+//; |
| 188 |
|
| 189 |
if ($t ne '') { |
| 190 |
# we didn't manage to eat the string |
| 191 |
print "NOT WHOLE\n" if $debug; |
| 192 |
if ($options{WHOLE}) { |
| 193 |
return (undef, "characters left over after parse") |
| 194 |
if wantarray(); |
| 195 |
return undef |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
# define a date if there isn't one already |
| 200 |
|
| 201 |
if (! defined $y and ! defined $m and ! defined $rd) { |
| 202 |
print "no date defined, trying to find one." if $debug; |
| 203 |
if (defined $rs or defined $H) { |
| 204 |
# we do have a time. |
| 205 |
if ($options{DATE_REQUIRED}) { |
| 206 |
return (undef, "no date specified") |
| 207 |
if wantarray(); |
| 208 |
return undef; |
| 209 |
} |
| 210 |
if (defined $rs) { |
| 211 |
print "simple offset: $rs\n" if $debug; |
| 212 |
my $rv = $now + $rs; |
| 213 |
return ($rv, $t) if wantarray(); |
| 214 |
return $rv; |
| 215 |
} |
| 216 |
$rd = 0; |
| 217 |
} else { |
| 218 |
print "no time either!\n" if $debug; |
| 219 |
return (undef, "no time specified") |
| 220 |
if wantarray(); |
| 221 |
return undef; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
if ($options{TIME_REQUIRED} && ! defined($rs) |
| 226 |
&& ! defined($H) && ! defined($rd)) |
| 227 |
{ |
| 228 |
return (undef, "no time found") |
| 229 |
if wantarray(); |
| 230 |
return undef; |
| 231 |
} |
| 232 |
|
| 233 |
my $secs; |
| 234 |
my $jd; |
| 235 |
|
| 236 |
if (defined $rd) { |
| 237 |
if (defined $rs || ! (defined($H) || defined($M) || defined($S))) { |
| 238 |
print "fully relative\n" if $debug; |
| 239 |
my ($j, $in, $it); |
| 240 |
my $definedrs = defined($rs) ? $rs : 0; |
| 241 |
my ($isdst_now, $isdst_then); |
| 242 |
my $r = $now + $rd * 86400 + $definedrs; |
| 243 |
# |
| 244 |
# It's possible that there was a timezone shift |
| 245 |
# during the time specified. If so, keep the |
| 246 |
# hours the "same". |
| 247 |
# |
| 248 |
$isdst_now = (localtime($r))[8]; |
| 249 |
$isdst_then = (localtime($now))[8]; |
| 250 |
if (($isdst_now == $isdst_then) || $options{GMT}) |
| 251 |
{ |
| 252 |
return ($r, $t) if wantarray(); |
| 253 |
return $r |
| 254 |
} |
| 255 |
|
| 256 |
print "localtime changed DST during time period!\n" if $debug; |
| 257 |
} |
| 258 |
|
| 259 |
print "relative date\n" if $debug; |
| 260 |
$jd = $options{GMT} |
| 261 |
? gm_julian_day($now) |
| 262 |
: local_julian_day($now); |
| 263 |
print "jd($now) = $jd\n" if $debug; |
| 264 |
$jd += $rd; |
| 265 |
} else { |
| 266 |
unless (defined $y) { |
| 267 |
if ($options{PREFER_PAST}) { |
| 268 |
my ($day, $mon011); |
| 269 |
($day, $mon011, $y) = (&righttime($now))[3,4,5]; |
| 270 |
|
| 271 |
print "calc year -past $day-$d $mon011-$m $y\n" if $debug; |
| 272 |
$y -= 1 if ($mon011+1 < $m) || |
| 273 |
(($mon011+1 == $m) && ($day < $d)); |
| 274 |
} elsif ($options{PREFER_FUTURE}) { |
| 275 |
print "calc year -future\n" if $debug; |
| 276 |
my ($day, $mon011); |
| 277 |
($day, $mon011, $y) = (&righttime($now))[3,4,5]; |
| 278 |
$y += 1 if ($mon011 >= $m) || |
| 279 |
(($mon011+1 == $m) && ($day > $d)); |
| 280 |
} else { |
| 281 |
print "calc year -this\n" if $debug; |
| 282 |
$y = (localtime($now))[5]; |
| 283 |
} |
| 284 |
$y += 1900; |
| 285 |
} |
| 286 |
|
| 287 |
$y = expand_two_digit_year($y, $now, %options) |
| 288 |
if $y < 100; |
| 289 |
|
| 290 |
if ($options{VALIDATE}) { |
| 291 |
require Time::DaysInMonth; |
| 292 |
my $dim = Time::DaysInMonth::days_in($y, $m); |
| 293 |
if ($y < 1000 or $m < 1 or $d < 1 |
| 294 |
or $y > 9999 or $m > 12 or $d > $dim) |
| 295 |
{ |
| 296 |
return (undef, "illegal YMD: $y, $m, $d") |
| 297 |
if wantarray(); |
| 298 |
return undef; |
| 299 |
} |
| 300 |
} |
| 301 |
$jd = julian_day($y, $m, $d); |
| 302 |
print "jd($y, $m, $d) = $jd\n" if $debug; |
| 303 |
} |
| 304 |
|
| 305 |
# put time into HMS |
| 306 |
|
| 307 |
if (! defined($H)) { |
| 308 |
if (defined($rd) || defined($rs)) { |
| 309 |
($S, $M, $H) = &righttime($now, %options); |
| 310 |
print "HMS set to $H $M $S\n" if $debug; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
my $carry; |
| 315 |
|
| 316 |
print "before ", (defined($rs) ? "$rs" : ""), |
| 317 |
" $jd $H $M $S\n" |
| 318 |
if $debug; |
| 319 |
# |
| 320 |
# add in relative seconds. Do it this way because we want to |
| 321 |
# preserve the localtime across DST changes. |
| 322 |
# |
| 323 |
|
| 324 |
$S = 0 unless $S; # -w |
| 325 |
$M = 0 unless $M; # -w |
| 326 |
$H = 0 unless $H; # -w |
| 327 |
|
| 328 |
if ($options{VALIDATE} and |
| 329 |
($S < 0 or $M < 0 or $H < 0 or $S > 59 or $M > 59 or $H > 23)) |
| 330 |
{ |
| 331 |
return (undef, "illegal HMS: $H, $M, $S") if wantarray(); |
| 332 |
return undef; |
| 333 |
} |
| 334 |
|
| 335 |
$S += $rs if defined $rs; |
| 336 |
$carry = int($S / 60) - ($S < 0 && $S % 60 && 1); |
| 337 |
$S -= $carry * 60; |
| 338 |
$M += $carry; |
| 339 |
$carry = int($M / 60) - ($M < 0 && $M % 60 && 1); |
| 340 |
$M %= 60; |
| 341 |
$H += $carry; |
| 342 |
$carry = int($H / 24) - ($H < 0 && $H % 24 && 1); |
| 343 |
$H %= 24; |
| 344 |
$jd += $carry; |
| 345 |
|
| 346 |
print "after rs $jd $H $M $S\n" if $debug; |
| 347 |
|
| 348 |
$secs = jd_secondsgm($jd, $H, $M, $S); |
| 349 |
print "jd_secondsgm($jd, $H, $M, $S) = $secs\n" if $debug; |
| 350 |
|
| 351 |
# |
| 352 |
# If we see something link 3pm CST then and we want to end |
| 353 |
# up with a GMT seconds, then we convert the 3pm to GMT and |
| 354 |
# subtract in the offset for CST. We subtract because we |
| 355 |
# are converting from CST to GMT. |
| 356 |
# |
| 357 |
my $tzadj; |
| 358 |
if ($tz) { |
| 359 |
$tzadj = tz_offset($tz, $secs); |
| 360 |
if (defined $tzadj) { |
| 361 |
print "adjusting secs for $tz: $tzadj\n" if $debug; |
| 362 |
$tzadj = tz_offset($tz, $secs-$tzadj); |
| 363 |
$secs -= $tzadj; |
| 364 |
} else { |
| 365 |
print "unknown timezone: $tz\n" if $debug; |
| 366 |
undef $secs; |
| 367 |
undef $t; |
| 368 |
} |
| 369 |
} elsif (defined $tzo) { |
| 370 |
print "adjusting time for offset: $tzo\n" if $debug; |
| 371 |
$secs -= $tzo; |
| 372 |
} else { |
| 373 |
unless ($options{GMT}) { |
| 374 |
if ($options{ZONE}) { |
| 375 |
$tzadj = tz_offset($options{ZONE}, $secs) || 0; |
| 376 |
$tzadj = tz_offset($options{ZONE}, $secs-$tzadj); |
| 377 |
unless (defined($tzadj)) { |
| 378 |
return (undef, "could not convert '$options{ZONE}' to time offset") |
| 379 |
if wantarray(); |
| 380 |
return undef; |
| 381 |
} |
| 382 |
print "adjusting secs for $options{ZONE}: $tzadj\n" if $debug; |
| 383 |
$secs -= $tzadj; |
| 384 |
} else { |
| 385 |
$tzadj = tz_local_offset($secs); |
| 386 |
print "adjusting secs for local offset: $tzadj\n" if $debug; |
| 387 |
# |
| 388 |
# Just in case we are very close to a time |
| 389 |
# change... |
| 390 |
# |
| 391 |
$tzadj = tz_local_offset($secs-$tzadj); |
| 392 |
$secs -= $tzadj; |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
print "returning $secs.\n" if $debug; |
| 398 |
|
| 399 |
return ($secs, $t) if wantarray(); |
| 400 |
return $secs; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
sub mkoff |
| 405 |
{ |
| 406 |
my($offset) = @_; |
| 407 |
|
| 408 |
if (defined $offset and $offset =~ s#^([-+])(\d\d):?(\d\d)$##) { |
| 409 |
return ($1 eq '+' ? |
| 410 |
3600 * $2 + 60 * $3 |
| 411 |
: -3600 * $2 + -60 * $3 ); |
| 412 |
} |
| 413 |
return undef; |
| 414 |
} |
| 415 |
|
| 416 |
sub parse_tz_only |
| 417 |
{ |
| 418 |
my($tr, $tz, $tzo) = @_; |
| 419 |
|
| 420 |
$$tr =~ s#^\s+##; |
| 421 |
my $o; |
| 422 |
|
| 423 |
if ($$tr =~ s#^ |
| 424 |
([-+]\d\d:?\d\d) |
| 425 |
\s+ |
| 426 |
\( |
| 427 |
"? |
| 428 |
(?: |
| 429 |
(?: |
| 430 |
[A-Z]{1,4}[TCW56] |
| 431 |
) |
| 432 |
| |
| 433 |
IDLE |
| 434 |
) |
| 435 |
\) |
| 436 |
$break |
| 437 |
##x) { #"emacs |
| 438 |
$$tzo = &mkoff($1); |
| 439 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 440 |
return 1; |
| 441 |
} elsif ($$tr =~ s#^GMT\s*([-+]\d{1,2})($break)##x) { |
| 442 |
$o = $1; |
| 443 |
if ($o < 24 and $o !~ /^0/) { |
| 444 |
# probably hours. |
| 445 |
printf "adjusted at %d. ($o 00)\n", __LINE__ if $debug; |
| 446 |
$o = "${o}00"; |
| 447 |
} |
| 448 |
$o =~ s/\b(\d\d\d)/0$1/; |
| 449 |
$$tzo = &mkoff($o); |
| 450 |
printf "matched at %d. ($$tzo, $o)\n", __LINE__ if $debug; |
| 451 |
return 1; |
| 452 |
} elsif ($$tr =~ s#^(?:GMT\s*)?([-+]\d\d:?\d\d)($break)##x) { |
| 453 |
$o = $1; |
| 454 |
$$tzo = &mkoff($o); |
| 455 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 456 |
return 1; |
| 457 |
} elsif ($$tr =~ s#^"?((?:[A-Z]{1,4}[TCW56])|IDLE)$break##x) { #" |
| 458 |
$$tz = $1; |
| 459 |
$$tz .= " DST" |
| 460 |
if $$tz eq 'MET' && $$tr =~ s#^DST$break##x; |
| 461 |
printf "matched at %d: '$$tz'.\n", __LINE__ if $debug; |
| 462 |
return 1; |
| 463 |
} |
| 464 |
return 0; |
| 465 |
} |
| 466 |
|
| 467 |
sub parse_date_only |
| 468 |
{ |
| 469 |
my ($tr, $yr, $mr, $dr, $uk) = @_; |
| 470 |
|
| 471 |
$$tr =~ s#^\s+##; |
| 472 |
|
| 473 |
if ($$tr =~ s#^(\d\d\d\d)([-./])(\d\d?)\2(\d\d?)(T|$break)##) { |
| 474 |
# yyyy/mm/dd |
| 475 |
|
| 476 |
($$yr, $$mr, $$dr) = ($1, $3, $4); |
| 477 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 478 |
return 1; |
| 479 |
} elsif ($$tr =~ s#^(\d\d?)([-./])(\d\d?)\2(\d\d\d\d?)($break)##) { |
| 480 |
# mm/dd/yyyy - is this safe? No. |
| 481 |
# -- or dd/mm/yyyy! If $1>12, then it's unambiguous. |
| 482 |
# Otherwise check option UK for UK style date. |
| 483 |
if ($uk || $1>12) { |
| 484 |
($$yr, $$mr, $$dr) = ($4, $3, $1); |
| 485 |
} else { |
| 486 |
($$yr, $$mr, $$dr) = ($4, $1, $3); |
| 487 |
} |
| 488 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 489 |
return 1; |
| 490 |
} elsif ($$tr =~ s#^(\d\d\d\d)/(\d\d?)$break##x) { |
| 491 |
# yyyy/mm |
| 492 |
|
| 493 |
($$yr, $$mr, $$dr) = ($1, $2, 1); |
| 494 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 495 |
return 1; |
| 496 |
} elsif ($$tr =~ s#^(?xi) |
| 497 |
(?: |
| 498 |
(?:Mon|Monday|Tue|Tuesday|Wed|Wednesday| |
| 499 |
Thu|Thursday|Fri|Friday| |
| 500 |
Sat|Saturday|Sun|Sunday),? |
| 501 |
\s+ |
| 502 |
)? |
| 503 |
(\d\d?) |
| 504 |
(\s+ | - | \. | /) |
| 505 |
(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\.? |
| 506 |
(?: |
| 507 |
\2 |
| 508 |
(\d\d (?:\d\d)? ) |
| 509 |
)? |
| 510 |
$break |
| 511 |
##) { |
| 512 |
# [Dow,] dd Mon [yy[yy]] |
| 513 |
($$yr, $$mr, $$dr) = ($4, $mtable{"\u\L$3"}, $1); |
| 514 |
|
| 515 |
printf "%d: %s - %s - %s\n", __LINE__, $1, $2, $3 if $debug; |
| 516 |
print "y undef\n" if ($debug && ! defined($$yr)); |
| 517 |
return 1; |
| 518 |
} elsif ($$tr =~ s#^(?xi) |
| 519 |
(?: |
| 520 |
(?:Mon|Monday|Tue|Tuesday|Wed|Wednesday| |
| 521 |
Thu|Thursday|Fri|Friday| |
| 522 |
Sat|Saturday|Sun|Sunday),? |
| 523 |
\s+ |
| 524 |
)? |
| 525 |
(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\.? |
| 526 |
((\s)+ | - | \. | /) |
| 527 |
|
| 528 |
(\d\d?) |
| 529 |
,? |
| 530 |
(?: |
| 531 |
(?: \2|\3+) |
| 532 |
(\d\d (?: \d\d)?) |
| 533 |
)? |
| 534 |
$break |
| 535 |
##) { |
| 536 |
# [Dow,] Mon dd [yyyy] |
| 537 |
# [Dow,] Mon d, [yy] |
| 538 |
($$yr, $$mr, $$dr) = ($5, $mtable{"\u\L$1"}, $4); |
| 539 |
printf "%d: %s - %s - %s\n", __LINE__, $1, $2, $4 if $debug; |
| 540 |
print "y undef\n" if ($debug && ! defined($$yr)); |
| 541 |
return 1; |
| 542 |
} elsif ($$tr =~ s#^(?xi) |
| 543 |
(January|Jan\.?|February|Feb\.?|March|Mar\.?|April|Apr\.?|May| |
| 544 |
June|Jun\.?|July|Jul\.?|August|Aug\.?|September|Sep\.?| |
| 545 |
October|Oct\.?|November|Nov\.?|December|Dec\.?) |
| 546 |
\s+ |
| 547 |
(\d+) |
| 548 |
(?:st|nd|rd|th)? |
| 549 |
\,? |
| 550 |
(?: |
| 551 |
\s+ |
| 552 |
(?: |
| 553 |
(\d\d\d\d) |
| 554 |
|(?:\' (\d\d)) |
| 555 |
) |
| 556 |
)? |
| 557 |
$break |
| 558 |
##) { |
| 559 |
# Month day{st,nd,rd,th}, 'yy |
| 560 |
# Month day{st,nd,rd,th}, year |
| 561 |
# Month day, year |
| 562 |
# Mon. day, year |
| 563 |
($$yr, $$mr, $$dr) = ($3 || $4, $mtable{"\u\L$1"}, $2); |
| 564 |
printf "%d: %s - %s - %s - %s\n", __LINE__, $1, $2, $3, $4 if $debug; |
| 565 |
print "y undef\n" if ($debug && ! defined($$yr)); |
| 566 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 567 |
return 1; |
| 568 |
} elsif ($$tr =~ s#^(\d\d?)([-/.])(\d\d?)\2(\d\d?)($break)##x) { |
| 569 |
if ($1 > 31 || (!$uk && $1 > 12 && $4 < 32)) { |
| 570 |
# yy/mm/dd |
| 571 |
($$yr, $$mr, $$dr) = ($1, $3, $4); |
| 572 |
} elsif ($1 > 12 || $uk) { |
| 573 |
# dd/mm/yy |
| 574 |
($$yr, $$mr, $$dr) = ($4, $3, $1); |
| 575 |
} else { |
| 576 |
# mm/dd/yy |
| 577 |
($$yr, $$mr, $$dr) = ($4, $1, $3); |
| 578 |
} |
| 579 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 580 |
return 1; |
| 581 |
} elsif ($$tr =~ s#^(\d\d?)/(\d\d?)($break)##x) { |
| 582 |
if ($1 > 31 || (!$uk && $1 > 12)) { |
| 583 |
# yy/mm |
| 584 |
($$yr, $$mr, $$dr) = ($1, $2, 1); |
| 585 |
} elsif ($2 > 31 || ($uk && $2 > 12)) { |
| 586 |
# mm/yy |
| 587 |
($$yr, $$mr, $$dr) = ($2, $1, 1); |
| 588 |
} elsif ($1 > 12 || $uk) { |
| 589 |
# dd/mm |
| 590 |
($$mr, $$dr) = ($2, $1); |
| 591 |
} else { |
| 592 |
# mm/dd |
| 593 |
($$mr, $$dr) = ($1, $2); |
| 594 |
} |
| 595 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 596 |
return 1; |
| 597 |
} elsif ($$tr =~ s#^(\d\d)(\d\d)(\d\d)($break)##x) { |
| 598 |
if ($1 > 31 || (!$uk && $1 > 12)) { |
| 599 |
# YYMMDD |
| 600 |
($$yr, $$mr, $$dr) = ($1, $2, $3); |
| 601 |
} elsif ($1 > 12 || $uk) { |
| 602 |
# DDMMYY |
| 603 |
($$yr, $$mr, $$dr) = ($3, $2, $1); |
| 604 |
} else { |
| 605 |
# MMDDYY |
| 606 |
($$yr, $$mr, $$dr) = ($3, $1, $2); |
| 607 |
} |
| 608 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 609 |
return 1; |
| 610 |
} elsif ($$tr =~ s#^(?xi) |
| 611 |
(\d{1,2}) |
| 612 |
(\s+ | - | \. | /) |
| 613 |
(January|Jan\.?|February|Feb\.?|March|Mar\.?|April|Apr\.?|May| |
| 614 |
June|Jun\.?|July|Jul\.?|August|Aug\.?|September|Sep\.?| |
| 615 |
October|Oct\.?|November|Nov\.?|December|Dec\.?) |
| 616 |
(?: |
| 617 |
\2 |
| 618 |
( |
| 619 |
\d\d |
| 620 |
(?:\d\d)? |
| 621 |
) |
| 622 |
) |
| 623 |
$break |
| 624 |
##) { |
| 625 |
# dd Month [yr] |
| 626 |
($$yr, $$mr, $$dr) = ($4, $mtable{"\u\L$3"}, $1); |
| 627 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 628 |
return 1; |
| 629 |
} elsif ($$tr =~ s#^(?xi) |
| 630 |
(\d+) |
| 631 |
(?:st|nd|rd|th)? |
| 632 |
\s+ |
| 633 |
(January|Jan\.?|February|Feb\.?|March|Mar\.?|April|Apr\.?|May| |
| 634 |
June|Jun\.?|July|Jul\.?|August|Aug\.?|September|Sep\.?| |
| 635 |
October|Oct\.?|November|Nov\.?|December|Dec\.?) |
| 636 |
(?: |
| 637 |
\,? |
| 638 |
\s+ |
| 639 |
(\d\d\d\d) |
| 640 |
)? |
| 641 |
$break |
| 642 |
##) { |
| 643 |
# day{st,nd,rd,th}, Month year |
| 644 |
($$yr, $$mr, $$dr) = ($3, $mtable{"\u\L$2"}, $1); |
| 645 |
printf "%d: %s - %s - %s - %s\n", __LINE__, $1, $2, $3, $4 if $debug; |
| 646 |
print "y undef\n" if ($debug && ! defined($$yr)); |
| 647 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 648 |
return 1; |
| 649 |
} |
| 650 |
return 0; |
| 651 |
} |
| 652 |
|
| 653 |
sub parse_time_only |
| 654 |
{ |
| 655 |
my ($tr, $hr, $mr, $sr, $tzr, %options) = @_; |
| 656 |
|
| 657 |
$$tr =~ s#^\s+##; |
| 658 |
|
| 659 |
if ($$tr =~ s/^(?x) |
| 660 |
(?: |
| 661 |
(?: |
| 662 |
([012]\d) (?# $1) |
| 663 |
(?: |
| 664 |
([0-5]\d) (?# $2) |
| 665 |
(?: |
| 666 |
([0-5]\d) (?# $3) |
| 667 |
)? |
| 668 |
) |
| 669 |
(?:\s*([apAP][mM]))? (?# $4) |
| 670 |
) | (?: |
| 671 |
(\d{1,2}) (?# $5) |
| 672 |
(?: |
| 673 |
\: |
| 674 |
(\d\d) (?# $6) |
| 675 |
(?: |
| 676 |
\: |
| 677 |
(\d\d) (?# $7) |
| 678 |
( |
| 679 |
(?# don't barf on database sub-second timings) |
| 680 |
[:.,] |
| 681 |
\d+ |
| 682 |
)? (?# $8) |
| 683 |
)? |
| 684 |
) |
| 685 |
(?:\s*([apAP][mM]))? (?# $9) |
| 686 |
) | (?: |
| 687 |
(\d{1,2}) (?# $10) |
| 688 |
([apAP][mM]) (?# ${11}) |
| 689 |
) |
| 690 |
) |
| 691 |
(?: |
| 692 |
(?: |
| 693 |
\s+ |
| 694 |
"? |
| 695 |
( (?# ${12}) |
| 696 |
(?: [A-Z]{1,4}[TCW56](?!(?:\+\d+|\s+DST)) ) |
| 697 |
| |
| 698 |
IDLE |
| 699 |
) |
| 700 |
) |
| 701 |
| |
| 702 |
([Zz]) (?# ${13}) |
| 703 |
)? |
| 704 |
$break |
| 705 |
//) { #"emacs |
| 706 |
# HH[[:]MM[:SS]]meridian [zone] |
| 707 |
my $ampm; |
| 708 |
$$hr = $1 || $5 || $10 || 0; # 10 is undef, but 5 is defined.. |
| 709 |
$$mr = $2 || $6 || 0; |
| 710 |
$$sr = $3 || $7 || 0; |
| 711 |
if (defined($8) && exists($options{SUBSECOND}) && $options{SUBSECOND}) { |
| 712 |
my($frac) = $8; |
| 713 |
substr($frac,0,1) = '.'; |
| 714 |
$$sr += $frac; |
| 715 |
} |
| 716 |
print "S = $$sr\n" if $debug; |
| 717 |
$ampm = $4 || $9 || $11 || ''; |
| 718 |
printf "ampm at %d: %s %s %s\n", __LINE__, $4||'(no)', $9||'(no)', $11||'(no)' if $debug; |
| 719 |
$$tzr = $12; |
| 720 |
$$tzr = 'UTC' if defined $13; |
| 721 |
$$hr += 12 if $ampm and "\U$ampm" eq "PM" && $$hr != 12; |
| 722 |
$$hr = 0 if $$hr == 12 && "\U$ampm" eq "AM"; |
| 723 |
printf "matched at %d, rem = %s.\n", __LINE__, $$tr if $debug; |
| 724 |
return 1; |
| 725 |
} elsif ($$tr =~ s#^noon$break##ix) { |
| 726 |
# noon |
| 727 |
($$hr, $$mr, $$sr) = (12, 0, 0); |
| 728 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 729 |
return 1; |
| 730 |
} elsif ($$tr =~ s#^midnight$break##ix) { |
| 731 |
# midnight |
| 732 |
($$hr, $$mr, $$sr) = (0, 0, 0); |
| 733 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 734 |
return 1; |
| 735 |
} |
| 736 |
return 0; |
| 737 |
} |
| 738 |
|
| 739 |
sub parse_time_offset |
| 740 |
{ |
| 741 |
my ($tr, $rsr, %options) = @_; |
| 742 |
|
| 743 |
$$tr =~ s/^\s+//; |
| 744 |
|
| 745 |
return 0 if $options{NO_RELATIVE}; |
| 746 |
|
| 747 |
if ($$tr =~ s{^(?xi) |
| 748 |
(?: |
| 749 |
(-) (?# 1) |
| 750 |
| |
| 751 |
[+] |
| 752 |
)? |
| 753 |
\s* |
| 754 |
(?: |
| 755 |
(\d+(?:\.\d+)?) (?# 2) |
| 756 |
| |
| 757 |
(?:(\d+)\s+(\d+)/(\d+)) (?# 3 4/5) |
| 758 |
) |
| 759 |
\s* |
| 760 |
(sec|second|min|minute|hour)s? (?# 6) |
| 761 |
( |
| 762 |
\s+ |
| 763 |
ago (?# 7) |
| 764 |
)? |
| 765 |
$break |
| 766 |
}{}) { |
| 767 |
# count units |
| 768 |
$$rsr = 0 unless defined $$rsr; |
| 769 |
return 0 if defined($5) && $5 == 0; |
| 770 |
my $num = defined($2) |
| 771 |
? $2 |
| 772 |
: $3 + $4/$5; |
| 773 |
$num = -$num if $1; |
| 774 |
$$rsr += $umult{"\L$6"} * $num; |
| 775 |
|
| 776 |
$$rsr = -$$rsr if $7 || |
| 777 |
$$tr =~ /\b(day|mon|month|year)s?\s*ago\b/; |
| 778 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 779 |
return 1; |
| 780 |
} |
| 781 |
return 0; |
| 782 |
} |
| 783 |
|
| 784 |
# |
| 785 |
# What to you do with a date that has a two-digit year? |
| 786 |
# There's not much that can be done except make a guess. |
| 787 |
# |
| 788 |
# Some example situations to handle: |
| 789 |
# |
| 790 |
# now year |
| 791 |
# |
| 792 |
# 1999 01 |
| 793 |
# 1999 71 |
| 794 |
# 2010 71 |
| 795 |
# 2110 09 |
| 796 |
# |
| 797 |
|
| 798 |
sub expand_two_digit_year |
| 799 |
{ |
| 800 |
my ($yr, $now, %options) = @_; |
| 801 |
|
| 802 |
return $yr if $yr > 100; |
| 803 |
|
| 804 |
my ($y) = (&righttime($now, %options))[5]; |
| 805 |
$y += 1900; |
| 806 |
my $century = int($y / 100) * 100; |
| 807 |
my $within = $y % 100; |
| 808 |
|
| 809 |
my $r = $yr + $century; |
| 810 |
|
| 811 |
if ($options{PREFER_PAST}) { |
| 812 |
if ($yr > $within) { |
| 813 |
$r = $yr + $century - 100; |
| 814 |
} |
| 815 |
} elsif ($options{PREFER_FUTURE}) { |
| 816 |
# being strict here would be silly |
| 817 |
if ($yr < $within-20) { |
| 818 |
# it's 2019 and the date is '08' |
| 819 |
$r = $yr + $century + 100; |
| 820 |
} |
| 821 |
} elsif ($options{UNAMBIGUOUS}) { |
| 822 |
# we really shouldn't guess |
| 823 |
return undef; |
| 824 |
} else { |
| 825 |
# prefer the current century in most cases |
| 826 |
|
| 827 |
if ($within > 80 && $within - $yr > 60) { |
| 828 |
$r = $yr + $century + 100; |
| 829 |
} |
| 830 |
|
| 831 |
if ($within < 30 && $yr - $within > 59) { |
| 832 |
$r = $yr + $century - 100; |
| 833 |
} |
| 834 |
} |
| 835 |
print "two digit year '$yr' expanded into $r\n" if $debug; |
| 836 |
return $r; |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
sub calc |
| 841 |
{ |
| 842 |
my ($rsr, $yr, $mr, $dr, $rdr, $now, $units, $count, %options) = @_; |
| 843 |
|
| 844 |
confess unless $units; |
| 845 |
$units = "\L$units"; |
| 846 |
print "calc based on $units\n" if $debug; |
| 847 |
|
| 848 |
if ($units eq 'day') { |
| 849 |
$$rdr = $count; |
| 850 |
} elsif ($units eq 'week') { |
| 851 |
$$rdr = $count * 7; |
| 852 |
} elsif ($umult{$units}) { |
| 853 |
$$rsr = $count * $umult{$units}; |
| 854 |
} elsif ($units eq 'mon' || $units eq 'month') { |
| 855 |
($$yr, $$mr, $$dr) = &monthoff($now, $count, %options); |
| 856 |
$$rsr = 0 unless $$rsr; |
| 857 |
} elsif ($units eq 'year') { |
| 858 |
($$yr, $$mr, $$dr) = &monthoff($now, $count * 12, %options); |
| 859 |
$$rsr = 0 unless $$rsr; |
| 860 |
} else { |
| 861 |
carp "interal error"; |
| 862 |
} |
| 863 |
print "calced rsr $$rsr rdr $$rdr, yr $$yr mr $$mr dr $$dr.\n" if $debug; |
| 864 |
} |
| 865 |
|
| 866 |
sub monthoff |
| 867 |
{ |
| 868 |
my ($now, $months, %options) = @_; |
| 869 |
|
| 870 |
# months are 0..11 |
| 871 |
my ($d, $m11, $y) = (&righttime($now, %options)) [ 3,4,5 ] ; |
| 872 |
|
| 873 |
$y += 1900; |
| 874 |
|
| 875 |
print "m11 = $m11 + $months, y = $y\n" if $debug; |
| 876 |
|
| 877 |
$m11 += $months; |
| 878 |
|
| 879 |
print "m11 = $m11, y = $y\n" if $debug; |
| 880 |
if ($m11 > 11 || $m11 < 0) { |
| 881 |
$y -= 1 if $m11 < 0 && ($m11 % 12 != 0); |
| 882 |
$y += int($m11/12); |
| 883 |
|
| 884 |
# this is required to work around a bug in perl 5.003 |
| 885 |
no integer; |
| 886 |
$m11 %= 12; |
| 887 |
} |
| 888 |
print "m11 = $m11, y = $y\n" if $debug; |
| 889 |
|
| 890 |
# |
| 891 |
# What is "1 month from January 31st?" |
| 892 |
# I think the answer is February 28th most years. |
| 893 |
# |
| 894 |
# Similarly, what is one year from February 29th, 1980? |
| 895 |
# I think it's February 28th, 1981. |
| 896 |
# |
| 897 |
# If you disagree, change the following code. |
| 898 |
# |
| 899 |
if ($d > 30 or ($d > 28 && $m11 == 1)) { |
| 900 |
require Time::DaysInMonth; |
| 901 |
my $dim = Time::DaysInMonth::days_in($y, $m11+1); |
| 902 |
print "dim($y,$m11+1)= $dim\n" if $debug; |
| 903 |
$d = $dim if $d > $dim; |
| 904 |
} |
| 905 |
return ($y, $m11+1, $d); |
| 906 |
} |
| 907 |
|
| 908 |
sub righttime |
| 909 |
{ |
| 910 |
my ($time, %options) = @_; |
| 911 |
if ($options{GMT}) { |
| 912 |
return gmtime($time); |
| 913 |
} else { |
| 914 |
return localtime($time); |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
sub parse_year_only |
| 919 |
{ |
| 920 |
my ($tr, $yr, $now, %options) = @_; |
| 921 |
|
| 922 |
$$tr =~ s#^\s+##; |
| 923 |
|
| 924 |
if ($$tr =~ s#^(\d\d\d\d)$break##) { |
| 925 |
$$yr = $1; |
| 926 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 927 |
return 1; |
| 928 |
} elsif ($$tr =~ s#\'(\d\d)$break##) { |
| 929 |
$$yr = expand_two_digit_year($1, $now, %options); |
| 930 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 931 |
return 1; |
| 932 |
} |
| 933 |
return 0; |
| 934 |
} |
| 935 |
|
| 936 |
sub parse_date_offset |
| 937 |
{ |
| 938 |
my ($tr, $now, $yr, $mr, $dr, $rdr, $rsr, %options) = @_; |
| 939 |
|
| 940 |
return 0 if $options{NO_RELATIVE}; |
| 941 |
|
| 942 |
# now - current seconds_since_epoch |
| 943 |
# yr - year return |
| 944 |
# mr - month return |
| 945 |
# dr - day return |
| 946 |
# rdr - relative day return |
| 947 |
# rsr - relative second return |
| 948 |
|
| 949 |
my $j; |
| 950 |
my $wday = (&righttime($now, %options))[6]; |
| 951 |
|
| 952 |
$$tr =~ s#^\s+##; |
| 953 |
|
| 954 |
if ($$tr =~ s#^(?xi) |
| 955 |
\s* |
| 956 |
(\d+) |
| 957 |
\s* |
| 958 |
(day|week|month|year)s? |
| 959 |
( |
| 960 |
\s+ |
| 961 |
ago |
| 962 |
)? |
| 963 |
$break |
| 964 |
##) { |
| 965 |
my $amt = $1 + 0; |
| 966 |
my $units = $2; |
| 967 |
$amt = -$amt if $3 || |
| 968 |
$$tr =~ m#\b(sec|second|min|minute|hour)s?\s*ago\b#; |
| 969 |
&calc($rsr, $yr, $mr, $dr, $rdr, $now, $units, |
| 970 |
$amt, %options); |
| 971 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 972 |
return 1; |
| 973 |
} elsif ($$tr =~ s#^(?xi) |
| 974 |
(?: |
| 975 |
(?: |
| 976 |
now |
| 977 |
\s+ |
| 978 |
)? |
| 979 |
(\+ | \-) |
| 980 |
\s* |
| 981 |
)? |
| 982 |
(\d+) |
| 983 |
\s* |
| 984 |
(day|week|month|year)s? |
| 985 |
$break |
| 986 |
##) { |
| 987 |
my $one = $1 || ''; |
| 988 |
my $two = $2 || ''; |
| 989 |
my $amt = "$one$two"+0; |
| 990 |
&calc($rsr, $yr, $mr, $dr, $rdr, $now, $3, |
| 991 |
$amt, %options); |
| 992 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 993 |
return 1; |
| 994 |
} elsif ($$tr =~ s#^(?xi) |
| 995 |
(Mon|Tue|Wed|Thu|Fri|Sat|Sun|Monday|Tuesday |
| 996 |
|Wednesday|Thursday|Friday|Saturday|Sunday) |
| 997 |
\s+ |
| 998 |
after |
| 999 |
\s+ |
| 1000 |
next |
| 1001 |
$break |
| 1002 |
##) { |
| 1003 |
# Dow "after next" |
| 1004 |
$$rdr = $wdays{"\L$1"} - $wday + ( $wdays{"\L$1"} > $wday ? 7 : 14); |
| 1005 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 1006 |
return 1; |
| 1007 |
} elsif ($$tr =~ s#^(?xi) |
| 1008 |
(Mon|Tue|Wed|Thu|Fri|Sat|Sun|Monday|Tuesday |
| 1009 |
|Wednesday|Thursday|Friday|Saturday|Sunday) |
| 1010 |
\s+ |
| 1011 |
before |
| 1012 |
\s+ |
| 1013 |
last |
| 1014 |
$break |
| 1015 |
##) { |
| 1016 |
# Dow "before last" |
| 1017 |
$$rdr = $wdays{"\L$1"} - $wday - ( $wdays{"\L$1"} < $wday ? 7 : 14); |
| 1018 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 1019 |
return 1; |
| 1020 |
} elsif ($$tr =~ s#^(?xi) |
| 1021 |
next\s+ |
| 1022 |
(Mon|Tue|Wed|Thu|Fri|Sat|Sun|Monday|Tuesday |
| 1023 |
|Wednesday|Thursday|Friday|Saturday|Sunday) |
| 1024 |
$break |
| 1025 |
##) { |
| 1026 |
# "next" Dow |
| 1027 |
$$rdr = $wdays{"\L$1"} - $wday |
| 1028 |
+ ( $wdays{"\L$1"} > $wday ? 0 : 7); |
| 1029 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 1030 |
return 1; |
| 1031 |
} elsif ($$tr =~ s#^(?xi) |
| 1032 |
last\s+ |
| 1033 |
(Mon|Tue|Wed|Thu|Fri|Sat|Sun|Monday|Tuesday |
| 1034 |
|Wednesday|Thursday|Friday|Saturday|Sunday) |
| 1035 |
$break##) { |
| 1036 |
# "last" Dow |
| 1037 |
printf "c %d - %d + ( %d < %d ? 0 : -7 \n", $wdays{"\L$1"}, $wday, $wdays{"\L$1"}, $wday if $debug; |
| 1038 |
$$rdr = $wdays{"\L$1"} - $wday + ( $wdays{"\L$1"} < $wday ? 0 : -7); |
| 1039 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 1040 |
return 1; |
| 1041 |
} elsif ($options{PREFER_PAST} and $$tr =~ s#^(?xi) |
| 1042 |
(Mon|Tue|Wed|Thu|Fri|Sat|Sun|Monday|Tuesday |
| 1043 |
|Wednesday|Thursday|Friday|Saturday|Sunday) |
| 1044 |
$break##) { |
| 1045 |
# Dow |
| 1046 |
printf "c %d - %d + ( %d < %d ? 0 : -7 \n", $wdays{"\L$1"}, $wday, $wdays{"\L$1"}, $wday if $debug; |
| 1047 |
$$rdr = $wdays{"\L$1"} - $wday + ( $wdays{"\L$1"} < $wday ? 0 : -7); |
| 1048 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 1049 |
return 1; |
| 1050 |
} elsif ($options{PREFER_FUTURE} and $$tr =~ s#^(?xi) |
| 1051 |
(Mon|Tue|Wed|Thu|Fri|Sat|Sun|Monday|Tuesday |
| 1052 |
|Wednesday|Thursday|Friday|Saturday|Sunday) |
| 1053 |
$break |
| 1054 |
##) { |
| 1055 |
# Dow |
| 1056 |
$$rdr = $wdays{"\L$1"} - $wday |
| 1057 |
+ ( $wdays{"\L$1"} > $wday ? 0 : 7); |
| 1058 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 1059 |
return 1; |
| 1060 |
} elsif ($$tr =~ s#^today$break##xi) { |
| 1061 |
# today |
| 1062 |
$$rdr = 0; |
| 1063 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 1064 |
return 1; |
| 1065 |
} elsif ($$tr =~ s#^tomorrow$break##xi) { |
| 1066 |
$$rdr = 1; |
| 1067 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 1068 |
return 1; |
| 1069 |
} elsif ($$tr =~ s#^yesterday$break##xi) { |
| 1070 |
$$rdr = -1; |
| 1071 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 1072 |
return 1; |
| 1073 |
} elsif ($$tr =~ s#^last\s+(week|month|year)$break##xi) { |
| 1074 |
&calc($rsr, $yr, $mr, $dr, $rdr, $now, $1, -1, %options); |
| 1075 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 1076 |
return 1; |
| 1077 |
} elsif ($$tr =~ s#^next\s+(week|month|year)$break##xi) { |
| 1078 |
&calc($rsr, $yr, $mr, $dr, $rdr, $now, $1, 1, %options); |
| 1079 |
printf "matched at %d.\n", __LINE__ if $debug; |
| 1080 |
return 1; |
| 1081 |
} elsif ($$tr =~ s#^now $break##x) { |
| 1082 |
$$rdr = 0; |
| 1083 |
return 1; |
| 1084 |
} |
| 1085 |
return 0; |
| 1086 |
} |
| 1087 |
|
| 1088 |
sub debug_display |
| 1089 |
{ |
| 1090 |
my ($tz, $tzo, $H, $M, $S, $m, $d, $y, $rs, $rd, $rel, $passes, $parse, $t) = @_; |
| 1091 |
print "---------<<\n"; |
| 1092 |
print defined($tz) ? "tz: $tz.\n" : "no tz\n"; |
| 1093 |
print defined($tzo) ? "tzo: $tzo.\n" : "no tzo\n"; |
| 1094 |
print "HMS: "; |
| 1095 |
print defined($H) ? "$H, " : "no H, "; |
| 1096 |
print defined($M) ? "$M, " : "no M, "; |
| 1097 |
print defined($S) ? "$S\n" : "no S.\n"; |
| 1098 |
print "mdy: "; |
| 1099 |
print defined($m) ? "$m, " : "no m, "; |
| 1100 |
print defined($d) ? "$d, " : "no d, "; |
| 1101 |
print defined($y) ? "$y\n" : "no y.\n"; |
| 1102 |
print defined($rs) ? "rs: $rs.\n" : "no rs\n"; |
| 1103 |
print defined($rd) ? "rd: $rd.\n" : "no rd\n"; |
| 1104 |
print $rel ? "relative\n" : "not relative\n"; |
| 1105 |
print "passes: $passes\n"; |
| 1106 |
print "parse:$parse\n"; |
| 1107 |
print "t: $t.\n"; |
| 1108 |
print "--------->>\n"; |
| 1109 |
} |
| 1110 |
1; |
| 1111 |
|
| 1112 |
__END__ |
| 1113 |
|
| 1114 |
=head1 NAME |
| 1115 |
|
| 1116 |
Time::ParseDate -- date parsing both relative and absolute |
| 1117 |
|
| 1118 |
=head1 SYNOPSIS |
| 1119 |
|
| 1120 |
use Time::ParseDate; |
| 1121 |
$seconds_since_jan1_1970 = parsedate("12/11/94 2pm", NO_RELATIVE => 1) |
| 1122 |
$seconds_since_jan1_1970 = parsedate("12/11/94 2pm", %options) |
| 1123 |
|
| 1124 |
=head1 OPTIONS |
| 1125 |
|
| 1126 |
Date parsing can also use options. The options are as follows: |
| 1127 |
|
| 1128 |
FUZZY -> it's okay not to parse the entire date string |
| 1129 |
NOW -> the "current" time for relative times (defaults to time()) |
| 1130 |
ZONE -> local timezone (defaults to $ENV{TZ}) |
| 1131 |
WHOLE -> the whole input string must be parsed |
| 1132 |
GMT -> input time is assumed to be GMT, not localtime |
| 1133 |
UK -> prefer UK style dates (dd/mm over mm/dd) |
| 1134 |
DATE_REQUIRED -> do not default the date |
| 1135 |
TIME_REQUIRED -> do not default the time |
| 1136 |
NO_RELATIVE -> input time is not relative to NOW |
| 1137 |
TIMEFIRST -> try parsing time before date [not default] |
| 1138 |
PREFER_PAST -> when year or day of week is ambiguous, assume past |
| 1139 |
PREFER_FUTURE -> when year or day of week is ambiguous, assume future |
| 1140 |
SUBSECOND -> parse fraction seconds |
| 1141 |
VALIDATE -> only accept normal values for HHMMSS, YYMMDD. Otherwise |
| 1142 |
days like -1 might give the last day of the previous month. |
| 1143 |
|
| 1144 |
=head1 DATE FORMATS RECOGNIZED |
| 1145 |
|
| 1146 |
=head2 Absolute date formats |
| 1147 |
|
| 1148 |
Dow, dd Mon yy |
| 1149 |
Dow, dd Mon yyyy |
| 1150 |
Dow, dd Mon |
| 1151 |
dd Mon yy |
| 1152 |
dd Mon yyyy |
| 1153 |
Month day{st,nd,rd,th}, year |
| 1154 |
Month day{st,nd,rd,th} |
| 1155 |
Mon dd yyyy |
| 1156 |
yyyy/mm/dd |
| 1157 |
yyyy-mm-dd (usually the best date specification syntax) |
| 1158 |
yyyy/mm |
| 1159 |
mm/dd/yy |
| 1160 |
mm/dd/yyyy |
| 1161 |
mm/yy |
| 1162 |
yy/mm (only if year > 12, or > 31 if UK) |
| 1163 |
yy/mm/dd (only if year > 12 and day < 32, or year > 31 if UK) |
| 1164 |
dd/mm/yy (only if UK, or an invalid mm/dd/yy or yy/mm/dd) |
| 1165 |
dd/mm/yyyy (only if UK, or an invalid mm/dd/yyyy) |
| 1166 |
dd/mm (only if UK, or an invalid mm/dd) |
| 1167 |
|
| 1168 |
=head2 Relative date formats: |
| 1169 |
|
| 1170 |
count "days" |
| 1171 |
count "weeks" |
| 1172 |
count "months" |
| 1173 |
count "years" |
| 1174 |
Dow "after next" |
| 1175 |
Dow "before last" |
| 1176 |
Dow (requires PREFER_PAST or PREFER_FUTURE) |
| 1177 |
"next" Dow |
| 1178 |
"tomorrow" |
| 1179 |
"today" |
| 1180 |
"yesterday" |
| 1181 |
"last" dow |
| 1182 |
"last week" |
| 1183 |
"now" |
| 1184 |
"now" "+" count units |
| 1185 |
"now" "-" count units |
| 1186 |
"+" count units |
| 1187 |
"-" count units |
| 1188 |
count units "ago" |
| 1189 |
|
| 1190 |
=head2 Absolute time formats: |
| 1191 |
|
| 1192 |
hh:mm:ss[.ddd] |
| 1193 |
hh:mm |
| 1194 |
hh:mm[AP]M |
| 1195 |
hh[AP]M |
| 1196 |
hhmmss[[AP]M] |
| 1197 |
"noon" |
| 1198 |
"midnight" |
| 1199 |
|
| 1200 |
=head2 Relative time formats: |
| 1201 |
|
| 1202 |
count "minutes" (count can be franctional "1.5" or "1 1/2") |
| 1203 |
count "seconds" |
| 1204 |
count "hours" |
| 1205 |
"+" count units |
| 1206 |
"+" count |
| 1207 |
"-" count units |
| 1208 |
"-" count |
| 1209 |
count units "ago" |
| 1210 |
|
| 1211 |
=head2 Timezone formats: |
| 1212 |
|
| 1213 |
[+-]dddd |
| 1214 |
GMT[+-]d+ |
| 1215 |
[+-]dddd (TZN) |
| 1216 |
TZN |
| 1217 |
|
| 1218 |
=head2 Special formats: |
| 1219 |
|
| 1220 |
[ d]d/Mon/yyyy:hh:mm:ss [[+-]dddd] |
| 1221 |
yy/mm/dd.hh:mm |
| 1222 |
|
| 1223 |
=head1 DESCRIPTION |
| 1224 |
|
| 1225 |
This module recognizes the above date/time formats. Usually a |
| 1226 |
date and a time are specified. There are numerous options for |
| 1227 |
controlling what is recognized and what is not. |
| 1228 |
|
| 1229 |
The return code is always the time in seconds since January 1st, 1970 |
| 1230 |
or undef if it was unable to parse the time. |
| 1231 |
|
| 1232 |
If a timezone is specified it must be after the time. Year specifications |
| 1233 |
can be tacked onto the end of absolute times. |
| 1234 |
|
| 1235 |
If C<parsedate()> is called from array context, then it will return two |
| 1236 |
elements. On successful parses, it will return the seconds and what |
| 1237 |
remains of its input string. On unsuccessful parses, it will return |
| 1238 |
C<undef> and an error string. |
| 1239 |
|
| 1240 |
=head1 EXAMPLES |
| 1241 |
|
| 1242 |
$seconds = parsedate("Mon Jan 2 04:24:27 1995"); |
| 1243 |
$seconds = parsedate("Tue Apr 4 00:22:12 PDT 1995"); |
| 1244 |
$seconds = parsedate("04.04.95 00:22", ZONE => PDT); |
| 1245 |
$seconds = parsedate("Jan 1 1999 11:23:34.578", SUBSECOND => 1); |
| 1246 |
$seconds = parsedate("122212 950404", ZONE => PDT, TIMEFIRST => 1); |
| 1247 |
$seconds = parsedate("+3 secs", NOW => 796978800); |
| 1248 |
$seconds = parsedate("2 months", NOW => 796720932); |
| 1249 |
$seconds = parsedate("last Tuesday"); |
| 1250 |
$seconds = parsedate("Sunday before last"); |
| 1251 |
|
| 1252 |
($seconds, $remaining) = parsedate("today is the day"); |
| 1253 |
($seconds, $error) = parsedate("today is", WHOLE=>1); |
| 1254 |
|
| 1255 |
=head1 AUTHOR |
| 1256 |
|
| 1257 |
Best Practical Solutions, LLC E<lt>[email protected]<gt> |
| 1258 |
|
| 1259 |
=head1 ORIGINAL AUTHOR |
| 1260 |
|
| 1261 |
David Muir Sharnoff |
| 1262 |
|
| 1263 |
=head1 BUGS |
| 1264 |
|
| 1265 |
All bugs should be reported via email to |
| 1266 |
|
| 1267 |
L<[email protected]|mailto:[email protected]> |
| 1268 |
|
| 1269 |
or via the web at |
| 1270 |
|
| 1271 |
L<rt.cpan.org|http://rt.cpan.org/Public/Dist/Display.html?Name=Time-ParseDate>. |
| 1272 |
|
| 1273 |
=head1 LICENSE AND COPYRIGHT |
| 1274 |
|
| 1275 |
Copyright (C) 1996-2010 David Muir Sharnoff. |
| 1276 |
Copyright (C) 2011 Google, Inc. |
| 1277 |
Copyright (C) 2026 Best Practical Solutions, LLC. |
| 1278 |
|
| 1279 |
This library is free software; you can redistribute it and/or modify |
| 1280 |
it under the same terms as Perl itself. |
| 1281 |
|