time.c 9.67 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
/*
Bob Halley's avatar
Bob Halley committed
2
 * Copyright (C) 1998, 1999, 2000  Internet Software Consortium.
Bob Halley's avatar
Bob Halley committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */
Bob Halley's avatar
add  
Bob Halley committed
17

Bob Halley's avatar
Bob Halley committed
18
19
#include <config.h>

Bob Halley's avatar
add  
Bob Halley committed
20
#include <errno.h>
21
#include <limits.h>
22
#include <time.h>
Bob Halley's avatar
add  
Bob Halley committed
23

24
25
#include <sys/time.h>	/* Required for struct timeval on some platforms. */

26
#include <isc/string.h>
Bob Halley's avatar
add  
Bob Halley committed
27
#include <isc/time.h>
Bob Halley's avatar
Bob Halley committed
28
#include <isc/util.h>
Bob Halley's avatar
add  
Bob Halley committed
29

30
31
32
33
34
35
36
37
38
39
40
#define NS_PER_S	1000000000	/* Nanoseconds per second. */
#define NS_PER_US	1000		/* Nanoseconds per microsecond. */
#define US_PER_S	1000000		/* Microseconds per second. */

/*
 * All of the INSIST()s checks of nanoseconds < NS_PER_S are for 
 * consistency checking of the type. In lieu of magic numbers, it
 * is the best we've got.  The check is only performed on functions which
 * need an initialized type.
 */

41
42
43
44
/***
 *** Intervals
 ***/

45
46
47
static isc_interval_t zero_interval = { 0, 0 };
isc_interval_t *isc_interval_zero = &zero_interval;

48
void
Bob Halley's avatar
Bob Halley committed
49
isc_interval_set(isc_interval_t *i,
50
51
		 unsigned int seconds, unsigned int nanoseconds)
{
52
53
54
55
56
57
58
59

	/*
	 * Set 'i' to a value representing an interval of 'seconds' seconds
	 * and 'nanoseconds' nanoseconds, suitable for use in isc_time_add()
	 * and isc_time_subtract().
	 */

	REQUIRE(i != NULL);
60
	REQUIRE(nanoseconds < NS_PER_S);
61
62
63
64
65
66

	i->seconds = seconds;
	i->nanoseconds = nanoseconds;
}

isc_boolean_t
Bob Halley's avatar
Bob Halley committed
67
isc_interval_iszero(isc_interval_t *i) {
68
69
70
71
72
73

	/*
	 * Returns ISC_TRUE iff. 'i' is the zero interval.
	 */

	REQUIRE(i != NULL);
74
	INSIST(i->nanoseconds < NS_PER_S);
75
76
77
78
79
80
81
82
83
84
85
86

	if (i->seconds == 0 && i->nanoseconds == 0)
		return (ISC_TRUE);

	return (ISC_FALSE);
}


/***
 *** Absolute Times
 ***/

87
88
89
static isc_time_t epoch = { 0, 0 };
isc_time_t *isc_time_epoch = &epoch;

90
91
92
93
94
95
96
void
isc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
	/*
	 * Set 't' to a particular number of seconds + nanoseconds since the
	 * epoch.
	 */
	REQUIRE(t != NULL);
97
	REQUIRE(nanoseconds < NS_PER_S);
98
99
100
101
102

	t->seconds = seconds;
	t->nanoseconds = nanoseconds;
}

103
void
Bob Halley's avatar
Bob Halley committed
104
isc_time_settoepoch(isc_time_t *t) {
105
106
107
108
109
110
111
112
113
114
115
	/*
	 * Set 't' to the time of the epoch.
	 */

	REQUIRE(t != NULL);

	t->seconds = 0;
	t->nanoseconds = 0;
}

isc_boolean_t
Bob Halley's avatar
Bob Halley committed
116
isc_time_isepoch(isc_time_t *t) {
117
118
119
120
121
122

	/*
	 * Returns ISC_TRUE iff. 't' is the epoch ("time zero").
	 */

	REQUIRE(t != NULL);
123
	INSIST(t->nanoseconds < NS_PER_S);
124
125
126
127
128
129
130

	if (t->seconds == 0 && t->nanoseconds == 0)
		return (ISC_TRUE);

	return (ISC_FALSE);
}

Bob Halley's avatar
Bob Halley committed
131
isc_result_t
132
isc_time_now(isc_time_t *t) {
Bob Halley's avatar
add  
Bob Halley committed
133
134
135
	struct timeval tv;

	/*
136
	 * Set *t to the current absolute time.
Bob Halley's avatar
add  
Bob Halley committed
137
138
	 */
	
139
	REQUIRE(t != NULL);
Bob Halley's avatar
add  
Bob Halley committed
140
141
	
	if (gettimeofday(&tv, NULL) == -1) {
Bob Halley's avatar
Bob Halley committed
142
		UNEXPECTED_ERROR(__FILE__, __LINE__, strerror(errno));
Bob Halley's avatar
add  
Bob Halley committed
143
144
145
		return (ISC_R_UNEXPECTED);
	}

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
	/*
	 * Does POSIX guarantee the signedness of tv_sec and tv_usec?  If not,
	 * then this test will generate warnings for platforms on which it is
	 * unsigned.  In any event, the chances of any of these problems
	 * happening are pretty much zero, but since the libisc library ensures
	 * certain things to be true ...
	 */
	if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
		return (ISC_R_UNEXPECTED);

	/*
	 * Ensure the tv_sec value fits in t->seconds. 
	 */
	if (sizeof(tv.tv_sec) > sizeof(t->seconds) &&
	    ((tv.tv_sec | (unsigned int)-1) ^ (unsigned int)-1) != 0)
		return (ISC_R_RANGE);

163
	t->seconds = tv.tv_sec;
164
	t->nanoseconds = tv.tv_usec * NS_PER_US;
Bob Halley's avatar
add  
Bob Halley committed
165
166
167
168

	return (ISC_R_SUCCESS);
}

