【发布时间】:2014-04-09 22:21:24
【问题描述】:
我在将 ptime 对象从 boost 转换为要传递给函数的字符串时遇到问题。关于将提升时间对象输出到字符串(主要是到 cout),我发现了多个类似的其他线程,但我在它们上发现的都没有工作。
看来最简单的方法是将 ptime 对象插入到字符串流中,然后使用字符串流的字符串。正如其他线程上的一些答案所暗示的那样,我还尝试为字符串流注入 time_facet。但是,我无法创建 time_facet 对象。它给了我类模板的参数列表丢失的错误。令人困惑的是,互联网上没有任何地方提到 time_facet 的参数列表,甚至 boost 的文档页面也显示 time_facet 的默认构造函数仅仅是 time_facet()。
以下是我尝试过的简单版本:
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
boost::posix_time::ptime time = boost::posix_time::time_from_string("1981-08-20 08:05:00");
std::stringstream sstream;
sstream << time;
_updateStatement->setString(1, (sql::SQLString)sstream.str());
在字符串流中插入时间给了我一堆类似于
的编译错误error C2220: warning treated as error - no 'object' file generated C:\code\trunk\Development\External\boost\include\boost/date_time/time_facet.hpp(247) :while compiling class template member function 'boost::date_time::time_facet<time_type,CharT>::time_facet(size_t)'
with
[
time_type=boost::posix_time::ptime,
CharT=char
]
尽管我没有使用任何 time_facet 对象。
当我尝试使用 time_facet 对象执行此操作时,我添加了
sstream.imbue(std::locale(sstream.getloc(), new boost::date_time::time_facet("%Y-%m-%d %H:%M:%S")));
在将时间插入字符串流之前。错误在于它需要本文顶部提到的参数列表。
在 boost 中是否有一个函数与 boost::posix_time::time_from_string() 相反?如果没有,任何其他帮助将不胜感激。谢谢。
【问题讨论】: