forked from sq5bpf/telive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telive_util.c
35 lines (27 loc) · 883 Bytes
/
telive_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//#include <sys/time.h>
//#include <time.h>
#include "telive_util.h"
/* Subtract the struct timevalvalues X and Y, return result in miliseconds
*
* This function is shamelessly ripped from the glibc manual
*/
long timeval_subtract_ms (struct timeval *x, struct timeval *y)
{
struct timeval result;
/* Perform the carry for the later subtraction by updating y. */
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000) {
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
/* Compute the time remaining to wait.
* tv_usec is certainly positive. */
result.tv_sec = x->tv_sec - y->tv_sec;
result.tv_usec = x->tv_usec - y->tv_usec;
return((result.tv_usec/1000)+(result.tv_sec*1000));
}