169
170
171
172
173
174
175
176
177
178
isc_result_t
isc_time_nowplusinterval(isc_time_t *t, isc_interval_t *i) {
	struct timeval tv;

	/*
	 * Set *t to the current absolute time + i.
	 */
	
	REQUIRE(t != NULL);
	REQUIRE(i != NULL);
179
180
	INSIST(i->nanoseconds < NS_PER_S);

181
182
183
184
185
	if (gettimeofday(&tv, NULL) == -1) {
		UNEXPECTED_ERROR(__FILE__, __LINE__, strerror(errno));
		return (ISC_R_UNEXPECTED);
	}

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
	/*
	 * Does POSIX guarantee the signedness of tv_sec and tv_usec?  If not,
	 * then this test will generate warnings for platforms on which it is
	 * unsigned.  In any event, the chances of any of these problems
	 * happening are pretty much zero, but since the libisc library ensures
	 * certain things to be true ...
	 */
	if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
		return (ISC_R_UNEXPECTED);

	/*
	 * Ensure the resulting seconds value fits in the size of an
	 * unsigned int.  (It is written this way as a slight optimization;
	 * note that even if both values == INT_MAX, then when added
	 * and getting another 1 added below the result is UINT_MAX.)
	 */
	if ((tv.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
	    ((long long)tv.tv_sec + i->seconds > UINT_MAX))
		return (ISC_R_RANGE);

206
	t->seconds = tv.tv_sec + i->seconds;
207
208
	t->nanoseconds = tv.tv_usec * NS_PER_US + i->nanoseconds;
	if (t->nanoseconds > NS_PER_S) {
209
		t->seconds++;
210
		t->nanoseconds -= NS_PER_S;
211
212
213
214
215
	}

	return (ISC_R_SUCCESS);
}

Bob Halley's avatar
add  
Bob Halley committed
216
int
Bob Halley's avatar
Bob Halley committed
217
isc_time_compare(isc_time_t *t1, isc_time_t *t2) {
218

Bob Halley's avatar
add  
Bob Halley committed
219
	/*
Bob Halley's avatar
Bob Halley committed
220
	 * Compare the times referenced by 't1' and 't2'
Bob Halley's avatar
add  
Bob Halley committed
221
222
	 */

Bob Halley's avatar
Bob Halley committed
223
	REQUIRE(t1 != NULL && t2 != NULL);
224
	INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
Bob Halley's avatar
add  
Bob Halley committed
225

Bob Halley's avatar
Bob Halley committed
226
	if (t1->seconds < t2->seconds)
Bob Halley's avatar
add  
Bob Halley committed
227
		return (-1);
Bob Halley's avatar
Bob Halley committed
228
	if (t1->seconds > t2->seconds)
Bob Halley's avatar
add  
Bob Halley committed
229
		return (1);
Bob Halley's avatar
Bob Halley committed
230
	if (t1->nanoseconds < t2->nanoseconds)
Bob Halley's avatar
add  
Bob Halley committed
231
		return (-1);
Bob Halley's avatar
Bob Halley committed
232
	if (t1->nanoseconds > t2->nanoseconds)
Bob Halley's avatar
add  
Bob Halley committed
233
234
235
236
		return (1);
	return (0);
}

237
238
isc_result_t
isc_time_add(isc_time_t *t, isc_interval_t *i, isc_time_t *result) {
Bob Halley's avatar
add  
Bob Halley committed
239
	/*
240
	 * Add 't' to 'i', storing the result in 'result'.
Bob Halley's avatar
add  
Bob Halley committed
241
242
	 */

243
	REQUIRE(t != NULL && i != NULL && result != NULL);
244
245
246
247
248
249
250
251
252
253
254
	INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);

	/*
	 * Ensure the resulting seconds value fits in the size of an
	 * unsigned int.  (It is written this way as a slight optimization;
	 * note that even if both values == INT_MAX, then when added
	 * and getting another 1 added below the result is UINT_MAX.)
	 */
	if ((t->seconds > INT_MAX || i->seconds > INT_MAX) &&
	    ((long long)t->seconds + i->seconds > UINT_MAX))
		return (ISC_R_RANGE);
Bob Halley's avatar
add  
Bob Halley committed
255

256
257
	result->seconds = t->seconds + i->seconds;
	result->nanoseconds = t->nanoseconds + i->nanoseconds;
258
	if (result->nanoseconds > NS_PER_S) {
259
		result->seconds++;
260
		result->nanoseconds -= NS_PER_S;
Bob Halley's avatar
add  
Bob Halley committed
261
	}
262
263

	return (ISC_R_SUCCESS);
Bob Halley's avatar
add  
Bob Halley committed
264
265
}

