120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 1999-2001, 2004, 2005, 2007, 2016 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley */
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater/* $Id: stdtime.c,v 1.19 2007/06/19 23:47:18 tbox Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley#include <config.h>
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley
e7fb847ed570dd8c1bcdacabb3d69bd81feb79aeMark Andrews#include <stddef.h> /* NULL */
382c4ce5a3b9f82e42027d8e8614aed73791075cMark Andrews#include <stdlib.h> /* NULL */
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews#include <syslog.h>
382c4ce5a3b9f82e42027d8e8614aed73791075cMark Andrews
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley#include <sys/time.h>
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley#include <isc/stdtime.h>
364a82f7c25b62967678027043425201a5e5171aBob Halley#include <isc/util.h>
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews#ifndef ISC_FIX_TV_USEC
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews#define ISC_FIX_TV_USEC 1
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews#endif
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews#define US_PER_S 1000000
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews#if ISC_FIX_TV_USEC
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrewsstatic inline void
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrewsfix_tv_usec(struct timeval *tv) {
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews isc_boolean_t fixed = ISC_FALSE;
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews if (tv->tv_usec < 0) {
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews fixed = ISC_TRUE;
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews do {
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews tv->tv_sec -= 1;
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews tv->tv_usec += US_PER_S;
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews } while (tv->tv_usec < 0);
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews } else if (tv->tv_usec >= US_PER_S) {
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews fixed = ISC_TRUE;
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews do {
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews tv->tv_sec += 1;
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews tv->tv_usec -= US_PER_S;
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews } while (tv->tv_usec >=US_PER_S);
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews }
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews /*
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews * Call syslog directly as we are called from the logging functions.
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews */
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews if (fixed)
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson (void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews}
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews#endif
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews
58aaab3687aac838542ee4ef65a9c094a5d34ab0Michael Graffvoid
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrenceisc_stdtime_get(isc_stdtime_t *t) {
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley struct timeval tv;
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley /*
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley * Set 't' to the number of seconds since 00:00:00 UTC, January 1,
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley * 1970.
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley REQUIRE(t != NULL);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
58aaab3687aac838542ee4ef65a9c094a5d34ab0Michael Graff RUNTIME_CHECK(gettimeofday(&tv, NULL) != -1);
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews#if ISC_FIX_TV_USEC
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews fix_tv_usec(&tv);
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews INSIST(tv.tv_usec >= 0);
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews#else
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews INSIST(tv.tv_usec >= 0 && tv.tv_usec < US_PER_S);
bc508906db43dda7eab0988348dd0ae3f3023a9bMark Andrews#endif
ce1b0e505e812cca9e1d3fd82cf9f1372b4f29d3Mark Andrews
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley *t = (unsigned int)tv.tv_sec;
120e76fc5442dcec479bccf5b4864e39c969f07fBob Halley}