"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.16.7/lib/isc/unix/stdtime.c" (4 Sep 2020, 1651 Bytes) of package /linux/misc/dns/bind9/9.16.7/bind-9.16.7.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stdtime.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 9.17.2_vs_9.17.3.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 /*! \file */
   13 
   14 #include <errno.h>
   15 #include <stdbool.h>
   16 #include <stddef.h> /* NULL */
   17 #include <stdlib.h> /* NULL */
   18 #include <syslog.h>
   19 #include <time.h>
   20 
   21 #include <isc/stdtime.h>
   22 #include <isc/strerr.h>
   23 #include <isc/util.h>
   24 
   25 #define NS_PER_S 1000000000 /*%< Nanoseconds per second. */
   26 
   27 #if defined(CLOCK_REALTIME_COARSE)
   28 #define CLOCKSOURCE CLOCK_REALTIME_COARSE
   29 #elif defined(CLOCK_REALTIME_FAST)
   30 #define CLOCKSOURCE CLOCK_REALTIME_FAST
   31 #else /* if defined(CLOCK_REALTIME_COARSE) */
   32 #define CLOCKSOURCE CLOCK_REALTIME
   33 #endif /* if defined(CLOCK_REALTIME_COARSE) */
   34 
   35 void
   36 isc_stdtime_get(isc_stdtime_t *t) {
   37     REQUIRE(t != NULL);
   38 
   39     struct timespec ts;
   40 
   41     if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
   42         char strbuf[ISC_STRERRORSIZE];
   43         strerror_r(errno, strbuf, sizeof(strbuf));
   44         isc_error_fatal(__FILE__, __LINE__, "clock_gettime failed: %s",
   45                 strbuf);
   46     }
   47 
   48     REQUIRE(ts.tv_sec > 0 && ts.tv_nsec >= 0 && ts.tv_nsec < NS_PER_S);
   49 
   50     *t = (isc_stdtime_t)ts.tv_sec;
   51 }
   52 
   53 void
   54 isc_stdtime_tostring(isc_stdtime_t t, char *out, size_t outlen) {
   55     time_t when;
   56 
   57     REQUIRE(out != NULL);
   58     REQUIRE(outlen >= 26);
   59 
   60     UNUSED(outlen);
   61 
   62     /* time_t and isc_stdtime_t might be different sizes */
   63     when = t;
   64     INSIST((ctime_r(&when, out) != NULL));
   65     *(out + strlen(out) - 1) = '\0';
   66 }