Subject: | get_days() fails if range spans more than a month |
The attached patch fixes a problem with DateTime::BusinessHours which
occurs with date ranges of more than one month. For example,
use DateTime::BusinessHours;
my $datetime1=DateTime->new(
year => 2008,
month => 8,
day => 1,
);
my $datetime2=DateTime->new(
year => 2008,
month => 7,
day => 1,
);
my $testing = DateTime::BusinessHours->new(
datetime1 => $datetime1,
datetime2 => $datetime2,
weekends=>[6,7], #saturday , sunday
);
print "days=", $testing->getdays(), "\n";
prints days=0 although there's a month's worth of business days in
between. With the patch applied, DateTime::BusinessHours makes sure to
convert the datetime duration to days (not days and months) before
calculating the business days in it.
Would be great if you could roll this out in a new version of your
module, thanks!
-- Mike
Subject: | patch.txt |
diff -Naur DateTime-BusinessHours-1.01/lib/DateTime/BusinessHours.pm DateTime-BusinessHours-1.01.patched/lib/DateTime/BusinessHours.pm
--- DateTime-BusinessHours-1.01/lib/DateTime/BusinessHours.pm 2007-11-10 18:20:13.000000000 -0800
+++ DateTime-BusinessHours-1.01.patched/lib/DateTime/BusinessHours.pm 2008-09-19 17:31:52.000000000 -0700
@@ -35,11 +35,17 @@
sub getdays
{
my $self = shift;
- my $datediff=$self->datetime2-$self->datetime1;
- my $days = $datediff->delta_days;
+
+ my $start_date = $self->datetime1;
+ my $end_date = $self->datetime2;
+ ($start_date, $end_date) = ($end_date, $start_date) if
+ $start_date > $end_date;
+
+ my $days = $end_date->delta_days($start_date)->in_units('days');
+
my $noofweeks = $days/7;
my $extradays = $days%7;
- my $startday = $self->datetime1->day_of_week;
+ my $startday = $start_date->day_of_week;
#exclude any day in the week marked as holiday (ex: saturday , sunday)
$days = $days - ($noofweeks * ($#{$self->weekends}+1));