使用std :: get_time转换ANSI C的asctime()格式

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)

附加信息

  1. 我无法使用strptime因为musl libc中存在错误.
  2. 我试过设置一些时区但是:

它不需要设置tzname,timezone和daylight.

这里开始

  1. std :: get_time示例也失败了吗?

Max*_*kin 5

看起来%d格式需要两个数字的月份,与以下描述相反std::get_time:

d:将月中的日期解析为十进制数(范围[01,31]),允许但不是必需的前导零.

这是GNU C++标准库中报告的错误:set :: get_time()需要%H和朋友的前导0.