Subject: | [PATCH] support "january 2007", "feb 1992", etc |
This patch adds support for phrases like "january 2007", "feb 1992",
"decem 2012", etc. The patch depends on the patch attached to
rt.cpan.org #25001
Subject: | month-year.txt |
=== t/month-year.t
==================================================================
--- t/month-year.t (revision 2038)
+++ t/month-year.t (patch - level 1)
@@ -0,0 +1,110 @@
+use strict;
+use warnings;
+use Test::More;
+use Time::Local;
+use Date::PeriodParser;
+use POSIX qw( strftime );
+require 't/helpers.pl';
+
+# Tests for "january 2007", etc
+
+my %phrases = (
+ 'january 2007' => [
+ [
+ '2007-03-01T09:23:07', # base
+ '2007-01-01T00:00:00', # expected from
+ '2007-01-31T23:59:59', # expected to
+ ],
+ [
+ '2007-01-01T10:07:22', # base
+ '2007-01-01T00:00:00', # expected from
+ '2007-01-31T23:59:59', # expected to
+ ],
+ [
+ '2006-11-23T10:47:58', # base
+ '2007-01-01T00:00:00', # expected from
+ '2007-01-31T23:59:59', # expected to
+ ],
+ ],
+ 'jan 2007' => [
+ [
+ '2007-03-01T09:23:07', # base
+ '2007-01-01T00:00:00', # expected from
+ '2007-01-31T23:59:59', # expected to
+ ],
+ [
+ '2007-01-01T10:07:22', # base
+ '2007-01-01T00:00:00', # expected from
+ '2007-01-31T23:59:59', # expected to
+ ],
+ [
+ '2006-11-23T10:47:58', # base
+ '2007-01-01T00:00:00', # expected from
+ '2007-01-31T23:59:59', # expected to
+ ],
+ ],
+ 'february 1993' => [
+ [
+ '2007-03-01T09:23:07', # base
+ '1993-02-01T00:00:00', # expected from
+ '1993-02-28T23:59:59', # expected to
+ ],
+ [
+ '2007-01-01T10:07:22', # base
+ '1993-02-01T00:00:00', # expected from
+ '1993-02-28T23:59:59', # expected to
+ ],
+ [
+ '2006-11-23T10:47:58', # base
+ '1993-02-01T00:00:00', # expected from
+ '1993-02-28T23:59:59', # expected to
+ ],
+ ],
+ 'feb 1993' => [
+ [
+ '2007-03-01T09:23:07', # base
+ '1993-02-01T00:00:00', # expected from
+ '1993-02-28T23:59:59', # expected to
+ ],
+ [
+ '2007-01-01T10:07:22', # base
+ '1993-02-01T00:00:00', # expected from
+ '1993-02-28T23:59:59', # expected to
+ ],
+ [
+ '2006-11-23T10:47:58', # base
+ '1993-02-01T00:00:00', # expected from
+ '1993-02-28T23:59:59', # expected to
+ ],
+ ],
+ 'febr 1993' => [
+ [
+ '2007-03-01T09:23:07', # base
+ '1993-02-01T00:00:00', # expected from
+ '1993-02-28T23:59:59', # expected to
+ ],
+ [
+ '2007-01-01T10:07:22', # base
+ '1993-02-01T00:00:00', # expected from
+ '1993-02-28T23:59:59', # expected to
+ ],
+ [
+ '2006-11-23T10:47:58', # base
+ '1993-02-01T00:00:00', # expected from
+ '1993-02-28T23:59:59', # expected to
+ ],
+ ],
+);
+
+plan tests => 2 * 15;
+
+while ( my ($phrase, $tests) = each %phrases ) {
+ for my $test (@$tests) {
+ my ($base, $right_from, $right_to) = @$test;
+ set_time($base);
+
+ my ( $from, $to ) = parse_period($phrase);
+ is( iso($from), $right_from, "$phrase 'from' ok" );
+ is( iso($to), $right_to, "$phrase 'to' ok" );
+ }
+}
=== MANIFEST
==================================================================
--- MANIFEST (revision 2038)
+++ MANIFEST (patch - level 1)
@@ -13,6 +13,7 @@
t/08now.t
t/09vague.t
t/helpers.pl
+t/month-year.t
t/pod.t
t/pod-coverage.t
t/this-week.t
=== PeriodParser.pm
==================================================================
--- PeriodParser.pm (revision 2038)
+++ PeriodParser.pm (patch - level 1)
@@ -10,6 +10,7 @@
Date_to_Time
Day_of_Week
Days_in_Month
+ Decode_Month
);
use constant GIBBERISH => -1;
@@ -100,6 +101,19 @@
return ( _timelocal(@first), _timelocal(@last) );
}
+ # "january 2007", "dec 1991", etc
+ if (m{\A (\w+) \s+ (\d{4}) \z}xms) {
+ my $month = $1;
+ my $year = $2;
+
+ if ( $month = Decode_Month($month) ) {
+ my @first = ( $year, $month, 1, 0, 0, 0 ); # first day at midnight
+ my $last_day_of_month = Days_in_Month( $year, $month );
+ my @last = ( $year, $month, $last_day_of_month, 23, 59, 59 );
+ return ( _timelocal(@first), _timelocal(@last) );
+ }
+ }
+
# Recent times
if (/(the day (before|after) )?(yesterday|today|tomorrow)/ ||
/^this (morning|afternoon|evening|lunchtime)/ ||
@@ -329,6 +343,10 @@
Monday and end of Sunday. "Last" means the week or month preceeding "this
week/month".
+=item * january 2007, dec 2005, jul 1982
+
+A month name followed by a four-digit year.
+
=item * "ago" and "from now"
Offsets in days and "a week" are accepted; you cannot cross a month