sub hour_in_range {
my ($dt_s, $dt_e, @hours) = @_;
my $dt_span = DateTime::Span->from_datetimes(
start => $dt_s, end => $dt_e
);
for my $hour (@hours) {
my $dt = $dt_s->clone()
->truncate( to => 'day' )
->add( hours => $hour );
$dt->add( days => 1 ) if $dt < $dt_s;
my $hour_span = DateTime::Span->from_datetimes(
start => $dt, end => $dt
);
return 1 if $dt_span->intersects($hour_span);
}
return 0;
}
It's not as simple and efficient as your approach, but it shows how to use span intersection.
OTOH, I misunderstood the OP and was thinking that @hours = (1,11) was an interval (1am to 11am), not a list of hours to match. |