summaryrefslogtreecommitdiffstats
path: root/lib/isc/time.c
blob: b03f377a3e75ce5c2e1a91de820d866a36c57b22 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

/*! \file */

#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/time.h> /* Required for struct timeval on some platforms. */
#include <syslog.h>
#include <time.h>

#include <isc/log.h>
#include <isc/print.h>
#include <isc/string.h>
#include <isc/time.h>
#include <isc/tm.h>
#include <isc/util.h>

#if defined(CLOCK_REALTIME)
#define CLOCKSOURCE_HIRES CLOCK_REALTIME
#endif /* #if defined(CLOCK_REALTIME) */

#if defined(CLOCK_REALTIME_COARSE)
#define CLOCKSOURCE CLOCK_REALTIME_COARSE
#elif defined(CLOCK_REALTIME_FAST)
#define CLOCKSOURCE CLOCK_REALTIME_FAST
#else /* if defined(CLOCK_REALTIME_COARSE) */
#define CLOCKSOURCE CLOCK_REALTIME
#endif /* if defined(CLOCK_REALTIME_COARSE) */

#if !defined(CLOCKSOURCE_HIRES)
#define CLOCKSOURCE_HIRES CLOCKSOURCE
#endif /* #ifndef CLOCKSOURCE_HIRES */

/*%
 *** Intervals
 ***/

#if !defined(UNIT_TESTING)
static const isc_interval_t zero_interval = { 0, 0 };
const isc_interval_t *const isc_interval_zero = &zero_interval;
#endif

void
isc_interval_set(isc_interval_t *i, unsigned int seconds,
		 unsigned int nanoseconds) {
	REQUIRE(i != NULL);
	REQUIRE(nanoseconds < NS_PER_SEC);

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

bool
isc_interval_iszero(const isc_interval_t *i) {
	REQUIRE(i != NULL);
	INSIST(i->nanoseconds < NS_PER_SEC);

	if (i->seconds == 0 && i->nanoseconds == 0) {
		return (true);
	}

	return (false);
}

unsigned int
isc_interval_ms(const isc_interval_t *i) {
	REQUIRE(i != NULL);
	INSIST(i->nanoseconds < NS_PER_SEC);

	return ((i->seconds * MS_PER_SEC) + (i->nanoseconds / NS_PER_MS));
}

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

#if !defined(UNIT_TESTING)
static const isc_time_t epoch = { 0, 0 };
const isc_time_t *const isc_time_epoch = &epoch;
#endif

void
isc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
	REQUIRE(t != NULL);
	REQUIRE(nanoseconds < NS_PER_SEC);

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

void
isc_time_settoepoch(isc_time_t *t) {
	REQUIRE(t != NULL);

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

bool
isc_time_isepoch(const isc_time_t *t) {
	REQUIRE(t != NULL);
	INSIST(t->nanoseconds < NS_PER_SEC);

	if (t->seconds == 0 && t->nanoseconds == 0) {
		return (true);
	}

	return (false);
}

static isc_result_t
time_now(isc_time_t *t, clockid_t clock) {
	struct timespec ts;

	REQUIRE(t != NULL);

	if (clock_gettime(clock, &ts) == -1) {
		UNEXPECTED_SYSERROR(errno, "clock_gettime()");
		return (ISC_R_UNEXPECTED);
	}

	if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= NS_PER_SEC) {
		return (ISC_R_UNEXPECTED);
	}

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

	t->seconds = ts.tv_sec;
	t->nanoseconds = ts.tv_nsec;

	return (ISC_R_SUCCESS);
}

isc_result_t
isc_time_now_hires(isc_time_t *t) {
	return time_now(t, CLOCKSOURCE_HIRES);
}

isc_result_t
isc_time_now(isc_time_t *t) {
	return time_now(t, CLOCKSOURCE);
}

isc_result_t
isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
	struct timespec ts;

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

	if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
		UNEXPECTED_SYSERROR(errno, "clock_gettime()");
		return (ISC_R_UNEXPECTED);
	}

	if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= NS_PER_SEC) {
		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 ((ts.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
	    ((long long)ts.tv_sec + i->seconds > UINT_MAX))
	{
		return (ISC_R_RANGE);
	}

	t->seconds = ts.tv_sec + i->seconds;
	t->nanoseconds = ts.tv_nsec + i->nanoseconds;
	if (t->nanoseconds >= NS_PER_SEC) {
		t->seconds++;
		t->nanoseconds -= NS_PER_SEC;
	}

	return (ISC_R_SUCCESS);
}

int
isc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
	REQUIRE(t1 != NULL && t2 != NULL);
	INSIST(t1->nanoseconds < NS_PER_SEC && t2->nanoseconds < NS_PER_SEC);

	if (t1->seconds < t2->seconds) {
		return (-1);
	}
	if (t1->seconds > t2->seconds) {
		return (1);
	}
	if (t1->nanoseconds < t2->nanoseconds) {
		return (-1);
	}
	if (t1->nanoseconds > t2->nanoseconds) {
		return (1);
	}
	return (0);
}

