Get the number of trading days in two days

I am trying to get the number of trading dates between two dates that will exclude only weekends and will not consider any holidays. I use Boost and C ++ 11 standards.

using namespace boost::gregorian;
long dateDifference( string start_date, string end_date ) {

            date _start_date(from_simple_string(start_date));
            date _end_date(from_simple_string(end_date));


            long difference = ( _start_date - _end_date ).days();

            return difference;

        }

This returns the number of days between two dates excluding weekends. Can someone point me in the right direction. I can not understand the solution.

Thanks Maxx

+4
source share
4 answers

Just run the day iterator and calculate the days of the week manually:

#include <boost/date_time.hpp>

using namespace boost::gregorian;

long dateDifference( string start_date, string end_date ) 
{
    date _start_date(from_simple_string(start_date));
    date _end_date(from_simple_string(end_date));

    // counter for weekdays
    int cnt=0;
    for(day_iterator iter = _start_date; iter!=_end_date; ++iter)
    {
        // increment counter if it no saturday and no sunday
        if(    iter->day_of_week() !=  boost::date_time::Saturday
            && iter->day_of_week() !=  boost::date_time::Sunday)
            ++cnt;
    }
    return cnt;
}

Answer ported from this answer: fooobar.com/questions/1530742 / ...

+4
source

O (1) without loops:

#include <boost/date_time.hpp>
using namespace std;
using namespace boost::gregorian;

long countWeekDays( string d0str, string d1str ) {
    date d0(from_simple_string(d0str));
    date d1(from_simple_string(d1str));
    long ndays = (d1-d0).days() + 1; // +1 for inclusive
    long nwkends = 2*( (ndays+d0.day_of_week())/7 ); // 2*Saturdays
    if( d0.day_of_week() == boost::date_time::Sunday ) ++nwkends;
    if( d1.day_of_week() == boost::date_time::Saturday ) --nwkends;
    return ndays - nwkends;
}

, , (ndays+d0.day_of_week())/7. , , , .

:

#include <iostream>
#include <cassert>
#include <string>

//      January 2014    
//  Su Mo Tu We Th Fr Sa
//            1  2  3  4
//   5  6  7  8  9 10 11
//  12 13 14 15 16 17 18
//  19 20 21 22 23 24 25
//  26 27 28 29 30 31
int main()
{
  assert(countWeekDays("2014-01-01","2014-01-01") == 1);
  assert(countWeekDays("2014-01-01","2014-01-02") == 2);
  assert(countWeekDays("2014-01-01","2014-01-03") == 3);
  assert(countWeekDays("2014-01-01","2014-01-04") == 3);
  assert(countWeekDays("2014-01-01","2014-01-05") == 3);
  assert(countWeekDays("2014-01-01","2014-01-06") == 4);
  assert(countWeekDays("2014-01-01","2014-01-10") == 8);
  assert(countWeekDays("2014-01-01","2014-01-11") == 8);
  assert(countWeekDays("2014-01-01","2014-01-12") == 8);
  assert(countWeekDays("2014-01-01","2014-01-13") == 9);
  assert(countWeekDays("2014-01-02","2014-01-13") == 8);
  assert(countWeekDays("2014-01-03","2014-01-13") == 7);
  assert(countWeekDays("2014-01-04","2014-01-13") == 6);
  assert(countWeekDays("2014-01-05","2014-01-13") == 6);
  assert(countWeekDays("2014-01-06","2014-01-13") == 6);
  assert(countWeekDays("2014-01-07","2014-01-13") == 5);
  cout << "All tests pass." << endl;
  return 0;
}

, 1400-10000. , . , 1752 ,

   September 1752
Su Mo Tu We Th Fr Sa
       1  2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
+6

- boost:: gregorian:: day_of_week() , , .

start_date (), end_date , , .

, " " , , .

+2

O (1) , , :

int get_weekdays_count(const boost::gregorian::date& a,const boost::gregorian::date& b)
    {
        int na=(a<b) ? a.day_of_week().as_number() : b.day_of_week().as_number();
        int diff=(a-b).days();
        if(diff!=0){
            if(diff<0) diff*=-1;
            int rslt=diff/7;       //number of saturdays
            rslt*=2;               // times 2 for sundays
            rslt+= (diff%7) >=(boost::gregorian::Saturday-na)%7 ? 1 : 0; // handle special case for saturdays
            rslt+= (diff%7) >=(boost::gregorian::Sunday-na)%7 ? 1 : 0; //special case for sundays
            return 1+diff-rslt;
        }
        else return (na==boost::gregorian::Saturday || na==boost::gregorian::Sunday) ? 0 : 1;
    };

, a > b, .

Speed ​​in the for loop: 25 nanoseconds / call according to VS2012 Release mode // proc i5 4690K

0
source

Source: https://habr.com/ru/post/1530741/


All Articles