Questions about mktime() - and answer

Karl Heuer karl at ima.isc.com
Thu Feb 14 14:24:09 AEST 1991


In article <1991Feb8.062954.13509 at druid.uucp> darcy at druid.uucp (D'Arcy J.M. Cain) writes:
>time_t	mktime(struct tm *tp) { ...
>    *tp = *localtime(&t);		/* set other fields in structure */
>    return(t);
>}

Sorry, this violates the Standard.  "The implementation shall behave as if
no other library function calls [localtime()]." [4.12.3].  In particular,
the user has the right to assume that the static structure maintained by
localtime() is not destroyed by a call to mktime().

Fix: move the guts of localtime() into void _do_time(time_t, struct tm *),
reimplement localtime(time_t *pt) as _do_time(*pt, &statictm), and change
"*tp = *localtime(&t);" in mktime() to "_do_time(t, tp)".

Karl W. Z. Heuer (karl at ima.isc.com or uunet!ima!karl), The Walking Lint



More information about the Comp.std.c mailing list