[musl] strptime("UTC", "%Z", &tm) in musl

Haelwenn (lanodan) Monnier contact@hacktivis.me
Sun Apr 6 20:55:46 GMT 2025


[2025-04-06 21:57:35+0200] Alejandro Colomar:
>I was trying to develop a function that parses a UTC date in the shadow
>project, and I came up with this:
><https://github.com/shadow-maint/shadow/pull/1217/>
>
>	$ grepc -htfd get_date .
>	static long
>	get_date(const char *s)
>	{
>		struct tm   tm;
>		const char  *p;
>
>		bzero(&tm, sizeof(tm));
>
>		p = strptime("UTC", "%Z", &tm);
>		if (p == NULL || !streq(p, ""))
>			return -1;

That bzero seems like a bit of a bad idea, `struct tm` can have some extra
fields where 0 isn't the correct value.
Quite like how `tm_isdst` uses -1 for unknown state, 0 for no-DST, 1 for DST.

Could probably use `if(!gmtime_r(0, &tm)) { /* handle err */ }` instead
which also has the nice effect of initializing tm for UTC,
allowing to skip the problematic strptime %Z.

Best regards


More information about the Libc-alpha mailing list