Debian Codegraph @

lib/Date/Manip/Date.pm

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