I was developing an Excel interface into a library that uses boost's ptime class to represent DateTime.
Here is what I use to convert between XLL's datetime (OA DateTime, OA Date, OLE Automation date) and boost ptime:
// modified from http://lists.boost.org/Archives/boost/2009/06/153413.php to improve speed and include ticks into conversion
#include<boost/date_time/date_defs.hpp>
#include
<boost/date_time/time_defs.hpp>
#include
<boost/date_time/time_duration.hpp>
#include
<boost/date_time/special_defs.hpp>
template<class time_type>
class oa_date_converter
{
public:
typedeftypename time_type::date_type date_type;
typedeftypename time_type::time_duration_type time_duration_type;
typedeftypename time_type::time_system_type::impl_type::int_type int_type;
static time_type from_oadate(double oa_date)
{
staticconst time_type base_time(date_type(1899, 12, 30), time_duration_type(0,0,0));
staticconst int_type ticks_per_day(86400 * time_duration_type::rep_type::res_adjust());
return base_time + time_duration_type(0, 0, 0, (int_type)(oa_date * ticks_per_day));
}
staticdouble to_oadate(time_type time)
{
staticconst time_type base_time(date_type(1899, 12, 30), time_duration_type(0,0,0));
staticconstdouble days_per_tick(1.0 / (86400 * time_duration_type::rep_type::res_adjust()));
return (time - base_time).ticks() * days_per_tick;
}
};