asctime.c
Olson, Arthur David (NIH/NCI)
olsona at dc37a.nci.nih.gov
Tue Jul 27 19:34:57 UTC 2004
> But the C standard says that sometimes fewer bytes of output must be
generated (in particular, if the year is in the range -99 through 999)...
What I find at
http://www.opengroup.org/onlinepubs/009695399/functions/asctime.html
(you may have to register via
http://www.unix-systems.org/version3/iso_std.html to access the above, and
your URLage may vary) is this text:
The asctime() function shall convert the broken-down time in the
structure pointed to by timeptr into a string in the form:
Sun Sep 16 01:03:52 1973\n\0
using the equivalent of the following algorithm:
char *asctime(const struct tm *timeptr)
{
static char wday_name[7][3] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static char mon_name[12][3] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static char result[26];
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);
return result;
}
That last "%d" in the sprintf would indeed produce less than four digits of
year output in some cases.
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).
Sidebar: I believe kre is proposing padding with spaces rather than zeroes.
--ado
More information about the tz
mailing list