From bdd7f9df981a44df7efab7d96976dcc8f74bc3ad Mon Sep 17 00:00:00 2001 From: Ben Moon Date: Sun, 27 Mar 2016 10:04:32 +0100 Subject: [PATCH 1/4] Move main regex out of handle --- lib/DDG/Goodie/DateMath.pm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/DDG/Goodie/DateMath.pm b/lib/DDG/Goodie/DateMath.pm index 024ef3e04..64d044802 100755 --- a/lib/DDG/Goodie/DateMath.pm +++ b/lib/DDG/Goodie/DateMath.pm @@ -124,10 +124,12 @@ sub get_result_action { return build_result($result, $formatted_input); } +my $full_date_regex = qr/^((what ((is|was|will) the )?)?(?date|time|day)( (was it|will it be|is it|be))? )?($operation_re|$from_re|$ago_re)[\?.]?$/i; + handle query_lc => sub { my $query = $_; - return unless $query =~ /^((what ((is|was|will) the )?)?(?date|time|day)( (was it|will it be|is it|be))? )?($operation_re|$from_re|$ago_re)[\?.]?$/i; + return unless $query =~ $full_date_regex; my $action = $+{action}; my $date = $+{date}; From 363ca5862db380f869c69a209ca1c21cfe0e86a5 Mon Sep 17 00:00:00 2001 From: Ben Moon Date: Sun, 27 Mar 2016 10:10:43 +0100 Subject: [PATCH 2/4] Split main regex into parts You know what would handle this *really* well? WhatIs. --- lib/DDG/Goodie/DateMath.pm | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/DDG/Goodie/DateMath.pm b/lib/DDG/Goodie/DateMath.pm index 64d044802..53de4d3d8 100755 --- a/lib/DDG/Goodie/DateMath.pm +++ b/lib/DDG/Goodie/DateMath.pm @@ -124,7 +124,13 @@ sub get_result_action { return build_result($result, $formatted_input); } -my $full_date_regex = qr/^((what ((is|was|will) the )?)?(?date|time|day)( (was it|will it be|is it|be))? )?($operation_re|$from_re|$ago_re)[\?.]?$/i; +my $what_re = qr/what ((is|was|will) the )?/i; + +my $dort_re = qr/(?date|time|day)/i; + +my $will_re = qr/ (was it|will it be|is it|be)/i; + +my $full_date_regex = qr/^($what_re?$dort_re$will_re? )?($operation_re|$from_re|$ago_re)[\?.]?$/i; handle query_lc => sub { my $query = $_; From c50527c85c859eb4b9769fbb7b50273ec4a8755a Mon Sep 17 00:00:00 2001 From: Ben Moon Date: Sun, 27 Mar 2016 10:31:15 +0100 Subject: [PATCH 3/4] Allow after/before forms So you can do things like '3 days before today'. --- lib/DDG/Goodie/DateMath.pm | 10 +++++----- t/DateMath.t | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/DDG/Goodie/DateMath.pm b/lib/DDG/Goodie/DateMath.pm index 53de4d3d8..992c26a46 100755 --- a/lib/DDG/Goodie/DateMath.pm +++ b/lib/DDG/Goodie/DateMath.pm @@ -10,7 +10,7 @@ use Lingua::EN::Numericalize; triggers any => qw(second minute hour day week month year); triggers any => qw(seconds minutes hours days weeks months years); -triggers any => qw(plus minus + -); +triggers any => qw(plus minus + - before after); triggers any => qw(date time); zci is_cached => 0; @@ -27,8 +27,8 @@ sub get_duration { sub get_action_for { my $action = shift; - return '+' if $action =~ /^(\+|plus|from|in|add)$/i; - return '-' if $action =~ /^(\-|minus|ago|subtract)$/i; + return '+' if $action =~ /^(\+|plus|from|in|add|after)$/i; + return '-' if $action =~ /^(\-|minus|ago|subtract|before)$/i; } sub is_clock_unit { @@ -68,8 +68,8 @@ my $action_re = qr/(?plus|add|\+|\-|minus|subtract)/i; my $date_re = qr/(?$datestring_regex)/i; my $operation_re = qr/$date_re(?:\s+$action_re\s+$relative_regex)?/i; -my $from_re = qr/$relative_regex\s+(?from)\s+$date_re?|(?in)\s+$relative_regex/i; -my $ago_re = qr/$relative_regex\s+(?ago)/i; +my $from_re = qr/$relative_regex\s+(?from|after)\s+$date_re?|(?in)\s+$relative_regex/i; +my $ago_re = qr/$relative_regex\s+(?ago)|$relative_regex\s+(?before)\s+$date_re?/i; my $time_24h = time_24h_regex(); my $time_12h = time_12h_regex(); my $relative_dates = relative_dates_regex(); diff --git a/t/DateMath.t b/t/DateMath.t index 061556d84..32c4f568f 100755 --- a/t/DateMath.t +++ b/t/DateMath.t @@ -109,6 +109,10 @@ location_test([ qw( DDG::Goodie::DateMath ) ], 'What date was it 3 days ago' => build_test('09 Jan 2014', '3 days ago'), 'What date was it 3 days ago?' => build_test('09 Jan 2014', '3 days ago'), 'What day was it 3 days ago?' => build_test('09 Jan 2014', '3 days ago'), + # After/before + '3 days before 21st March' => build_test('18 Mar 2014', '21 Mar 2014 - 3 days'), + '3 days after 21st March' => build_test('24 Mar 2014', '21 Mar 2014 + 3 days'), + 'two hours before now' => build_test('12 Jan 2014 13:30:00 IST', '12 Jan 2014 15:30:00 IST - 2 hours'), # Specified relative 'date 21st Jan' => undef, 'date January 1st' => undef, From 3918f70fa0e0096e600057c190b2b91330829c0c Mon Sep 17 00:00:00 2001 From: Ben Moon Date: Sun, 27 Mar 2016 22:31:12 +0100 Subject: [PATCH 4/4] Rename dort to day_or_time --- lib/DDG/Goodie/DateMath.pm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/DDG/Goodie/DateMath.pm b/lib/DDG/Goodie/DateMath.pm index 992c26a46..c9304aeca 100755 --- a/lib/DDG/Goodie/DateMath.pm +++ b/lib/DDG/Goodie/DateMath.pm @@ -126,11 +126,11 @@ sub get_result_action { my $what_re = qr/what ((is|was|will) the )?/i; -my $dort_re = qr/(?date|time|day)/i; +my $day_or_time_re = qr/(?date|time|day)/i; my $will_re = qr/ (was it|will it be|is it|be)/i; -my $full_date_regex = qr/^($what_re?$dort_re$will_re? )?($operation_re|$from_re|$ago_re)[\?.]?$/i; +my $full_date_regex = qr/^($what_re?$day_or_time_re$will_re? )?($operation_re|$from_re|$ago_re)[\?.]?$/i; handle query_lc => sub { my $query = $_; @@ -141,10 +141,10 @@ handle query_lc => sub { my $date = $+{date}; my $number = $+{number}; my $unit = $+{unit}; - my $dort = $+{dort}; + my $day_or_time = $+{day_or_time}; my $specified_time = $query =~ /$time_24h|$time_12h/; - my $use_clock = $specified_time || should_use_clock $unit, $dort; + my $use_clock = $specified_time || should_use_clock $unit, $day_or_time; return get_result_relative($date, $use_clock) unless defined $number; return get_result_action $action, $date, $number, $unit, $use_clock;