[comp.lang.c] 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               +
+---------------------------------------------------------------------------+

stanley@phoenix.com (John Stanley) (12/20/90)

ghenniga@nmsu.edu (Gary Hennigan) writes:

> 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.

   Because you are passing garbage to gmtime().

> ----------------------------------------------------------------------
> #include <stdio.h>
> #include <time.h>
> #include <sys/time.h>
> 
> main()
> {
>    struct tm *clock;
>    int i1=1, i2;
>    char *tod;
	 time_t mytime;

     time(&mytime);
     clock = gmtime( &mytime );
>    tod = asctime( clock );
> 
>    printf("%s\n", tod );
>  
>    return 1;
> }
> ----------------------------------------------------------------------

   I don't have access to the UNIX man pages for gmtime, but I can't 
believe they don't show a required parameter. Wouldn't it be nice if
all compilers would use prototypes. My TC 2.0 refused to compile the
original code, telling me that gmtime was missing required parameters.