Pergunta

I'm trying to parse a string into a struct tm using strptime. Looks easy enough, but it is not filling in the year correctly (yes, I know about the 1900 offset) - after the call, the year is set to 0. Other fields look ok. See below:

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);

After the strptime call, the tm struct is as follows - note that tm_year=0, where I am expecting it to be 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}

I'm sure I'm missing something simple - can anyone point it out to me?

Foi útil?

Solução

On my system (Mac OS X), man strptime has this note about %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.

And indeed, if I change your "UTC" to "GMT", the code suddenly works. Note that Python for whatever reason is smarter here, and supports "UTC" as well.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top