JobLog/TimeGrammar.pm untested

filter: Statementstmt 0.0% 0 / 365 Branchbran 0.0% 0 / 200 Conditioncond 0.0% 0 / 81 MC/DCmcdc 0.0% 0 / 43 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 735 CC 100 SCAR 63.2
CC
100
Cov
0%
fix_date
1640.0
parse
132.0
decontextualized_date
110.0
CRAP 554.2
1 package App::JobLog::TimeGrammar;
2 $App::JobLog::TimeGrammar::VERSION = '1.042';
3 # ABSTRACT: parse natural (English) language time expressions
4  
5  
6 use Exporter 'import';
7 our @EXPORT = qw(
8 parse
9 daytime
10 );
11  
12 use Modern::Perl;
13 use DateTime;
14 use Class::Autouse qw(
15 App::JobLog::Log
16 );
17 use Carp 'croak';
18 use autouse 'App::JobLog::Config' => qw(
19 log
20 sunday_begins_week
21 pay_period_length
22 start_pay_period
23 DIRECTORY
24 );
25 use autouse 'App::JobLog::Time' => qw(
26 now
27 today
28 tz
29 );
30 no if $] >= 5.018, warnings => "experimental::smartmatch";
31  
32 # some variables we need visible inside the date parsing regex
33 # %matches holds a complete parsing
34 # %buffer, as its name suggests, is a temporary buffer
35 # $d1 and $d2 are the starting and ending dates
36 our ( %matches, %buffer, $d1, $d2 );
37  
38 # buffers for numeric month, day, or year
39 our ( $b1, $b2 );
40  
41 # holds time of day information
42 our $time_buffer;
43  
44 # static maps for translating month and day names to numbers
45 my ( %month_abbr, %day_abbr );
46  
47 # the master date parsing regex
48 my $re = qr{
49 \A \s*+ (?: (?&ever) | (?&span) ) \s*+ \Z
50  
51 (?(DEFINE)
52  
53 (?<ever> (?: all | always | ever | (?:(?:the \s++)? (?: entire | whole ) \s++ )? log ) (?{ $matches{ever} = 1 }) )
54
55 (?<span>
56 ((?&date)) (?{ $d1 = $^N; stow($d1) })
57 (?: (?&span_divider) ((?&date)) (?{ $d2 = $^N; stow($d2) }) )?
58 )
59  
60 (?<span_divider> \s*+ (?: -++ | \b(?: through | thru | to | till?+ | until )\b ) \s*+)
61  
62 (?<at> at | @ )
63  
64 (?<at_time>
65 (?{ $time_buffer = undef })
66 (?: (?: \s++ | \s*+ (?&at) \s*+ ) (?&time))?
67 )
68  
69 (?<at_time_on> (?:(?&at) \s++)? (?&time) \s++ on \s++ )
70  
71 (?<date>
72 (?{ (%buffer, $b1, $b2, $time_buffer) = ()})
73 (?: (?&numeric) | (?&verbal) )
74 (?{ $buffer{time} = $time_buffer if $time_buffer })
75 )
76
77 (?<time>
78 (?{ $time_buffer = undef })
79 (
80 \d{1,2}
81 (?:
82 : \d{2}
83 (?:
84 : \d{2}
85 )?
86 )?
87 (?: \s*+ (?&time_suffix))?
88 )
89 (?{ $time_buffer = $^N })
90 )
91  
92 (?<time_suffix> [ap] (?:m|\.m\.))
93  
94 (?<numeric>
95 (?:
96 (?&year)
97 |
98 (?&ym)
99 |
100 (?&at_time_on) (?&numeric_no_time)
101 |
102 (?&numeric_no_time) (?&at_time))
103 (?{ $buffer{type} = 'numeric' })
104 )
105
106 (?<year> (?{ %buffer = () }) (\d{4}) (?{ $buffer{year} = $^N }) )
107
108 (?<ym> (?&year) (?&divider) (\d{1,2}) (?{ @buffer{qw(month unit)} = ($^N, 'months') }) )
109  
110 (?<numeric_no_time> (?{ %buffer = () }) (?&us) | (?&iso) | (?&md) | (?&dom) )
111  
112 (?<us>
113 (\d{1,2}) (?{ $b1 = $^N })
114 ((?&divider))
115 (\d{1,2}) (?{ $b2 = $^N })
116 \g{-2}
117 (\d{4})
118 (?{
119 $buffer{year} = $^N;
120 $buffer{month} = $b1;
121 $buffer{day} = $b2;
122 })
123 )
124  
125 (?<iso>
126 (\d{4}) (?{ $b1 = $^N })
127 ((?&divider))
128 (\d{1,2}) (?{ $b2 = $^N })
129 \g{-2}
130 (\d{1,2})
131 (?{
132 $buffer{year} = $b1;
133 $buffer{month} = $b2;
134 $buffer{day} = $^N;
135 })
136 )
137  
138 (?<md>
139 (\d{1,2}) (?{ $b1 = $^N })
140 (?&divider)
141 (\d{1,2})
142 (?{
143 $buffer{month} = $b1;
144 $buffer{day} = $^N;
145 })
146 )
147  
148 (?<dom>
149 (\d{1,2})
150 (?{ $buffer{day} = $^N })
151 )
152  
153 (?<verbal>
154 (?: (?&my) | (?&named_period) | (?&relative_period) | (?&month_day) | (?&full) )
155 (?{ $buffer{type} = 'verbal' })
156 )
157  
158 (?<named_period> (?&modifiable_day) | (?&modifiable_month) | (?&modifiable_period) )
159  
160 (?<modifiable_day> (?&at_time_on) (?&modifiable_day_no_time) | (?&modifiable_day_no_time) (?&at_time))
161  
162 (?<modifiable_day_no_time>
163 ((?:(?&modifier) \s++ )?) (?{ $b1 = $^N })
164 ((?&weekday))
165 (?{
166 $buffer{modifier} = $b1 if $b1;
167 $buffer{day} = $^N;
168 })
169 )
170  
171 (?<modifiable_month>
172 ((?:(?&month_modifier) \s++ )?) (?{ $b1 = $^N })
173 ((?&month))
174 (?{
175 $buffer{modifier} = $b1 if $b1;
176 $buffer{month} = $^N;
177 })
178 )
179  
180 (?<modifiable_period>
181 (?{ $b1 = undef })
182 (?:((?&period_modifier)) \s*+ (?{ $b1 = $^N }))?
183 ((?&period))
184 (?{
185 $buffer{modifier} = $b1 if $b1;
186 $buffer{period} = $^N;
187 })
188 )
189  
190 (?<pay> pay | pp | pay \s*+ period )
191  
192 (?<relative_period>
193 (?:(?&at) \s*+)? (?&time) \s++ (?&relative_period_no_time)
194 |
195 (?&relative_period_no_time) (?&at_time)
196 |
197 (?&now)
198 )
199
200 (?<now> now (?{ $buffer{day} = 'today' }))
201  
202 (?<relative_period_no_time> ( yesterday | today | tomorrow ) (?{ $buffer{day} = $^N }))
203  
204 (?<month_day> (?&at_time_on) (?&month_day_no_time) | (?&month_day_no_time) (?&at_time))
205  
206 (?<month_day_no_time> (?&month_first) | (?&day_first) )
207  
208 (?<month_first>
209 ((?&month)) (?{ $b1 = $^N })
210 \s++
211 (\d{1,2})
212 (?{
213 $buffer{month} = $b1;
214 $buffer{day} = $^N;
215 })
216 )
217
218 (?<my> ((?&month)) ,? \s*+ (?&year) (?{ @buffer{qw(month unit)} = ($^N, 'months') }) )
219  
220 (?<day_first>
221 (\d{1,2}) (?{ $b1 = $^N })
222 \s++
223 ((?&month))
224 (?{
225 $buffer{month} = $^N;
226 $buffer{day} = $b1;
227 })
228 )
229  
230 (?<full> (?&at_time_on) (?&full_no_time) | (?&full_no_time) (?&at_time))
231  
232 (?<full_no_time> (?&dm_full) | (?&md_full) )
233  
234 (?<dm_full>
235 (\d{1,2}) (?{ $b1 = $^N })
236 \s++
237 ((?&month)) (?{ $b2 = $^N })
238 ,? \s++
239 (\d{4})
240 (?{
241 $buffer{year} = $^N;
242 $buffer{month} = $b2;
243 $buffer{day} = $b1;
244 })
245 )
246  
247 (?<md_full>
248 ((?&month)) (?{ $b2 = $^N })
249 \s++
250 (\d{1,2}) (?{ $b1 = $^N })
251 , \s++
252 (\d{4})
253 (?{
254 $buffer{year} = $^N;
255 $buffer{month} = $b2;
256 $buffer{day} = $b1;
257 })
258 )
259  
260 (?<weekday> (?&full_weekday) | (?&short_weekday) )
261  
262 (?<full_weekday> sunday | monday | tuesday | wednesday | thursday | friday | saturday )
263  
264 (?<short_weekday> sun | mon | tue | wed | thu | fri | sat )
265  
266 (?<month> (?&full_month) | (?&short_month) )
267  
268 (?<full_month> january | february | march | april | may | june | july | august | september | october | november | december )
269  
270 (?<short_month> jan | feb | mar | apr | may | jun | jul | aug | sep | oct | nov | dec )
271  
272 (?<modifier> last | this | next )
273  
274 (?<period_modifier> (?&modifier) | (?&termini) (?: \s++ of (?: \s++ the )? )? )
275
276 (?<period> week | month | year | (?&pay) )
277  
278 (?<month_modifier> (?&modifier) | (?&termini) (?: \s++ of )? )
279  
280 (?<termini> (?: the \s++ )? (?: (?&beginning) | end ) )
281  
282 (?<beginning> beg(?:in(?:ning)?)?)
283  
284 (?<divider> [-/.])
285  
286 )
287 }xi;
288  
289 # stows everything matched so far in %matches
290 sub stow {
291 my %h = %buffer;
292 $matches{ $_[0] } = \%h;
293 %buffer = ();
294 }
295  
296  
297 sub daytime {
298 my $time = shift;
299  
300 #parse
301 $time =~ /(?<hour>\d++)
302 (?:
303 : (?<minute>\d++)
304 (?:
305 : (?<second>\d++)
306 )?
307 )?
308 (?: \s*+ (?<suffix>[ap]) (\.?)m\g{-1})?
309 /ix;
310 my ( $hour, $minute, $second, $suffix ) =
311 ( $+{hour}, $+{minute} || 0, $+{second} || 0, lc( $+{suffix} || 'x' ) );
312 $hour += 12 if $suffix eq 'p' && $hour < 12;
313 $suffix = 'p' if $hour > 11;
314 $hour = 0 if $hour == 12 && $suffix eq 'a';
315 croak
316 "impossible time: $time" #<--- syntax error at (eval 4158) line 23, near "croak "impossible time: $time""
317  
318 if $hour > 23
319 || $minute > 59
320 || $second > 59
321 || $suffix eq 'a' && $hour > 12;
322 $hour = 0 if $suffix eq 'a' && $hour == 12;
323 return (
324 hour => $hour,
325 minute => $minute,
326 second => $second,
327 suffix => $suffix
328 );
329 }
330  
331  
332 sub parse {
333 my $phrase = shift;
334 local ( %matches, %buffer, $d1, $d2, $b1, $b2, $time_buffer );
335 if ( $phrase =~ $re ) {
336 if ( $matches{ever} ) {
337  
338 # we want the entire timespan of the log
339 my ($se) = App::JobLog::Log->new->first_event;
340 if ($se) {
341 return $se->start, now, 0;
342 }
343 else {
344 return now->subtract( seconds => 1 ), now, 0;
345 }
346 }
347  
348 my $h1 = $matches{$d1};
349 my $unit = delete $h1->{unit};
350 normalize($h1);
351 if ($unit) {
352  
353 # $h1 is necessarily fixed and there is no time associated
354 $h1 = fix_date( $h1, 1 );
355 my $h2 = $h1->clone->add( $unit => 1 )->subtract( seconds => 1 );
356 return $h1, $h2, 1;
357 }
358 else {
359 my %t1 = extract_time( $h1, 1 );
360 my ( $h2, $count, %t2 );
361 if ( $d2 && $matches{$d2} ) {
362 $h2 = $matches{$d2};
363 normalize($h2);
364 %t2 = extract_time($h2);
365 $count = 2;
366 }
367 else {
368 $h2 = {%$h1};
369 %t2 = ( hour => 23, minute => 59, second => 59 );
370 $count = 1;
371 }
372 infer_modifier( $h1, $h2 );
373 my ( $s1, $s2 ) = ( $t1{suffix}, $t2{suffix} );
374 delete $t1{suffix}, delete $t2{suffix};
375 if ( is_fixed($h1) ) {
376 ( $h1, $h2 ) = fixed_start( $h1, $h2, $count == 2 );
377 }
378 elsif ( is_fixed($h2) ) {
379 ( $h1, $h2 ) = fixed_end( $h1, $h2 );
380 }
381 else {
382 ( $h1, $h2 ) = before_now( $h1, $h2, $count == 2 );
383 }
384 croak "dates in \"$phrase\" are out of order"
385 unless DateTime->compare( $h1, $h2 ) <= 0;
386 $h1->set(%t1);
387 $h2->set(%t2);
388 if ( $h1 > $h2 ) {
389 if ( $h1->year == $h2->year
390 && $h1->month == $h2->month
391 && $h1->day == $h2->day
392 && $h2->hour < 12
393 && $s2 eq 'x' )
394 {
395  
396 # we inferred the 12 hour period of the second endpoint incorrectly;
397 # it was in the evening rather than morning
398 $h2->add( hours => 12 );
399 }
400 else {
401 croak "dates in \"$phrase\" are out of order";
402 }
403 }
404 return $h1, $h2, $count == 2;
405 }
406 }
407 croak "cannot parse \"$phrase\" as a date expression";
408 }
409  
410 # if the sole expression is a unit identifier, infer the modifier 'this'
411 sub infer_modifier {
412 my ( $h1, $h2 ) = @_;
413 if ( keys %$h1 == 2 && keys %$h2 == 2 && $h1->{period} && $h2->{period} ) {
414 $h1->{modifier} = $h2->{modifier} = 'this';
415 }
416 }
417  
418 # pulls time expression -- 11:00 am, e.g. -- out of hash and converts it
419 # to a series of units
420 sub extract_time {
421 my ( $h, $is_start ) = @_;
422 my $time = $h->{time};
423 if ( defined $time ) {
424 delete $h->{time};
425  
426 return daytime($time);
427 }
428 else {
429  
430 #return default values
431 return $is_start
432 ? ( hour => 0, minute => 0, second => 0, suffix => 'a' )
433 : ( hour => 23, minute => 59, second => 59, suffix => 'p' );
434 }
435 }
436  
437 # produces interpretation of date expression consistent with a fixed end date
438 sub fixed_end {
439 my ( $h1, $h2 ) = @_;
440 $h2 = fix_date($h2);
441 if ( is_fixed($h1) ) {
442 $h1 = fix_date( $h1, 1 );
443 }
444 else {
445 my ( $unit, $amt ) = time_unit($h1);
446 $h1 = decontextualized_date( $h1, 1 );
447 if ( ref $h1 eq 'DateTime' ) {
448 while ( DateTime->compare( $h1, $h2 ) > 0 ) {
449 $h1->subtract( $unit => $amt );
450 }
451 }
452 else {
453  
454 # we just have a floating weekday
455 $h1 = adjust_weekday( $h1, $h2 );
456 }
457 }
458 return ( $h1, $h2 );
459 }
460  
461 # picks a day of the week before a given date
462 sub adjust_weekday {
463 my ( $ref, $date ) = @_;
464 my $delta = $ref->{day_of_week}
465 || die 'should always be day_of_week key at this point';
466 my $d = $date->clone;
467 $delta = $date->day_of_week - $delta;
468 $delta += 7 if $delta <= 0;
469 $d->subtract( days => $delta );
470 return $d;
471 }
472  
473 # determines the finest grained unit of time by which a given date can be modified
474 sub time_unit {
475 my $h = shift;
476 if ( $h->{type} eq 'numeric' ) {
477 return 'years' => 1 if exists $h->{month};
478 return 'months' => 1;
479 }
480 else {
481 if ( my $period = $h->{period} ) {
482 for ($period) {
483 when ('mon') { return 'months' => 1 }
484 when ('wee') { return 'weeks' => 1 }
485 when ('pay') { return 'days' => pay_period_length() }
486 }
487 }
488 else {
489 return 'years' => 1 if exists $h->{month};
490 return 'weeks' => 1 if exists $h->{day};
491 return 'months' => 1;
492 }
493 }
494 }
495  
496 # produces interpretation of date expression consistent with a fixed start date
497 sub fixed_start {
498 my ( $h1, $h2, $two_endpoints ) = @_;
499 $h1 = fix_date( $h1, 1 );
500 unless ( $two_endpoints || $h2->{type} ne 'numeric' ) {
501 return $h1, $h1->clone if defined $h2->{day};
502 return $h1, $h1->clone->add( years => 1 )->subtract( days => 1 );
503 }
504 if ( is_fixed($h2) ) {
505 $h2 = fix_date($h2);
506 }
507 else {
508 my ( $unit, $amt ) = time_unit($h2);
509 $h2 = decontextualized_date($h2);
510 $h2 = adjust_weekday( $h2, $h1 ) unless ref $h2 eq 'DateTime';
511 $h2->subtract( $unit => $amt ) while $h2 > $h1;
512 $h2->add( $unit => $amt );
513 }
514 return ( $h1, $h2 );
515 }
516  
517 # date relative to now not yet adjusted relative to its position in the span or
518 # another fixed date
519 sub decontextualized_date {
520 my ( $h, $is_start ) = @_;
521 return decontextualized_numeric_date( $h, $is_start )
522 if $h->{type} eq 'numeric';
523 for ( $h->{modifier} ) {
524 when ('end') { $is_start = 0 }
525 when ('beginning') { $is_start = 1 }
526 }
527 if ( my $period = $h->{period} ) {
528 my $date = today;
529 for ($period) {
530 when ('mon') {
531 $date->truncate( to => 'month' );
532 $date->add( months => 1 ) unless $is_start;
533 }
534 when ('wee') {
535 $date->truncate( to => 'week' );
536 $date->subtract( days => 1 ) if sunday_begins_week;
537 $date->add( weeks => 1 ) unless $is_start;
538 }
539 when ('pay') {
540 my $days =
541 $date->delta_days(start_pay_period)->in_units('days');
542 $days %= pay_period_length;
543 $date->subtract( days => $days );
544 $date->add( days => pay_period_length ) unless $is_start;
545 }
546 default {
547 croak 'DEBUG'
548 }
549 }
550 $date->subtract( days => 1 ) unless $is_start;
551 return $date;
552 }
553 else {
554 if ( exists $h->{day} && $h->{day} !~ /^\d++$/ ) {
555 init_day_abbr();
556 $h->{day_of_week} = $day_abbr{ $h->{day} };
557 delete $h->{day};
558 return $h;
559 }
560 if ( exists $h->{month} ) {
561 init_month_abbr();
562 $h->{month} = $month_abbr{ $h->{month} };
563 }
564 return decontextualized_numeric_date( $h, $is_start );
565 }
566 }
567  
568 sub decontextualized_numeric_date {
569 my ( $h, $is_start ) = @_;
570 my $date = today;
571 delete $h->{type};
572 delete $h->{modifier};
573 $h->{year} //= $date->year;
574 $h->{month} //= $date->month;
575 my $day_unspecified = !exists $h->{day};
576 $date = DateTime->new( time_zone => tz(), %$h, day => $h->{day} // 1 );
577  
578 if ( !( exists $h->{day} || $is_start ) ) {
579 $date->add( months => 1 );
580 $date->subtract( days => 1 );
581 }
582 return $date;
583 }
584  
585 sub fix_date {
586 my ( $d, $is_start ) = @_;
587 if ( $d->{type} eq 'verbal' ) {
588 if ( $d->{year} ) {
589 init_month_abbr();
590 $d->{month} = $month_abbr{ $d->{month} };
591 delete $d->{type};
592 return DateTime->new( time_zone => tz(), %$d );
593 }
594 elsif ( my $day = $d->{day} ) {
595 my $date = today;
596 return $date if $day eq 'tod';
597 if ( $day eq 'yes' ) {
598 $date->subtract( days => 1 );
599 return $date;
600 }
601 elsif ( $day eq 'tom' ) {
602 $date->add( days => 1 );
603 return $date;
604 }
605 init_day_abbr();
606 my $day_num = $day_abbr{$day};
607 my $todays_num = $date->day_of_week;
608 if ( $d->{modifier} eq 'this' ) {
609 return $date if $day_num == $todays_num;
610 my $delta =
611 $day_num > $todays_num
612 ? $day_num - $todays_num
613 : 7 - $todays_num + $day_num;
614 $date->add( days => $delta );
615 return $date;
616 }
617 else {
618 my $delta = 7;
619 if ( $day_num < $todays_num ) {
620 $delta = $todays_num - $day_num;
621 }
622 elsif ( $day_num > $todays_num ) {
623 $delta = 7 - $day_num + $todays_num;
624 }
625 $date->subtract( days => $delta );
626 $date->add( days => 14 ) if $d->{modifier} eq 'next';
627 return $date;
628 }
629 }
630  
631 if ( my $period = $d->{period} ) {
632 my $date = today;
633 if ( $d->{modifier} eq 'this' ) {
634 for ($period) {
635 when ('mon') {
636 $date->truncate( to => 'month' );
637 $date->add( months => 1 ) unless $is_start;
638 }
639 when ('wee') {
640 my $is_sunday = $date->day_of_week == 7;
641 $date->truncate( to => 'week' );
642 if (sunday_begins_week) {
643 $date->subtract( days => $is_sunday ? -6 : 1 );
644 }
645 $date->add( weeks => 1 ) unless $is_start;
646 }
647 when ('yea') {
648 $date->truncate( to => 'year' );
649 $date->add( years => 1 ) unless $is_start;
650 }
651 when ('pay') {
652 my $days =
653 $date->delta_days(start_pay_period)->in_units('days');
654 $days %= pay_period_length;
655 $date->subtract( days => $days );
656 $date->add( days => pay_period_length )
657 unless $is_start;
658 }
659 }
660 $date->subtract( days => 1 ) unless $is_start;
661 }
662 else {
663 for ($period) {
664 when ('mon') {
665 $date->truncate( to => 'month' );
666 if ($is_start) {
667 $date->subtract( months => 1 );
668 }
669 else {
670 $date->subtract( days => 1 );
671 }
672 $date->add( months => 2 ) if $d->{modifier} eq 'next';
673 }
674 when ('wee') {
675 my $is_sunday = $date->day_of_week == 7;
676 $date->truncate( to => 'week' );
677 if (sunday_begins_week) {
678 $date->subtract( days => $is_sunday ? -6 : 1 );
679 }
680 if ($is_start) {
681 $date->subtract( weeks => 1 );
682 }
683 else {
684 $date->subtract( days => 1 );
685 }
686 $date->add( days => 14 ) if $d->{modifier} eq 'next';
687 }
688 when ('yea') {
689 $date->truncate( to => 'year' );
690 if ($is_start) {
691 $date->subtract( years => 1 );
692 }
693 else {
694 $date->subtract( days => 1 );
695 }
696 $date->add( years => 2 ) if $d->{modifier} eq 'next';
697 }
698 when ('pay') {
699 my $days =
700 $date->delta_days(start_pay_period)->in_units('days');
701 $days %= pay_period_length;
702 $date->subtract( days => $days );
703 if ($is_start) {
704 $date->subtract( days => pay_period_length );
705 }
706 else {
707 $date->subtract( days => 1 );
708 }
709 $date->add( days => 2 * pay_period_length )
710 if $d->{modifier} eq 'next';
711 }
712 }
713 }
714 return $date;
715 }
716  
717 init_month_abbr();
718 my $date = today;
719 $date->truncate( to => 'month' );
720 my $month_num = $month_abbr{ $d->{month} };
721 my $todays_num = $date->month;
722 if ( $d->{modifier} eq 'this' ) {
723 my $delta = 0;
724 if ( $todays_num > $month_num ) {
725 $delta = 12 - $todays_num + $month_num;
726 }
727 elsif ( $todays_num < $month_num ) {
728 $delta = $month_num - $todays_num;
729 }
730 $delta++ unless $is_start;
731 $date->add( months => $delta );
732 }
733 else {
734 my $delta = 12;
735 if ( $todays_num > $month_num ) {
736 $delta = $todays_num - $month_num;
737 }
738 elsif ( $todays_num < $month_num ) {
739 $delta -= $month_num - $todays_num;
740 }
741 $delta-- unless $is_start;
742 $date->subtract( months => $delta );
743 }
744 $date->subtract( days => 1 ) unless $is_start;
745 return $date;
746 }
747  
748 # numeric date
749 delete $d->{type};
750 return DateTime->new( time_zone => tz(), %$d );
751 }
752  
753 # lazy initialization of verbal -> numeric month map
754 sub init_month_abbr {
755 unless (%month_abbr) {
756 my @months = qw(jan feb mar apr may jun jul aug sep oct nov dec);
757 init_hash( \%month_abbr, \@months );
758 }
759 }
760  
761 # lazy initialization of verbal -> numeric day of week map
762 sub init_day_abbr {
763 unless (%day_abbr) {
764 my @days = qw(mon tue wed thu fri sat sun);
765 init_hash( \%day_abbr, \@days );
766 }
767 }
768  
769 # generic verbal -> numeric map generator
770 sub init_hash {
771 my ( $h, $units ) = @_;
772 while (@$units) {
773 my $i = @$units;
774 my $u = pop @$units;
775 $h->{$u} = $i;
776 }
777 }
778  
779 # produces interpretation of date expression such that neither date ends after
780 # the present
781 sub before_now {
782 my ( $h1, $h2, $two_endpoints ) = @_;
783 infer_missing( $h1, $h2 ) if $two_endpoints;
784 my $now = today;
785 my ( $u1, $amt1, $u2, $amt2 ) = ( time_unit($h1), time_unit($h2) );
786 ( $h1, $h2 ) =
787 ( decontextualized_date( $h1, 1 ), decontextualized_date($h2) );
788 $h2 = adjust_weekday( $h2, $now ) unless ref $h2 eq 'DateTime';
789 $h1 = adjust_weekday( $h1, $now ) unless ref $h1 eq 'DateTime';
790 while ( $now < $h2 ) {
791 $h2->subtract( $u2 => $amt2 );
792 }
793 while ( $h2 < $h1 ) {
794 $h1->subtract( $u1 => $amt1 );
795 }
796  
797 if ($two_endpoints) {
798  
799 # move the two dates as close together as possible
800 while ( $h1 < $h2 ) {
801 $h2->subtract( $u2 => $amt2 );
802 }
803 $h2->add( $u2 => $amt2 );
804 }
805 return $h1, $h2;
806 }
807  
808 # fill in missing fields in two date hashes, each using the other as context
809 # this is a bit of a hack, but a natural hack
810 sub infer_missing {
811 my ( $h1, $h2 ) = @_;
812 if ( $h1->{type} eq $h2->{type} ) {
813 while ( my ( $k, $v ) = each %$h1 ) {
814 $h2->{$k} //= $v;
815 }
816 while ( my ( $k, $v ) = each %$h2 ) {
817 $h1->{$k} //= $v;
818 }
819 }
820 elsif ( $h2->{type} eq 'numeric' ) {
821 if ( $h1->{month} && !$h2->{month} ) {
822 init_month_abbr();
823 $h2->{month} = $month_abbr{ $h1->{month} };
824 }
825 }
826 else {
827  
828 # I don't think we have any problems in this case
829 }
830 }
831  
832 # normalizes string values
833 sub normalize {
834 my $h = shift;
835 delete $h->{debug};
836 if ( $h->{type} eq 'verbal' ) {
837 for my $key (qw(day month period)) {
838 if ( my $value = $h->{$key} ) {
839 next if $value =~ /\d/;
840 $value = lc $value;
841 if ( $value =~ /^p/ ) {
842 croak 'pay period not defined'
843 unless defined start_pay_period;
844 $h->{$key} = 'pay';
845 }
846 else {
847 $h->{$key} = substr $value, 0, 3;
848 }
849 }
850 }
851 for ( $h->{modifier} || '' ) {
852 when (/beg/) { $h->{modifier} = 'beginning' }
853 when (/end/) { $h->{modifier} = 'end' }
854 when (/las/) { $h->{modifier} = 'last' }
855 when (/thi/) { $h->{modifier} = 'this' }
856 when (/nex/) { $h->{modifier} = 'next' }
857 }
858 }
859 }
860  
861 # whether the particular date expression refers to a fixed
862 # rather than relative date
863 sub is_fixed {
864 my $h = shift;
865 return 1
866 if exists $h->{year};
867 if ( $h->{type} eq 'verbal' ) {
868 if ( exists $h->{modifier} ) {
869 return 1 if $h->{modifier} =~ /this|last|next/;
870 }
871 if ( exists $h->{day} ) {
872 return 1 if $h->{day} =~ /yes|tod|tom/;
873 }
874 }
875 return 0;
876 }
877  
878 1;
879  
880 __END__