我尝试使用 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 研究、编码、调试,但无法弄清楚。任何帮助将不胜感激。
我发现了许多示例,说明如何将boost :: beast :: multi_buffer的内容复制到字符串中,但是如何将字符串的内容分配给之前在类实例中创建的multi_buffer?
如果我在我的班级中有示例声明:
private:
boost::beast::websocket::stream<boost::asio::ip::tcp::socket> ws_;
boost::asio::strand<boost::asio::io_context::executor_type> strand_;
boost::beast::multi_buffer buffer_;
Run Code Online (Sandbox Code Playgroud)
如何在调用之前将字符串分配给buffer_:
ws_.async_write(buffer_.data(), boost::asio::bind_executor(strand_,
std::bind(&CNXG_session::on_write,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
Run Code Online (Sandbox Code Playgroud)