isc_result_t
isc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result) {
	REQUIRE(t != NULL && i != NULL && result != NULL);
	REQUIRE(t->nanoseconds < NS_PER_SEC && i->nanoseconds < NS_PER_SEC);

	/* Seconds */
#if HAVE_BUILTIN_OVERFLOW
	if (__builtin_uadd_overflow(t->seconds, i->seconds, &result->seconds)) {
		return (ISC_R_RANGE);
	}
#else
	if (t->seconds > UINT_MAX - i->seconds) {
		return (ISC_R_RANGE);
	}
	result->seconds = t->seconds + i->seconds;
#endif

	/* Nanoseconds */
	result->nanoseconds = t->nanoseconds + i->nanoseconds;
	if (result->nanoseconds >= NS_PER_SEC) {
		if (result->seconds == UINT_MAX) {
			return (ISC_R_RANGE);
		}
		result->nanoseconds -= NS_PER_SEC;
		result->seconds++;
	}

	return (ISC_R_SUCCESS);
}

isc_result_t
isc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
		  isc_time_t *result) {
	REQUIRE(t != NULL && i != NULL && result != NULL);
	REQUIRE(t->nanoseconds < NS_PER_SEC && i->nanoseconds < NS_PER_SEC);

	/* Seconds */
#if HAVE_BUILTIN_OVERFLOW
	if (__builtin_usub_overflow(t->seconds, i->seconds, &result->seconds)) {
		return (ISC_R_RANGE);
	}
#else
	if (t->seconds < i->seconds) {
		return (ISC_R_RANGE);
	}
	result->seconds = t->seconds - i->seconds;
#endif

	/* Nanoseconds */
	if (t->nanoseconds >= i->nanoseconds) {
		result->nanoseconds = t->nanoseconds - i->nanoseconds;
	} else {
		if (result->seconds == 0) {
			return (ISC_R_RANGE);
		}
		result->seconds--;
		result->nanoseconds = NS_PER_SEC + t->nanoseconds -
				      i->nanoseconds;
	}

	return (ISC_R_SUCCESS);
}

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

	REQUIRE(t1 != NULL && t2 != NULL);
	INSIST(t1->nanoseconds < NS_PER_SEC && t2->nanoseconds < NS_PER_SEC);

	i1 = (uint64_t)t1->seconds * NS_PER_SEC + t1->nanoseconds;
	i2 = (uint64_t)t2->seconds * NS_PER_SEC + t2->nanoseconds;

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

	i3 = i1 - i2;

	/*
	 * Convert to microseconds.
	 */
	i3 /= NS_PER_US;

	return (i3);
}

uint32_t
isc_time_seconds(const isc_time_t *t) {
	REQUIRE(t != NULL);
	INSIST(t->nanoseconds < NS_PER_SEC);

	return ((uint32_t)t->seconds);
}

isc_result_t
isc_time_secondsastimet(const isc_time_t *t, time_t *secondsp) {
	time_t seconds;

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

	/*
	 * 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 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.
	 *
	 * If the paradox in the if clause below is true, t->seconds is
	 * out of range for time_t.
	 */
	seconds = (time_t)t->seconds;

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

	if (t->seconds > (~0U >> 1) && seconds <= (time_t)(~0U >> 1)) {
		return (ISC_R_RANGE);
	}

	*secondsp = seconds;

	return (ISC_R_SUCCESS);
}

uint32_t
isc_time_nanoseconds(const isc_time_t *t) {
	REQUIRE(t != NULL);

	ENSURE(t->nanoseconds < NS_PER_SEC);

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

void
isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
	time_t now;
	unsigned int flen;
	struct tm tm;

	REQUIRE(t != NULL);
	INSIST(t->nanoseconds < NS_PER_SEC);
	REQUIRE(buf != NULL);
	REQUIRE(len > 0);

	now = (time_t)t->seconds;
	flen = strftime(buf, len, "%d-%b-%Y %X", localtime_r(&now, &tm));
	INSIST(flen < len);
	if (flen != 0) {
		snprintf(buf + flen, len - flen, ".%03u",
			 t->nanoseconds / NS_PER_MS);
	} else {
		strlcpy(buf, "99-Bad-9999 99:99:99.999", len);
	}
}

void
isc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
	time_t now;
	unsigned int flen;
	struct tm tm;

	REQUIRE(t != NULL);
	INSIST(t->nanoseconds < NS_PER_SEC);
	REQUIRE(buf != NULL);
	REQUIRE(len > 0);

	/*
	 * 5 spaces, 1 comma, 3 GMT, 2 %d, 4 %Y, 8 %H:%M:%S, 3+ %a, 3+
	 * %b (29+)
	 */
	now = (time_t)t->seconds;
	flen = strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT",
			gmtime_r(&now, &tm));
	INSIST(flen < len);
}

