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