asctime.c

Clive D.W. Feather clive at demon.net
Wed Jul 28 04:54:00 UTC 2004


Olson, Arthur David (NIH/NCI) said:
> What I find at
> 	http://www.opengroup.org/onlinepubs/009695399/functions/asctime.html

matches the C Standard.

> 	    sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
> 		wday_name[timeptr->tm_wday],
> 		mon_name[timeptr->tm_mon],
> 		timeptr->tm_mday, timeptr->tm_hour,
> 		timeptr->tm_min, timeptr->tm_sec,
> 		1900 + timeptr->tm_year);

Analysing this, we get:
* tm_wday must be in the range 0 to 6, and 3 characters are output
  - if it's outside this range, the behaviour is undefined
* a space is output
* tm_mon must be in the range 0 to 11, and 3 characters are output
  - if it's outside this range, the behaviour is undefined
* tm_mday is output:
  - if it is in the range 0 to 9, two spaces preceed it
  - else if it is in the range -9 to 99, one space preceeds it
  - otherwise it is output unchanged
* a space is output
* tm_hour, tm_min, and tm_sec are output, separated by a colon
  - for each, at least two digits are to appear (-1 appears as "-01")
* a space is output
* 1900+tm_year is output using the minimum number of characters possible.
* a newline is output

If the result involves outputting more than 25 characters, the behaviour is
undefined. If it only involves 25, the output *MUST* take that form.

> That last "%d" in the sprintf would indeed produce less than four digits of
> year output in some cases.

Right. Consider the case where tm_year = -1200 and tm_hour = 999.

    "Sun Jan 12 999:01:01 700"

is *required* as the result.

> Of course we might also take the above as meaning that it's mandatory for
> asctime to overflow a buffer (and potentially dump core) for other cases
> (take tm_year >= 10000, please).

It's mandatory to overflow the buffer, at which point the code can do
whatever it likes because C and POSIX don't mandate any behaviour.

-- 
Clive D.W. Feather  | Work:  <clive at demon.net>   | Tel:    +44 20 8495 6138
Internet Expert     | Home:  <clive at davros.org>  | Fax:    +44 870 051 9937
Demon Internet      | WWW: http://www.davros.org | Mobile: +44 7973 377646
Thus plc            |                            |



More information about the tz mailing list