
-----Original Message----- From: Robert Elz [mailto:kre@munnari.OZ.AU] Sent: Tuesday, July 27, 2004 5:24 AM To: Paul Eggert Cc: Olson, Arthur David (NIH/NCI); Tz Subject: Re: asctime.c Date: Tue, 27 Jul 2004 00:37:12 -0700 From: Paul Eggert <eggert@CS.UCLA.EDU> Message-ID: <87zn5lew9j.fsf@penguin.cs.ucla.edu> None of my messages (these days) ever make it to the list - I suspect some absurd anti-spam "protection" killing all of my mail. But never mind that for now. | /* | ** The format used in the (2004) standard is | ** "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n" | ** Use "%02d", as it is a bit more portable than "%.2d". | */ The most postable format is %02.2d - that works on every printf I've ever seen that has any method to make a 2 column zero-filled numeric field. Nothing else works on everything (the %02d version is original Research unix/BSD variants, the %.2d version is original Sys III type stuff I think - for a 2 character field, it probably doesn't matter, except possibly for the value 0 (where a system that likes %.2d might produce just " 0" if given %02d - the "0" in the format (SysIII variant) requires a leading 0 in the result, or something like that, so for 1, you should get 01, but for 0 ...) | result = snprintf(buf, STANDARD_BUFFER_SIZE, | "%.3s %.3s%3d %02d:%02d:%02d %ld\n", Please make that %4ld - it never mattered before, because the year was always between 1900 and 21xx, so there were always 4 digits anyway. But now if you're going to allow for year 10, etc, that 4 digit field needs to be forced to be 4 characters wide. It really is supposed to be that, not just "the rest of the line". The \n should be in buf[25] and absolutely nowhere else (for correct historical compatibility). That is, doing buf[25] = '\0'; to the result of asctime() is the "time honoured" way of printing the result without the trailing \n (that or using "%.24s" which amounts to the same thing). kre

Olson, Arthur David (NIH/NCI) said:
for a 2 character field, it probably doesn't matter, except possibly for the value 0 (where a system that likes %.2d might produce just " 0" if given %02d - the "0" in the format (SysIII variant) requires a leading 0 in the result, or something like that, so for 1, you should get 01, but for 0 ...)
"%02d" means "minimum 2 characters, fill with zeroes" "%.2d" means "minimum 2 digits, no fill"; "%0.2d", "%02.2d", and "%2.2d" all mean the same. For values in the range 0 to 9, both generate "00" to "09". For values like -4, the former generates "-4" and the latter "-04". -- Clive D.W. Feather | Work: <clive@demon.net> | Tel: +44 20 8495 6138 Internet Expert | Home: <clive@davros.org> | Fax: +44 870 051 9937 Demon Internet | WWW: http://www.davros.org | Mobile: +44 7973 377646 Thus plc | |
participants (2)
-
Clive D.W. Feather
-
Olson, Arthur David (NIH/NCI)