time(2 or 3)

chris at umcp-cs.UUCP chris at umcp-cs.UUCP
Mon Jun 16 05:05:04 AEST 1986


In article <798 at isis.UUCP> jay at isis.UUCP (Jay Batson) writes:
[approximately]
>#include <time.h>
>
>some_fcn(params)
>sometype params;
>{
>	long somevar1;
>	struct tm *somevar2, *localtime();
>
>	/* ok, first get ahold of the number of seconds since Jan 1, 1970 */
>	somevar1 = time(0);

This code is wrong!  It is missing two things, both of which are
non-fatal on Vaxen and Suns and probably 70% of the machines out
there.  Specifically, time() is a function returning long, taking
a pointer to long; or a function returning time_t, taking a pointer
to time_t (just which depends on your Unix variant).  Some systems
do not define time() in <time.h>.  Change to:

	struct tm *somevar2, *localtime();
	long time();

	somevar1 = time((long *) 0);

or add `#include <sys/types.h>' and use `time_t' in place of `long'
above.

>	somevar2 = localtime(&somevar1); /* note &somevar1 passes a pointer */
>	
>	printf("The current time is: %2d:%2d:%2d (on a 24 hour clock)\n",
>		somevar2->tm_hour, somevar2->tm_min, somevar2->tm_sec);
>} /* end some_fcn */

Just for prettiness, the minute and second conversion formats should
be `%02d'.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list