cannot convert »boost::gregorian::date_duration« to »int

1.5k views Asked by At

I am using the boost libs and would just work with an int :

but I am receiving the error :

cannot convert »boost::gregorian::date_duration« to »int

What could I do ? Please help, I just want to check if my date passed the current date.. I get the right answer but could not work with. (range.length())

#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <ctime>
#include <fstream>

namespace bdt = boost::gregorian;
using namespace std;

int main(void)
{

time_t now = time(0);
tm *ltm = localtime(&now);

//Aktuelles Datum
    int y = 1900 + ltm->tm_year;
    int d = ltm->tm_mday;
    int m = 1 + ltm->tm_mon;
    bdt::date today(bdt::date(y, m, d));


//Ziel                               y   m   d  
    bdt::date electionDay(bdt::date(2012, 6, 6));
    bdt::date_period range(today, electionDay);


    std::cout  << range.length() << std::endl;
            int z = range.length() ;  



    return 0;
}
2

There are 2 answers

0
Mike Seymour On BEST ANSWER

Assuming you want the integer to represent the number of days in the period, then:

range.length().days()

Or, if you want to compare dates, you can do that without converting to periods, durations, integers, or anything else:

if (today > electionDay) {
    std::cout << "Election day is in the past\n";
}
0
Igor R. On

length() returns date_duration instance. You can use one of its member-functions to get what you want. Eg., range.length().days();