]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/bind9/lib/isc/tm.c
Copy stable/9 to releng/9.3 as part of the 9.3-RELEASE cycle.
[FreeBSD/releng/9.3.git] / contrib / bind9 / lib / isc / tm.c
1 /*
2  * Copyright (C) 2014  Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /*-
18  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
19  * All rights reserved.
20  *
21  * This code was contributed to The NetBSD Foundation by Klaus Klein.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the above copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
33  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
34  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
36  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGE.
43  */
44
45 #include <config.h>
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <time.h>
51 #include <ctype.h>
52
53 #include <isc/tm.h>
54 #include <isc/util.h>
55
56 /*
57  * Portable conversion routines for struct tm, replacing
58  * timegm() and strptime(), which are not available on all
59  * platforms and don't always behave the same way when they
60  * are.
61  */
62
63 /*
64  * We do not implement alternate representations. However, we always
65  * check whether a given modifier is allowed for a certain conversion.
66  */
67 #define ALT_E                   0x01
68 #define ALT_O                   0x02
69 #define LEGAL_ALT(x)            { if (alt_format & ~(x)) return (0); }
70
71 #ifndef TM_YEAR_BASE
72 #define TM_YEAR_BASE 1900
73 #endif
74
75 static const char *day[7] = {
76         "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
77         "Friday", "Saturday"
78 };
79 static const char *abday[7] = {
80         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
81 };
82 static const char *mon[12] = {
83         "January", "February", "March", "April", "May", "June", "July",
84         "August", "September", "October", "November", "December"
85 };
86 static const char *abmon[12] = {
87         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
88         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
89 };
90 static const char *am_pm[2] = {
91         "AM", "PM"
92 };
93
94 static int
95 conv_num(const char **buf, int *dest, int llim, int ulim) {
96         int result = 0;
97
98         /* The limit also determines the number of valid digits. */
99         int rulim = ulim;
100
101         if (**buf < '0' || **buf > '9')
102                 return (0);
103
104         do {
105                 result *= 10;
106                 result += *(*buf)++ - '0';
107                 rulim /= 10;
108         } while ((result * 10 <= ulim) &&
109                  rulim && **buf >= '0' && **buf <= '9');
110
111         if (result < llim || result > ulim)
112                 return (0);
113
114         *dest = result;
115         return (1);
116 }
117
118 time_t
119 isc_tm_timegm(struct tm *tm) {
120         time_t ret;
121         int i, yday = 0, leapday;
122         int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 };
123
124         leapday = ((((tm->tm_year + 1900 ) % 4) == 0 &&
125                     ((tm->tm_year + 1900 ) % 100) != 0) ||
126                    ((tm->tm_year + 1900 ) % 400) == 0) ? 1 : 0;
127         mdays[1] += leapday;
128
129         yday = tm->tm_mday - 1;
130         for (i = 1; i <= tm->tm_mon; i++)
131                 yday += mdays[i - 1];
132         ret = tm->tm_sec +
133               (60 * tm->tm_min) +
134               (3600 * tm->tm_hour) +
135               (86400 * (yday +
136                        ((tm->tm_year - 70) * 365) +
137                        ((tm->tm_year - 69) / 4) -
138                        ((tm->tm_year - 1) / 100) +
139                        ((tm->tm_year + 299) / 400)));
140         return (ret);
141 }
142
143 char *
144 isc_tm_strptime(const char *buf, const char *fmt, struct tm *tm) {
145         char c, *ret;
146         const char *bp;
147         size_t len = 0;
148         int alt_format, i, split_year = 0;
149
150         bp = buf;
151
152         while ((c = *fmt) != '\0') {
153                 /* Clear `alternate' modifier prior to new conversion. */
154                 alt_format = 0;
155
156                 /* Eat up white-space. */
157                 if (isspace((unsigned char) c)) {
158                         while (isspace((unsigned char) *bp))
159                                 bp++;
160
161                         fmt++;
162                         continue;
163                 }
164
165                 if ((c = *fmt++) != '%')
166                         goto literal;
167
168
169 again:          switch (c = *fmt++) {
170                 case '%':       /* "%%" is converted to "%". */
171 literal:
172                         if (c != *bp++)
173                                 return (0);
174                         break;
175
176                 /*
177                  * "Alternative" modifiers. Just set the appropriate flag
178                  * and start over again.
179                  */
180                 case 'E':       /* "%E?" alternative conversion modifier. */
181                         LEGAL_ALT(0);
182                         alt_format |= ALT_E;
183                         goto again;
184
185                 case 'O':       /* "%O?" alternative conversion modifier. */
186                         LEGAL_ALT(0);
187                         alt_format |= ALT_O;
188                         goto again;
189
190                 /*
191                  * "Complex" conversion rules, implemented through recursion.
192                  */
193                 case 'c':       /* Date and time, using the locale's format. */
194                         LEGAL_ALT(ALT_E);
195                         if (!(bp = isc_tm_strptime(bp, "%x %X", tm)))
196                                 return (0);
197                         break;
198
199                 case 'D':       /* The date as "%m/%d/%y". */
200                         LEGAL_ALT(0);
201                         if (!(bp = isc_tm_strptime(bp, "%m/%d/%y", tm)))
202                                 return (0);
203                         break;
204
205                 case 'R':       /* The time as "%H:%M". */
206                         LEGAL_ALT(0);
207                         if (!(bp = isc_tm_strptime(bp, "%H:%M", tm)))
208                                 return (0);
209                         break;
210
211                 case 'r':       /* The time in 12-hour clock representation. */
212                         LEGAL_ALT(0);
213                         if (!(bp = isc_tm_strptime(bp, "%I:%M:%S %p", tm)))
214                                 return (0);
215                         break;
216
217                 case 'T':       /* The time as "%H:%M:%S". */
218                         LEGAL_ALT(0);
219                         if (!(bp = isc_tm_strptime(bp, "%H:%M:%S", tm)))
220                                 return (0);
221                         break;
222
223                 case 'X':       /* The time, using the locale's format. */
224                         LEGAL_ALT(ALT_E);
225                         if (!(bp = isc_tm_strptime(bp, "%H:%M:%S", tm)))
226                                 return (0);
227                         break;
228
229                 case 'x':       /* The date, using the locale's format. */
230                         LEGAL_ALT(ALT_E);
231                         if (!(bp = isc_tm_strptime(bp, "%m/%d/%y", tm)))
232                                 return (0);
233                         break;
234
235                 /*
236                  * "Elementary" conversion rules.
237                  */
238                 case 'A':       /* The day of week, using the locale's form. */
239                 case 'a':
240                         LEGAL_ALT(0);
241                         for (i = 0; i < 7; i++) {
242                                 /* Full name. */
243                                 len = strlen(day[i]);
244                                 if (strncasecmp(day[i], bp, len) == 0)
245                                         break;
246
247                                 /* Abbreviated name. */
248                                 len = strlen(abday[i]);
249                                 if (strncasecmp(abday[i], bp, len) == 0)
250                                         break;
251                         }
252
253                         /* Nothing matched. */
254                         if (i == 7)
255                                 return (0);
256
257                         tm->tm_wday = i;
258                         bp += len;
259                         break;
260
261                 case 'B':       /* The month, using the locale's form. */
262                 case 'b':
263                 case 'h':
264                         LEGAL_ALT(0);
265                         for (i = 0; i < 12; i++) {
266                                 /* Full name. */
267                                 len = strlen(mon[i]);
268                                 if (strncasecmp(mon[i], bp, len) == 0)
269                                         break;
270
271                                 /* Abbreviated name. */
272                                 len = strlen(abmon[i]);
273                                 if (strncasecmp(abmon[i], bp, len) == 0)
274                                         break;
275                         }
276
277                         /* Nothing matched. */
278                         if (i == 12)
279                                 return (0);
280
281                         tm->tm_mon = i;
282                         bp += len;
283                         break;
284
285                 case 'C':       /* The century number. */
286                         LEGAL_ALT(ALT_E);
287                         if (!(conv_num(&bp, &i, 0, 99)))
288                                 return (0);
289
290                         if (split_year) {
291                                 tm->tm_year = (tm->tm_year % 100) + (i * 100);
292                         } else {
293                                 tm->tm_year = i * 100;
294                                 split_year = 1;
295                         }
296                         break;
297
298                 case 'd':       /* The day of month. */
299                 case 'e':
300                         LEGAL_ALT(ALT_O);
301                         if (!(conv_num(&bp, &tm->tm_mday, 1, 31)))
302                                 return (0);
303                         break;
304
305                 case 'k':       /* The hour (24-hour clock representation). */
306                         LEGAL_ALT(0);
307                         /* FALLTHROUGH */
308                 case 'H':
309                         LEGAL_ALT(ALT_O);
310                         if (!(conv_num(&bp, &tm->tm_hour, 0, 23)))
311                                 return (0);
312                         break;
313
314                 case 'l':       /* The hour (12-hour clock representation). */
315                         LEGAL_ALT(0);
316                         /* FALLTHROUGH */
317                 case 'I':
318                         LEGAL_ALT(ALT_O);
319                         if (!(conv_num(&bp, &tm->tm_hour, 1, 12)))
320                                 return (0);
321                         if (tm->tm_hour == 12)
322                                 tm->tm_hour = 0;
323                         break;
324
325                 case 'j':       /* The day of year. */
326                         LEGAL_ALT(0);
327                         if (!(conv_num(&bp, &i, 1, 366)))
328                                 return (0);
329                         tm->tm_yday = i - 1;
330                         break;
331
332                 case 'M':       /* The minute. */
333                         LEGAL_ALT(ALT_O);
334                         if (!(conv_num(&bp, &tm->tm_min, 0, 59)))
335                                 return (0);
336                         break;
337
338                 case 'm':       /* The month. */
339                         LEGAL_ALT(ALT_O);
340                         if (!(conv_num(&bp, &i, 1, 12)))
341                                 return (0);
342                         tm->tm_mon = i - 1;
343                         break;
344
345                 case 'p':       /* The locale's equivalent of AM/PM. */
346                         LEGAL_ALT(0);
347                         /* AM? */
348                         if (strcasecmp(am_pm[0], bp) == 0) {
349                                 if (tm->tm_hour > 11)
350                                         return (0);
351
352                                 bp += strlen(am_pm[0]);
353                                 break;
354                         }
355                         /* PM? */
356                         else if (strcasecmp(am_pm[1], bp) == 0) {
357                                 if (tm->tm_hour > 11)
358                                         return (0);
359
360                                 tm->tm_hour += 12;
361                                 bp += strlen(am_pm[1]);
362                                 break;
363                         }
364
365                         /* Nothing matched. */
366                         return (0);
367
368                 case 'S':       /* The seconds. */
369                         LEGAL_ALT(ALT_O);
370                         if (!(conv_num(&bp, &tm->tm_sec, 0, 61)))
371                                 return (0);
372                         break;
373
374                 case 'U':       /* The week of year, beginning on sunday. */
375                 case 'W':       /* The week of year, beginning on monday. */
376                         LEGAL_ALT(ALT_O);
377                         /*
378                          * XXX This is bogus, as we can not assume any valid
379                          * information present in the tm structure at this
380                          * point to calculate a real value, so just check the
381                          * range for now.
382                          */
383                          if (!(conv_num(&bp, &i, 0, 53)))
384                                 return (0);
385                          break;
386
387                 case 'w':       /* The day of week, beginning on sunday. */
388                         LEGAL_ALT(ALT_O);
389                         if (!(conv_num(&bp, &tm->tm_wday, 0, 6)))
390                                 return (0);
391                         break;
392
393                 case 'Y':       /* The year. */
394                         LEGAL_ALT(ALT_E);
395                         if (!(conv_num(&bp, &i, 0, 9999)))
396                                 return (0);
397
398                         tm->tm_year = i - TM_YEAR_BASE;
399                         break;
400
401                 case 'y':       /* The year within 100 years of the epoch. */
402                         LEGAL_ALT(ALT_E | ALT_O);
403                         if (!(conv_num(&bp, &i, 0, 99)))
404                                 return (0);
405
406                         if (split_year) {
407                                 tm->tm_year = ((tm->tm_year / 100) * 100) + i;
408                                 break;
409                         }
410                         split_year = 1;
411                         if (i <= 68)
412                                 tm->tm_year = i + 2000 - TM_YEAR_BASE;
413                         else
414                                 tm->tm_year = i + 1900 - TM_YEAR_BASE;
415                         break;
416
417                 /*
418                  * Miscellaneous conversions.
419                  */
420                 case 'n':       /* Any kind of white-space. */
421                 case 't':
422                         LEGAL_ALT(0);
423                         while (isspace((unsigned char) *bp))
424                                 bp++;
425                         break;
426
427
428                 default:        /* Unknown/unsupported conversion. */
429                         return (0);
430                 }
431
432
433         }
434
435         /* LINTED functional specification */
436         DE_CONST(bp, ret);
437         return (ret);
438 }