About timeval

Hamish Moffatt hamish at cloud.net.au
Wed Jul 18 13:39:20 UTC 2007


On Fri, Jul 13, 2007 at 04:49:29PM -0400, Roberto Riggio wrote:
> Hi,
> gettimeofday(&t1, (struct timezone *)0);
> 
> ...
> 
> t2.tv_sec = t1.tv_sec + delta;
> t2.tv_usec = t1.tv_usec + ((unsigned long)(delta*1000000) % 
> 1000000);
> 
> printf("t2 set to: %s\n",tvtoa(endtime));
> 
> The output of printf is always wrong. SOmetime it is a time before 
> t1,
> other times it is after.

Suggestions;

1. You printf endtime, not t2. You didn't say how endtime was
calculated.

2. (delta * 10000000) % 1000000 appears to be 0 always. Unless delta
happens to be a float; you didn't say.

3. (delta * 1000000) might overflow an unsigned long, which is only
32-bit. Perhaps you need to use a long long.


Hamish
-- 
Hamish Moffatt VK3SB <hamish at debian.org> <hamish at cloud.net.au>



More information about the uClibc mailing list