获取两天之间的交易日数

Max*_*axx 4 c++ boost c++11

我试图获得两个日期之间的交易日期数量,这将只排除周末,不会考虑任何假期.我正在使用Boost和c ++ 11标准.

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;

        }
Run Code Online (Sandbox Code Playgroud)

这只返回两个日期之间的天数而不考虑周末.有人能指出我正确的方向.我似乎无法弄清楚解决方案.

谢谢,Maxx

Mat*_*att 6

O(1)解决方案没有循环:

#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;
}
Run Code Online (Sandbox Code Playgroud)

基本的想法是首先计算所有星期六,这可以通过公式方便地给出(ndays+d0.day_of_week())/7.加上这个给你所有的星期六和星期日,除了开始和结束日期可能在周末的情况下,通过2个简单测试调整.

测试它:

#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;
}
Run Code Online (Sandbox Code Playgroud)

这适用于格里高利历中的任何日期范围,这增加了当前支持1400 - 10000年.请注意,不同的国家/地区在不同时间采用了公历.例如,英国人在1752年9月从朱利安换成格里高利历,所以他们那个月的日历看起来像

   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
Run Code Online (Sandbox Code Playgroud)


Mar*_*ölk 5

只需运行一天迭代器并手动计算工作日:

#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's 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;
}
Run Code Online (Sandbox Code Playgroud)

从此答案移植的答案:https ://stackoverflow.com/a/7342989/3187827