asctime.c

Robert Elz kre at munnari.OZ.AU
Tue Jul 27 09:23:42 UTC 2004


    Date:        Tue, 27 Jul 2004 00:37:12 -0700
    From:        Paul Eggert <eggert at CS.UCLA.EDU>
    Message-ID:  <87zn5lew9j.fsf at 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




More information about the tz mailing list