File Coverage

blib/lib/DateTime/Format/Flexible/lang/en.pm
Criterion Covered Total %
statement 33 37 89.1
branch 2 2 100.0
condition 6 6 100.0
subroutine 20 22 90.9
pod 11 11 100.0
total 72 78 92.3


line stmt bran cond sub pod time code
1             package DateTime::Format::Flexible::lang::en;
2              
3 17     17   14024 use strict;
  17         130  
  17         546  
4 17     17   130 use warnings;
  17         57  
  17         24132  
5              
6             sub new
7             {
8 0     0 1 0 my ( $class , %params ) = @_;
9 0         0 my $self = bless \%params , $class;
10 0         0 return $self;
11             }
12              
13             sub months
14             {
15             return (
16 7664     7664 1 86037 qr{Jan(?:uary)?}i => 1,
17             qr{Feb(?:ruary)?}i => 2,
18             qr{Mar(?:ch)?}i => 3,
19             qr{Apr(?:il)?}i => 4,
20             qr{May}i => 5,
21             qr{Jun(?:e)?}i => 6,
22             qr{Jul(?:y)?}i => 7,
23             qr{Aug(?:ust)?}i => 8,
24             qr{Sep(?:t)?(?:ember)?}i => 9,
25             qr{Oct(?:ober)?}i => 10,
26             qr{Nov(?:ember)?}i => 11,
27             qr{Dec(?:ember)?}i => 12,
28             );
29             }
30              
31             sub days
32             {
33             return (
34 7645     7645 1 60115 qr{\bMon(?:day)?\b}i => 1,
35             qr{\bTue(?:sday)?\b}i => 2,
36             qr{\bWed(?:nesday)?\b}i => 3,
37             qr{\bThu(?:rsday)?\b}i => 4,
38             qr{\bFri(?:day)?\b}i => 5,
39             qr{\bSat(?:urday)?\b}i => 6,
40             qr{\bSun(?:day)?\b}i => 7,
41             );
42             }
43              
44             sub day_numbers
45             {
46             return (
47 3808     3808 1 118899 qr{first} => 1,
48             qr{second} => 2,
49             qr{third} => 3,
50             qr{fourth} => 4,
51             qr{fifth} => 5,
52             qr{sixth} => 6,
53             qr{seventh} => 7,
54             qr{eighth} => 8,
55             qr{ninth} => 9,
56             qr{tenth} => 10,
57             qr{eleventh} => 11,
58             qr{twelfth} => 12,
59             qr{thirteenth} => 13,
60             qr{fourteenth} => 14,
61             qr{fifteenth} => 15,
62             qr{sixteenth} => 16,
63             qr{seventeenth} => 17,
64             qr{eithteenth} => 18,
65             qr{ninteenth} => 19,
66             qr{twentieth} => 20,
67             qr{twenty\s?first} => 21,
68             qr{twenty\s?second} => 22,
69             qr{twenty\s?third} => 23,
70             qr{twenty\s?fourth} => 24,
71             qr{twenty\s?fifth} => 25,
72             qr{twenty\s?sixth} => 26,
73             qr{twenty\s?seventh} => 27,
74             qr{twenty\s?eighth} => 28,
75             qr{twenty\s?ninth} => 29,
76             qr{thirtieth} => 30,
77             qr{thirty\s?first} => 31,
78             );
79             }
80              
81             sub hours
82             {
83             return (
84 3808     3808 1 37272 noon => '12:00:00',
85             midnight => '00:00:00',
86             one => '01:00:00',
87             two => '02:00:00',
88             three => '03:00:00',
89             four => '04:00:00',
90             five => '05:00:00',
91             six => '06:00:00',
92             seven => '07:00:00',
93             eight => '08:00:00',
94             nine => '09:00:00',
95             ten => '10:00:00',
96             eleven => '11:00:00',
97             twelve => '12:00:00',
98             );
99             }
100              
101             sub remove_strings
102             {
103             return (
104             # remove ' of ' as in '16th of November 2003'
105 3808     3808 1 16948 qr{\bof\b}i,
106             # remove number extensions. 1st, etc
107             # these must be followed by a digit, which
108             # is not captured.
109             qr{(?<=\d)(?:st|nd|rd|th)\b,?}i,
110             # next sunday
111             qr{\bnext\b}i,
112             );
113             }
114              
115             sub parse_time
116             {
117 3808     3808 1 8198 my ( $self, $date ) = @_;
118              
119 3808 100       14250 return $date if ( not $date =~ m{\s?at\s?}mx );
120              
121 60         458 my ( $pre, $time, $post ) = $date =~ m{\A(.+)?\s?at\s?([\d\.:]+)(.+)?\z}mx;
122              
123             # this will remove warnings if we don't have values for some of the variables
124             # eg: not a date matches on the 'at' in date
125 60   100     212 $pre ||= q{};
126 60   100     152 $time ||= q{};
127 60   100     248 $post ||= q{};
128              
129             # if there is an 'at' string, we want to remove any time that was set on the date by default
130             # 20050612T12:13:14 <-- T12:13:14
131 60         137 $pre =~ s{T[^\s]+}{};
132              
133 60         204 $date = $pre . 'T' . $time . 'T' . $post;
134 60         159 return $date;
135             }
136              
137             sub string_dates
138             {
139 3808     3808 1 10883 my $base_dt = DateTime::Format::Flexible->base;
140              
141             return (
142 7     7   30 now => sub { return $base_dt->datetime } ,
143 19     19   70 today => sub { return $base_dt->clone->truncate( to => 'day' )->ymd } ,
144 4     4   18 tomorrow => sub { return $base_dt->clone->truncate( to => 'day' )->add( days => 1 )->ymd },
145 2     2   8 yesterday => sub { return $base_dt->clone->truncate( to => 'day' )->subtract( days => 1 )->ymd },
146 2     2   10 overmorrow => sub { return $base_dt->clone->truncate( to => 'day' )->add( days => 2 )->ymd },
147 1     1   8 allballs => sub { return $base_dt->clone->truncate( to => 'day' ) },
148              
149 1     1   7 epoch => sub { return DateTime->from_epoch( epoch => 0 ) },
150 0     0   0 '-infinity' => sub { '-infinity' },
151 4     4   12 infinity => sub { 'infinity' },
152 3808         987273 );
153             }
154              
155             sub relative
156             {
157             return (
158             # as in 3 years ago, -3 years
159 3808     3808 1 26380 ago => qr{\bago\b|\A\-}i,
160             # as in 3 years from now, +3 years
161             from => qr{\bfrom\b\s\bnow\b|\A\+}i,
162             # as in next Monday
163             next => qr{\bnext\b}i,
164             # as in last Monday
165             last => qr{\blast\b}i,
166             );
167             }
168              
169             sub math_strings
170             {
171             return (
172 19     19 1 162 year => 'years' ,
173             years => 'years' ,
174             month => 'months' ,
175             months => 'months' ,
176             day => 'days' ,
177             days => 'days' ,
178             hour => 'hours' ,
179             hours => 'hours' ,
180             minute => 'minutes' ,
181             minutes => 'minutes' ,
182             week => 'weeks',
183             weeks => 'weeks',
184             );
185             }
186              
187             sub timezone_map
188             {
189             # http://home.tiscali.nl/~t876506/TZworld.html
190             return (
191 3808     3808 1 34344 EST => 'America/New_York',
192             EDT => 'America/New_York',
193             CST => 'America/Chicago',
194             CDT => 'America/Chicago',
195             MST => 'America/Denver',
196             MDT => 'America/Denver',
197             PST => 'America/Los_Angeles',
198             PDT => 'America/Los_Angeles',
199             AKST => 'America/Juneau',
200             AKDT => 'America/Juneau',
201             HAST => 'America/Adak',
202             HADT => 'America/Adak',
203             HST => 'Pacific/Honolulu',
204             );
205             }
206              
207             1;
208             __END__
209              
210             =encoding utf-8
211              
212             =head1 NAME
213              
214             DateTime::Format::Flexible::lang::en - the english language plugin
215              
216             =head1 DESCRIPTION
217              
218             You should not need to use this module directly.
219              
220             If you only want to use one language, specify the lang property when parsing a date.
221              
222             example:
223              
224             my $dt = DateTime::Format::Flexible->parse_datetime(
225             'Wed, Jun 10, 2009' ,
226             lang => ['en']
227             );
228             # $dt is now 2009-06-10T00:00:00
229              
230             Note that this is not required, by default ALL languages are scanned when trying to parse a date.
231              
232             =head2 new
233              
234             Instantiate a new instance of this module.
235              
236             =head2 months
237              
238             month name regular expressions along with the month numbers (Jan(?:uary)? => 1)
239              
240             =head2 days
241              
242             day name regular expressions along the the day numbers (Mon(?:day)? => 1)
243              
244             =head2 day_numbers
245              
246             maps day of month names to the corresponding numbers (first => 01)
247              
248             =head2 hours
249              
250             maps hour names to numbers (noon => 12:00:00)
251              
252             =head2 remove_strings
253              
254             strings to remove from the date (rd as in 3rd)
255              
256             =head2 parse_time
257              
258             searches for the string 'at' to help determine a time substring (sunday at 3:00)
259              
260             =head2 string_dates
261              
262             maps string names to real dates (now => DateTime->now)
263              
264             =head2 relative
265              
266             parse relative dates (ago => ago, from => from now, next => next, last => last)
267              
268             =head2 math_strings
269              
270             useful strings when doing datetime math
271              
272             =head2 timezone_map
273              
274             maps unofficial timezones to official timezones for this language (CST => America/Chicago)
275              
276              
277             =head1 AUTHOR
278              
279             Tom Heady
280             CPAN ID: thinc
281             Punch, Inc.
282             cpan@punch.net
283             http://www.punch.net/
284              
285             =head1 COPYRIGHT & LICENSE
286              
287             Copyright 2011 Tom Heady.
288              
289             This program is free software; you can redistribute it and/or
290             modify it under the terms of either:
291              
292             =over 4
293              
294             =item * the GNU General Public License as published by the Free
295             Software Foundation; either version 1, or (at your option) any
296             later version, or
297              
298             =item * the Artistic License.
299              
300             =back
301              
302             =head1 SEE ALSO
303              
304             F<DateTime::Format::Flexible>
305              
306             =cut