use*_*053 2 c++ boost boost-date-time
我尝试使用 C++ Boost date_time 库将格式为“16:43 December 12, 2012”的字符串加载到日期输入方面为“%H:%M %B %d, %Y”的字符串流中。接下来,我想从 stringstream 创建一个 Boost ptime 对象,以便我可以进行日期/时间数学运算。我无法让它工作 - 下面是代码:
std::string autoMatchTimeStr(row["message_time"]);
ptime autoMatchTime(time_from_string(autoMatchTimeStr));
date_input_facet* fin = new date_input_facet("%H:%M %B %d, %Y");
stringstream dtss;
dtss.imbue(std::locale(std::locale::classic(), fin));
dtss << msg.getDate(); //msg.getDate() returns “16:43 December 12, 2012”
ptime autoMatchReplyTime;
dtss >> autoMatchReplyTime;
if( autoMatchReplyTime < autoMatchTime + minutes(15)) {
stats[ "RespTimeLow" ] = "increment";
sysLog << "RespTimeLow" << flush;
}
Run Code Online (Sandbox Code Playgroud)
autoMatchTime 包含有效的日期/时间值,但 autoMatchReplyTime 不包含。我想了解这应该如何工作,但是如果我必须使用 C strptime 来初始化 ptime 构造函数的 struct tm ,我可以这样做。我花了很多时间用 gdb 研究、编码、调试,但无法弄清楚。任何帮助将不胜感激。
所以......你为什么要尝试使用date_input_facet而不是time_input_facet?以下示例工作正常。
#include <sstream>
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
const std::string time = "16:43 December 12, 2012";
boost::posix_time::time_input_facet* facet =
new boost::posix_time::time_input_facet("%H:%M %B %d, %Y");
std::stringstream ss;
ss.imbue(std::locale(std::locale(), facet));
ss << time;
boost::posix_time::ptime pt;
ss >> pt;
std::cout << pt << std::endl;
}
Run Code Online (Sandbox Code Playgroud)