Halt
Halt

Reputation: 2994

Convert Boost Gregorian Date to POD

Is there an easy way to convert a boost::gregorian::date to a Plain old data type? I need to be able to pass it through a C DLL interface. I'd rather not convert it to it's year/month/day components back and forth.

Upvotes: 0

Views: 290

Answers (2)

Igor R.
Igor R.

Reputation: 15075

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

int main() 
{
    boost::gregorian::date date1(2013, boost::date_time::Feb, 25);
    std::cout << date1 << std::endl;
    boost::gregorian::date::date_int_type pod = date1.day_number();
    std::cout << pod << std::endl;
    boost::gregorian::date date2(pod);
    std::cout << date2 << std::endl;
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500665

You could pick some epoch date, and then get the date_duration between your "current" date and the epoch, then call days() on the duration to get a long. Pass the long through your C DLL, then on the other side create a new date_duration with the same number of days, add it to the epoch, and you should be back to your original date.

You should be able to easily wrap this in a couple of functions, e.g.

long days_since_epoch_from_date(date)
date date_from_days_since_epoch(long) 

Upvotes: 1

Related Questions