File Coverage

blib/lib/DateTime/Format/Alami/EN.pm
Criterion Covered Total %
statement 24 56 42.8
branch n/a
condition n/a
subroutine 15 47 31.9
pod 0 40 0.0
total 39 143 27.2


line stmt bran cond sub pod time code
1             package DateTime::Format::Alami::EN;
2              
3             our $DATE = '2016-06-30'; # DATE
4             our $VERSION = '0.13'; # VERSION
5              
6 2     2   533 use 5.014000;
  2         5  
7 2     2   6 use strict;
  2         2  
  2         31  
8 2     2   5 use warnings;
  2         1  
  2         47  
9              
10 2     2   775 use Parse::Number::EN qw(parse_number_en);
  2         492  
  2         975  
11              
12 2     2 0 6 sub o_num { $Parse::Number::EN::Pat }
13 9     9   21 sub _parse_num { parse_number_en(text => $_[1]) }
14 4     4 0 11 sub w_year { ["year", "years", "y"] }
15 4     4 0 10 sub w_month { ["month", "months", "mon"] }
16 4     4 0 11 sub w_week { ["week", "weeks", "wk", "wks"] }
17 4     4 0 11 sub w_day { ["day", "days", "d"] }
18 4     4 0 12 sub w_hour { ["hour", "hours", "h"] }
19 4     4 0 11 sub w_minute { ["minute", "minutes", "min", "mins"] }
20 4     4 0 20 sub w_second { ["second", "seconds", "sec", "secs", "s"] }
21              
22 0     0 0   sub w_jan { ["january", "jan"] }
23 0     0 0   sub w_feb { ["february", "feb"] }
24 0     0 0   sub w_mar { ["march", "mar"] }
25 0     0 0   sub w_apr { ["april", "apr"] }
26 0     0 0   sub w_may { ["may"] }
27 0     0 0   sub w_jun { ["june", "jun"] }
28 0     0 0   sub w_jul { ["july", "jul"] }
29 0     0 0   sub w_aug { ["august", "aug"] }
30 0     0 0   sub w_sep { ["september", "sept", "sep"] }
31 0     0 0   sub w_oct { ["october", "oct"] }
32 0     0 0   sub w_nov { ["november", "nov"] }
33 0     0 0   sub w_dec { ["december", "dec"] }
34              
35 0     0 0   sub w_monday { ["monday", "mon"] }
36 0     0 0   sub w_tuesday { ["tuesday", "tue"] }
37 0     0 0   sub w_wednesday { ["wednesday", "wed"] }
38 0     0 0   sub w_thursday { ["thursday", "thu"] }
39 0     0 0   sub w_friday { ["friday", "fri"] }
40 0     0 0   sub w_saturday { ["saturday", "sat"] }
41 0     0 0   sub w_sunday { ["sunday", "sun"] }
42              
43 0     0 0   sub p_now { "(?:(?:(?:right|just) \\s+ )?now|immediately)" }
44 0     0 0   sub p_today { "(?:today|this \\s+ day)" }
45 0     0 0   sub p_tomorrow { "(?:tomorrow|tom)" }
46 0     0 0   sub p_yesterday { "(?:yesterday|yest)" }
47              
48 0     0 0   sub o_cardinal_suffix { '(?:\s*(?:th|nd|st))' }
49              
50 0     0 0   sub p_dateymd { join(
51             # we use the 'local' trick here in embedded code (see perlre) to be
52             # backtrack-safe. we want to unset $m->{o_yearint} when date does not
53             # contain year. $m->{o_yearint} might be set when we try the patterns but
54             # might end up needing to be unset if the matching pattern ends up not
55             # having year.
56             "",
57             '(?{ $DateTime::Format::Alami::_has_year = 0 })',
58             '(?: ? (?:\\s*|[ /-]) | (?:\\s*|[ /-]) ?\\b | [/-]\\b )',
59             '(?: \\s*[,/-]?\\s* (?{ local $DateTime::Format::Alami::_has_year = $DateTime::Format::Alami::_has_year + 1 }))?',
60             '(?{ delete $DateTime::Format::Alami::m->{o_yearint} unless $DateTime::Format::Alami::_has_year })',
61             )}
62              
63 0     0 0   sub p_dur_ago { " \\s+ (?:ago)" }
64 0     0 0   sub p_dur_later { " \\s+ (?:later) | in \\s+ " }
65              
66 0     0 0   sub p_which_dow { join(
67             "",
68             '(?{ $DateTime::Format::Alami::_offset = 0 })',
69             "(?:",
70             ' (?: (?:last \s+)(?{ local $DateTime::Format::Alami::_offset = -1 }) | (?:next \s+)(?{ local $DateTime::Format::Alami::_offset = 1 }) | (?:this \s+)?)',
71             ' ',
72             ")",
73             '(?{ $DateTime::Format::Alami::m->{offset} = $DateTime::Format::Alami::_offset })',
74             )}
75              
76 0     0 0   sub o_date { "(?: ||||)" }
77 0     0 0   sub o_ampm { "(?: am|pm)" }
78 0     0 0   sub p_time { "(?: [:.](?: [:.])? \\s* ?)" } # XXX am/pm
79 0     0 0   sub p_date_time { "(?: \\s+ (?:(?:on|at) \\s+)? )" }
80              
81             # the ordering is a bit weird because: we need to apply role at compile-time
82             # before the precomputed $RE mentions $o & $m thus creating the package
83             # DateTime::Format::Alami and this makes Role::Tiny::With complains that DT:F:A
84             # is not a role. then, if we are to apply the role, we need to already declare
85             # the methods required by the role.
86              
87 2     2   700 use Role::Tiny::With;
  2         6596  
  2         103  
