c/c++ strptime() does not parse %Z Timezone name

会有一股神秘感。 提交于 2019-12-05 10:29:46

I'm not sure what you're doing will work. The glibc source code at github has this to say on the matter:

case 'Z':
    /* XXX How to handle this? */
    break

followed by some slightly more "meaty" handling of the lowercase 'z' stuff :-)

So what's most likely happening here is that the string pointer isn't advancing past the EET when the format string is %Z, so that when it tries to process %Y, it complains, rightly so, that EET is not a valid year. This is confirmed by the simple case of "%%" where the code actually does advance the input string pointer rp:

case '%':
    /* Match the `%' character itself.  */
    match_char ('%', *rp++);
    break;

The Linux man page also states on the extensions (of which 'Z' is one):

For reasons of symmetry, glibc tries to support for strptime() the same format characters as for strftime(3). (In most cases the corresponding fields are parsed, but no field in tm is changed.)

In addition the GNU docs state (my italics):

%Z: The timezone name. Note: Currently, this is not fully implemented. The format is recognized, input is consumed but no field in tm is set.

So I actually consider this a bug, albeit one that can be easily fixed with a documentation change to stop pretending it can handle Z-type timezones.

I cannot find any relevant bug in bugzilla so I've raised a bug on glibc there. You can track it here.


Addendum: as per the bug report link in the previous paragraph and the glibc 2.19 release notice, the change I suggested has been made to align the code with the documentation. Hopefully, there's no bugs in it or I'm going to look pretty silly given it's only five lines of code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!