C++ strptime 未填写年份

标签 c++ strptime

我正在尝试使用 strptime 将字符串解析为 struct tm。看起来很简单,但它没有正确填写年份(是的,我知道 1900 偏移量) - 调用后,年份设置为 0。其他字段看起来没问题。见下文:

struct tm tm;
memset(&tm, 0, sizeof(struct tm));
strptime("Fri Jan 17 09:44:33 UTC 2014", "%a %b %d %H:%M:%S %Z %Y", &tm);

在 strptime 调用之后,tm 结构如下 - 请注意 tm_year=0,我预计它是 114:

{tm_sec = 33, tm_min = 44, tm_hour = 9, tm_mday = 17, tm_mon = 0, tm_year = 0,
 tm_wday = 5, tm_yday = 0, tm_isdst = 0, tm_gmtoff = 0, tm_zone = 0x0}

我确信我错过了一些简单的东西 - 有人能向我指出吗?

最佳答案

在我的系统 (Mac OS X) 上,man strptime 有关于 %Z 的注释:

The %Z format specifier only accepts time zone abbreviations of the local time zone, or the value "GMT". This limitation is because of ambiguity due to of the over loading of time zone abbreviations. One such example is EST which is both Eastern Standard Time and Eastern Australia Summer Time.

事实上,如果我将你的“UTC”更改为“GMT”,代码就会突然起作用。请注意,无论出于何种原因,Python 在这里都更聪明,并且也支持“UTC”。

关于C++ strptime 未填写年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21187314/

相关文章:

python - ISO 时间到 python 中的人类可读时间

c++ - 函数模板中的 is_const<func-param> 总是返回 false

android - Qt Android : Why is a QtApp-debug. apk 为发布版本创建?

c++ - 非常快速的序列化。随机访问 C++

c - 如何在 C 中的 strptime 中设置当前年份

python - 时间戳问题: Python doesn't recognize Timestamp

python - 在 Python 中将解析时间与今天的日期结合起来

c++ - 将指针转换为模板参数 : Comeau & MSVC compile, GCC 失败

c++ - 如何使 Clang 静态分析器从命令行输出其工作?

r - 如何解析毫秒?