user1408865
user1408865

Reputation: 171

How to get the number of days between two dates using boost::date_time

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.

Upvotes: 4

Views: 3427

Answers (1)

sehe
sehe

Reputation: 392911

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

Upvotes: 6

Related Questions