2012-04-12 22:33:01 -07:00
|
|
|
package DDG::Goodie::DaysBetween;
|
2012-04-21 15:41:19 -07:00
|
|
|
# ABSTRACT: Give the number of days between two given dates.
|
2012-04-12 22:33:01 -07:00
|
|
|
|
|
|
|
use DDG::Goodie;
|
|
|
|
use Date::Calc qw( Date_to_Days);
|
|
|
|
use Time::localtime;
|
|
|
|
|
2012-04-21 16:00:52 -07:00
|
|
|
triggers start => "days", "daysbetween", "days_between";
|
2012-05-23 19:05:08 -07:00
|
|
|
|
2012-04-12 22:33:01 -07:00
|
|
|
zci is_cached => 1;
|
|
|
|
zci answer_type => "days_between";
|
|
|
|
|
2012-11-06 13:03:50 -08:00
|
|
|
primary_example_queries 'days between 01/01/2000 01/01/2001';
|
|
|
|
secondary_example_queries 'days between 01/01/2000 01/01/2001 inclusive';
|
|
|
|
description 'calculate the number of days between two dates';
|
|
|
|
name 'DaysBetween';
|
|
|
|
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/DaysBetween.pm';
|
|
|
|
category 'calculations';
|
2012-11-06 21:59:54 -08:00
|
|
|
topics 'everyday';
|
2012-11-06 13:03:50 -08:00
|
|
|
attribution github => ['http://github.com/JetFault', 'JetFault'];
|
|
|
|
|
|
|
|
|
2012-04-21 15:52:45 -07:00
|
|
|
handle query_lc => sub {
|
2012-04-12 22:33:01 -07:00
|
|
|
|
2012-04-21 15:52:45 -07:00
|
|
|
s/^days(?:\s|_)*between//;
|
2012-04-21 17:13:32 -07:00
|
|
|
my @dates = $_ =~ m#([01]?[0-9])/([0-3]?[0-9])/([0-9]{4}(?=\s|$))#g;
|
2012-04-12 22:33:01 -07:00
|
|
|
|
|
|
|
if(scalar(@dates) == 3) {
|
2012-04-21 17:13:32 -07:00
|
|
|
my $tm = localtime;
|
2012-04-12 22:33:01 -07:00
|
|
|
push(@dates, $tm->mon + 1, $tm->mday, $tm->year + 1900);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(scalar(@dates) == 6) {
|
2012-05-01 20:49:55 -07:00
|
|
|
my ($days1, $days2);
|
|
|
|
my $inclusive = '';
|
2012-04-12 22:33:01 -07:00
|
|
|
|
|
|
|
eval {
|
2012-04-21 17:13:32 -07:00
|
|
|
$days1 = Date_to_Days(@dates[2,0,1]);
|
|
|
|
$days2 = Date_to_Days(@dates[5,3,4]);
|
2012-04-12 22:33:01 -07:00
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
return;
|
|
|
|
}
|
2012-04-21 17:13:32 -07:00
|
|
|
my $daysBetween = abs($days2 - $days1);
|
2012-04-21 14:58:37 -07:00
|
|
|
if(/inclusive/) {
|
|
|
|
$daysBetween += 1;
|
|
|
|
$inclusive = ', inclusive';
|
|
|
|
}
|
2012-04-21 17:13:32 -07:00
|
|
|
my $startDate = join '/', @dates[0,1,2];
|
|
|
|
my $endDate = join '/', @dates[3,4,5];
|
2012-04-21 14:58:37 -07:00
|
|
|
return 'There are ' . $daysBetween ." days between ". $startDate . ' and ' . $endDate . $inclusive . '.';
|
2012-04-12 22:33:01 -07:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
1;
|