time.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor * Copyright (C) 1998-2005, 2007, 2009-2012, 2014, 2016 Internet Systems Consortium, Inc. ("ISC")
ea08352ef148842f2db8c00add52444487c0f638lgentis * This Source Code Form is subject to the terms of the Mozilla Public
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor * License, v. 2.0. If a copy of the MPL was not distributed with this
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor * file, You can obtain one at http://mozilla.org/MPL/2.0/.
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor/*! \file */
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor#include <isc/string.h> /* Required for HP/UX (and others?) */
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzorstatic const int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzordns_time64_totext(isc_int64_t t, isc_buffer_t *target) {
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor unsigned int l;
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor * Warning. Do NOT use arguments with side effects with these macros.
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor#define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor#define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400)
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor#define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400)
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor while (t < 0) {
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor while (86400 <= t) {
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor t -= 86400;
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor while (3600 <= t) {
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor while (60 <= t) {
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor /* yyyy mm dd HH MM SS */
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor * Adjust the time to the closest epoch. This should be changed
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor * to use a 64-bit counterpart to isc_stdtime_get() if one ever
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor * is defined, but even the current code is good until the year
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor return (t);
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzordns_time32_totext(isc_uint32_t value, isc_buffer_t *target) {
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor return (dns_time64_totext(dns_time64_from32(value), target));
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzordns_time64_fromtext(const char *source, isc_int64_t *target) {
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor } while (0)
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor * Confirm the source only consists digits. sscanf() allows some
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor * minor exceptions.
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor for (i = 0; i < 14; i++) {
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor * Use a simplified range to silence Coverity warning (in
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor * arithmetic with day below).
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor#endif /* __COVERITY__ */
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor * Calculate seconds from epoch.
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor * Note: this uses a idealized calendar.
ceb80b53389aae381f54a02e75cca69077c4fb2fgryzor value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);