4

Are there any APIs available in boost::date_time to get the number of days between two dates that is also calendar specific?

Example, Number of days between 2005/01/01 and 2006/12/31 would be 730 in a seven day calendar and would be 504 in a five day calendar.

Barry
  • 286,269
  • 29
  • 621
  • 977
user1408865
  • 136
  • 1
  • 13
  • possible duplicate of [weekdays' duration using boost date](http://stackoverflow.com/questions/7340986/weekdays-duration-using-boost-date) – NathanOliver May 21 '15 at 19:33

1 Answers1

6

Yup: posix_time to the rescue

Live On Coliru

#include <boost/date_time/gregorian/greg_date.hpp>
#include <iostream>

int main() {
    using boost::gregorian::date;

    date a { 2005, 1, 1 }, b { 2006, 12, 31 };

    std::cout << (b-a).days() << "\n";
}

Prints

729 [1]

Use posix_time::ptime if you want to use gregorian date + time of day (hh:mm:ss.fffffff). local_time::local_date_time adds timezone awareness for this (to be correct about daylight savings e.g.)


Trivia Incidentally this number is also the square of 27, and the cube of 9, and as a consequence of these properties, a perfect totient number, a centered octagonal number, and a Smith number. It is not the sum of four consecutive primes

sehe
  • 374,641
  • 47
  • 450
  • 633