xref: /freebsd/contrib/ntp/libntp/lib/isc/unix/time.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert  * Copyright (C) 2004-2008, 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert  * Copyright (C) 1998-2001, 2003  Internet Software Consortium.
4*a466cc55SCy Schubert  *
5*a466cc55SCy Schubert  * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert  * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert  * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert  *
9*a466cc55SCy Schubert  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert  * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert  */
17*a466cc55SCy Schubert 
18*a466cc55SCy Schubert /* $Id$ */
19*a466cc55SCy Schubert 
20*a466cc55SCy Schubert /*! \file */
21*a466cc55SCy Schubert 
22*a466cc55SCy Schubert #include <config.h>
23*a466cc55SCy Schubert 
24*a466cc55SCy Schubert #include <errno.h>
25*a466cc55SCy Schubert #include <limits.h>
26*a466cc55SCy Schubert #include <syslog.h>
27*a466cc55SCy Schubert #include <time.h>
28*a466cc55SCy Schubert 
29*a466cc55SCy Schubert #include <sys/time.h>	/* Required for struct timeval on some platforms. */
30*a466cc55SCy Schubert 
31*a466cc55SCy Schubert #include <isc/log.h>
32*a466cc55SCy Schubert #include <isc/print.h>
33*a466cc55SCy Schubert #include <isc/strerror.h>
34*a466cc55SCy Schubert #include <isc/string.h>
35*a466cc55SCy Schubert #include <isc/time.h>
36*a466cc55SCy Schubert #include <isc/util.h>
37*a466cc55SCy Schubert 
38*a466cc55SCy Schubert #define NS_PER_S	1000000000	/*%< Nanoseconds per second. */
39*a466cc55SCy Schubert #define NS_PER_US	1000		/*%< Nanoseconds per microsecond. */
40*a466cc55SCy Schubert #define US_PER_S	1000000		/*%< Microseconds per second. */
41*a466cc55SCy Schubert 
42*a466cc55SCy Schubert /*
43*a466cc55SCy Schubert  * All of the INSIST()s checks of nanoseconds < NS_PER_S are for
44*a466cc55SCy Schubert  * consistency checking of the type. In lieu of magic numbers, it
45*a466cc55SCy Schubert  * is the best we've got.  The check is only performed on functions which
46*a466cc55SCy Schubert  * need an initialized type.
47*a466cc55SCy Schubert  */
48*a466cc55SCy Schubert 
49*a466cc55SCy Schubert #ifndef ISC_FIX_TV_USEC
50*a466cc55SCy Schubert #define ISC_FIX_TV_USEC 1
51*a466cc55SCy Schubert #endif
52*a466cc55SCy Schubert 
53*a466cc55SCy Schubert /*%
54*a466cc55SCy Schubert  *** Intervals
55*a466cc55SCy Schubert  ***/
56*a466cc55SCy Schubert 
57*a466cc55SCy Schubert static isc_interval_t zero_interval = { 0, 0 };
58*a466cc55SCy Schubert isc_interval_t *isc_interval_zero = &zero_interval;
59*a466cc55SCy Schubert 
60*a466cc55SCy Schubert #if ISC_FIX_TV_USEC
61*a466cc55SCy Schubert static inline void
fix_tv_usec(struct timeval * tv)62*a466cc55SCy Schubert fix_tv_usec(struct timeval *tv) {
63*a466cc55SCy Schubert 	isc_boolean_t fixed = ISC_FALSE;
64*a466cc55SCy Schubert 
65*a466cc55SCy Schubert 	if (tv->tv_usec < 0) {
66*a466cc55SCy Schubert 		fixed = ISC_TRUE;
67*a466cc55SCy Schubert 		do {
68*a466cc55SCy Schubert 			tv->tv_sec -= 1;
69*a466cc55SCy Schubert 			tv->tv_usec += US_PER_S;
70*a466cc55SCy Schubert 		} while (tv->tv_usec < 0);
71*a466cc55SCy Schubert 	} else if (tv->tv_usec >= US_PER_S) {
72*a466cc55SCy Schubert 		fixed = ISC_TRUE;
73*a466cc55SCy Schubert 		do {
74*a466cc55SCy Schubert 			tv->tv_sec += 1;
75*a466cc55SCy Schubert 			tv->tv_usec -= US_PER_S;
76*a466cc55SCy Schubert 		} while (tv->tv_usec >=US_PER_S);
77*a466cc55SCy Schubert 	}
78*a466cc55SCy Schubert 	/*
79*a466cc55SCy Schubert 	 * Call syslog directly as was are called from the logging functions.
80*a466cc55SCy Schubert 	 */
81*a466cc55SCy Schubert 	if (fixed)
82*a466cc55SCy Schubert 		(void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
83*a466cc55SCy Schubert }
84*a466cc55SCy Schubert #endif
85*a466cc55SCy Schubert 
86*a466cc55SCy Schubert void
isc_interval_set(isc_interval_t * i,unsigned int seconds,unsigned int nanoseconds)87*a466cc55SCy Schubert isc_interval_set(isc_interval_t *i,
88*a466cc55SCy Schubert 		 unsigned int seconds, unsigned int nanoseconds)
89*a466cc55SCy Schubert {
90*a466cc55SCy Schubert 	REQUIRE(i != NULL);
91*a466cc55SCy Schubert 	REQUIRE(nanoseconds < NS_PER_S);
92*a466cc55SCy Schubert 
93*a466cc55SCy Schubert 	i->seconds = seconds;
94*a466cc55SCy Schubert 	i->nanoseconds = nanoseconds;
95*a466cc55SCy Schubert }
96*a466cc55SCy Schubert 
97*a466cc55SCy Schubert isc_boolean_t
isc_interval_iszero(const isc_interval_t * i)98*a466cc55SCy Schubert isc_interval_iszero(const isc_interval_t *i) {
99*a466cc55SCy Schubert 	REQUIRE(i != NULL);
100*a466cc55SCy Schubert 	INSIST(i->nanoseconds < NS_PER_S);
101*a466cc55SCy Schubert 
102*a466cc55SCy Schubert 	if (i->seconds == 0 && i->nanoseconds == 0)
103*a466cc55SCy Schubert 		return (ISC_TRUE);
104*a466cc55SCy Schubert 
105*a466cc55SCy Schubert 	return (ISC_FALSE);
106*a466cc55SCy Schubert }
107*a466cc55SCy Schubert 
108*a466cc55SCy Schubert 
109*a466cc55SCy Schubert /***
110*a466cc55SCy Schubert  *** Absolute Times
111*a466cc55SCy Schubert  ***/
112*a466cc55SCy Schubert 
113*a466cc55SCy Schubert static isc_time_t epoch = { 0, 0 };
114*a466cc55SCy Schubert isc_time_t *isc_time_epoch = &epoch;
115*a466cc55SCy Schubert 
116*a466cc55SCy Schubert void
isc_time_set(isc_time_t * t,unsigned int seconds,unsigned int nanoseconds)117*a466cc55SCy Schubert isc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
118*a466cc55SCy Schubert 	REQUIRE(t != NULL);
119*a466cc55SCy Schubert 	REQUIRE(nanoseconds < NS_PER_S);
120*a466cc55SCy Schubert 
121*a466cc55SCy Schubert 	t->seconds = seconds;
122*a466cc55SCy Schubert 	t->nanoseconds = nanoseconds;
123*a466cc55SCy Schubert }
124*a466cc55SCy Schubert 
125*a466cc55SCy Schubert void
isc_time_settoepoch(isc_time_t * t)126*a466cc55SCy Schubert isc_time_settoepoch(isc_time_t *t) {
127*a466cc55SCy Schubert 	REQUIRE(t != NULL);
128*a466cc55SCy Schubert 
129*a466cc55SCy Schubert 	t->seconds = 0;
130*a466cc55SCy Schubert 	t->nanoseconds = 0;
131*a466cc55SCy Schubert }
132*a466cc55SCy Schubert 
133*a466cc55SCy Schubert isc_boolean_t
isc_time_isepoch(const isc_time_t * t)134*a466cc55SCy Schubert isc_time_isepoch(const isc_time_t *t) {
135*a466cc55SCy Schubert 	REQUIRE(t != NULL);
136*a466cc55SCy Schubert 	INSIST(t->nanoseconds < NS_PER_S);
137*a466cc55SCy Schubert 
138*a466cc55SCy Schubert 	if (t->seconds == 0 && t->nanoseconds == 0)
139*a466cc55SCy Schubert 		return (ISC_TRUE);
140*a466cc55SCy Schubert 
141*a466cc55SCy Schubert 	return (ISC_FALSE);
142*a466cc55SCy Schubert }
143*a466cc55SCy Schubert 
144*a466cc55SCy Schubert 
145*a466cc55SCy Schubert isc_result_t
isc_time_now(isc_time_t * t)146*a466cc55SCy Schubert isc_time_now(isc_time_t *t) {
147*a466cc55SCy Schubert 	struct timeval tv;
148*a466cc55SCy Schubert 	char strbuf[ISC_STRERRORSIZE];
149*a466cc55SCy Schubert 
150*a466cc55SCy Schubert 	REQUIRE(t != NULL);
151*a466cc55SCy Schubert 
152*a466cc55SCy Schubert 	if (gettimeofday(&tv, NULL) == -1) {
153*a466cc55SCy Schubert 		isc__strerror(errno, strbuf, sizeof(strbuf));
154*a466cc55SCy Schubert 		UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
155*a466cc55SCy Schubert 		return (ISC_R_UNEXPECTED);
156*a466cc55SCy Schubert 	}
157*a466cc55SCy Schubert 
158*a466cc55SCy Schubert 	/*
159*a466cc55SCy Schubert 	 * Does POSIX guarantee the signedness of tv_sec and tv_usec?  If not,
160*a466cc55SCy Schubert 	 * then this test will generate warnings for platforms on which it is
161*a466cc55SCy Schubert 	 * unsigned.  In any event, the chances of any of these problems
162*a466cc55SCy Schubert 	 * happening are pretty much zero, but since the libisc library ensures
163*a466cc55SCy Schubert 	 * certain things to be true ...
164*a466cc55SCy Schubert 	 */
165*a466cc55SCy Schubert #if ISC_FIX_TV_USEC
166*a466cc55SCy Schubert 	fix_tv_usec(&tv);
167*a466cc55SCy Schubert 	if (tv.tv_sec < 0)
168*a466cc55SCy Schubert 		return (ISC_R_UNEXPECTED);
169*a466cc55SCy Schubert #else
170*a466cc55SCy Schubert 	if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
171*a466cc55SCy Schubert 		return (ISC_R_UNEXPECTED);
172*a466cc55SCy Schubert #endif
173*a466cc55SCy Schubert 
174*a466cc55SCy Schubert 	/*
175*a466cc55SCy Schubert 	 * Ensure the tv_sec value fits in t->seconds.
176*a466cc55SCy Schubert 	 */
177*a466cc55SCy Schubert 	if (sizeof(tv.tv_sec) > sizeof(t->seconds) &&
178*a466cc55SCy Schubert 	    ((tv.tv_sec | (unsigned int)-1) ^ (unsigned int)-1) != 0U)
179*a466cc55SCy Schubert 		return (ISC_R_RANGE);
180*a466cc55SCy Schubert 
181*a466cc55SCy Schubert 	t->seconds = tv.tv_sec;
182*a466cc55SCy Schubert 	t->nanoseconds = tv.tv_usec * NS_PER_US;
183*a466cc55SCy Schubert 
184*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
185*a466cc55SCy Schubert }
186*a466cc55SCy Schubert 
187*a466cc55SCy Schubert isc_result_t
isc_time_nowplusinterval(isc_time_t * t,const isc_interval_t * i)188*a466cc55SCy Schubert isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
189*a466cc55SCy Schubert 	struct timeval tv;
190*a466cc55SCy Schubert 	char strbuf[ISC_STRERRORSIZE];
191*a466cc55SCy Schubert 
192*a466cc55SCy Schubert 	REQUIRE(t != NULL);
193*a466cc55SCy Schubert 	REQUIRE(i != NULL);
194*a466cc55SCy Schubert 	INSIST(i->nanoseconds < NS_PER_S);
195*a466cc55SCy Schubert 
196*a466cc55SCy Schubert 	if (gettimeofday(&tv, NULL) == -1) {
197*a466cc55SCy Schubert 		isc__strerror(errno, strbuf, sizeof(strbuf));
198*a466cc55SCy Schubert 		UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
199*a466cc55SCy Schubert 		return (ISC_R_UNEXPECTED);
200*a466cc55SCy Schubert 	}
201*a466cc55SCy Schubert 
202*a466cc55SCy Schubert 	/*
203*a466cc55SCy Schubert 	 * Does POSIX guarantee the signedness of tv_sec and tv_usec?  If not,
204*a466cc55SCy Schubert 	 * then this test will generate warnings for platforms on which it is
205*a466cc55SCy Schubert 	 * unsigned.  In any event, the chances of any of these problems
206*a466cc55SCy Schubert 	 * happening are pretty much zero, but since the libisc library ensures
207*a466cc55SCy Schubert 	 * certain things to be true ...
208*a466cc55SCy Schubert 	 */
209*a466cc55SCy Schubert #if ISC_FIX_TV_USEC
210*a466cc55SCy Schubert 	fix_tv_usec(&tv);
211*a466cc55SCy Schubert 	if (tv.tv_sec < 0)
212*a466cc55SCy Schubert 		return (ISC_R_UNEXPECTED);
213*a466cc55SCy Schubert #else
214*a466cc55SCy Schubert 	if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
215*a466cc55SCy Schubert 		return (ISC_R_UNEXPECTED);
216*a466cc55SCy Schubert #endif
217*a466cc55SCy Schubert 
218*a466cc55SCy Schubert 	/*
219*a466cc55SCy Schubert 	 * Ensure the resulting seconds value fits in the size of an
220*a466cc55SCy Schubert 	 * unsigned int.  (It is written this way as a slight optimization;
221*a466cc55SCy Schubert 	 * note that even if both values == INT_MAX, then when added
222*a466cc55SCy Schubert 	 * and getting another 1 added below the result is UINT_MAX.)
223*a466cc55SCy Schubert 	 */
224*a466cc55SCy Schubert 	if ((tv.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
225*a466cc55SCy Schubert 	    ((long long)tv.tv_sec + i->seconds > UINT_MAX))
226*a466cc55SCy Schubert 		return (ISC_R_RANGE);
227*a466cc55SCy Schubert 
228*a466cc55SCy Schubert 	t->seconds = tv.tv_sec + i->seconds;
229*a466cc55SCy Schubert 	t->nanoseconds = tv.tv_usec * NS_PER_US + i->nanoseconds;
230*a466cc55SCy Schubert 	if (t->nanoseconds >= NS_PER_S) {
231*a466cc55SCy Schubert 		t->seconds++;
232*a466cc55SCy Schubert 		t->nanoseconds -= NS_PER_S;
233*a466cc55SCy Schubert 	}
234*a466cc55SCy Schubert 
235*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
236*a466cc55SCy Schubert }
237*a466cc55SCy Schubert 
238*a466cc55SCy Schubert int
isc_time_compare(const isc_time_t * t1,const isc_time_t * t2)239*a466cc55SCy Schubert isc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
240*a466cc55SCy Schubert 	REQUIRE(t1 != NULL && t2 != NULL);
241*a466cc55SCy Schubert 	INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
242*a466cc55SCy Schubert 
243*a466cc55SCy Schubert 	if (t1->seconds < t2->seconds)
244*a466cc55SCy Schubert 		return (-1);
245*a466cc55SCy Schubert 	if (t1->seconds > t2->seconds)
246*a466cc55SCy Schubert 		return (1);
247*a466cc55SCy Schubert 	if (t1->nanoseconds < t2->nanoseconds)
248*a466cc55SCy Schubert 		return (-1);
249*a466cc55SCy Schubert 	if (t1->nanoseconds > t2->nanoseconds)
250*a466cc55SCy Schubert 		return (1);
251*a466cc55SCy Schubert 	return (0);
252*a466cc55SCy Schubert }
253*a466cc55SCy Schubert 
254*a466cc55SCy Schubert isc_result_t
isc_time_add(const isc_time_t * t,const isc_interval_t * i,isc_time_t * result)255*a466cc55SCy Schubert isc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result)
256*a466cc55SCy Schubert {
257*a466cc55SCy Schubert 	REQUIRE(t != NULL && i != NULL && result != NULL);
258*a466cc55SCy Schubert 	INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
259*a466cc55SCy Schubert 
260*a466cc55SCy Schubert 	/*
261*a466cc55SCy Schubert 	 * Ensure the resulting seconds value fits in the size of an
262*a466cc55SCy Schubert 	 * unsigned int.  (It is written this way as a slight optimization;
263*a466cc55SCy Schubert 	 * note that even if both values == INT_MAX, then when added
264*a466cc55SCy Schubert 	 * and getting another 1 added below the result is UINT_MAX.)
265*a466cc55SCy Schubert 	 */
266*a466cc55SCy Schubert 	if ((t->seconds > INT_MAX || i->seconds > INT_MAX) &&
267*a466cc55SCy Schubert 	    ((long long)t->seconds + i->seconds > UINT_MAX))
268*a466cc55SCy Schubert 		return (ISC_R_RANGE);
269*a466cc55SCy Schubert 
270*a466cc55SCy Schubert 	result->seconds = t->seconds + i->seconds;
271*a466cc55SCy Schubert 	result->nanoseconds = t->nanoseconds + i->nanoseconds;
272*a466cc55SCy Schubert 	if (result->nanoseconds >= NS_PER_S) {
273*a466cc55SCy Schubert 		result->seconds++;
274*a466cc55SCy Schubert 		result->nanoseconds -= NS_PER_S;
275*a466cc55SCy Schubert 	}
276*a466cc55SCy Schubert 
277*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
278*a466cc55SCy Schubert }
279*a466cc55SCy Schubert 
280*a466cc55SCy Schubert isc_result_t
isc_time_subtract(const isc_time_t * t,const isc_interval_t * i,isc_time_t * result)281*a466cc55SCy Schubert isc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
282*a466cc55SCy Schubert 		  isc_time_t *result)
283*a466cc55SCy Schubert {
284*a466cc55SCy Schubert 	REQUIRE(t != NULL && i != NULL && result != NULL);
285*a466cc55SCy Schubert 	INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
286*a466cc55SCy Schubert 
287*a466cc55SCy Schubert 	if ((unsigned int)t->seconds < i->seconds ||
288*a466cc55SCy Schubert 	    ((unsigned int)t->seconds == i->seconds &&
289*a466cc55SCy Schubert 	     t->nanoseconds < i->nanoseconds))
290*a466cc55SCy Schubert 	    return (ISC_R_RANGE);
291*a466cc55SCy Schubert 
292*a466cc55SCy Schubert 	result->seconds = t->seconds - i->seconds;
293*a466cc55SCy Schubert 	if (t->nanoseconds >= i->nanoseconds)
294*a466cc55SCy Schubert 		result->nanoseconds = t->nanoseconds - i->nanoseconds;
295*a466cc55SCy Schubert 	else {
296*a466cc55SCy Schubert 		result->nanoseconds = NS_PER_S - i->nanoseconds +
297*a466cc55SCy Schubert 			t->nanoseconds;
298*a466cc55SCy Schubert 		result->seconds--;
299*a466cc55SCy Schubert 	}
300*a466cc55SCy Schubert 
301*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
302*a466cc55SCy Schubert }
303*a466cc55SCy Schubert 
304*a466cc55SCy Schubert isc_uint64_t
isc_time_microdiff(const isc_time_t * t1,const isc_time_t * t2)305*a466cc55SCy Schubert isc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
306*a466cc55SCy Schubert 	isc_uint64_t i1, i2, i3;
307*a466cc55SCy Schubert 
308*a466cc55SCy Schubert 	REQUIRE(t1 != NULL && t2 != NULL);
309*a466cc55SCy Schubert 	INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
310*a466cc55SCy Schubert 
311*a466cc55SCy Schubert 	i1 = (isc_uint64_t)t1->seconds * NS_PER_S + t1->nanoseconds;
312*a466cc55SCy Schubert 	i2 = (isc_uint64_t)t2->seconds * NS_PER_S + t2->nanoseconds;
313*a466cc55SCy Schubert 
314*a466cc55SCy Schubert 	if (i1 <= i2)
315*a466cc55SCy Schubert 		return (0);
316*a466cc55SCy Schubert 
317*a466cc55SCy Schubert 	i3 = i1 - i2;
318*a466cc55SCy Schubert 
319*a466cc55SCy Schubert 	/*
320*a466cc55SCy Schubert 	 * Convert to microseconds.
321*a466cc55SCy Schubert 	 */
322*a466cc55SCy Schubert 	i3 /= NS_PER_US;
323*a466cc55SCy Schubert 
324*a466cc55SCy Schubert 	return (i3);
325*a466cc55SCy Schubert }
326*a466cc55SCy Schubert 
327*a466cc55SCy Schubert isc_uint32_t
isc_time_seconds(const isc_time_t * t)328*a466cc55SCy Schubert isc_time_seconds(const isc_time_t *t) {
329*a466cc55SCy Schubert 	REQUIRE(t != NULL);
330*a466cc55SCy Schubert 	INSIST(t->nanoseconds < NS_PER_S);
331*a466cc55SCy Schubert 
332*a466cc55SCy Schubert 	return ((isc_uint32_t)t->seconds);
333*a466cc55SCy Schubert }
334*a466cc55SCy Schubert 
335*a466cc55SCy Schubert isc_result_t
isc_time_secondsastimet(const isc_time_t * t,time_t * secondsp)336*a466cc55SCy Schubert isc_time_secondsastimet(const isc_time_t *t, time_t *secondsp) {
337*a466cc55SCy Schubert 	time_t seconds;
338*a466cc55SCy Schubert 
339*a466cc55SCy Schubert 	REQUIRE(t != NULL);
340*a466cc55SCy Schubert 	INSIST(t->nanoseconds < NS_PER_S);
341*a466cc55SCy Schubert 
342*a466cc55SCy Schubert 	/*
343*a466cc55SCy Schubert 	 * Ensure that the number of seconds represented by t->seconds
344*a466cc55SCy Schubert 	 * can be represented by a time_t.  Since t->seconds is an unsigned
345*a466cc55SCy Schubert 	 * int and since time_t is mostly opaque, this is trickier than
346*a466cc55SCy Schubert 	 * it seems.  (This standardized opaqueness of time_t is *very*
347*a466cc55SCy Schubert 	 * frustrating; time_t is not even limited to being an integral
348*a466cc55SCy Schubert 	 * type.)
349*a466cc55SCy Schubert 	 *
350*a466cc55SCy Schubert 	 * The mission, then, is to avoid generating any kind of warning
351*a466cc55SCy Schubert 	 * about "signed versus unsigned" while trying to determine if the
352*a466cc55SCy Schubert 	 * the unsigned int t->seconds is out range for tv_sec, which is
353*a466cc55SCy Schubert 	 * pretty much only true if time_t is a signed integer of the same
354*a466cc55SCy Schubert 	 * size as the return value of isc_time_seconds.
355*a466cc55SCy Schubert 	 *
356*a466cc55SCy Schubert 	 * If the paradox in the if clause below is true, t->seconds is out
357*a466cc55SCy Schubert 	 * of range for time_t.
358*a466cc55SCy Schubert 	 */
359*a466cc55SCy Schubert 	seconds = (time_t)t->seconds;
360*a466cc55SCy Schubert 
361*a466cc55SCy Schubert 	INSIST(sizeof(unsigned int) == sizeof(isc_uint32_t));
362*a466cc55SCy Schubert 	INSIST(sizeof(time_t) >= sizeof(isc_uint32_t));
363*a466cc55SCy Schubert 
364*a466cc55SCy Schubert 	if (t->seconds > (~0U>>1) && seconds <= (time_t)(~0U>>1))
365*a466cc55SCy Schubert 		return (ISC_R_RANGE);
366*a466cc55SCy Schubert 
367*a466cc55SCy Schubert 	*secondsp = seconds;
368*a466cc55SCy Schubert 
369*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
370*a466cc55SCy Schubert }
371*a466cc55SCy Schubert 
372*a466cc55SCy Schubert isc_uint32_t
isc_time_nanoseconds(const isc_time_t * t)373*a466cc55SCy Schubert isc_time_nanoseconds(const isc_time_t *t) {
374*a466cc55SCy Schubert 	REQUIRE(t != NULL);
375*a466cc55SCy Schubert 
376*a466cc55SCy Schubert 	ENSURE(t->nanoseconds < NS_PER_S);
377*a466cc55SCy Schubert 
378*a466cc55SCy Schubert 	return ((isc_uint32_t)t->nanoseconds);
379*a466cc55SCy Schubert }
380*a466cc55SCy Schubert 
381*a466cc55SCy Schubert void
isc_time_formattimestamp(const isc_time_t * t,char * buf,unsigned int len)382*a466cc55SCy Schubert isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
383*a466cc55SCy Schubert 	time_t now;
384*a466cc55SCy Schubert 	unsigned int flen;
385*a466cc55SCy Schubert 
386*a466cc55SCy Schubert 	REQUIRE(len > 0);
387*a466cc55SCy Schubert 
388*a466cc55SCy Schubert 	now = (time_t) t->seconds;
389*a466cc55SCy Schubert 	flen = strftime(buf, len, "%d-%b-%Y %X", localtime(&now));
390*a466cc55SCy Schubert 	INSIST(flen < len);
391*a466cc55SCy Schubert 	if (flen != 0)
392*a466cc55SCy Schubert 		snprintf(buf + flen, len - flen,
393*a466cc55SCy Schubert 			 ".%03u", t->nanoseconds / 1000000);
394*a466cc55SCy Schubert 	else
395*a466cc55SCy Schubert 		snprintf(buf, len, "99-Bad-9999 99:99:99.999");
396*a466cc55SCy Schubert }
397*a466cc55SCy Schubert 
398*a466cc55SCy Schubert void
isc_time_formathttptimestamp(const isc_time_t * t,char * buf,unsigned int len)399*a466cc55SCy Schubert isc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
400*a466cc55SCy Schubert 	time_t now;
401*a466cc55SCy Schubert 	unsigned int flen;
402*a466cc55SCy Schubert 
403*a466cc55SCy Schubert 	REQUIRE(len > 0);
404*a466cc55SCy Schubert 
405*a466cc55SCy Schubert 	now = (time_t)t->seconds;
406*a466cc55SCy Schubert 	flen = strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&now));
407*a466cc55SCy Schubert 	INSIST(flen < len);
408*a466cc55SCy Schubert }
409*a466cc55SCy Schubert 
410*a466cc55SCy Schubert void
isc_time_formatISO8601(const isc_time_t * t,char * buf,unsigned int len)411*a466cc55SCy Schubert isc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) {
412*a466cc55SCy Schubert 	time_t now;
413*a466cc55SCy Schubert 	unsigned int flen;
414*a466cc55SCy Schubert 
415*a466cc55SCy Schubert 	REQUIRE(len > 0);
416*a466cc55SCy Schubert 
417*a466cc55SCy Schubert 	now = (time_t)t->seconds;
418*a466cc55SCy Schubert 	flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
419*a466cc55SCy Schubert 	INSIST(flen < len);
420*a466cc55SCy Schubert }
421