266
isc_result_t
Bob Halley's avatar
Bob Halley committed
267
isc_time_subtract(isc_time_t *t, isc_interval_t *i, isc_time_t *result) {
Bob Halley's avatar
add  
Bob Halley committed
268
	/*
269
	 * Subtract 'i' from 't', storing the result in 'result'.
Bob Halley's avatar
add  
Bob Halley committed
270
271
	 */

272
	REQUIRE(t != NULL && i != NULL && result != NULL);
273
274
275
276
277
278
279
	INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);

	if ((unsigned int)t->seconds < i->seconds ||
	    ((unsigned int)t->seconds == i->seconds &&
	     t->nanoseconds < i->nanoseconds))
	    return (ISC_R_RANGE);

280
281
282
	result->seconds = t->seconds - i->seconds;
	if (t->nanoseconds >= i->nanoseconds)
		result->nanoseconds = t->nanoseconds - i->nanoseconds;
Bob Halley's avatar
add  
Bob Halley committed
283
	else {
284
		result->nanoseconds = NS_PER_S - i->nanoseconds +
285
286
			t->nanoseconds;
		result->seconds--;
Bob Halley's avatar
add  
Bob Halley committed
287
	}
288
289

	return (ISC_R_SUCCESS);
Bob Halley's avatar
add  
Bob Halley committed
290
}
291
292
293
294
295
296

isc_uint64_t
isc_time_microdiff(isc_time_t *t1, isc_time_t *t2) {
	isc_uint64_t i1, i2, i3;

	REQUIRE(t1 != NULL && t2 != NULL);
297
	INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
298

299
300
	i1 = (isc_uint64_t)t1->seconds * NS_PER_S + t1->nanoseconds;
	i2 = (isc_uint64_t)t2->seconds * NS_PER_S + t2->nanoseconds;
301
302
303
304
305
306
307
308
309

	if (i1 <= i2)
		return (0);

	i3 = i1 - i2;

	/*
	 * Convert to microseconds.
	 */
310
	i3 = (i1 - i2) / NS_PER_US;
311
312
313

	return (i3);
}
314
315
316

isc_uint32_t
isc_time_seconds(isc_time_t *t) {
317
	REQUIRE(t != NULL);
318
	INSIST(t->nanoseconds < NS_PER_S);
319

320
321
322
	return ((isc_uint32_t)t->seconds);
}

323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
isc_result_t
isc_time_secondsastimet(isc_time_t *t, time_t *secondsp) {
	isc_uint64_t i;
	time_t seconds;

	REQUIRE(t != NULL);
	INSIST(t->nanoseconds < NS_PER_S);

	/*
	 * Ensure that the number of seconds represented by t->seconds
	 * can be represented by a time_t.  Since t->seconds is an unsigned
	 * int and since time_t is mostly opaque, this is trickier than
	 * it seems.  (This standardized opaqueness of time_t is *very*
	 * frustrating; time_t is not even limited to being an integral
	 * type.)
	 *
	 * The mission, then, is to avoid generating any kind of warning
	 * about "signed versus unsigned" while trying to determine if the
	 * the unsigned int t->seconds is out range for tv_sec, which is
	 * pretty much only true if time_t is a signed integer of the same
	 * size as the return value of isc_time_seconds. 
	 *
	 * The use of a 64 bit integer takes advantage of C's conversion rules
	 * to either zero fill or sign extend the widened type.
	 */
	seconds = (time_t)t->seconds;

	INSIST(sizeof(unsigned int) == sizeof(isc_uint32_t));
	INSIST(sizeof(time_t) >= sizeof(isc_uint32_t));

	if (sizeof(time_t) == sizeof(isc_uint32_t) &&	       /* Same size. */
	    (time_t)0.5 != 0.5 &&	       /* Not a floating point type. */
	    (i = (time_t)-1) != 4294967295u &&		       /* Is signed. */
356
357
358
359
360
361
362
363
	    (seconds & (1 << (sizeof(time_t) * 8 - 1))) != 0) {	/* Negative. */
		/*
		 * This is here to shut up the IRIX compiler:
		 *	variable "i" was set but never used
		 * when the value of i *was* used in the third test.
		 * (Let's hope the compiler got the actual test right.)
		 */
		UNUSED(i);
364
		return (ISC_R_RANGE);
365
	}
366
367
368
369
370
371

	*secondsp = seconds;

	return (ISC_R_SUCCESS);
}

372
373
isc_uint32_t
isc_time_nanoseconds(isc_time_t *t) {
374
375
	REQUIRE(t != NULL);

376
	ENSURE(t->nanoseconds < NS_PER_S);
377
378
379

	return ((isc_uint32_t)t->nanoseconds);
}