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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/*
* tod.c -- time of day pseudo-class implementation
*
* Copyright (c) 1995 John H. Poplett
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice immediately at the beginning of the file, without modification,
* this list of conditions, and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Absolutely no warranty of function or purpose is made by the author
* John H. Poplett.
* 4. This work was done expressly for inclusion into FreeBSD. Other use
* is allowed if this notation is included.
* 5. Modifications may be freely made to this file if the above conditions
* are met.
*
*/
#include <sys/types.h>
#include <sys/time.h>
#include <assert.h>
#include <stdio.h>
#include "tod.h"
#define USP 1000000
int tod_cmp (const struct timeval *a, const struct timeval *b)
{
int rc;
assert (a->tv_usec <= USP);
assert (b->tv_usec <= USP);
rc = a->tv_sec - b->tv_sec;
if (rc == 0)
rc = a->tv_usec - b->tv_usec;
return rc;
}
/*
TOD < command
*/
int tod_lt (const struct timeval *a, const struct timeval *b)
{
return tod_cmp (a, b) < 0;
}
int tod_gt (const struct timeval *a, const struct timeval *b)
{
return tod_cmp (a, b) > 0;
}
int tod_lte (const struct timeval *a, const struct timeval *b)
{
return tod_cmp (a, b) <= 0;
}
int tod_gte (const struct timeval *a, const struct timeval *b)
{
return tod_cmp (a, b) >= 0;
}
int tod_eq (const struct timeval *a, const struct timeval *b)
{
return tod_cmp (a, b) == 0;
}
/*
TOD += command
*/
void tod_addto (struct timeval *a, const struct timeval *b)
{
a->tv_usec += b->tv_usec;
a->tv_sec += b->tv_sec + a->tv_usec / USP;
a->tv_usec %= USP;
}
/*
TOD -= command
*/
void tod_subfrom (struct timeval *a, struct timeval b)
{
assert (a->tv_usec <= USP);
assert (b.tv_usec <= USP);
if (b.tv_usec > a->tv_usec)
{
a->tv_usec += USP;
a->tv_sec -= 1;
}
a->tv_usec -= b.tv_usec;
a->tv_sec -= b.tv_sec;
}
void tod_gettime (struct timeval *tp)
{
gettimeofday (tp, NULL);
tp->tv_sec += tp->tv_usec / USP;
tp->tv_usec %= USP;
}
/* end of tod.c */
|