如何在c++中从字符串解析日期/时间
How to parse date/time from a string in C++
如何将字符串与模板"%d.%m.%Y %H:%M:%S"
(例如,28.05.2013 17:42:00)转换为boost::posix_time::ptime
或类似的东西?
下面是我的非工作代码。
boost::posix_time::ptime stringToDateTime(const std::string &text) {
const std::locale fct
( std::locale::classic()
, new boost::gregorian::date_input_facet("%d.%m.%Y %H:%M:%S")
);
std::istringstream is(text);
is.imbue(fct);
boost::posix_time::ptime date;
is >> date;
return date;
}
有一种难看的方式我不接受
boost::posix_time::ptime stringToDateTime(const std::string &text) {
return boost::posix_time::time_from_string
( text.substr(6, 4) // year
+ "-"
+ text.substr(3, 2) // month
+ "-"
+ text.substr(0, 2) // day
+ text.substr(10, 9) // hour,min,sec
);
}
boost::posix_time::ptime stringToDateTime(const std::string &text) {
const std::locale fct
( std::locale::classic()
, new boost::posix_time::time_input_facet("%d.%m.%Y %H:%M:%S")
); //^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - that was an error
std::istringstream is(text);
is.imbue(fct);
boost::posix_time::ptime date;
is >> date;
return date;
}
相关文章:
- 使用 C++将时间戳转换为格式化的日期时间
- C++代码的 C# 日期时间等效项是什么?
- 如何在提升日期时间中创建日期范围?
- 在 c++ 中将 ISO 日期时间转换为本地日期时间并提取时间
- Rcpp/C++/R:比较日期时间矢量与日期时间时出错(错误:"运算符>"的不明确重载)
- 如何将毫秒的日期时间字符串转换为 UNIX 时间戳
- 扩展中的日期时间对象方法C++问题
- 将DateTime字符串分为C 的日期时间
- C++以微秒为单位解析日期/时间
- 仅将纪元字符串保存到 mysql 日期时间年/月/日,但没有小时分钟秒
- 使用提升解析日期时间字符串:采用个位数小时格式
- C++ Poco SQL 查询不返回具有'between'和'and'日期时间绑定的结果
- 快速将带有时区的日期时间字符串转换为 UNIX 时间戳(C++)
- 从C 的时间戳提取日期时间戳
- 标准::get_time 中的日期时间格式
- 生成包含分数和时区的完整 iso 日期时间表示字符串
- 通过 ODBC C++将带有日期时间的记录插入 SQL Server 2014 的问题
- 在 C++20 中获取当前日期/时间是否线程安全?
- mongocxx:插入日期时间
- Poco 日期时间格式化程序 - 使用当前时区打印时间戳