Value of timeout.tv_usec off by a factor of 1000

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
Post Reply
chrisj
Posts: 5
Joined: Tue May 06, 2008 6:01 am
Location: Portland, OR

Value of timeout.tv_usec off by a factor of 1000

Post by chrisj »

In zm_remote_camera.cpp, a timeval struct is initialized as follows:

Code: Select all

                timeout.tv_sec = config.http_timeout/1000;
                timeout.tv_usec = config.http_timeout%1000;
Since the tv_usec field is supposed to be the number of microseconds, it should actually be initialized as:

Code: Select all

               timeout.tv_sec = config.http_timeout/1000;
               timeout.tv_usec = (config.http_timeout%1000) * 1000;
Since the value of config.http_timeout%1000 gives the remaining number of milliseconds not covered by the tv_sec field.

Not a big deal, really, but if you set the timeout to less than 1000ms, then you may notice the difference.
User avatar
Normando
Posts: 219
Joined: Sun Aug 17, 2008 5:34 am
Location: Rosario - Argentina

Post by Normando »

I have knoledge with C++ but it is a simple mathematics.

1 sec = 1000000 usec

So:

Code: Select all

                timeout.tv_sec = config.http_timeout/1000;
                timeout.tv_usec = config.http_timeout%1000; 
should be:

Code: Select all

                timeout.tv_sec = config.http_timeout/1000;
                timeout.tv_usec = config.http_timeout*1000; 
Phil, can you confirm this?

Thanks
User avatar
zoneminder
Site Admin
Posts: 5215
Joined: Wed Jul 09, 2003 2:07 pm
Location: Bristol, UK
Contact:

Post by zoneminder »

The correct way is

Code: Select all

timeout.tv_usec = (config.http_timeout%1000) * 1000; 
however this is how it is currently implemented anyway.

You need to do the %1000 to get the milliseconds and then multiply by 1000 to get microseconds. Just multiplying by 1000 will include the seconds in the calculation and so will be too large.
Phil
User avatar
Normando
Posts: 219
Joined: Sun Aug 17, 2008 5:34 am
Location: Rosario - Argentina

Post by Normando »

Ohh, yes, I now I understand the concept.

Thanks for the clarification Phil

Regards
Post Reply