Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 47 lines (37 sloc) 1.101 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
package DDG::Goodie::DaysBetween;
# 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";


handle query_lc => sub {

s/^days(?:\s|_)*between//;
my @dates = $_ =~ m#([01]?[0-9])/([0-3]?[0-9])/([0-9]{4}(?=\s|$))#g;

if(scalar(@dates) == 3) {
my $tm = localtime;
push(@dates, $tm->mon + 1, $tm->mday, $tm->year + 1900);
}

if(scalar(@dates) == 6) {
        my ($days1, $days2);
        my $inclusive = '';

eval {
$days1 = Date_to_Days(@dates[2,0,1]);
$days2 = Date_to_Days(@dates[5,3,4]);
};
if ($@) {
return;
}
my $daysBetween = abs($days2 - $days1);
        if(/inclusive/) {
            $daysBetween += 1;
            $inclusive = ', inclusive';
        }
        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;
Something went wrong with that request. Please try again.