----- Original Message -----
From: Tim Pettman <tjp / vbc.net>
To: ruby-talk ML <ruby-talk / ruby-lang.org>
Sent: Monday, March 26, 2001 5:05 AM
Subject: [ruby-talk:13218] Help with time.


> Hi there,
>
> I am hoping that you will be able to help me a problem I am having. I am
> quite new to Ruby (and for that matter programming) and this is my first
> useful program. What I need to do is find out if a time range crosses one
> or more fixed time boundaries.
>
> I have defined that start and end time of the range:
>  timeStart = Time.local(a,b,c,d,e,f)
>  timeEnd = Time.local(u,v,w,x,y,z)
> The range lasts anything between 1 and many thousands of seconds.
>
> I need to know which (if any) of the following boundaries the range
> crosses:
>  Midnight between Friday and Saturday
>  Midnight between Sunday and Monday
>  08:00 Monday to Friday
>  18:00 Monday to Friday

[snip]

There is more than one way to approach this.

I think what I would do is this: Take the start time and calculate the
number
of seconds to each of the boundaries in question. This in itself is not
quite
trivial, of course. But it reduces the problem to four simpler ones.

Then for each case, take the difference between the start time and the
boundary time. If it's less than the start-end difference, we must have
crossed
that boundary.

Then case 1 (Fri/Sat midnight) would go something like this. (I'm assuming
here that midnight belongs with the previous day, though it really probably
doesn't.)
  1. Take the number of seconds remaining until midnight TODAY;
  2. Add the number of days remaining till Friday (times 86400 to convert to
       seconds).

Case 2 would be similar.
Cases 3 and 4 would involve a decision about how long it is to the nearest
weekday (based on where in the week we are).

Hope this is a little help.

Hal Fulton