File Coverage

blib/lib/DateTime/Format/Alami/EN.pm
Criterion Covered Total %
statement 24 57 42.1
branch n/a
condition n/a
subroutine 15 48 31.2
pod 0 41 0.0
total 39 146 26.7


line stmt bran cond sub pod time code
1             package DateTime::Format::Alami::EN;
2              
3             our $DATE = '2017-04-25'; # DATE
4             our $VERSION = '0.14'; # VERSION
5              
6 2     2   938 use 5.014000;
  2         9  
7 2     2   13 use strict;
  2         4  
  2         53  
8 2     2   12 use warnings;
  2         4  
  2         114  
9              
10 2     2   1209 use Parse::Number::EN qw(parse_number_en);
  2         959  
  2         1721  
11              
12 2     2 0 9 sub o_num { $Parse::Number::EN::Pat }
13 9     9   58 sub _parse_num { parse_number_en(text => $_[1]) }
14 4     4 0 21 sub w_year { ["year", "years", "y"] }
15 4     4 0 20 sub w_month { ["month", "months", "mon"] }
16 4     4 0 20 sub w_week { ["week", "weeks", "wk", "wks"] }
17 4     4 0 19 sub w_day { ["day", "days", "d"] }
18 4     4 0 19 sub w_hour { ["hour", "hours", "h"] }
19 4     4 0 19 sub w_minute { ["minute", "minutes", "min", "mins"] }
20 4     4 0 32 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             '(?: <o_dayint><o_cardinal_suffix>? (?:\\s*|[ /-]) <o_monthname> | <o_monthname> (?:\\s*|[ /-]) <o_dayint><o_cardinal_suffix>?\\b | <o_monthint>[/-]<o_dayint>\\b )',
59             '(?: \\s*[,/-]?\\s* <o_yearint> (?{ 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_dateym { join(
64             "",
65             '(?: <o_monthname> )',
66             '(?:\s*[,/-]?\s* <o_year4int> | \s*\'<o_year2int>)',
67             )}
68              
69 0     0 0   sub p_dur_ago { "<o_dur> \\s+ (?:ago)" }
70 0     0 0   sub p_dur_later { "<o_dur> \\s+ (?:later) | in \\s+ <o_dur>" }
71              
72 0     0 0   sub p_which_dow { join(
73             "",
74             '(?{ $DateTime::Format::Alami::_offset = 0 })',
75             "(?:",
76             ' (?: (?:last \s+)(?{ local $DateTime::Format::Alami::_offset = -1 }) | (?:next \s+)(?{ local $DateTime::Format::Alami::_offset = 1 }) | (?:this \s+)?)',
77             ' <o_dow>',
78             ")",
79             '(?{ $DateTime::Format::Alami::m->{offset} = $DateTime::Format::Alami::_offset })',
80             )}
81              
82 0     0 0   sub o_date { "(?: <p_which_dow>|<p_today>|<p_tomorrow>|<p_yesterday>|<p_dateymd>)" }
83 0     0 0   sub o_ampm { "(?: am|pm)" }
84 0     0 0   sub p_time { "(?: <o_hour>[:.]<o_minute>(?: [:.]<o_second>)? \\s* <o_ampm>?)" } # XXX am/pm
85 0     0 0   sub p_date_time { "(?:<o_date> \\s+ (?:(?:on|at) \\s+)? <p_time>)" }
86              
87             # the ordering is a bit weird because: we need to apply role at compile-time
88             # before the precomputed $RE mentions $o & $m thus creating the package
89             # DateTime::Format::Alami and this makes Role::Tiny::With complains that DT:F:A
90             # is not a role. then, if we are to apply the role, we need to already declare
91             # the methods required by the role.
92              
93 2     2   565 use Role::Tiny::With;
  2         8241  
  2         137  
