xref: /freebsd/contrib/ntp/libntp/lib/isc/unix/stdtime.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert  * Copyright (C) 1999-2001  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: stdtime.c,v 1.19 2007/06/19 23:47:18 tbox Exp $ */
19*a466cc55SCy Schubert 
20*a466cc55SCy Schubert /*! \file */
21*a466cc55SCy Schubert 
22*a466cc55SCy Schubert #include <config.h>
23*a466cc55SCy Schubert 
24*a466cc55SCy Schubert #include <stddef.h>	/* NULL */
25*a466cc55SCy Schubert #include <stdlib.h>	/* NULL */
26*a466cc55SCy Schubert #include <syslog.h>
27*a466cc55SCy Schubert 
28*a466cc55SCy Schubert #include <sys/time.h>
29*a466cc55SCy Schubert 
30*a466cc55SCy Schubert #include <isc/stdtime.h>
31*a466cc55SCy Schubert #include <isc/util.h>
32*a466cc55SCy Schubert 
33*a466cc55SCy Schubert #ifndef ISC_FIX_TV_USEC
34*a466cc55SCy Schubert #define ISC_FIX_TV_USEC 1
35*a466cc55SCy Schubert #endif
36*a466cc55SCy Schubert 
37*a466cc55SCy Schubert #define US_PER_S 1000000
38*a466cc55SCy Schubert 
39*a466cc55SCy Schubert #if ISC_FIX_TV_USEC
40*a466cc55SCy Schubert static inline void
fix_tv_usec(struct timeval * tv)41*a466cc55SCy Schubert fix_tv_usec(struct timeval *tv) {
42*a466cc55SCy Schubert 	isc_boolean_t fixed = ISC_FALSE;
43*a466cc55SCy Schubert 
44*a466cc55SCy Schubert 	if (tv->tv_usec < 0) {
45*a466cc55SCy Schubert 		fixed = ISC_TRUE;
46*a466cc55SCy Schubert 		do {
47*a466cc55SCy Schubert 			tv->tv_sec -= 1;
48*a466cc55SCy Schubert 			tv->tv_usec += US_PER_S;
49*a466cc55SCy Schubert 		} while (tv->tv_usec < 0);
50*a466cc55SCy Schubert 	} else if (tv->tv_usec >= US_PER_S) {
51*a466cc55SCy Schubert 		fixed = ISC_TRUE;
52*a466cc55SCy Schubert 		do {
53*a466cc55SCy Schubert 			tv->tv_sec += 1;
54*a466cc55SCy Schubert 			tv->tv_usec -= US_PER_S;
55*a466cc55SCy Schubert 		} while (tv->tv_usec >=US_PER_S);
56*a466cc55SCy Schubert 	}
57*a466cc55SCy Schubert 	/*
58*a466cc55SCy Schubert 	 * Call syslog directly as we are called from the logging functions.
59*a466cc55SCy Schubert 	 */
60*a466cc55SCy Schubert 	if (fixed)
61*a466cc55SCy Schubert 		(void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
62*a466cc55SCy Schubert }
63*a466cc55SCy Schubert #endif
64*a466cc55SCy Schubert 
65*a466cc55SCy Schubert void
isc_stdtime_get(isc_stdtime_t * t)66*a466cc55SCy Schubert isc_stdtime_get(isc_stdtime_t *t) {
67*a466cc55SCy Schubert 	struct timeval tv;
68*a466cc55SCy Schubert 
69*a466cc55SCy Schubert 	/*
70*a466cc55SCy Schubert 	 * Set 't' to the number of seconds since 00:00:00 UTC, January 1,
71*a466cc55SCy Schubert 	 * 1970.
72*a466cc55SCy Schubert 	 */
73*a466cc55SCy Schubert 
74*a466cc55SCy Schubert 	REQUIRE(t != NULL);
75*a466cc55SCy Schubert 
76*a466cc55SCy Schubert 	RUNTIME_CHECK(gettimeofday(&tv, NULL) != -1);
77*a466cc55SCy Schubert 
78*a466cc55SCy Schubert #if ISC_FIX_TV_USEC
79*a466cc55SCy Schubert 	fix_tv_usec(&tv);
80*a466cc55SCy Schubert 	INSIST(tv.tv_usec >= 0);
81*a466cc55SCy Schubert #else
82*a466cc55SCy Schubert 	INSIST(tv.tv_usec >= 0 && tv.tv_usec < US_PER_S);
83*a466cc55SCy Schubert #endif
84*a466cc55SCy Schubert 
85*a466cc55SCy Schubert 	*t = (unsigned int)tv.tv_sec;
86*a466cc55SCy Schubert }
87