diff --git a/lib/DDG/Goodie/DateMath.pm b/lib/DDG/Goodie/DateMath.pm index 024ef3e04..c9304aeca 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(); @@ -124,19 +124,27 @@ sub get_result_action { return build_result($result, $formatted_input); } +my $what_re = qr/what ((is|was|will) the )?/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?$day_or_time_re$will_re? )?($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}; 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; diff --git a/t/DateMath.t b/t/DateMath.t index 70ce47974..02d8b267e 100755 --- a/t/DateMath.t +++ b/t/DateMath.t @@ -110,6 +110,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,