2 * Powerdog Industries kindly requests feedback from anyone modifying
5 * Date: Thu, 05 Jun 1997 23:17:17 -0400
6 * From: Kevin Ruddy <kevin.ruddy@powerdog.com>
7 * To: James FitzGibbon <james@nexis.net>
8 * Subject: Re: Use of your strptime(3) code (fwd)
10 * The reason for the "no mod" clause was so that modifications would
11 * come back and we could integrate them and reissue so that a wider
12 * audience could use it (thereby spreading the wealth). This has
13 * made it possible to get strptime to work on many operating systems.
14 * I'm not sure why that's "plain unacceptable" to the FreeBSD team.
16 * Anyway, you can change it to "with or without modification" as
20 * Powerdog Industries, Inc.
23 * Copyright (c) 1994 Powerdog Industries. All rights reserved.
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer
32 * in the documentation and/or other materials provided with the
34 * 3. All advertising materials mentioning features or use of this
35 * software must display the following acknowledgement:
36 * This product includes software developed by Powerdog Industries.
37 * 4. The name of Powerdog Industries may not be used to endorse or
38 * promote products derived from this software without specific prior
41 * THIS SOFTWARE IS PROVIDED BY POWERDOG INDUSTRIES ``AS IS'' AND ANY
42 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE POWERDOG INDUSTRIES BE
45 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
48 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
49 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
50 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
51 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 static const char rcsid[] =
56 "$FreeBSD: src/lib/libc/stdtime/strptime.c,v 1.17.2.3 2002/03/12 17:24:54 phantom Exp $";
61 static char copyright[] =
62 "@(#) Copyright (c) 1994 Powerdog Industries. All rights reserved.";
63 static char sccsid[] = "@(#)strptime.c 0.1 (Powerdog) 94/03/27";
64 #endif /* !defined NOID */
74 #include "pthread_private.h"
76 #include "timelocal.h"
78 static char * _strptime(const char *, const char *, struct tm *);
81 static struct pthread_mutex _gotgmt_mutexd = PTHREAD_MUTEX_STATIC_INITIALIZER;
82 static pthread_mutex_t gotgmt_mutex = &_gotgmt_mutexd;
86 #define asizeof(a) (sizeof (a) / sizeof ((a)[0]))
89 _strptime(const char *buf, const char *fmt, struct tm *tm)
95 int Ealternative, Oalternative;
96 struct lc_time_T *tptr = __get_current_time_locale();
106 if (isspace((unsigned char)c))
107 while (*buf != 0 && isspace((unsigned char)*buf))
109 else if (c != *buf++)
126 buf = _strptime(buf, tptr->date_fmt, tm);
132 if (!isdigit((unsigned char)*buf))
135 /* XXX This will break for 3-digit centuries. */
137 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
145 tm->tm_year = i * 100 - 1900;
149 buf = _strptime(buf, tptr->c_fmt, tm);
155 buf = _strptime(buf, "%m/%d/%y", tm);
161 if (Ealternative || Oalternative)
167 if (Ealternative || Oalternative)
173 buf = _strptime(buf, "%Y-%m-%d", tm);
179 buf = _strptime(buf, "%H:%M", tm);
185 buf = _strptime(buf, tptr->ampm_fmt, tm);
191 buf = _strptime(buf, "%H:%M:%S", tm);
197 buf = _strptime(buf, tptr->X_fmt, tm);
203 buf = _strptime(buf, tptr->x_fmt, tm);
209 if (!isdigit((unsigned char)*buf))
213 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
218 if (i < 1 || i > 366)
226 if (*buf == 0 || isspace((unsigned char)*buf))
229 if (!isdigit((unsigned char)*buf))
233 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
249 if (*buf != 0 && isspace((unsigned char)*buf))
250 while (*ptr != 0 && !isspace((unsigned char)*ptr))
259 * Of these, %l is the only specifier explicitly
260 * documented as not being zero-padded. However,
261 * there is no harm in allowing zero-padding.
263 * XXX The %l specifier may gobble one too many
264 * digits if used incorrectly.
266 if (!isdigit((unsigned char)*buf))
270 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
275 if (c == 'H' || c == 'k') {
283 if (*buf != 0 && isspace((unsigned char)*buf))
284 while (*ptr != 0 && !isspace((unsigned char)*ptr))
290 * XXX This is bogus if parsed before hour-related
293 len = strlen(tptr->am);
294 if (strncasecmp(buf, tptr->am, len) == 0) {
295 if (tm->tm_hour > 12)
297 if (tm->tm_hour == 12)
303 len = strlen(tptr->pm);
304 if (strncasecmp(buf, tptr->pm, len) == 0) {
305 if (tm->tm_hour > 12)
307 if (tm->tm_hour != 12)
317 for (i = 0; i < asizeof(tptr->weekday); i++) {
318 len = strlen(tptr->weekday[i]);
319 if (strncasecmp(buf, tptr->weekday[i],
322 len = strlen(tptr->wday[i]);
323 if (strncasecmp(buf, tptr->wday[i],
327 if (i == asizeof(tptr->weekday))
337 * XXX This is bogus, as we can not assume any valid
338 * information present in the tm structure at this
339 * point to calculate a real value, so just check the
342 if (!isdigit((unsigned char)*buf))
346 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
354 if (*buf != 0 && isspace((unsigned char)*buf))
355 while (*ptr != 0 && !isspace((unsigned char)*ptr))
360 if (!isdigit((unsigned char)*buf))
369 if (*buf != 0 && isspace((unsigned char)*buf))
370 while (*ptr != 0 && !isspace((unsigned char)*ptr))
377 * The %e specifier is explicitly documented as not
378 * being zero-padded but there is no harm in allowing
381 * XXX The %e specifier may gobble one too many
382 * digits if used incorrectly.
384 if (!isdigit((unsigned char)*buf))
388 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
398 if (*buf != 0 && isspace((unsigned char)*buf))
399 while (*ptr != 0 && !isspace((unsigned char)*ptr))
406 for (i = 0; i < asizeof(tptr->month); i++) {
409 len = strlen(tptr->alt_month[i]);
416 len = strlen(tptr->month[i]);
417 if (strncasecmp(buf, tptr->month[i],
420 len = strlen(tptr->mon[i]);
421 if (strncasecmp(buf, tptr->mon[i],
426 if (i == asizeof(tptr->month))
434 if (!isdigit((unsigned char)*buf))
438 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
448 if (*buf != 0 && isspace((unsigned char)*buf))
449 while (*ptr != 0 && !isspace((unsigned char)*ptr))
458 t = strtol(buf, &cp, 10);
469 if (*buf == 0 || isspace((unsigned char)*buf))
472 if (!isdigit((unsigned char)*buf))
475 len = (c == 'Y') ? 4 : 2;
476 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
483 if (c == 'y' && i < 69)
490 if (*buf != 0 && isspace((unsigned char)*buf))
491 while (*ptr != 0 && !isspace((unsigned char)*ptr))
500 for (cp = buf; *cp && isupper((unsigned char)*cp); ++cp) {/*empty*/}
502 zonestr = alloca(cp - buf + 1);
503 strncpy(zonestr, buf, cp - buf);
504 zonestr[cp - buf] = '\0';
506 if (0 == strcmp(zonestr, "GMT")) {
508 } else if (0 == strcmp(zonestr, tzname[0])) {
510 } else if (0 == strcmp(zonestr, tzname[1])) {
526 strptime(const char *buf, const char *fmt, struct tm *tm)
531 pthread_mutex_lock(&gotgmt_mutex);
535 ret = _strptime(buf, fmt, tm);
536 if (ret && got_GMT) {
537 time_t t = timegm(tm);
543 pthread_mutex_unlock(&gotgmt_mutex);