xref: /netbsd-src/external/bsd/ntp/dist/libntp/lib/isc/unix/stdtime.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: stdtime.c,v 1.2 2024/08/18 20:47:16 christos Exp $	*/
2897be3a4Schristos 
3897be3a4Schristos /*
4897be3a4Schristos  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
5897be3a4Schristos  * Copyright (C) 1999-2001  Internet Software Consortium.
6897be3a4Schristos  *
7897be3a4Schristos  * Permission to use, copy, modify, and/or distribute this software for any
8897be3a4Schristos  * purpose with or without fee is hereby granted, provided that the above
9897be3a4Schristos  * copyright notice and this permission notice appear in all copies.
10897be3a4Schristos  *
11897be3a4Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12897be3a4Schristos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13897be3a4Schristos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14897be3a4Schristos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15897be3a4Schristos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16897be3a4Schristos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17897be3a4Schristos  * PERFORMANCE OF THIS SOFTWARE.
18897be3a4Schristos  */
19897be3a4Schristos 
20897be3a4Schristos /* Id: stdtime.c,v 1.19 2007/06/19 23:47:18 tbox Exp  */
21897be3a4Schristos 
22897be3a4Schristos /*! \file */
23897be3a4Schristos 
24897be3a4Schristos #include <config.h>
25897be3a4Schristos 
26897be3a4Schristos #include <stddef.h>	/* NULL */
27897be3a4Schristos #include <stdlib.h>	/* NULL */
28897be3a4Schristos #include <syslog.h>
29897be3a4Schristos 
30897be3a4Schristos #include <sys/time.h>
31897be3a4Schristos 
32897be3a4Schristos #include <isc/stdtime.h>
33897be3a4Schristos #include <isc/util.h>
34897be3a4Schristos 
35897be3a4Schristos #ifndef ISC_FIX_TV_USEC
36897be3a4Schristos #define ISC_FIX_TV_USEC 1
37897be3a4Schristos #endif
38897be3a4Schristos 
39897be3a4Schristos #define US_PER_S 1000000
40897be3a4Schristos 
41897be3a4Schristos #if ISC_FIX_TV_USEC
42897be3a4Schristos static inline void
43897be3a4Schristos fix_tv_usec(struct timeval *tv) {
44897be3a4Schristos 	isc_boolean_t fixed = ISC_FALSE;
45897be3a4Schristos 
46897be3a4Schristos 	if (tv->tv_usec < 0) {
47897be3a4Schristos 		fixed = ISC_TRUE;
48897be3a4Schristos 		do {
49897be3a4Schristos 			tv->tv_sec -= 1;
50897be3a4Schristos 			tv->tv_usec += US_PER_S;
51897be3a4Schristos 		} while (tv->tv_usec < 0);
52897be3a4Schristos 	} else if (tv->tv_usec >= US_PER_S) {
53897be3a4Schristos 		fixed = ISC_TRUE;
54897be3a4Schristos 		do {
55897be3a4Schristos 			tv->tv_sec += 1;
56897be3a4Schristos 			tv->tv_usec -= US_PER_S;
57897be3a4Schristos 		} while (tv->tv_usec >=US_PER_S);
58897be3a4Schristos 	}
59897be3a4Schristos 	/*
60897be3a4Schristos 	 * Call syslog directly as we are called from the logging functions.
61897be3a4Schristos 	 */
62897be3a4Schristos 	if (fixed)
63897be3a4Schristos 		(void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
64897be3a4Schristos }
65897be3a4Schristos #endif
66897be3a4Schristos 
67897be3a4Schristos void
68897be3a4Schristos isc_stdtime_get(isc_stdtime_t *t) {
69897be3a4Schristos 	struct timeval tv;
70897be3a4Schristos 
71897be3a4Schristos 	/*
72897be3a4Schristos 	 * Set 't' to the number of seconds since 00:00:00 UTC, January 1,
73897be3a4Schristos 	 * 1970.
74897be3a4Schristos 	 */
75897be3a4Schristos 
76897be3a4Schristos 	REQUIRE(t != NULL);
77897be3a4Schristos 
78897be3a4Schristos 	RUNTIME_CHECK(gettimeofday(&tv, NULL) != -1);
79897be3a4Schristos 
80897be3a4Schristos #if ISC_FIX_TV_USEC
81897be3a4Schristos 	fix_tv_usec(&tv);
82897be3a4Schristos 	INSIST(tv.tv_usec >= 0);
83897be3a4Schristos #else
84897be3a4Schristos 	INSIST(tv.tv_usec >= 0 && tv.tv_usec < US_PER_S);
85897be3a4Schristos #endif
86897be3a4Schristos 
87897be3a4Schristos 	*t = (unsigned int)tv.tv_sec;
88897be3a4Schristos }
89