88 2     2   8 BEGIN { with 'DateTime::Format::Alami' };
89              
90             our $RE_DT = qr((?&top)(?(DEFINE)(?(?&p_dur_later)|(?&p_dur_ago)|(?&p_date_time)|(?&p_time)|(?&p_which_dow)|(?&p_today)|(?&p_tomorrow)|(?&p_yesterday)|(?&p_dateymd)|(?&p_now))(? (\b (?&o_dur) \s+ (?:later) | in \s+ (?&o_dur) \b)(?{ $DateTime::Format::Alami::m->{p_dur_later} = $^N })(?{ $DateTime::Format::Alami::o->{_pat} = "p_dur_later"; $DateTime::Format::Alami::o->a_dur_later($DateTime::Format::Alami::m) }))(? (\b (?&o_dur) \s+ (?:ago) \b)(?{ $DateTime::Format::Alami::m->{p_dur_ago} = $^N })(?{ $DateTime::Format::Alami::o->{_pat} = "p_dur_ago"; $DateTime::Format::Alami::o->a_dur_ago($DateTime::Format::Alami::m) }))(? (\b (?:(?&o_date) \s+ (?:(?:on|at) \s+)? (?&p_time)) \b)(?{ $DateTime::Format::Alami::m->{p_date_time} = $^N })(?{ $DateTime::Format::Alami::o->{_pat} = "p_date_time"; $DateTime::Format::Alami::o->a_date_time($DateTime::Format::Alami::m) }))(? ((?:((?:[+-]?(?:(?:\d{1,3}(?:[,]\d{3})+|\d+)(?:[.]\d*)?|[.]\d+)(?:[Ee][+-]?\d+)?)\s*(?:year|years|y|month|months|mon|week|weeks|wk|wks|day|days|d|hour|hours|h|minute|minutes|min|mins|second|seconds|sec|secs|s)\s*(?:,\s*)?)+))(?{ $DateTime::Format::Alami::m->{o_dur} = $^N }))(? ((?: (?&p_which_dow)|(?&p_today)|(?&p_tomorrow)|(?&p_yesterday)|(?&p_dateymd)))(?{ $DateTime::Format::Alami::m->{o_date} = $^N }))(? (\b (?: (?&o_hour)[:.](?&o_minute)(?: [:.](?&o_second))? \s* (?&o_ampm)?) \b)(?{ $DateTime::Format::Alami::m->{p_time} = $^N })(?{ $DateTime::Format::Alami::o->{_pat} = "p_time"; $DateTime::Format::Alami::o->a_time($DateTime::Format::Alami::m) }))(? (\b (?{ $DateTime::Format::Alami::_offset = 0 })(?: (?: (?:last \s+)(?{ local $DateTime::Format::Alami::_offset = -1 }) | (?:next \s+)(?{ local $DateTime::Format::Alami::_offset = 1 }) | (?:this \s+)?) (?&o_dow))(?{ $DateTime::Format::Alami::m->{offset} = $DateTime::Format::Alami::_offset }) \b)(?{ $DateTime::Format::Alami::m->{p_which_dow} = $^N })(?{ $DateTime::Format::Alami::o->{_pat} = "p_which_dow"; $DateTime::Format::Alami::o->a_which_dow($DateTime::Format::Alami::m) }))(? (\b (?:today|this \s+ day) \b)(?{ $DateTime::Format::Alami::m->{p_today} = $^N })(?{ $DateTime::Format::Alami::o->{_pat} = "p_today"; $DateTime::Format::Alami::o->a_today($DateTime::Format::Alami::m) }))(? (\b (?:tomorrow|tom) \b)(?{ $DateTime::Format::Alami::m->{p_tomorrow} = $^N })(?{ $DateTime::Format::Alami::o->{_pat} = "p_tomorrow"; $DateTime::Format::Alami::o->a_tomorrow($DateTime::Format::Alami::m) }))(? (\b (?:yesterday|yest) \b)(?{ $DateTime::Format::Alami::m->{p_yesterday} = $^N })(?{ $DateTime::Format::Alami::o->{_pat} = "p_yesterday"; $DateTime::Format::Alami::o->a_yesterday($DateTime::Format::Alami::m) }))(? (\b (?{ $DateTime::Format::Alami::_has_year = 0 })(?: (?&o_dayint)(?&o_cardinal_suffix)? (?:\s*|[ /-]) (?&o_monthname) | (?&o_monthname) (?:\s*|[ /-]) (?&o_dayint)(?&o_cardinal_suffix)?\b | (?&o_monthint)[/-](?&o_dayint)\b )(?: \s*[,/-]?\s* (?&o_yearint) (?{ local $DateTime::Format::Alami::_has_year = $DateTime::Format::Alami::_has_year + 1 }))?(?{ delete $DateTime::Format::Alami::m->{o_yearint} unless $DateTime::Format::Alami::_has_year }) \b)(?{ $DateTime::Format::Alami::m->{p_dateymd} = $^N })(?{ $DateTime::Format::Alami::o->{_pat} = "p_dateymd"; $DateTime::Format::Alami::o->a_dateymd($DateTime::Format::Alami::m) }))(? ((?:[0-9][0-9]?))(?{ $DateTime::Format::Alami::m->{o_hour} = $^N }))(? ((?:[0-9][0-9]?))(?{ $DateTime::Format::Alami::m->{o_minute} = $^N }))(? ((?:[0-9][0-9]?))(?{ $DateTime::Format::Alami::m->{o_second} = $^N }))(? ((?: am|pm))(?{ $DateTime::Format::Alami::m->{o_ampm} = $^N }))(? ((?:monday|mon|tuesday|tue|wednesday|wed|thursday|thu|friday|fri|saturday|sat|sunday|sun))(?{ $DateTime::Format::Alami::m->{o_dow} = $^N }))(? ((?:january|jan|february|feb|march|mar|april|apr|may|june|jun|july|jul|august|aug|september|sept|sep|october|oct|november|nov|december|dec))(?{ $DateTime::Format::Alami::m->{o_monthname} = $^N }))(? ((?:\s*(?:th|nd|st)))(?{ $DateTime::Format::Alami::m->{o_cardinal_suffix} = $^N }))(? ((?:0?[1-9]|1[012]))(?{ $DateTime::Format::Alami::m->{o_monthint} = $^N }))(? ((?:[12][0-9]|3[01]|0?[1-9]))(?{ $DateTime::Format::Alami::m->{o_dayint} = $^N }))(? ((?:[0-9]{4}|[0-9]{2}))(?{ $DateTime::Format::Alami::m->{o_yearint} = $^N }))(? ((?:((?:[+-]?(?:(?:\d{1,3}(?:[,]\d{3})+|\d+)(?:[.]\d*)?|[.]\d+)(?:[Ee][+-]?\d+)?)\s*(?:hour|hours|h|minute|minutes|min|mins|second|seconds|sec|secs|s)\s*(?:,\s*)?)+))(?{ $DateTime::Format::Alami::m->{o_timedur} = $^N }))(? (\b (?:(?:(?:right|just) \s+ )?now|immediately) \b)(?{ $DateTime::Format::Alami::m->{p_now} = $^N })(?{ $DateTime::Format::Alami::o->{_pat} = "p_now"; $DateTime::Format::Alami::o->a_now($DateTime::Format::Alami::m) }))(? ((?:year|years|y|month|months|mon|week|weeks|wk|wks|day|days|d|hour|hours|h|minute|minutes|min|mins|second|seconds|sec|secs|s))(?{ $DateTime::Format::Alami::m->{o_durwords} = $^N }))(? ((?:[+-]?(?:(?:\d{1,3}(?:[,]\d{3})+|\d+)(?:[.]\d*)?|[.]\d+)(?:[Ee][+-]?\d+)?))(?{ $DateTime::Format::Alami::m->{o_num} = $^N }))(? ((?:hour|hours|h|minute|minutes|min|mins|second|seconds|sec|secs|s))(?{ $DateTime::Format::Alami::m->{o_timedurwords} = $^N }))))ix; # PRECOMPUTED FROM: do { DateTime::Format::Alami::EN->new; $DateTime::Format::Alami::EN::RE_DT }
91             our $RE_DUR = qr((?&top)(?(DEFINE)(?(?&pdur_dur))(? (\b (?:(?&odur_dur)) \b)(?{ $DateTime::Format::Alami::m->{pdur_dur} = $^N })(?{ $DateTime::Format::Alami::o->{_pat} = "pdur_dur"; $DateTime::Format::Alami::o->adur_dur($DateTime::Format::Alami::m) }))(? ((?:((?:[+-]?(?:(?:\d{1,3}(?:[,]\d{3})+|\d+)(?:[.]\d*)?|[.]\d+)(?:[Ee][+-]?\d+)?)\s*(?:year|years|y|month|months|mon|week|weeks|wk|wks|day|days|d|hour|hours|h|minute|minutes|min|mins|second|seconds|sec|secs|s)\s*(?:,\s*)?)+))(?{ $DateTime::Format::Alami::m->{odur_dur} = $^N }))))ix; # PRECOMPUTED FROM: do { DateTime::Format::Alami::EN->new; $DateTime::Format::Alami::EN::RE_DUR }
92             our $MAPS = {dow=>{fri=>5,friday=>5,mon=>1,monday=>1,sat=>6,saturday=>6,sun=>7,sunday=>7,thu=>4,thursday=>4,tue=>2,tuesday=>2,wed=>3,wednesday=>3},months=>{apr=>4,april=>4,aug=>8,august=>8,dec=>12,december=>12,feb=>2,february=>2,jan=>1,january=>1,jul=>7,july=>7,jun=>6,june=>6,mar=>3,march=>3,may=>5,nov=>11,november=>11,oct=>10,october=>10,sep=>9,sept=>9,september=>9}}; # PRECOMPUTED FROM: do { DateTime::Format::Alami::EN->new; $DateTime::Format::Alami::EN::MAPS }
93              
94             1;
95             # ABSTRACT: Parse human date/time/duration expression (English)
96              
97             __END__