2014-01-14 05:52:21 -08:00
|
|
|
use strict;
|
|
|
|
|
|
|
|
package DDG::Goodie::WorkdaysBetween;
|
2014-01-14 16:50:32 -08:00
|
|
|
# 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;
|
2014-08-11 14:29:43 -07:00
|
|
|
with 'DDG::GoodieRole::Dates';
|
2014-01-14 05:52:21 -08:00
|
|
|
|
2014-02-04 10:01:03 -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";
|
2014-09-27 06:42:57 -07:00
|
|
|
zci is_cached => 0;
|
2014-01-14 05:52:21 -08:00
|
|
|
|
|
|
|
primary_example_queries 'workdays between 01/31/2000 01/31/2001';
|
2014-01-14 16:50:32 -08:00
|
|
|
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';
|
2015-01-07 10:24:47 -08:00
|
|
|
attribution github => ['http://github.com/mgarriott', 'Matt Garriott'];
|
2014-01-14 05:52:21 -08:00
|
|
|
|
2014-08-31 12:03:21 -07:00
|
|
|
my $datestring_regex = datestring_regex();
|
2014-08-11 14:29:43 -07:00
|
|
|
|
2014-01-14 05:52:21 -08:00
|
|
|
handle remainder => sub {
|
2014-08-11 14:29:43 -07:00
|
|
|
my $query = $_;
|
2014-08-31 12:03:21 -07:00
|
|
|
|
|
|
|
return unless ($query =~ qr/($datestring_regex) (?:(?:and|to) )?($datestring_regex)/i);
|
2014-08-31 16:16:35 -07:00
|
|
|
my ($start_date, $end_date) = parse_all_datestrings_to_date($1, $2);
|
2014-08-31 12:03:21 -07:00
|
|
|
|
2014-08-11 14:29:43 -07:00
|
|
|
return unless ($start_date && $end_date);
|
2014-10-28 11:36:15 -07:00
|
|
|
|
2014-08-31 12:03:21 -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
|
|
|
|
2014-02-04 10:01:03 -08:00
|
|
|
my $calendar = Date::Calendar->new($Profiles->{US});
|
2014-08-11 14:29:43 -07:00
|
|
|
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
|
|
|
|
2014-08-11 19:05:56 -07: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
|
|
|
|
2014-01-25 09:42:03 -08:00
|
|
|
my $verb = $workdays == 1 ? 'is' : 'are';
|
2015-01-09 00:09:45 -08:00
|
|
|
my $number = $workdays == 1 ? 'Workday' : 'Workdays';
|
2014-01-25 09:42:03 -08:00
|
|
|
|
2014-09-26 12:21:38 -07:00
|
|
|
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;
|