[net.lang.c] time

zingman@lll-lcc.UUcp (Jonathan Zingman) (06/10/86)

  I am trying to use the time function described in my UNIX programmers
  manual.  It is referenced in localtime(2) as time(2) as well as
  time(3).  Unfortunately, it doesn't appear in the description of
  either of those libraries.  W
Posted: Mon Jun  9 17:02:44 1986


  Jon Zingman
  L-23 LLNL

  zingman@lll-lcc.arpa

zingman@lll-lcc.UUcp (Jonathan Zingman) (06/10/86)

My last message got somewhat garbled, so I'll try again.  I want to use the
routine localtime, which breaks down a pointer "such as returned by 
time(2)."  Later in the same section of the manual it says "SEE ALSO
time(3)"  I can't find time in either of those sections, and my guesses
at how to call it so far have not worked.  Any help would
be appreciated

Jon Zingman
L-23 Livernmore National Lab
Livermore, Ca. 94550

zingman@lll-lcc.arpa

jay@isis.UUCP (Jay Batson) (06/11/86)

In article <337@lll-lcc.UUcp> zingman@lll-lcc.UUcp (Jonathan Zingman) writes:
> ... I want to use the
>routine localtime, which breaks down a pointer "such as returned by 
>time(2)."  Later in the same section of the manual it says "SEE ALSO
 ^^^^^^^  I would hope you can find it - I can't imagine you'd have
	  ctime(3) and _not_ have time(2).  look again, or in you online man.
>time(3)"  I can't find time in either of those sections, and my guesses
 ^^^^^^^  my manual doesn't say this, but then again, I'm still on v7...
>at how to call it so far have not worked....

Easy.

#include <time.h>

some_fcn(params)
sometype params;
{
	long somevar1;
	struct tm *somevar2;
	/* or... */
	struct tm *somevar2, *localtime();
	/* depending on whether or not your <time.h> already defines
	   localtime or not... */

	/* ok, first get ahold of the number of seconds since Jan 1, 1970 */
	somevar1 = time(0);

	/* ok, now pass localtime a pointer to somevar1.  localtime will
	   will go about declaring the structure it will use, and fill the
	   structure up with appropriate info - we just want to expect to
	   be returned a pointer to what localtime comes up with */
	somevar2 = localtime(&somevar1); /* note &somevar1 passes a pointer */
     /* ^^^ pointer recv'g what retd */	 /* to what we got from time(2)     */

	/* ok, now to get at what localtime has prepared for us, here
	   is an example */
	
	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 */

See?  Easy.
--------

"OK, so now, after it gets dark Lancelot and I will jump out of the rabbit, and
 take the castle by supr........ oh."

Jay Batson
{seismo,hplabs}!hao!isis!jay

chris@umcp-cs.UUCP (06/15/86)

In article <798@isis.UUCP> jay@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@umcp-cs		ARPA:	chris@mimsy.umd.edu

mouse@mcgill-vision.UUCP (der Mouse) (06/16/86)

In article <798@isis.UUCP>, jay@isis.UUCP (Jay Batson) writes:
> In article <337@lll-lcc.UUcp> zingman@lll-lcc.UUcp (Jonathan Zingman) writes:
>> [stuff about time(2) and time(3)]
> Easy.
> #include <time.h>
...
> {
> 	long somevar1;
...
> 	/* ok, first get ahold of the number of seconds since Jan 1, 1970 */
> 	somevar1 = time(0);
...
> } /* end some_fcn */

     Not quite that simple.

(1) Does time.h declare time() to return long?  Make sure it does, or
declare it yourself.

(2) The call to time() is wrong.  It should be time((long *)0), unless
of course you have an ANSI-conforming compiler and time.h gives a
prototype instead of a bare declaration (how many people do you think
are so lucky?  Not many.).
-- 
					der Mouse

USA: {ihnp4,decvax,akgua,utzoo,etc}!utcsri!mcgill-vision!mouse
     philabs!micomvax!musocs!mcgill-vision!mouse
Europe: mcvax!decvax!utcsri!mcgill-vision!mouse
        mcvax!seismo!cmcl2!philabs!micomvax!musocs!mcgill-vision!mouse
ARPAnet: utcsri!mcgill-vision!mouse@uw-beaver.arpa

"Come with me a few minutes, mortal, and we shall talk."

jay@isis.UUCP (Jay Batson) (06/17/86)

In article <2021@umcp-cs.UUCP> chris@maryland.UUCP (Chris Torek) shows
how in article <798@isis.UUCP> jay@isis.UUCP (Jay Batson) I was wrong
with the following statement (among other comments):
>>	/* ok, first get ahold of the number of seconds since Jan 1, 1970 */
>>	somevar1 = time(0);
>
>       Specifically, time() is a function returning long, taking
>a _pointer_ to long...;
>Change to:
>	...
>	somevar1 = time((long *) 0);

Yes, I do feel like a total idiot, and do feel soundly trounced.  Of course
Chris is right;  I just ':r some_prog_of_mine' that _worked_(!), and failed to
exercise the cardinal rule - don't give advice until you _verify_ what
you are saying with the _newest_ manual.  Henceforth, I vow to get "A's" instead
of "B's" in class, and never make another mistake again.  Ever.  In my life.
And then some.  (You get the point).

Of course, it helps my case a bit that _my_ manual (albiet an old V7)
has the following in time(2):  (honest)

	long time(0)          <<<< this is what I recommended.

	long time(tloc)       <<<< this is what Chris recommended
	long *tloc;

	#... (more stuff)


Thanks for clearing up the "error" Chris.

--------

"OK, so now, after it gets dark Lancelot and I will jump out of the rabbit, and
 take the castle by supr........ oh."

Jay Batson
{seismo,hplabs}!hao!isis!jay

guy@sun.UUCP (06/19/86)

> In article <2021@umcp-cs.UUCP> chris@maryland.UUCP (Chris Torek) shows
> >>	somevar1 = time(0);
> >
> >       Specifically, time() is a function returning long, taking
> >a _pointer_ to long...;
> >Change to:
> >	...
> >	somevar1 = time((long *) 0);
> 
> Of course Chris is right;  I just ':r some_prog_of_mine' that _worked_(!)...

More precisely, that happened to work on the machine you built it on.  "This
program works" (which almost always means "this program works on some subset
of the machines on which it could conceivably work) is not equivalent to
"this program is correct".  There are *lots* of machines on which "somevar =
time(0);" will *not* work, and such machines have perfectly legal C
implementations.  Those of you on VAXes, Suns, Pyramids, 3Bs, etc. may not
think it matters to write type-correct code, since you can get away with not
doing so; however, you'll regret it the day somebody wants your program
ported to, say, a PC using the large memory model, or a small 68000-based
machine using 16-bit "int"s.

Chris' complaint was perfectly legitimate, as would "lint"s complaint about
the same statement (you *do* run "lint", of course).

> Of course, it helps my case a bit that _my_ manual (albiet an old V7)
> has the following in time(2):  (honest)
> 
> 	long time(0)          <<<< this is what I recommended.

Yes, the V7 manual is old, and you could get away with that sort of thing on
the PDP-11 where V7 was first done.
-- 
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com (or guy@sun.arpa)