1 /* $NetBSD: strptime.c,v 1.28 2008/04/28 20:23:01 martin Exp $ */
4 * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
7 * This code was contributed to The NetBSD Foundation by Klaus Klein.
8 * Heavily optimised by David Laight
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: strptime.c,v 1.28 2008/04/28 20:23:01 martin Exp $");
40 #include "namespace.h"
41 #include <sys/localedef.h>
50 __weak_alias(strptime,_strptime)
57 const char *abmon[12];
63 const char *t_fmt_ampm;
66 _TimeLocale _DefaultTimeLocale =
69 "Sun","Mon","Tue","Wed","Thu","Fri","Sat",
72 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
76 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
77 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
80 "January", "February", "March", "April", "May", "June", "July",
81 "August", "September", "October", "November", "December"
86 "%a %b %e %H:%M:%S %Y",
92 _TimeLocale *_CurrentTimeLocale = &_DefaultTimeLocale;
95 #define _ctloc(x) (_CurrentTimeLocale->x)
98 * We do not implement alternate representations. However, we always
99 * check whether a given modifier is allowed for a certain conversion.
103 #define LEGAL_ALT(x) { if (alt_format & ~(x)) return NULL; }
105 static const char gmt[4] = { "GMT" };
107 static const u_char *conv_num(const unsigned char *, int *, uint, uint);
108 static const u_char *find_string(const u_char *, int *, const char * const *,
109 const char * const *, int);
113 strptime(const char *buf, const char *fmt, struct tm *tm)
116 const unsigned char *bp;
117 int alt_format, i, split_year = 0;
120 bp = (const u_char *)buf;
122 while (bp != NULL && (c = *fmt++) != '\0') {
123 /* Clear `alternate' modifier prior to new conversion. */
127 /* Eat up white-space. */
138 again: switch (c = *fmt++) {
139 case '%': /* "%%" is converted to "%". */
147 * "Alternative" modifiers. Just set the appropriate flag
148 * and start over again.
150 case 'E': /* "%E?" alternative conversion modifier. */
155 case 'O': /* "%O?" alternative conversion modifier. */
161 * "Complex" conversion rules, implemented through recursion.
163 case 'c': /* Date and time, using the locale's format. */
164 new_fmt = _ctloc(d_t_fmt);
167 case 'D': /* The date as "%m/%d/%y". */
168 new_fmt = "%m/%d/%y";
172 case 'F': /* The date as "%Y-%m-%d". */
173 new_fmt = "%Y-%m-%d";
177 case 'R': /* The time as "%H:%M". */
182 case 'r': /* The time in 12-hour clock representation. */
183 new_fmt =_ctloc(t_fmt_ampm);
187 case 'T': /* The time as "%H:%M:%S". */
188 new_fmt = "%H:%M:%S";
192 case 'X': /* The time, using the locale's format. */
193 new_fmt =_ctloc(t_fmt);
196 case 'x': /* The date, using the locale's format. */
197 new_fmt =_ctloc(d_fmt);
199 bp = (const u_char *)strptime((const char *)bp,
205 * "Elementary" conversion rules.
207 case 'A': /* The day of week, using the locale's form. */
209 bp = find_string(bp, &tm->tm_wday, _ctloc(day),
214 case 'B': /* The month, using the locale's form. */
217 bp = find_string(bp, &tm->tm_mon, _ctloc(mon),
222 case 'C': /* The century number. */
224 bp = conv_num(bp, &i, 0, 99);
226 i = i * 100 - TM_YEAR_BASE;
228 i += tm->tm_year % 100;
234 case 'd': /* The day of month. */
236 bp = conv_num(bp, &tm->tm_mday, 1, 31);
240 case 'k': /* The hour (24-hour clock representation). */
244 bp = conv_num(bp, &tm->tm_hour, 0, 23);
248 case 'l': /* The hour (12-hour clock representation). */
252 bp = conv_num(bp, &tm->tm_hour, 1, 12);
253 if (tm->tm_hour == 12)
258 case 'j': /* The day of year. */
260 bp = conv_num(bp, &i, 1, 366);
265 case 'M': /* The minute. */
266 bp = conv_num(bp, &tm->tm_min, 0, 59);
270 case 'm': /* The month. */
272 bp = conv_num(bp, &i, 1, 12);
277 case 'p': /* The locale's equivalent of AM/PM. */
278 bp = find_string(bp, &i, _ctloc(am_pm), NULL, 2);
279 if (tm->tm_hour > 11)
281 tm->tm_hour += i * 12;
285 case 'S': /* The seconds. */
286 bp = conv_num(bp, &tm->tm_sec, 0, 61);
290 case 'U': /* The week of year, beginning on sunday. */
291 case 'W': /* The week of year, beginning on monday. */
293 * XXX This is bogus, as we can not assume any valid
294 * information present in the tm structure at this
295 * point to calculate a real value, so just check the
298 bp = conv_num(bp, &i, 0, 53);
302 case 'w': /* The day of week, beginning on sunday. */
303 bp = conv_num(bp, &tm->tm_wday, 0, 6);
307 case 'Y': /* The year. */
308 i = TM_YEAR_BASE; /* just for data sanity... */
309 bp = conv_num(bp, &i, 0, 9999);
310 tm->tm_year = i - TM_YEAR_BASE;
314 case 'y': /* The year within 100 years of the epoch. */
315 /* LEGAL_ALT(ALT_E | ALT_O); */
316 bp = conv_num(bp, &i, 0, 99);
319 /* preserve century */
320 i += (tm->tm_year / 100) * 100;
324 i = i + 2000 - TM_YEAR_BASE;
326 i = i + 1900 - TM_YEAR_BASE;
333 if (strncmp((const char *)bp, gmt, 3) == 0) {
343 const unsigned char *ep;
345 ep = find_string(bp, &i,
346 (const char * const *)tzname,
351 tm->TM_GMTOFF = -(timezone);
354 tm->TM_ZONE = tzname[i];
362 * Miscellaneous conversions.
364 case 'n': /* Any kind of white-space. */
372 default: /* Unknown/unsupported conversion. */
381 static const u_char *
382 conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim)
387 /* The limit also determines the number of valid digits. */
391 if (ch < '0' || ch > '9')
399 } while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
401 if (result < llim || result > ulim)
408 static const u_char *
409 find_string(const u_char *bp, int *tgt, const char * const *n1,
410 const char * const *n2, int c)
415 /* check full name - then abbreviated ones */
416 for (; n1 != NULL; n1 = n2, n2 = NULL) {
417 for (i = 0; i < c; i++, n1++) {
419 if (strncasecmp(*n1, (const char *)bp, len) == 0) {
426 /* Nothing matched */