本文介绍了如何忽略周末和节假日在提升日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个问题



我有一个boost日期对象,如下所示:



boost :: gregorian :: date今天(2012,02,13)



我从Today中减去一个datepart,如下:



今天 - 几个月(240)或今天 - 天(X)等。



我想如果有办法排除周末和特殊假期时做上述减法?

第二个问题


如果今天是星期三,今天 -

同样,我使用类似下面的东西来做时间操作:



ptime ptNow(day_clock :: local_day());
ptNow - minutes(1170);




  • 再次有一种方法可以忽略从分钟/小时减去的周末/节假日? / li>
  • 我真的希望我的1170分钟是1170交易分钟(这是19.5交易小时/每天6.5小时)。换句话说,我希望我的开始是美国东部时间上午9:30,结束时间是美国东部时间下午16:00,如果我打电话给ptNow - 分钟(1170),那么我应该回到1170个交易日。


解决方案

在处理假期时,真的只有一个选项:你应该迭代,一次一天,如果一天计数,根据需要添加/减。在伪代码中:

 日期add(date startDate,int daysToAdd){
int i:= 0
endDate:= startDate
while(i< daysToAdd){
endDate ++
if(NOT(isWeekend(endDate))
AND NOT(isHoliday(endDate)){
i ++
}
}
return endDate
}



isWeekend()是微不足道的实现; isHoliday()处理它的最简单的方法是有一个已知的假日表,并检查作为参数传递的日期是否与任何一个一致。在我看来,最好有一个基于规则的方法,可以计算是否给定日期是假日。


First Question

I have a boost date object as follows:

boost::gregorian::date Today(2012, 02, 13)

I substract a datepart from Today as follows:

Today - months(240) or Today - days(X) etc ..

I would like to if there is a way to exclude weekends and special holidays when doing the above subtraction? I mean if Today is Wednesday, Today - days(4) should be last Friday.

Second Question

Similarly, i use something like the following to do time manipulations:

ptime ptNow(day_clock::local_day());ptNow - minutes(1170);

  • Again is there a way to ignore weekends/holidays from minutes/hours substraction?
  • i really want my 1170 minutes to be 1170 trading minutes (which is 19.5 trading hours / 6.5 hours a day). In otherwords i want my start of the day to be 9:30 AM EST and End to be 16:00 PM EST and If i call ptNow - minutes(1170), it should take me back to 1170 trading minutes.

解决方案

When dealing with holidays, there's really only one option: one day at a time. You should iterate, one day at a time, and add/substract as needed if a day "counts". In pseudo code:

date add(date startDate, int daysToAdd) {
    int i:=0
    endDate:=startDate
    while (i<daysToAdd) {
        endDate++
        if (    NOT(isWeekend(endDate))
            AND NOT(isHoliday(endDate)) {
            i++
        }
    }
    return endDate
}

isWeekend() is trivial to implement; isHoliday(), on the other hand, is a very tough nut to crack. The easiest way to deal with it is to have a table of known holidays and check if the date passed as parameter coincides with any of those. In my opinion, it's better to have a rule-based method that can compute whether a given date is a holiday or not.

这篇关于如何忽略周末和节假日在提升日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 20:47