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