anthony.green@bbc.co.uk
10/25/2006 5:42:00 PM
> Another way to compute timeslots which avoids this might be:
>
> timeslots = (0..6*24).map {|h| first + h.hour}
>
> Generalizing this to arbitray start and end times is left as an
> exercise to the reader.
Something like this ?
# add a to date method for Time class
class Time
def to_date
Date.new(year, month, day)
rescue NameError
nil
end
end
# first date and last date
first = Time.local(2006, 10, 21, 0, 0, 0)
last = Time.local(2006, 10, 28, 0, 0, 0)
# calculate the number of days between them
range = last.to_date - first.to_date
# create your array
timeslots = (0..range*24).map {|h| first + h.hour}
# remove the last one as it'll be the first time slot of the next day
@timeslots = timeslots[0, timeslots.size - 1]
# sort by timeslot then by day
@timeslots.sort! {|a, b| a.strftime("%H%M%S%Y%m%d").to_i <=>
b.strftime("%H%M%S%Y%m%d").to_i}
Tony