File: timeval.c

package info (click to toggle)
modemu 0.0.1-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 200 kB
  • ctags: 266
  • sloc: ansic: 1,860; lex: 146; makefile: 72
file content (45 lines) | stat: -rw-r--r-- 962 bytes parent folder | download | duplicates (4)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <sys/time.h>	/*timeval*/
#include "timeval.h"	/*(timevalXXX)*/

/* a = b [10mS] */
void
timevalSet10ms(struct timeval *ap, int b)
{
    ap->tv_sec = b / 100;
    ap->tv_usec = (b % 100) * 10*1000;
}

/* a += b */
void
timevalAdd(struct timeval *ap, const struct timeval *bp)
{
    ap->tv_sec += bp->tv_sec;
    ap->tv_usec += bp->tv_usec;
    if (ap->tv_usec >= 1000*1000) {
	ap->tv_usec -= 1000*1000;
	ap->tv_sec += 1;
    }
}

/* a -= b */
void
timevalSub(struct timeval *ap, const struct timeval *bp)
{
    ap->tv_sec -= bp->tv_sec;
    ap->tv_usec -= bp->tv_usec;
    if (ap->tv_usec < 0) {
	ap->tv_usec += 1000*1000;
	ap->tv_sec -= 1;
    }
}

/* (a < b): -1, (a==b): 0, (a > b): 1 */
int
timevalCmp(const struct timeval *ap, const struct timeval *bp)
{
    if (ap->tv_sec < bp->tv_sec) return -1;
    if (ap->tv_sec > bp->tv_sec) return 1;
    if (ap->tv_usec < bp->tv_usec) return -1;
    if (ap->tv_usec > bp->tv_usec) return 1;
    return 0;
}