For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > May 2004 > <sys/time.h>: timercmp









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author <sys/time.h>: timercmp
Adrian 'Dagurashibanipal' von Bidder

2004-05-15, 3:31 pm

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Yo all!

In the glibc 2.3.2 headers, I find the following:

/* NOTE: `timercmp' does not work for >= or <=. */
....
# define timercmp(a, b, CMP) \
(((a)->tv_sec == (b)->tv_sec) ? \
((a)->tv_usec CMP (b)->tv_usec) : \
((a)->tv_sec CMP (b)->tv_sec))

I'm probably just tired, but why would >=/<= not work?

thanks & bye
- -- vbi

- --
The content of this message may or may not reflect the opinion of me, my
employer, my girlfriend, my cat or anybody else, regardless of the fact
whether such an employer, girlfriend, cat, or anybody else exists. I
(or my employer, girlfriend, cat or whoever) disclaim any legal
obligations resulting from the above message. You, as the reader of
this message, may or may not have the permission to redistribute this
message as a whole or in parts, verbatim or in modified form, or to
distribute any message at all.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: get my key from http://fortytwo.ch/gpg/92082481

iKcEARECAGcFAkCmZahgGmh0dHA6Ly9mb3J0eXR3
by5jaC9sZWdhbC9ncGcvZW1h
aWwuMjAwMjA4MjI/ dmVyc2lvbj0xLjUmbWQ1c3VtPTVkZmY4NjhkMTE4
NDMyNzYw
NzFiMjVlYjcwMDZkYTNlAAoJEIukMYvlp/fWSnYAn3hBS2YXAt5OVZGWZ+a767l5
ZHrpAKCjDcz5cLWd8Xp+GLrWhe/HCezl1g==
=0DrT
-----END PGP SIGNATURE-----
Fletcher Glenn

2004-05-17, 12:35 pm



Adrian 'Dagurashibanipal' von Bidder wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Yo all!
>
> In the glibc 2.3.2 headers, I find the following:
>
> /* NOTE: `timercmp' does not work for >= or <=. */
> ...
> # define timercmp(a, b, CMP) \
> (((a)->tv_sec == (b)->tv_sec) ? \
> ((a)->tv_usec CMP (b)->tv_usec) : \
> ((a)->tv_sec CMP (b)->tv_sec))
>
> I'm probably just tired, but why would >=/<= not work?
>


A struct timeval looks like this:

struct timeval
{
time_t tv_sec; // seconds
time_t tv_usec; // microseconds
};

You cannot directly compare one struct with another. You must instead
compare the elements. Now think about what you know if
a->tv_sec != b->tv_sec. Right, you cannot conclude anything execpt
> or <. The <= and >= can only work if a->tv_sec == b->tv_sec.


--

Fletcher Glenn

Adrian 'Dagurashibanipal' von Bidder

2004-05-19, 3:31 am

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Clinging to sanity, Fletcher Glenn mumbled in his beard:
(well, in this case it's probably me who clings to sanity, or tries to)=


=20[color=darkred]
\[color=darkred]
[color=darkred]
[color=darkred]
> struct timeval
> {
> time_t tv_sec; // seconds
> time_t tv_usec; // microseconds
> };
>=20
> You cannot directly compare one struct with another. You must instea=

d
> compare the elements. Now think about what you know if
> a->tv_sec !=3D b->tv_sec. Right, you cannot conclude anything execpt=

[color=darkred]
ec.

Hmm.

Let's say a and b are struct timevals.

If a < b and sec are !=3D, timercmp(a, b, <=3D) should return true, whi=
ch it
does because the third line of the define is used (a->sec <=3D b->sec)

If a < b and sec are =3D=3D, result is the same, from the second line o=
f the
define (a->usec <=3D b->usec)

If a =3D=3D b, again, the comparison will be done on usec and the resul=
t will be
as expected.

If a > b within the same sec, the usec will be compared and result is f=
alse
as a->usec is not <=3D b->usec.

If a > b and not within the same sec, same but comparison is a->sec <=3D=

b->sec which is not true.

Still don't see the light, sorry.

Of course, the probability of two gettimeofday() (or two intervals
cal=C3=B6culated with timersub) being =3D=3D is near zero, but in my ca=
se I have a
sorted list where some of the timevals are actually really equal. Using=

timercmp(.. <=3D) ensures that new records with equal time are sorted i=
n the
right position in that list automatically and makes the if (...)
conditional shorter.

greetings
- -- vbi


=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D
#include <stdio.h>
#include <sys/time.h>

struct timeval a, b;

void print_case(struct timeval * a, struct timeval * b) {
if (timercmp(a, b, <=3D))
printf("a <=3D b\n");
else
printf("a > b\n");
}

int main() {
// case 1
a.tv_sec =3D 500;
a.tv_usec =3D 265346;
b.tv_sec =3D 501;
b.tv_usec =3D 23454;
print_case(&a, &b);
// case 2
a.tv_sec =3D 500;
a.tv_usec =3D 26546;
b.tv_sec =3D 500;
b.tv_usec =3D 2345456;
print_case(&a, &b);
// case 3
a.tv_sec =3D 500;
a.tv_usec =3D 265346;
b.tv_sec =3D 500;
b.tv_usec =3D 265346;
print_case(&a, &b);
// case 1
a.tv_sec =3D 500;
a.tv_usec =3D 265346;
b.tv_sec =3D 500;
b.tv_usec =3D 23454;
print_case(&a, &b);
// case 1
a.tv_sec =3D 502;
a.tv_usec =3D 265346;
b.tv_sec =3D 501;
b.tv_usec =3D 23454;
print_case(&a, &b);

return 0;
}
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D

- --=20
Today is Prickle-Prickle, the 66th day of Discord in the YOLD 3170

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: get my key from http://fortytwo.ch/gpg/92082481

iKcEARECAGcFAkCrAGBgGmh0dHA6Ly9mb3J0eXR3
by5jaC9sZWdhbC9ncGcvZW1h
aWwuMjAwMjA4MjI/ dmVyc2lvbj0xLjUmbWQ1c3VtPTVkZmY4NjhkMTE4
NDMyNzYw
NzFiMjVlYjcwMDZkYTNlAAoJEIukMYvlp/fWiYcAoOYOGZOKODRrjht8bEMhSlrz
mOEUAKDBTe/MfH14FPTF63vMeNELgPcEcQ=3D=3D
=3DWGMr
-----END PGP SIGNATURE-----
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com