while ($self->is_holiday($base_date)) {
my $sub_next = $self->get_push_amt($base_date);
+ # Ensure we're subtracting when we need to be
+ $sub_next = $sub_next > 0 ? 0 - $sub_next : $sub_next;
$base_date->add(days => $sub_next);
}
}
# start and end should not be closed days
- my $days = $start_dt->delta_days($end_dt)->delta_days;
+ my $delta_days = $start_dt->delta_days($end_dt)->delta_days;
while( $start_dt->compare($end_dt) < 1 ) {
- $days-- if $self->is_holiday($start_dt);
+ $delta_days-- if $self->is_holiday($start_dt);
$start_dt->add( days => 1 );
}
- return DateTime::Duration->new( days => $days );
+ return DateTime::Duration->new( days => $delta_days );
}
sub hours_between {
my ($self, $start_date, $end_date) = @_;
my $start_dt = $start_date->clone()->set_time_zone('floating');
my $end_dt = $end_date->clone()->set_time_zone('floating');
+
my $duration = $end_dt->delta_ms($start_dt);
$start_dt->truncate( to => 'day' );
$end_dt->truncate( to => 'day' );
+
# NB this is a kludge in that it assumes all days are 24 hours
# However for hourly loans the logic should be expanded to
# take into account open/close times then it would be a duration
# of library open hours
- # start and end should not be closed days
my $skipped_days = 0;
while( $start_dt->compare($end_dt) < 1 ) {
- $start_dt->add( days => 1 );
$skipped_days++ if $self->is_holiday($start_dt);
+ $start_dt->add( days => 1 );
}
+
if ($skipped_days) {
$duration->subtract_duration(DateTime::Duration->new( hours => 24 * $skipped_days));
}
C<$return_by_hour> is an integer value representing the opening hour for the branch
+=head2 get_push_amt
+
+ my $amt = $calendar->get_push_amt($date)
+
+C<$date> is a DateTime object representing a closed return date
+
+Using the days_mode syspref value and the nature of the closed return
+date, return how many days we should jump forward to find another return date
=head2 addDays
Passed two dates returns a DateTime::Duration object measuring the length between them
ignoring closed days. Always returns a positive number irrespective of the
-relative order of the parameters
+relative order of the parameters.
+
+Note: This routine assumes neither the passed start_dt nor end_dt can be a closed day
+
+=head2 hours_between
+
+$duration = $calendar->hours_between($start_dt, $end_dt);
+
+Passed two dates returns a DateTime::Duration object measuring the length between them
+ignoring closed days. Always returns a positive number irrespective of the
+relative order of the parameters.
+
+Note: This routine assumes neither the passed start_dt nor end_dt can be a closed day
=head2 next_open_days