2 * Ported from NetBSD to Windows by Ron Koenderink, 2007
3 * Port rebased by Markus Armbruster, 2009
8 typedef unsigned char u_char;
9 typedef unsigned int uint;
14 const char *abmon[12];
20 const char *t_fmt_ampm;
23 static const TimeLocale DefaultTimeLocale =
26 "Sun","Mon","Tue","Wed","Thu","Fri","Sat",
29 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
33 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
34 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
37 "January", "February", "March", "April", "May", "June", "July",
38 "August", "September", "October", "November", "December"
43 "%a %b %e %H:%M:%S %Y",
49 #define _CurrentTimeLocale (&DefaultTimeLocale)
50 #define TM_YEAR_BASE 1900
51 #define __UNCONST(a) ((void *)(a))
53 /* $NetBSD: strptime.c,v 1.28 2008/04/28 20:23:01 martin Exp $ */
56 * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
57 * All rights reserved.
59 * This code was contributed to The NetBSD Foundation by Klaus Klein.
60 * Heavily optimised by David Laight
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
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.
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.
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 $");
90 #include "namespace.h"
91 #include <sys/localedef.h>
101 __weak_alias(strptime,_strptime)
105 #define _ctloc(x) (_CurrentTimeLocale->x)
108 * We do not implement alternate representations. However, we always
109 * check whether a given modifier is allowed for a certain conversion.
113 #define LEGAL_ALT(x) { if (alt_format & ~(x)) return NULL; }
115 static const char gmt[4] = { "GMT" };
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);
123 strptime(const char *buf, const char *fmt, struct tm *tm)
126 const unsigned char *bp;
127 int alt_format, i, split_year = 0;
130 bp = (const u_char *)buf;
132 while (bp != NULL && (c = *fmt++) != '\0') {
133 /* Clear `alternate' modifier prior to new conversion. */
137 /* Eat up white-space. */
148 again: switch (c = *fmt++) {
149 case '%': /* "%%" is converted to "%". */
157 * "Alternative" modifiers. Just set the appropriate flag
158 * and start over again.
160 case 'E': /* "%E?" alternative conversion modifier. */
165 case 'O': /* "%O?" alternative conversion modifier. */
171 * "Complex" conversion rules, implemented through recursion.
173 case 'c': /* Date and time, using the locale's format. */
174 new_fmt = _ctloc(d_t_fmt);
177 case 'D': /* The date as "%m/%d/%y". */
178 new_fmt = "%m/%d/%y";
182 case 'F': /* The date as "%Y-%m-%d". */
183 new_fmt = "%Y-%m-%d";
187 case 'R': /* The time as "%H:%M". */
192 case 'r': /* The time in 12-hour clock representation. */
193 new_fmt =_ctloc(t_fmt_ampm);
197 case 'T': /* The time as "%H:%M:%S". */
198 new_fmt = "%H:%M:%S";
202 case 'X': /* The time, using the locale's format. */
203 new_fmt =_ctloc(t_fmt);
206 case 'x': /* The date, using the locale's format. */
207 new_fmt =_ctloc(d_fmt);
209 bp = (const u_char *)strptime((const char *)bp,
215 * "Elementary" conversion rules.
217 case 'A': /* The day of week, using the locale's form. */
219 bp = find_string(bp, &tm->tm_wday, _ctloc(day),
224 case 'B': /* The month, using the locale's form. */
227 bp = find_string(bp, &tm->tm_mon, _ctloc(mon),
232 case 'C': /* The century number. */
234 bp = conv_num(bp, &i, 0, 99);
236 i = i * 100 - TM_YEAR_BASE;
238 i += tm->tm_year % 100;
244 case 'd': /* The day of month. */
246 bp = conv_num(bp, &tm->tm_mday, 1, 31);
250 case 'k': /* The hour (24-hour clock representation). */
254 bp = conv_num(bp, &tm->tm_hour, 0, 23);
258 case 'l': /* The hour (12-hour clock representation). */
262 bp = conv_num(bp, &tm->tm_hour, 1, 12);
263 if (tm->tm_hour == 12)
268 case 'j': /* The day of year. */
270 bp = conv_num(bp, &i, 1, 366);
275 case 'M': /* The minute. */
276 bp = conv_num(bp, &tm->tm_min, 0, 59);
280 case 'm': /* The month. */
282 bp = conv_num(bp, &i, 1, 12);
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)
291 tm->tm_hour += i * 12;
295 case 'S': /* The seconds. */
296 bp = conv_num(bp, &tm->tm_sec, 0, 61);
300 case 'U': /* The week of year, beginning on sunday. */
301 case 'W': /* The week of year, beginning on monday. */
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
308 bp = conv_num(bp, &i, 0, 53);
312 case 'w': /* The day of week, beginning on sunday. */
313 bp = conv_num(bp, &tm->tm_wday, 0, 6);
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;
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);
329 /* preserve century */
330 i += (tm->tm_year / 100) * 100;
334 i = i + 2000 - TM_YEAR_BASE;
336 i = i + 1900 - TM_YEAR_BASE;
343 if (strncmp((const char *)bp, gmt, 3) == 0) {
353 const unsigned char *ep;
355 ep = find_string(bp, &i,
356 (const char * const *)tzname,
361 tm->TM_GMTOFF = -(timezone);
364 tm->TM_ZONE = tzname[i];
372 * Miscellaneous conversions.
374 case 'n': /* Any kind of white-space. */
382 default: /* Unknown/unsupported conversion. */
387 return __UNCONST(bp);
391 static const u_char *
392 conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim)
397 /* The limit also determines the number of valid digits. */
401 if (ch < '0' || ch > '9')
409 } while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
411 if (result < llim || result > ulim)
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)
425 /* check full name - then abbreviated ones */
426 for (; n1 != NULL; n1 = n2, n2 = NULL) {
427 for (i = 0; i < c; i++, n1++) {
429 if (strncasecmp(*n1, (const char *)bp, len) == 0) {
436 /* Nothing matched */