1290001Sglebius/*
2290001Sglebius * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3290001Sglebius * Copyright (C) 1999-2001  Internet Software Consortium.
4290001Sglebius *
5290001Sglebius * Permission to use, copy, modify, and/or distribute this software for any
6290001Sglebius * purpose with or without fee is hereby granted, provided that the above
7290001Sglebius * copyright notice and this permission notice appear in all copies.
8290001Sglebius *
9290001Sglebius * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10290001Sglebius * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11290001Sglebius * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12290001Sglebius * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13290001Sglebius * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14290001Sglebius * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15290001Sglebius * PERFORMANCE OF THIS SOFTWARE.
16290001Sglebius */
17290001Sglebius
18290001Sglebius/* $Id: stdtime.c,v 1.19 2007/06/19 23:47:18 tbox Exp $ */
19290001Sglebius
20290001Sglebius/*! \file */
21290001Sglebius
22290001Sglebius#include <config.h>
23290001Sglebius
24290001Sglebius#include <stddef.h>	/* NULL */
25290001Sglebius#include <stdlib.h>	/* NULL */
26290001Sglebius#include <syslog.h>
27290001Sglebius
28290001Sglebius#include <sys/time.h>
29290001Sglebius
30290001Sglebius#include <isc/stdtime.h>
31290001Sglebius#include <isc/util.h>
32290001Sglebius
33290001Sglebius#ifndef ISC_FIX_TV_USEC
34290001Sglebius#define ISC_FIX_TV_USEC 1
35290001Sglebius#endif
36290001Sglebius
37290001Sglebius#define US_PER_S 1000000
38290001Sglebius
39290001Sglebius#if ISC_FIX_TV_USEC
40290001Sglebiusstatic inline void
41290001Sglebiusfix_tv_usec(struct timeval *tv) {
42290001Sglebius	isc_boolean_t fixed = ISC_FALSE;
43290001Sglebius
44290001Sglebius	if (tv->tv_usec < 0) {
45290001Sglebius		fixed = ISC_TRUE;
46290001Sglebius		do {
47290001Sglebius			tv->tv_sec -= 1;
48290001Sglebius			tv->tv_usec += US_PER_S;
49290001Sglebius		} while (tv->tv_usec < 0);
50290001Sglebius	} else if (tv->tv_usec >= US_PER_S) {
51290001Sglebius		fixed = ISC_TRUE;
52290001Sglebius		do {
53290001Sglebius			tv->tv_sec += 1;
54290001Sglebius			tv->tv_usec -= US_PER_S;
55290001Sglebius		} while (tv->tv_usec >=US_PER_S);
56290001Sglebius	}
57290001Sglebius	/*
58290001Sglebius	 * Call syslog directly as we are called from the logging functions.
59290001Sglebius	 */
60290001Sglebius	if (fixed)
61290001Sglebius		(void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
62290001Sglebius}
63290001Sglebius#endif
64290001Sglebius
65290001Sglebiusvoid
66290001Sglebiusisc_stdtime_get(isc_stdtime_t *t) {
67290001Sglebius	struct timeval tv;
68290001Sglebius
69290001Sglebius	/*
70290001Sglebius	 * Set 't' to the number of seconds since 00:00:00 UTC, January 1,
71290001Sglebius	 * 1970.
72290001Sglebius	 */
73290001Sglebius
74290001Sglebius	REQUIRE(t != NULL);
75290001Sglebius
76290001Sglebius	RUNTIME_CHECK(gettimeofday(&tv, NULL) != -1);
77290001Sglebius
78290001Sglebius#if ISC_FIX_TV_USEC
79290001Sglebius	fix_tv_usec(&tv);
80290001Sglebius	INSIST(tv.tv_usec >= 0);
81290001Sglebius#else
82290001Sglebius	INSIST(tv.tv_usec >= 0 && tv.tv_usec < US_PER_S);
83290001Sglebius#endif
84290001Sglebius
85290001Sglebius	*t = (unsigned int)tv.tv_sec;
86290001Sglebius}
87