Issue in time() function on 64bit machines.

Thu Mar 5 04:16:00 GMT 2009


I am having issues with time_t time (time_t *result) function
returning one second less than the actual system time. Oddly enough
this only happens on 64bit systems.

The machine specs. are, Target: x86_64-
suse-linux, host=x86_64-suse-linux, glibc-2.5-25, kernel

I have also tried this on a 64 bit Red Hat machine, with the same results.

I have tried using int stime (time_t *newtime) and int settimeofday
(const struct timeval *tp, const struct timezone *tzp) to set the
time, the system time comes out fine when extracted using int
gettimeofday (struct timeval *tp, struct timezone *tzp), however, when
I extract time using time_t time (time_t *result), it always comes up
one second short of the actual system time(on a 64 bit machine only).
While I tried this on a 32 bit machine, time() function returned the
correct value.

Here is the code:

#include <stdio.h>

int main()
   struct timespec tsTime_To_Set;
   time_t temptime, temp;
   int iRetCode = 0;
   struct timeval tsTime_To_Set1,var;

   temp = 1262284200;

   tsTime_To_Set.tv_sec = temp;
   tsTime_To_Set.tv_nsec = 0;

   tsTime_To_Set1.tv_sec = temp;
   tsTime_To_Set1.tv_usec = 0;

   /* iRetCode = settimeofday(&tsTime_To_Set1, NULL);*/ /*I even used this*/
   if (iRetCode == 0)
      printf("\nclock_settime() returned success :%d",iRetCode);

   temptime = time(0);

   printf("\nValue extraced using time(0) =%s", ctime(&temptime));

   printf("Seconds value decoded returns this time\n");
   printf("%s", ctime(&temp));

   printf("Value extracted using gettimeofday: %s\n",ctime(&var.tv_sec));

   return 0;

I confused as to why the same code works differently on 32 and 64bit
machines? Could this be an issue with the time_t time (time_t *result)
function? Is there a way to fix this problem while still using time_t
time (time_t *result) function.


More information about the Libc-help mailing list