- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在解决网站上的问题。我的代码如下:
string timeConversion(string s) {
std::tm t = {};
string result = "";
std::istringstream ss(s);
ss >> std::get_time(&t, "%I:%M:%S%p");
if (ss.fail()) {
std::ostringstream oss;
oss << std::put_time(&t, "%H:%M:%S") << endl;
result = oss.str();
} else {
result = "Parse failed";
}
return result;
}
当我输入 07:05:45PM 时,它返回 07:05:45。但我预计 19:05:45。怎么了?
更新
“if”语句的代码是错误的。一定是
string timeConversion(string s) {
std::tm t = {};
string result = "";
std::istringstream ss(s);
ss >> std::get_time(&t, "%I:%M:%S%p");
if (ss.fail()) {
result = "Parse failed";
} else {
std::ostringstream oss;
oss << std::put_time(&t, "%H:%M:%S") << endl;
result = oss.str();
}
return result;
}
代码返回 "Parse failed" for "07:05:45PM" (g++ 5.4.0) 这也是出乎意料的。
最佳答案
这是 gcc 中的错误 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71367
大胆强调是我的。
Ed Beroset 2016-05-31 21:17:27 UTC
Created attachment 38616 [details]Short program demonstrating lack of %r support in std::time_get
The "%r" and "%p" formats don't seem to be supported yet for time_get, even though they are both supported by time_put. The %p is supposed to be the locale's version of "AM" and "PM" (if any) and %r is equivalent to %I:%M:%S %p in the POSIX locale.
最后一条评论是这样说的:
Jakub Jelinek 2021-04-27 11:37:54 UTC
GCC 11.1 has been released, retargeting bugs to GCC 11.2.
关于c++ - std::get_time 有错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53110175/
我在尝试使用 put_time 和 get_time 函数时遇到了一些问题。 我拿了这段代码: #include #include #include #include int main() {
我正在解决网站上的问题。我的代码如下: string timeConversion(string s) { std::tm t = {}; string result = "";
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
如果我尝试使用 std::get_time 在 tm 中设置日期什么也没有发生,但是输入流处于失败状态,这意味着解析错误已经发生了。 下面的代码有什么问题? { // setting time w
我正在使用以下代码将字符串流解析为 tm 结构: std::tm tm; std::stringstream ss("Jan 9 2014 12:35:34"); ss >> std::get_tim
我正在尝试解析日期时间字符串并将结果放入 std::tm 结构中。下面是代码, #include #include #include #include std::stringstream ss
我在这个例子中花了很多时间,每次我得到错误 Unable to read cin with an ios_base::iostate equal to failbit 来自这段代码: #include
我有这个简单的功能: char* get_time() { char *buffer = malloc(sizeof(char)*10); /* HOW TO FREE IT ? */
使用 std::get_time 解析字符串是否需要分隔符?我找不到一个引用来说明它是。我正在尝试解析 ISO 日期/时间字符串,例如“20140105T123456”——例如: 例如, #inclu
我想弄清楚为什么我不能在 C++11 中将 std::get_time() 与 %F 格式说明符一起使用。它在 clang 3.8.0 或 gcc 5.4.0 中对我不起作用。我的印象是 strfti
我的类中有以下 C++ 代码,用于将 ISO 8601 字符串转换为 time_t 结构: #include #include #include #include #include ....
我正在尝试这样做但失败了: std::istringstream ss("1212"); ss >> std::get_time(&t, "%y%m"); if (ss.fail()) // ever
我正在使用 Visual Studio 2015 在 Windows 上执行以下代码。基本上我使用 std::get_time 来解析日期,但是当日期无效时,例如,一天大于31,它似乎没有在流上设置失
我正在尝试在 visual studio 2012 中使用 std::get_time 解析日期和时间字符串。时间字符串似乎可以正确解析,但不能正确解析日期字符串。我想最终将日期字符串转换为整数以进行
在 locale("en_US") 中,我无法通过 std::time_get::get_time() 读取时间。我尝试了以下格式,但它不起作用。当我将区域设置更改为 locale("en_GB")
我正在尝试按时间顺序对照片进行排序。因此,我从 EXIF 数据中将时间提取为字符串,然后将其转换为 std::time_t。但是我有时会得到不正确的结果。我已将问题简化为这个最小的例子。它具有三个时间
我今天正在处理一些时间函数,并注意到使用 %r(或 %p)的标准转换似乎不适用于通过 的输入g++ 或 clang++ 上的 std::get_time()。见 this live code vers
我正在尝试解析格式为 YYMMDD 的日期。作为测试,我尝试了以下代码: #include #include #include #include int main(){ std::tm
我想将 ANSI C 格式的字符串转换为 std::tm 对象。 例子 Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format 更多信息在这里 here
我正在尝试创建简单的 jni,但应用程序崩溃了。我无法找到我所犯的错误。这是我的代码: 示例.c #include double My_variable = 3.0; int fact(int
我是一名优秀的程序员,十分优秀!