gpt4 book ai didi

c++ - OLE 日期时间到 Posix_time

转载 作者:行者123 更新时间:2023-11-30 03:08:14 26 4
gpt4 key购买 nike

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

OLE 日期时间表示为 float 。

最佳答案

您必须手动执行...我找不到其他方法...

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com