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

Alejandro Colomar alx@kernel.org
Sun Apr 6 20:05:15 GMT 2025


[Add libc-alpha@]

On Sun, Apr 06, 2025 at 09:57:39PM +0200, Alejandro Colomar wrote:
> Hi Rich,
> 
> 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;
> 
> 		p = strptime(s, "%F", &tm);
> 		if (p == NULL || !streq(p, ""))
> 			return -1;
> 
> 		return dategm(&tm);
> 	}
> 
> Which works fine in glibc, but fails in musl.

Actually, I've now read the glibc sources, and it seems even worse in
glibc.  glibc ignores the parsed timezone at all.
So, both glibc and musl have a bug here, I think.

Is there any way to parse a UTC date?  That is, parse a "YYYY-MM-DD"
date that is not in local time, but in UTC time.

Have a lovely night!
Alex

> 
> 	$ grepc -tfd strptime . | sed -n '/case .Z.:/,/break/p'
> 		case 'Z':
> 			if (!strncmp(s, tzname[0], len = strlen(tzname[0]))) {
> 				tm->tm_isdst = 0;
> 				s += len;
> 			} else if (!strncmp(s, tzname[1], len=strlen(tzname[1]))) {
> 				tm->tm_isdst = 1;
> 				s += len;
> 			} else {
> 				/* FIXME: is this supposed to be an error? */
> 				while ((*s|32)-'a' <= 'z'-'a') s++;
> 			}
> 			break;
> 
> It seems "%Z" only accepts the timezone specified in the current locale
> (via tzname), but doesn't accept parsing a different timezone (in this
> case, I want specifically UTC, regardless of the locale).  I'd say this
> is a bug, and an important limitation, BTW.
> 
> Is there any way I can workaround this for musl?
> 
> 
> Have a lovely day!
> Alex
> 
> -- 
> <https://www.alejandro-colomar.es/>
> <https://www.alejandro-colomar.es:8443/>
> <http://www.alejandro-colomar.es:8080/>



-- 
<https://www.alejandro-colomar.es/>
<https://www.alejandro-colomar.es:8443/>
<http://www.alejandro-colomar.es:8080/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20250406/9394ae2e/attachment.sig>


More information about the Libc-alpha mailing list