94 2     2   14 BEGIN { with 'DateTime::Format::Alami' };
95              
96             our $RE_DT = qr((?&top)(?(DEFINE)(?<top>(?&p_dateym)|(?&p_dur_ago)|(?&p_date_time)|(?&p_dur_later)|(?&p_time)|(?&p_which_dow)|(?&p_today)|(?&p_tomorrow)|(?&p_yesterday)|(?&p_dateymd)|(?&p_now))(?<p_dateym> (\b (?: (?&o_monthname) )(?:\s*[,/-]?\s* (?&o_year4int) | \s*'(?&o_year2int)) \b)(?{ $DateTime::Format::Alami::m->{p_dateym} = $^N })(?{ $DateTime::Format::Alami::o->{_pat} = "p_dateym"; $DateTime::Format::Alami::o->a_dateym($DateTime::Format::Alami::m) }))(?<p_dur_ago> (\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) }))(?<p_date_time> (\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) }))(?<p_dur_later> (\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) }))(?<o_year4int> ((?:[0-9]{4}))(?{ $DateTime::Format::Alami::m->{o_year4int} = $^N }))(?<o_year2int> ((?:[0-9]{2}))(?{ $DateTime::Format::Alami::m->{o_year2int} = $^N }))(?<o_date> ((?: (?&p_which_dow)|(?&p_today)|(?&p_tomorrow)|(?&p_yesterday)|(?&p_dateymd)))(?{ $DateTime::Format::Alami::m->{o_date} = $^N }))(?<p_time> (\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) }))(?<o_dur> ((?:((?:[+-]?(?:(?:\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> (\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) }))(?<p_today> (\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) }))(?<p_tomorrow> (\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) }))(?<p_yesterday> (\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) }))(?<p_dateymd> (\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) }))(?<o_hour> ((?:[0-9][0-9]?))(?{ $DateTime::Format::Alami::m->{o_hour} = $^N }))(?<o_minute> ((?:[0-9][0-9]?))(?{ $DateTime::Format::Alami::m->{o_minute} = $^N }))(?<o_second> ((?:[0-9][0-9]?))(?{ $DateTime::Format::Alami::m->{o_second} = $^N }))(?<o_ampm> ((?: am|pm))(?{ $DateTime::Format::Alami::m->{o_ampm} = $^N }))(?<o_dow> ((?:monday|mon|tuesday|tue|wednesday|wed|thursday|thu|friday|fri|saturday|sat|sunday|sun))(?{ $DateTime::Format::Alami::m->{o_dow} = $^N }))(?<o_monthname> ((?: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 }))(?<o_cardinal_suffix> ((?:\s*(?:th|nd|st)))(?{ $DateTime::Format::Alami::m->{o_cardinal_suffix} = $^N }))(?<o_monthint> ((?:0?[1-9]|1[012]))(?{ $DateTime::Format::Alami::m->{o_monthint} = $^N }))(?<o_dayint> ((?:[12][0-9]|3[01]|0?[1-9]))(?{ $DateTime::Format::Alami::m->{o_dayint} = $^N }))(?<o_yearint> ((?:[0-9]{4}|[0-9]{2}))(?{ $DateTime::Format::Alami::m->{o_yearint} = $^N }))(?<o_timedur> ((?:((?:[+-]?(?:(?:\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 }))(?<p_now> (\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) }))(?<o_durwords> ((?: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 }))(?<o_num> ((?:[+-]?(?:(?:\d{1,3}(?:[,]\d{3})+|\d+)(?:[.]\d*)?|[.]\d+)(?:[Ee][+-]?\d+)?))(?{ $DateTime::Format::Alami::m->{o_num} = $^N }))(?<o_timedurwords> ((?: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 }
97             our $RE_DUR = qr((?&top)(?(DEFINE)(?<top>(?&pdur_dur))(?<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) }))(?<odur_dur> ((?:((?:[+-]?(?:(?:\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 }
98             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 }
99              
100             1;
101             # ABSTRACT: Parse human date/time/duration expression (English)
102              
103             __END__
104              
105             =pod
106              
107             =encoding UTF-8
108              
109             =head1 NAME
110              
111             DateTime::Format::Alami::EN - Parse human date/time/duration expression (English)
112              
113             =head1 VERSION
114              
115             This document describes version 0.14 of DateTime::Format::Alami::EN (from Perl distribution DateTime-Format-Alami), released on 2017-04-25.
116              
117             =head1 DESCRIPTION
118              
119             List of known date/time expressions:
120              
121             # p_now
122             (just|right)? now
123              
124             # p_today
125             today|this day
126              
127             # p_tomorrow
128             tommorow
129              
130             # p_yesterday
131             yesterday
132              
133             # p_dur_ago, p_dur_later
134             1 year 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds (ago|later)
135              
136             # p_dateymd
137             may 28
138             5/28
139             28 may 2016
140             may 28, 2016
141             5/28/2016
142             5-28-16
143              
144             # p_dateym
145             apr 2017
146             may-2018
147             jun '17
148              
149             # p_which_dow
150             (this|last|next) monday
151              
152             # p_time
153             2pm
154             3.45 am
155             (on|at)? 15:00
156              
157             # p_date_time
158             june 25 2pm
159             2016-06-25 10:00:00
160              
161             List of known duration expressions:
162              
163             # pdur_dur
164             1 year 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds
165              
166             =for Pod::Coverage ^((adur|a|pdur|p|odur|o|w)_.+)$
167              
168             =head1 HOMEPAGE
169              
170             Please visit the project's homepage at L<https://metacpan.org/release/DateTime-Format-Alami>.
171              
172             =head1 SOURCE
173              
174             Source repository is at L<https://github.com/perlancar/perl-DateTime-Format-Alami>.
175              
176             =head1 BUGS
177              
178             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=DateTime-Format-Alami>
179              
180             When submitting a bug or request, please include a test-file or a
181             patch to an existing test-file that illustrates the bug or desired
182             feature.
183              
184             =head1 SEE ALSO
185              
186             L<DateTime::Format::Natural>
187              
188             =head1 AUTHOR
189              
190             perlancar <perlancar@cpan.org>
191              
192             =head1 COPYRIGHT AND LICENSE
193              
194             This software is copyright (c) 2017, 2016, 2014 by perlancar@cpan.org.
195              
196             This is free software; you can redistribute it and/or modify it under
197             the same terms as the Perl 5 programming language system itself.
198              
199             =cut