isc_result_t
isc_time_parsehttptimestamp(char *buf, isc_time_t *t) {
	struct tm t_tm;
	time_t when;
	char *p;

	REQUIRE(buf != NULL);
	REQUIRE(t != NULL);

	p = isc_tm_strptime(buf, "%a, %d %b %Y %H:%M:%S", &t_tm);
	if (p == NULL) {
		return (ISC_R_UNEXPECTED);
	}
	when = isc_tm_timegm(&t_tm);
	if (when == -1) {
		return (ISC_R_UNEXPECTED);
	}
	isc_time_set(t, when, 0);
	return (ISC_R_SUCCESS);
}

void
isc_time_formatISO8601L(const isc_time_t *t, char *buf, unsigned int len) {
	time_t now;
	unsigned int flen;
	struct tm tm;

	REQUIRE(t != NULL);
	INSIST(t->nanoseconds < NS_PER_SEC);
	REQUIRE(buf != NULL);
	REQUIRE(len > 0);

	now = (time_t)t->seconds;
	flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", localtime_r(&now, &tm));
	INSIST(flen < len);
}

void
isc_time_formatISO8601Lms(const isc_time_t *t, char *buf, unsigned int len) {
	time_t now;
	unsigned int flen;
	struct tm tm;

	REQUIRE(t != NULL);
	INSIST(t->nanoseconds < NS_PER_SEC);
	REQUIRE(buf != NULL);
	REQUIRE(len > 0);

	now = (time_t)t->seconds;
	flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", localtime_r(&now, &tm));
	INSIST(flen < len);
	if (flen > 0U && len - flen >= 6) {
		snprintf(buf + flen, len - flen, ".%03u",
			 t->nanoseconds / NS_PER_MS);
	}
}

void
isc_time_formatISO8601Lus(const isc_time_t *t, char *buf, unsigned int len) {
	time_t now;
	unsigned int flen;
	struct tm tm;

	REQUIRE(t != NULL);
	INSIST(t->nanoseconds < NS_PER_SEC);
	REQUIRE(buf != NULL);
	REQUIRE(len > 0);

	now = (time_t)t->seconds;
	flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", localtime_r(&now, &tm));
	INSIST(flen < len);
	if (flen > 0U && len - flen >= 6) {
		snprintf(buf + flen, len - flen, ".%06u",
			 t->nanoseconds / NS_PER_US);
	}
}

void
isc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) {
	time_t now;
	unsigned int flen;
	struct tm tm;

	REQUIRE(t != NULL);
	INSIST(t->nanoseconds < NS_PER_SEC);
	REQUIRE(buf != NULL);
	REQUIRE(len > 0);

	now = (time_t)t->seconds;
	flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime_r(&now, &tm));
	INSIST(flen < len);
}

void
isc_time_formatISO8601ms(const isc_time_t *t, char *buf, unsigned int len) {
	time_t now;
	unsigned int flen;
	struct tm tm;

	REQUIRE(t != NULL);
	INSIST(t->nanoseconds < NS_PER_SEC);
	REQUIRE(buf != NULL);
	REQUIRE(len > 0);

	now = (time_t)t->seconds;
	flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime_r(&now, &tm));
	INSIST(flen < len);
	if (flen > 0U && len - flen >= 5) {
		flen -= 1; /* rewind one character (Z) */
		snprintf(buf + flen, len - flen, ".%03uZ",
			 t->nanoseconds / NS_PER_MS);
	}
}

void
isc_time_formatISO8601us(const isc_time_t *t, char *buf, unsigned int len) {
	time_t now;
	unsigned int flen;
	struct tm tm;

	REQUIRE(t != NULL);
	INSIST(t->nanoseconds < NS_PER_SEC);
	REQUIRE(buf != NULL);
	REQUIRE(len > 0);

	now = (time_t)t->seconds;
	flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime_r(&now, &tm));
	INSIST(flen < len);
	if (flen > 0U && len - flen >= 5) {
		flen -= 1; /* rewind one character (Z) */
		snprintf(buf + flen, len - flen, ".%06uZ",
			 t->nanoseconds / NS_PER_US);
	}
}

void
isc_time_formatshorttimestamp(const isc_time_t *t, char *buf,
			      unsigned int len) {
	time_t now;
	unsigned int flen;
	struct tm tm;

	REQUIRE(t != NULL);
	INSIST(t->nanoseconds < NS_PER_SEC);
	REQUIRE(buf != NULL);
	REQUIRE(len > 0);

	now = (time_t)t->seconds;
	flen = strftime(buf, len, "%Y%m%d%H%M%S", gmtime_r(&now, &tm));
	INSIST(flen < len);
	if (flen > 0U && len - flen >= 5) {
		snprintf(buf + flen, len - flen, "%03u",
			 t->nanoseconds / NS_PER_MS);
	}
}