tm.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2014  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /*-
00018  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
00019  * All rights reserved.
00020  *
00021  * This code was contributed to The NetBSD Foundation by Klaus Klein.
00022  *
00023  * Redistribution and use in source and binary forms, with or without
00024  * modification, are permitted provided that the following conditions
00025  * are met:
00026  * 1. Redistributions of source code must retain the above copyright
00027  *    notice, this list of conditions and the following disclaimer.
00028  * 2. Redistributions in binary form must reproduce the above copyright
00029  *    notice, this list of conditions and the following disclaimer in the
00030  *    documentation and/or other materials provided with the distribution.
00031  *
00032  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
00033  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00034  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00035  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
00036  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00037  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00038  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00039  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00040  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00041  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00042  * POSSIBILITY OF SUCH DAMAGE.
00043  */
00044 
00045 #include <config.h>
00046 
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 #include <string.h>
00050 #include <time.h>
00051 #include <ctype.h>
00052 
00053 #include <isc/tm.h>
00054 #include <isc/util.h>
00055 
00056 /*
00057  * Portable conversion routines for struct tm, replacing
00058  * timegm() and strptime(), which are not available on all
00059  * platforms and don't always behave the same way when they
00060  * are.
00061  */
00062 
00063 /*
00064  * We do not implement alternate representations. However, we always
00065  * check whether a given modifier is allowed for a certain conversion.
00066  */
00067 #define ALT_E                   0x01
00068 #define ALT_O                   0x02
00069 #define LEGAL_ALT(x)            { if (alt_format & ~(x)) return (0); }
00070 
00071 #ifndef TM_YEAR_BASE
00072 #define TM_YEAR_BASE 1900
00073 #endif
00074 
00075 static const char *day[7] = {
00076         "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
00077         "Friday", "Saturday"
00078 };
00079 static const char *abday[7] = {
00080         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
00081 };
00082 static const char *mon[12] = {
00083         "January", "February", "March", "April", "May", "June", "July",
00084         "August", "September", "October", "November", "December"
00085 };
00086 static const char *abmon[12] = {
00087         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
00088         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
00089 };
00090 static const char *am_pm[2] = {
00091         "AM", "PM"
00092 };
00093 
00094 static int
00095 conv_num(const char **buf, int *dest, int llim, int ulim) {
00096         int result = 0;
00097 
00098         /* The limit also determines the number of valid digits. */
00099         int rulim = ulim;
00100 
00101         if (**buf < '0' || **buf > '9')
00102                 return (0);
00103 
00104         do {
00105                 result *= 10;
00106                 result += *(*buf)++ - '0';
00107                 rulim /= 10;
00108         } while ((result * 10 <= ulim) &&
00109                  rulim && **buf >= '0' && **buf <= '9');
00110 
00111         if (result < llim || result > ulim)
00112                 return (0);
00113 
00114         *dest = result;
00115         return (1);
00116 }
00117 
00118 time_t
00119 isc_tm_timegm(struct tm *tm) {
00120         time_t ret;
00121         int i, yday = 0, leapday;
00122         int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 };
00123 
00124         leapday = ((((tm->tm_year + 1900 ) % 4) == 0 &&
00125                     ((tm->tm_year + 1900 ) % 100) != 0) ||
00126                    ((tm->tm_year + 1900 ) % 400) == 0) ? 1 : 0;
00127         mdays[1] += leapday;
00128 
00129         yday = tm->tm_mday - 1;
00130         for (i = 1; i <= tm->tm_mon; i++)
00131                 yday += mdays[i - 1];
00132         ret = tm->tm_sec +
00133               (60 * tm->tm_min) +
00134               (3600 * tm->tm_hour) +
00135               (86400 * (yday +
00136                        ((tm->tm_year - 70) * 365) +
00137                        ((tm->tm_year - 69) / 4) -
00138                        ((tm->tm_year - 1) / 100) +
00139                        ((tm->tm_year + 299) / 400)));
00140         return (ret);
00141 }
00142 
00143 char *
00144 isc_tm_strptime(const char *buf, const char *fmt, struct tm *tm) {
00145         char c, *ret;
00146         const char *bp;
00147         size_t len = 0;
00148         int alt_format, i, split_year = 0;
00149 
00150         REQUIRE(buf != NULL);
00151         REQUIRE(fmt != NULL);
00152         REQUIRE(tm != NULL);
00153 
00154         memset(tm, 0, sizeof(struct tm));
00155 
00156         bp = buf;
00157 
00158         while ((c = *fmt) != '\0') {
00159                 /* Clear `alternate' modifier prior to new conversion. */
00160                 alt_format = 0;
00161 
00162                 /* Eat up white-space. */
00163                 if (isspace((unsigned char) c)) {
00164                         while (isspace((unsigned char) *bp))
00165                                 bp++;
00166 
00167                         fmt++;
00168                         continue;
00169                 }
00170 
00171                 if ((c = *fmt++) != '%')
00172                         goto literal;
00173 
00174 
00175 again:          switch (c = *fmt++) {
00176                 case '%':       /* "%%" is converted to "%". */
00177 literal:
00178                         if (c != *bp++)
00179                                 return (0);
00180                         break;
00181 
00182                 /*
00183                  * "Alternative" modifiers. Just set the appropriate flag
00184                  * and start over again.
00185                  */
00186                 case 'E':       /* "%E?" alternative conversion modifier. */
00187                         LEGAL_ALT(0);
00188                         alt_format |= ALT_E;
00189                         goto again;
00190 
00191                 case 'O':       /* "%O?" alternative conversion modifier. */
00192                         LEGAL_ALT(0);
00193                         alt_format |= ALT_O;
00194                         goto again;
00195 
00196                 /*
00197                  * "Complex" conversion rules, implemented through recursion.
00198                  */
00199                 case 'c':       /* Date and time, using the locale's format. */
00200                         LEGAL_ALT(ALT_E);
00201                         if (!(bp = isc_tm_strptime(bp, "%x %X", tm)))
00202                                 return (0);
00203                         break;
00204 
00205                 case 'D':       /* The date as "%m/%d/%y". */
00206                         LEGAL_ALT(0);
00207                         if (!(bp = isc_tm_strptime(bp, "%m/%d/%y", tm)))
00208                                 return (0);
00209                         break;
00210 
00211                 case 'R':       /* The time as "%H:%M". */
00212                         LEGAL_ALT(0);
00213                         if (!(bp = isc_tm_strptime(bp, "%H:%M", tm)))
00214                                 return (0);
00215                         break;
00216 
00217                 case 'r':       /* The time in 12-hour clock representation. */
00218                         LEGAL_ALT(0);
00219                         if (!(bp = isc_tm_strptime(bp, "%I:%M:%S %p", tm)))
00220                                 return (0);
00221                         break;
00222 
00223                 case 'T':       /* The time as "%H:%M:%S". */
00224                         LEGAL_ALT(0);
00225                         if (!(bp = isc_tm_strptime(bp, "%H:%M:%S", tm)))
00226                                 return (0);
00227                         break;
00228 
00229                 case 'X':       /* The time, using the locale's format. */
00230                         LEGAL_ALT(ALT_E);
00231                         if (!(bp = isc_tm_strptime(bp, "%H:%M:%S", tm)))
00232                                 return (0);
00233                         break;
00234 
00235                 case 'x':       /* The date, using the locale's format. */
00236                         LEGAL_ALT(ALT_E);
00237                         if (!(bp = isc_tm_strptime(bp, "%m/%d/%y", tm)))
00238                                 return (0);
00239                         break;
00240 
00241                 /*
00242                  * "Elementary" conversion rules.
00243                  */
00244                 case 'A':       /* The day of week, using the locale's form. */
00245                 case 'a':
00246                         LEGAL_ALT(0);
00247                         for (i = 0; i < 7; i++) {
00248                                 /* Full name. */
00249                                 len = strlen(day[i]);
00250                                 if (strncasecmp(day[i], bp, len) == 0)
00251                                         break;
00252 
00253                                 /* Abbreviated name. */
00254                                 len = strlen(abday[i]);
00255                                 if (strncasecmp(abday[i], bp, len) == 0)
00256                                         break;
00257                         }
00258 
00259                         /* Nothing matched. */
00260                         if (i == 7)
00261                                 return (0);
00262 
00263                         tm->tm_wday = i;
00264                         bp += len;
00265                         break;
00266 
00267                 case 'B':       /* The month, using the locale's form. */
00268                 case 'b':
00269                 case 'h':
00270                         LEGAL_ALT(0);
00271                         for (i = 0; i < 12; i++) {
00272                                 /* Full name. */
00273                                 len = strlen(mon[i]);
00274                                 if (strncasecmp(mon[i], bp, len) == 0)
00275                                         break;
00276 
00277                                 /* Abbreviated name. */
00278                                 len = strlen(abmon[i]);
00279                                 if (strncasecmp(abmon[i], bp, len) == 0)
00280                                         break;
00281                         }
00282 
00283                         /* Nothing matched. */
00284                         if (i == 12)
00285                                 return (0);
00286 
00287                         tm->tm_mon = i;
00288                         bp += len;
00289                         break;
00290 
00291                 case 'C':       /* The century number. */
00292                         LEGAL_ALT(ALT_E);
00293                         if (!(conv_num(&bp, &i, 0, 99)))
00294                                 return (0);
00295 
00296                         if (split_year) {
00297                                 tm->tm_year = (tm->tm_year % 100) + (i * 100);
00298                         } else {
00299                                 tm->tm_year = i * 100;
00300                                 split_year = 1;
00301                         }
00302                         break;
00303 
00304                 case 'd':       /* The day of month. */
00305                 case 'e':
00306                         LEGAL_ALT(ALT_O);
00307                         if (!(conv_num(&bp, &tm->tm_mday, 1, 31)))
00308                                 return (0);
00309                         break;
00310 
00311                 case 'k':       /* The hour (24-hour clock representation). */
00312                         LEGAL_ALT(0);
00313                         /* FALLTHROUGH */
00314                 case 'H':
00315                         LEGAL_ALT(ALT_O);
00316                         if (!(conv_num(&bp, &tm->tm_hour, 0, 23)))
00317                                 return (0);
00318                         break;
00319 
00320                 case 'l':       /* The hour (12-hour clock representation). */
00321                         LEGAL_ALT(0);
00322                         /* FALLTHROUGH */
00323                 case 'I':
00324                         LEGAL_ALT(ALT_O);
00325                         if (!(conv_num(&bp, &tm->tm_hour, 1, 12)))
00326                                 return (0);
00327                         if (tm->tm_hour == 12)
00328                                 tm->tm_hour = 0;
00329                         break;
00330 
00331                 case 'j':       /* The day of year. */
00332                         LEGAL_ALT(0);
00333                         if (!(conv_num(&bp, &i, 1, 366)))
00334                                 return (0);
00335                         tm->tm_yday = i - 1;
00336                         break;
00337 
00338                 case 'M':       /* The minute. */
00339                         LEGAL_ALT(ALT_O);
00340                         if (!(conv_num(&bp, &tm->tm_min, 0, 59)))
00341                                 return (0);
00342                         break;
00343 
00344                 case 'm':       /* The month. */
00345                         LEGAL_ALT(ALT_O);
00346                         if (!(conv_num(&bp, &i, 1, 12)))
00347                                 return (0);
00348                         tm->tm_mon = i - 1;
00349                         break;
00350 
00351                 case 'p':       /* The locale's equivalent of AM/PM. */
00352                         LEGAL_ALT(0);
00353                         /* AM? */
00354                         if (strcasecmp(am_pm[0], bp) == 0) {
00355                                 if (tm->tm_hour > 11)
00356                                         return (0);
00357 
00358                                 bp += strlen(am_pm[0]);
00359                                 break;
00360                         }
00361                         /* PM? */
00362                         else if (strcasecmp(am_pm[1], bp) == 0) {
00363                                 if (tm->tm_hour > 11)
00364                                         return (0);
00365 
00366                                 tm->tm_hour += 12;
00367                                 bp += strlen(am_pm[1]);
00368                                 break;
00369                         }
00370 
00371                         /* Nothing matched. */
00372                         return (0);
00373 
00374                 case 'S':       /* The seconds. */
00375                         LEGAL_ALT(ALT_O);
00376                         if (!(conv_num(&bp, &tm->tm_sec, 0, 61)))
00377                                 return (0);
00378                         break;
00379 
00380                 case 'U':       /* The week of year, beginning on sunday. */
00381                 case 'W':       /* The week of year, beginning on monday. */
00382                         LEGAL_ALT(ALT_O);
00383                         /*
00384                          * XXX This is bogus, as we can not assume any valid
00385                          * information present in the tm structure at this
00386                          * point to calculate a real value, so just check the
00387                          * range for now.
00388                          */
00389                          if (!(conv_num(&bp, &i, 0, 53)))
00390                                 return (0);
00391                          break;
00392 
00393                 case 'w':       /* The day of week, beginning on sunday. */
00394                         LEGAL_ALT(ALT_O);
00395                         if (!(conv_num(&bp, &tm->tm_wday, 0, 6)))
00396                                 return (0);
00397                         break;
00398 
00399                 case 'Y':       /* The year. */
00400                         LEGAL_ALT(ALT_E);
00401                         if (!(conv_num(&bp, &i, 0, 9999)))
00402                                 return (0);
00403 
00404                         tm->tm_year = i - TM_YEAR_BASE;
00405                         break;
00406 
00407                 case 'y':       /* The year within 100 years of the epoch. */
00408                         LEGAL_ALT(ALT_E | ALT_O);
00409                         if (!(conv_num(&bp, &i, 0, 99)))
00410                                 return (0);
00411 
00412                         if (split_year) {
00413                                 tm->tm_year = ((tm->tm_year / 100) * 100) + i;
00414                                 break;
00415                         }
00416                         split_year = 1;
00417                         if (i <= 68)
00418                                 tm->tm_year = i + 2000 - TM_YEAR_BASE;
00419                         else
00420                                 tm->tm_year = i + 1900 - TM_YEAR_BASE;
00421                         break;
00422 
00423                 /*
00424                  * Miscellaneous conversions.
00425                  */
00426                 case 'n':       /* Any kind of white-space. */
00427                 case 't':
00428                         LEGAL_ALT(0);
00429                         while (isspace((unsigned char) *bp))
00430                                 bp++;
00431                         break;
00432 
00433 
00434                 default:        /* Unknown/unsupported conversion. */
00435                         return (0);
00436                 }
00437 
00438 
00439         }
00440 
00441         /* LINTED functional specification */
00442         DE_CONST(bp, ret);
00443         return (ret);
00444 }

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