我已经有了这方面的工作(丑陋)代码,但我还是要问:
我有工作日的时间间隔[09:15,10:00],[21:10,21:45]。给定时间t和若干秒s,如果t在间隔内,则必须计算t - s落入的日期和时间。
在C++中是否有一种干净的方法(boost::icl )?boost::date_time?)
我已经尝试过boost::icl,它当然可以保存interval_set<Time>中的时间范围,并找到某个Time所处的时间间隔,但是如果t - s时间点没有降到一个间隔范围内,我就看不出如何在该时间点之前找到最近的时间间隔,以及如何检测是否必须返回一天或整个周末。
发布于 2013-09-27 13:32:53
我认为这个问题太复杂,不可能有一个干净的解决办法,至少根据我对“清洁”的定义是这样的。
您将需要一个用于(不重叠)每日间隔的容器,有效地支持以下操作:
在我看来,boost::icl::interval_set<Time>是一个很好的解决方案。你的时间不需要记录日期,你可以把它分开。
您的算法将类似于:
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这或多或少就是您所说的代码所做的事情。我不认为它会更干净。
https://stackoverflow.com/questions/19051334
复制相似问题