1

Is there a way to convert the boost::date_duration to a double. I have the following code:

date be; 
date bd;
days t = (be - bd);
std::cout << "days are:" << t << std::endl;

This works fine and returns the no. of days. But I want to get the value in years, so if I divide t by 365, it only shows 0. setprecision() did not help either.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
itcplpl
  • 780
  • 4
  • 18
  • 29

1 Answers1

3

You're probably caught by the fact that boost::gregorian::days overloads operator/. Convert to an integer first using days() and then divide by a floating-point value to get floating-point division.

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

int main()
{
    boost::gregorian::date be(2000, 1, 1), bd(1950, 1, 1);
    boost::gregorian::days t = be - bd;
    std::cout << "days are: " << t << '\n'
              << "days/365 = " << t.days()/365.0 << '\n';
}

Note the output: your result is not equal to the number of years because a year is not 365.00 days.

Cubbi
  • 46,567
  • 13
  • 103
  • 169