zeroclickinfo-goodies/lib/DDG/Goodie/DaysBetween.pm

57 lines
1.5 KiB
Perl
Raw Normal View History

package DDG::Goodie::DaysBetween;
2012-04-21 15:41:19 -07:00
# ABSTRACT: Give the number of days between two given dates.
use DDG::Goodie;
use Date::Calc qw( Date_to_Days);
use Time::localtime;
triggers start => "days", "daysbetween", "days_between";
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';
topics 'everyday_goodies';
attribution github => ['http://github.com/JetFault', 'JetFault'];
handle query_lc => sub {
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;
if(scalar(@dates) == 3) {
2012-04-21 17:13:32 -07:00
my $tm = localtime;
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 = '';
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]);
};
if ($@) {
return;
}
2012-04-21 17:13:32 -07:00
my $daysBetween = abs($days2 - $days1);
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];
return 'There are ' . $daysBetween ." days between ". $startDate . ' and ' . $endDate . $inclusive . '.';
}
return;
};
1;