[tz] [PROPOSED 2/2] strftime: conform better to POSIX+

Robert Elz kre at munnari.OZ.AU
Sat Aug 8 21:17:35 UTC 2020

    Date:        Sat,  8 Aug 2020 12:29:23 -0700
    From:        Paul Eggert <eggert at cs.ucla.edu>
    Message-ID:  <20200808192923.23487-2-eggert at cs.ucla.edu>

  | a reliable way
  | to determine whether 0 represents failure or buffer exhaustion

This is not necessary.

The cases can be distinguished by setting errno to 0 before the call,
and checking afterwards (if 0 is returned) - if errno is set there was
an error, if it is still 0, it is something else.

A 0 return can indicate a successful return with no data, not
just conversion failure or buffer exhaustion (eg:
	 strftime(buf, sizeof buf, "%Z", tm);
in a timezone where there are no timezone names).

This is easy to avoid by simply using

	res = strftime(buf, sizeof buf, " %Z", tm);

where any successful return must be at least 1, so 0 always
indicates some kind of error (including when sizeof(buf) == 0,
though that's not a case worth worrying about).

Then the code simply uses buf+1 as the result (and res-1 as the
length) after it has been determined that there was no error
(ie: res >= 1).


More information about the tz mailing list