S.R*_*S.R 0 c++ datetime c++11
我想将ANSI C'格式的字符串转换为std::tm对象.
例
Sun Nov 6 08:49:37 1994; ANSI C的asctime()格式
这里更多的信息在这里.
#include <string>
#include <ctime>
#include <iomanip>
#include <locale>
bool parseAscTimeDate(std::tm& tm, const std::string& str) {
std::istringstream ss(str);
//ss.imbue(std::locale("en_US.UTF8")); //try timezone?
ss >> std::get_time(&tm, "%a %b %d %H:%M:%S %Y");
return !ss.fail();
}
Run Code Online (Sandbox Code Playgroud)
那为什么会失败?
/* ANSI C's asctime format */
std::tm tm;
if (parseAscTimeDate(tm, "Sun Nov 6 08:49:37 1994"))
std::cout << "Ok!" << std::endl;
else
std::cout << "Wrong!" << std::endl;
Run Code Online (Sandbox Code Playgroud)
strptime因为musl libc中存在错误. 它不需要设置tzname,timezone和daylight.
从这里开始
看起来%d格式需要两个数字的月份,与以下描述相反std::get_time:
d:将月中的日期解析为十进制数(范围[01,31]),允许但不是必需的前导零.
这是GNU C++标准库中报告的错误:set :: get_time()需要%H和朋友的前导0.
| 归档时间: |
|
| 查看次数: |
274 次 |
| 最近记录: |