[comp.unix.programmer] Unix and gmtime

ghenniga@nmsu.edu (Gary Hennigan) (12/20/90)

I'm fairly new to C programming and have run into a fairly difficult
problem, at least it seems difficult to me! I'm trying to get the
current time using the gmtime() and/or localtime() functions but what
I get is garbage. I know that my "tm" structure is being initialized
but it's being initialized to garbage, eg., I get a time of Dec. 31,
2002 or some such upon returning from the asctime() function.
	If anyone's ever used either of these functions on a UNIX
machine, or if you have any ideas as to what I'm doing wrong I would
greatly appreciate the assistance.
	Here's the short piece of code I'm working on:
----------------------------------------------------------------------
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

main()
{
   struct tm *clock;
   int i1=1, i2;
   char *tod;

   clock = gmtime();
   tod = asctime( clock );

   printf("%s\n", tod );
 
   return 1;
}
----------------------------------------------------------------------
	Again the structure "clock" is being initialized and asctime
is converting what it gets properly, it's just that what it gets is
incorrect.

Thanks in advance and please email if possible,
--
Gary Hennigan
+---------------------------------------------------------------------------+
+  e-mail: ghenniga@NMSU.Edu, henninsf@maxwel.NMSU.Edu                      +
+  Electrical Engineering; PhD Student, Computational Electromagnetics      +
+  Physical Science Laboratory (ASS)istant systems programmer               +
+---------------------------------------------------------------------------+

boyd@necisa.ho.necisa.oz.au (Boyd Roberts) (12/21/90)

In article <GHENNIGA.90Dec19124620@tesla.nmsu.edu> ghenniga@nmsu.edu (Gary Hennigan) writes:
>
>   clock = gmtime();

Bzzzt!  RTFM.

    struct tm	*gmtime(clock)
    long	*clock;

gmtime(3) expects an argument.  Didn't you use lint?


Boyd Roberts			boyd@necisa.ho.necisa.oz.au

``When the going gets wierd, the weird turn pro...''

brister@decwrl.dec.com (James Brister) (12/21/90)

On 19 Dec 90 19:46:20 GMT, ghenniga@nmsu.edu (Gary Hennigan) said:

> 	If anyone's ever used either of these functions on a UNIX
> machine, or if you have any ideas as to what I'm doing wrong I would
> greatly appreciate the assistance.

The gmtime function according to the FM requires a parameter--the time in a
long inteter. Here's a "correct" version

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

main()
{
   struct tm *clock;
   int i1=1, i2;
   long t ;
   char *tod;

   time (&t) ;
   clock = gmtime(&t);
   tod = asctime( clock );

   printf("%s\n", tod );
 
   return 1;
}


James
--
James Brister                                           brister@decwrl.dec.com
DEC Western Software Lab., Palo Alto, CA    {uunet,sun,pyramid}!decwrl!brister