最好的方法是什么,而不使用MS COM库将OLE日期时间格式转换为boost使用的posix_datetime?

OLE日期时间表示为浮点数。

最佳答案

您必须手动执行...我没有找到其他方式执行此操作...

boost::posix_time::ptime datetime_ole_to_posix(double ole_dt)
{
  static const boost::gregorian::date ole_zero(1899,12,30);

  boost::gregorian::days d(ole_dt);
  boost::posix_time::ptime pt(ole_zero + d);

  ole_dt -= d.days();
  ole_dt *= 24 * 60 * 60 * 1000;

  return pt + boost::posix_time::milliseconds(std::abs(ole_dt));
}

测试正确性:
void datetime_ole_to_posix_test()
{
  using boost::gregorian::date;
  using namespace boost::posix_time;

  /* http://msdn.microsoft.com/en-us/library/38wh24td.aspx */
  BOOST_ASSERT(datetime_ole_to_posix(-1.0) == ptime(date(1899,12,29)));
  BOOST_ASSERT(datetime_ole_to_posix(-1.25) == ptime(date(1899,12,29), hours(6)));
  BOOST_ASSERT(datetime_ole_to_posix(0.0) == ptime(date(1899,12,30)));
  BOOST_ASSERT(datetime_ole_to_posix(1.0) == ptime(date(1899,12,31)));
  BOOST_ASSERT(datetime_ole_to_posix(2.25) == ptime(date(1900,01,01), hours(6)));

  BOOST_ASSERT(datetime_ole_to_posix(2.0) == ptime(date(1900,01,01)));
  BOOST_ASSERT(datetime_ole_to_posix(5.0) == ptime(date(1900,01,04)));
  BOOST_ASSERT(datetime_ole_to_posix(5.25) == ptime(date(1900,01,04), hours(6)));
  BOOST_ASSERT(datetime_ole_to_posix(5.5) == ptime(date(1900,01,04), hours(12)));
  BOOST_ASSERT(datetime_ole_to_posix(5.875) == ptime(date(1900,01,04), hours(21)));
}

关于c++ - OLE日期时间到Posix_time,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5246140/

10-13 03:32