3

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.
Moe
  • 28,607
  • 10
  • 51
  • 67
aLayman
  • 43
  • 3
  • `holiday` and `weekend` are cultural conventions. Which rules do you want? E.g. Friday may be the first day of the weekend. – MSalters Feb 24 '12 at 10:06

1 Answers1

2

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.

Miguel Farah
  • 146
  • 1
  • 1
  • 7