Edw*_*ard 13 c++ time c++11 c++14
我今天正在使用一些时间函数,并注意到使用%r(或%p)的标准转换似乎不适用于通过std::get_time()g ++或clang ++ 进行输入.请参阅此 g ++和clang ++的实时代码版本.这确实似乎窗户用VC下按预期运行++(见密切相关的问题).另请注意,无论是否imbue包含该行,效果都相同.我的Linux机器上的语言环境设置为"en_US.UTF-8"重要.
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <ctime>
int main(){
std::tm t{};
std::stringstream ss{"1:23:45 PM"};
ss.imbue(std::locale(std::cin.getloc(),
new std::time_get_byname<char>("en_US")));
ss >> std::get_time(&t, "%r");
if (ss.fail()) {
std::cout << "Conversion failed\n" << std::put_time(&t, "%r") << '\n';
} else {
std::cout << std::put_time(&t, "%r") << '\n';
}
}
Run Code Online (Sandbox Code Playgroud)