more useful options PerlMonks

### Re^2: Check if Date interval contains Hour X

by ikegami (Pope)
 on Jul 23, 2009 at 19:21 UTC ( #782766=note: print w/replies, xml ) Need Help??

in reply to Re: Check if Date interval contains Hour X
in thread Check if Date interval contains Hour X

I thought you were on to something until I realized the real problem is finding the right date to go with the hour. Using ::Span makes things a lot more complicated since you now need to make 4 DateTime objects for every hour.
• Comment on Re^2: Check if Date interval contains Hour X

Replies are listed 'Best First'.
Re^3: Check if Date interval contains Hour X
by alexm (Chaplain) on Jul 23, 2009 at 20:08 UTC

The very first thing that came to my mind after reading the OP was the method DateTime::Span::contains. However, yours is the right approach since that method only works for sets that are fully inside, as the manual says.

Thanks!

contains wouldn't work, but intersects would
```...| 20 | 21 | 22 | 23 | 00 | 01 | 02 | 03 |...
(------------)     log
(----]   hour
(--)     intersection

You're right again, I just realized a bit later so I've been playing with intersects:

```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' )

\$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.

Create A New User
Node Status?
node history
Node Type: note [id://782766]
help
Chatterbox?
 [Eily]: The best example of that is the TGV, they couldn't decide who would get it so it's in the middle of nowhere halfway between the two [LanX]: Eily: that rings a bell [erix]: interesting difference, I suppose they use different data/routes (and shorter seems better, no?) [erix]: (I used http://afstandmete n.nl/ ) LanX The stations of Limburg Süd and Montabaur, which are approximately 20 km apart, ... [LanX]: teh route I get from gmaps is 1662 km long and is crossing the Swiss Alps (mounting 2400 m) ... I wouldn't try this in December...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2017-12-13 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What programming language do you hate the most?

Results (369 votes). Check out past polls.

Notices?