Add function for printing the list of tests
[ctestharness.git] / src / timeval_math.c
blob0df8677e857d352979c54e674fbdda1ec40c1058
1 #include "timeval_math.h"
3 #define US_PER_SECOND 1000000
5 void
6 timeval_sub(struct timeval t1, struct timeval t2, struct timeval *res)
8 res->tv_usec = t2.tv_usec - t1.tv_usec;
9 res->tv_sec = t2.tv_sec - t1.tv_sec;
10 if (res->tv_sec > 0 && res->tv_usec < 0) {
11 res->tv_usec += US_PER_SECOND;
12 res->tv_sec--;
13 } else if (res->tv_sec < 0 && res->tv_usec > 0) {
14 res->tv_usec -= US_PER_SECOND;
15 res->tv_sec++;
19 void
20 timeval_add(struct timeval t1, struct timeval t2, struct timeval *res)
22 res->tv_usec = t2.tv_usec + t1.tv_usec;
23 res->tv_sec = t2.tv_sec + t1.tv_sec;
24 if (res->tv_usec >= US_PER_SECOND) {
25 res->tv_usec -= US_PER_SECOND;
26 res->tv_sec++;
27 } else if (res->tv_usec <= -US_PER_SECOND) {
28 res->tv_usec += US_PER_SECOND;
29 res->tv_sec--;
33 int
34 timeval_cmp(struct timeval t1, struct timeval t2)
36 if (t1.tv_sec < t2.tv_sec) {
37 return -1;
38 } else if (t1.tv_sec > t2.tv_sec) {
39 return +1;
40 } else if (t1.tv_usec < t2.tv_usec) {
41 return -1;
42 } else if (t1.tv_usec > t2.tv_usec) {
43 return +1;
44 } else {
45 return 0;