我正在尝试使用 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/