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

56 lines
1.9 KiB
Perl
Raw Normal View History

2014-01-14 05:52:21 -08:00
use strict;
package DDG::Goodie::WorkdaysBetween;
# ABSTRACT: Give the number of work days between two given dates. Does not
# consider holidays.
2014-01-14 05:52:21 -08:00
use DDG::Goodie;
with 'DDG::GoodieRole::Dates';
2014-01-14 05:52:21 -08:00
use Date::Calendar;
use Date::Calendar::Profiles qw($Profiles);
2014-02-11 10:57:31 -08:00
triggers start => "workdays between", "business days between", "work days between", "working days", "workdays from";
2014-01-14 05:52:21 -08:00
zci answer_type => "workdays_between";
zci is_cached => 0;
2014-01-14 05:52:21 -08:00
primary_example_queries 'workdays between 01/31/2000 01/31/2001';
description 'Calculate the number of workdays between two dates. Does not consider holidays.';
2014-01-14 05:52:21 -08:00
name 'WorkDaysBetween';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/WorkdaysBetween.pm';
category 'calculations';
topics 'everyday';
attribution github => ['http://github.com/mgarriott', 'Matt Garriott'];
2014-01-14 05:52:21 -08:00
my $datestring_regex = datestring_regex();
2014-01-14 05:52:21 -08:00
handle remainder => sub {
my $query = $_;
return unless ($query =~ qr/($datestring_regex) (?:(?:and|to) )?($datestring_regex)/i);
my ($start_date, $end_date) = parse_all_datestrings_to_date($1, $2);
return unless ($start_date && $end_date);
2014-10-28 11:36:15 -07:00
($start_date, $end_date) = ($end_date, $start_date) if (DateTime->compare($start_date, $end_date) == 1);
2014-01-14 05:52:21 -08:00
my $calendar = Date::Calendar->new($Profiles->{US});
my $workdays = $calendar->delta_workdays($start_date->year(), $start_date->month(), $start_date->day(), $end_date->year(), $end_date->month(), $end_date->day(), 1, 1);
2014-01-14 05:52:21 -08:00
my $start_str = date_output_string($start_date);
my $end_str = date_output_string($end_date);
2014-01-14 05:52:21 -08:00
my $verb = $workdays == 1 ? 'is' : 'are';
my $number = $workdays == 1 ? 'Workday' : 'Workdays';
return "There $verb $workdays $number between $start_str and $end_str.",
structured_answer => {
input => [$start_str, $end_str],
operation => "$number between",
result => $workdays
};
2014-01-14 05:52:21 -08:00
};
1;