This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Issue in time() function on 64bit machines.
- From: Vicky <austinium at gmail dot com>
- To: libc-help at sourceware dot org
- Date: Thu, 5 Mar 2009 09:46:08 +0530
- Subject: Issue in time() function on 64bit machines.
Hi,
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 2.6.18.2-34.
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>
#include<sys/time.h>
#include<time.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*/
iRetCode=stime(&temp);
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));
gettimeofday(&var,NULL);
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.
Thanks,
Vikram