I already have working (ugly) code for this, but I will ask anyway:
I have the time intervals [09:15, 10:00), [21:10, 21:45) during weekdays. Given time t
and a number of seconds s
, if t
is within the intervals, I have to calculate the date and time where t - s
would fall into.
Is there a way of doing this in C++ cleanly (boost::icl? boost::date_time?)
I have tried boost::icl, it can certainly hold the time ranges in an interval_set<Time>
and find which interval a certain Time
is in, but if t - s
time point does not fall into an interval range, I don't see how I can find the nearest interval before that time point, and how to detect if I have to go back a day or through the whole weekend.
I think the problem is too complicated to allow for a clean solution, at least according to my definition of "clean".
You'll need a container for your (non-overlapping) daily intervals, supporting efficiently the following operations:
It seems to me that boost::icl::interval_set<Time>
is an adequate solution. Your time does not need to keep track of the date, you can have that separately.
Your algorithm will be something like:
let d and t be the date and time portions of your t
let i be the interval where t belongs
loop
if t-s belongs in i then
return t-s on day d
else
let j be the previous interval from i
if j does not exist (because i was the first) then
let j be the last interval
move d one weekday backwards
s := s - (t-start(i))
t := end(j)
i := j
This is more or less what you say that your code does. I don't think it can be much cleaner.