time.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007, 2009-2012, 2014  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1998-2003  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id$ */
00019 
00020 /*! \file */
00021 
00022 #include <config.h>
00023 
00024 #include <stdio.h>
00025 #include <isc/string.h>         /* Required for HP/UX (and others?) */
00026 #include <time.h>
00027 #include <ctype.h>
00028 
00029 #include <isc/print.h>
00030 #include <isc/region.h>
00031 #include <isc/serial.h>
00032 #include <isc/stdtime.h>
00033 #include <isc/util.h>
00034 
00035 #include <dns/result.h>
00036 #include <dns/time.h>
00037 
00038 static const int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
00039 
00040 isc_result_t
00041 dns_time64_totext(isc_int64_t t, isc_buffer_t *target) {
00042         struct tm tm;
00043         char buf[sizeof("YYYYMMDDHHMMSS")];
00044         int secs;
00045         unsigned int l;
00046         isc_region_t region;
00047 
00048 /*
00049  * Warning. Do NOT use arguments with side effects with these macros.
00050  */
00051 #define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
00052 #define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400)
00053 #define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400)
00054 
00055         tm.tm_year = 70;
00056         while (t < 0) {
00057                 if (tm.tm_year == 0)
00058                         return (ISC_R_RANGE);
00059                 tm.tm_year--;
00060                 secs = year_secs(tm.tm_year + 1900);
00061                 t += secs;
00062         }
00063         while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
00064                 t -= secs;
00065                 tm.tm_year++;
00066                 if (tm.tm_year + 1900 > 9999)
00067                         return (ISC_R_RANGE);
00068         }
00069         tm.tm_mon = 0;
00070         while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
00071                 t -= secs;
00072                 tm.tm_mon++;
00073         }
00074         tm.tm_mday = 1;
00075         while (86400 <= t) {
00076                 t -= 86400;
00077                 tm.tm_mday++;
00078         }
00079         tm.tm_hour = 0;
00080         while (3600 <= t) {
00081                 t -= 3600;
00082                 tm.tm_hour++;
00083         }
00084         tm.tm_min = 0;
00085         while (60 <= t) {
00086                 t -= 60;
00087                 tm.tm_min++;
00088         }
00089         tm.tm_sec = (int)t;
00090                                  /* yyyy  mm  dd  HH  MM  SS */
00091         snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",
00092                  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
00093                  tm.tm_hour, tm.tm_min, tm.tm_sec);
00094 
00095         isc_buffer_availableregion(target, &region);
00096         l = strlen(buf);
00097 
00098         if (l > region.length)
00099                 return (ISC_R_NOSPACE);
00100 
00101         memmove(region.base, buf, l);
00102         isc_buffer_add(target, l);
00103         return (ISC_R_SUCCESS);
00104 }
00105 
00106 isc_int64_t
00107 dns_time64_from32(isc_uint32_t value) {
00108         isc_stdtime_t now;
00109         isc_int64_t start;
00110         isc_int64_t t;
00111 
00112         /*
00113          * Adjust the time to the closest epoch.  This should be changed
00114          * to use a 64-bit counterpart to isc_stdtime_get() if one ever
00115          * is defined, but even the current code is good until the year
00116          * 2106.
00117          */
00118         isc_stdtime_get(&now);
00119         start = (isc_int64_t) now;
00120         if (isc_serial_gt(value, now))
00121                 t = start + (value - now);
00122         else
00123                 t = start - (now - value);
00124 
00125         return (t);
00126 }
00127 
00128 isc_result_t
00129 dns_time32_totext(isc_uint32_t value, isc_buffer_t *target) {
00130         return (dns_time64_totext(dns_time64_from32(value), target));
00131 }
00132 
00133 isc_result_t
00134 dns_time64_fromtext(const char *source, isc_int64_t *target) {
00135         int year, month, day, hour, minute, second;
00136         isc_int64_t value;
00137         int secs;
00138         int i;
00139 
00140 #define RANGE(min, max, value) \
00141         do { \
00142                 if (value < (min) || value > (max)) \
00143                         return (ISC_R_RANGE); \
00144         } while (0)
00145 
00146         if (strlen(source) != 14U)
00147                 return (DNS_R_SYNTAX);
00148         /*
00149          * Confirm the source only consists digits.  sscanf() allows some
00150          * minor exceptions.
00151          */
00152         for (i = 0; i < 14; i++) {
00153                 if (!isdigit((unsigned char)source[i]))
00154                         return (DNS_R_SYNTAX);
00155         }
00156         if (sscanf(source, "%4d%2d%2d%2d%2d%2d",
00157                    &year, &month, &day, &hour, &minute, &second) != 6)
00158                 return (DNS_R_SYNTAX);
00159 
00160         RANGE(0, 9999, year);
00161         RANGE(1, 12, month);
00162         RANGE(1, days[month - 1] +
00163                  ((month == 2 && is_leap(year)) ? 1 : 0), day);
00164 #ifdef __COVERITY__
00165         /*
00166          * Use a simplified range to silence Coverity warning (in
00167          * arithmetic with day below).
00168          */
00169         RANGE(1, 31, day);
00170 #endif /* __COVERITY__ */
00171 
00172         RANGE(0, 23, hour);
00173         RANGE(0, 59, minute);
00174         RANGE(0, 60, second);           /* 60 == leap second. */
00175 
00176         /*
00177          * Calculate seconds from epoch.
00178          * Note: this uses a idealized calendar.
00179          */
00180         value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
00181         for (i = 0; i < (month - 1); i++)
00182                 value += days[i] * 86400;
00183         if (is_leap(year) && month > 2)
00184                 value += 86400;
00185         if (year < 1970) {
00186                 for (i = 1969; i >= year; i--) {
00187                         secs = (is_leap(i) ? 366 : 365) * 86400;
00188                         value -= secs;
00189                 }
00190         } else {
00191                 for (i = 1970; i < year; i++) {
00192                         secs = (is_leap(i) ? 366 : 365) * 86400;
00193                         value += secs;
00194                 }
00195         }
00196 
00197         *target = value;
00198         return (ISC_R_SUCCESS);
00199 }
00200 
00201 isc_result_t
00202 dns_time32_fromtext(const char *source, isc_uint32_t *target) {
00203         isc_int64_t value64;
00204         isc_result_t result;
00205         result = dns_time64_fromtext(source, &value64);
00206         if (result != ISC_R_SUCCESS)
00207                 return (result);
00208         *target = (isc_uint32_t)value64;
00209 
00210         return (ISC_R_SUCCESS);
00211 }

Generated on Tue Apr 28 17:41:06 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha