DaysBetween: Add 'Days since' feature
parent
0975a379a6
commit
48a7feba0b
|
@ -3,21 +3,32 @@ package DDG::Goodie::DaysBetween;
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use DDG::Goodie;
|
use DDG::Goodie;
|
||||||
|
use DateTime;
|
||||||
with 'DDG::GoodieRole::Dates';
|
with 'DDG::GoodieRole::Dates';
|
||||||
|
|
||||||
triggers start => "days between", "days", "daysbetween", "days_between", "number of days between", "how many days between", "number of days from", "days from";
|
triggers start => "days between", "days", "daysbetween", "days_between", "number of days between", "how many days between", "number of days from", "days from", "days since", "how many days since", "number of days since";
|
||||||
|
|
||||||
zci is_cached => 1;
|
# 'Days since' will calculate days from the specified date to the present date, therefore answer cannot be cached
|
||||||
|
zci is_cached => 0;
|
||||||
zci answer_type => "days_between";
|
zci answer_type => "days_between";
|
||||||
|
|
||||||
my $datestring_regex = datestring_regex();
|
my $datestring_regex = datestring_regex();
|
||||||
|
my @monhs = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
|
||||||
|
|
||||||
handle remainder => sub {
|
handle query_lc => sub {
|
||||||
return unless $_ =~ qr/^($datestring_regex) (?:(?:and|to) )?($datestring_regex)(?:[,]? inclusive)?$/i;
|
return unless ($_ =~ qr/(days since)(?: *)($datestring_regex)/i) || ($_ =~ qr/($datestring_regex) (?:(?:and|to) )?($datestring_regex)(?:[,]? inclusive)?$/i);
|
||||||
|
my ($date1, $date2);
|
||||||
|
if ($1 ne "days since") {
|
||||||
|
($date1, $date2) = parse_all_datestrings_to_date($1, $2);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
my $date_object = DateTime->now;
|
||||||
|
my ($currentDay, $currentMonth, $currentYear) = ($date_object->day(), $date_object->month(), $date_object->year());
|
||||||
|
my $t = join " ", $currentDay, $monhs[$currentMonth - 1], $currentYear;
|
||||||
|
($date1, $date2) = parse_all_datestrings_to_date($2, $t);
|
||||||
|
}
|
||||||
|
|
||||||
my ($date1, $date2) = parse_all_datestrings_to_date($1, $2);
|
|
||||||
return unless ($date1 && $date2);
|
return unless ($date1 && $date2);
|
||||||
|
|
||||||
($date1, $date2) = ($date2, $date1) if ( DateTime->compare($date1, $date2) == 1 );
|
($date1, $date2) = ($date2, $date1) if ( DateTime->compare($date1, $date2) == 1 );
|
||||||
|
|
||||||
my $difference = $date1->delta_days($date2);
|
my $difference = $date1->delta_days($date2);
|
||||||
|
|
|
@ -26,13 +26,13 @@ sub build_structured_answer{
|
||||||
|
|
||||||
sub build_test{ test_zci(build_structured_answer(@_))}
|
sub build_test{ test_zci(build_structured_answer(@_))}
|
||||||
|
|
||||||
set_fixed_time('2016-07-14T22:36:00');
|
set_fixed_time('2016-08-03T22:36:00');
|
||||||
|
|
||||||
my $test_inclusive = ", inclusive";
|
my $test_inclusive = ", inclusive";
|
||||||
|
|
||||||
ddg_goodie_test(
|
ddg_goodie_test(
|
||||||
[qw( DDG::Goodie::DaysBetween)],
|
[qw( DDG::Goodie::DaysBetween)],
|
||||||
'days between today and tomorrow' => build_test('14 Jul 2016', '15 Jul 2016', 1, ''),
|
'days between today and tomorrow' => build_test('03 Aug 2016', '04 Aug 2016', 1, ''),
|
||||||
'days between jan 1 and jan 15 inclusive' => build_test('01 Jan 2016', '15 Jan 2016', 15, $test_inclusive),
|
'days between jan 1 and jan 15 inclusive' => build_test('01 Jan 2016', '15 Jan 2016', 15, $test_inclusive),
|
||||||
'days between jan 1 and 15th feb' => build_test('01 Jan 2016', '15 Feb 2016', 45, ''),
|
'days between jan 1 and 15th feb' => build_test('01 Jan 2016', '15 Feb 2016', 45, ''),
|
||||||
'how many days between feb 2 and feb 17' => build_test('02 Feb 2016', '17 Feb 2016', 15, ''),
|
'how many days between feb 2 and feb 17' => build_test('02 Feb 2016', '17 Feb 2016', 15, ''),
|
||||||
|
@ -47,6 +47,7 @@ ddg_goodie_test(
|
||||||
'number of days from 2015-02-02 and 2016-02-02 inclusive' => build_test('02 Feb 2015', '02 Feb 2016', 366, $test_inclusive),
|
'number of days from 2015-02-02 and 2016-02-02 inclusive' => build_test('02 Feb 2015', '02 Feb 2016', 366, $test_inclusive),
|
||||||
'number of days between 2014-02-02 and 2015-02-02' => build_test('02 Feb 2014', '02 Feb 2015', 365, ''),
|
'number of days between 2014-02-02 and 2015-02-02' => build_test('02 Feb 2014', '02 Feb 2015', 365, ''),
|
||||||
'number of days between 2014-02-02 and 2015-02-02 inclusive' => build_test('02 Feb 2014', '02 Feb 2015', 366, $test_inclusive),
|
'number of days between 2014-02-02 and 2015-02-02 inclusive' => build_test('02 Feb 2014', '02 Feb 2015', 366, $test_inclusive),
|
||||||
|
'days since 2016-07-31' => build_test('31 Jul 2016', '03 Aug 2016', 3)
|
||||||
'days between jan 1 2012 and jan 1 123456' => undef
|
'days between jan 1 2012 and jan 1 123456' => undef
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue