strptime.c revision 53941
128019Sjoerg/*
228021Sjoerg * Powerdog Industries kindly requests feedback from anyone modifying
328021Sjoerg * this function:
428021Sjoerg *
528021Sjoerg * Date: Thu, 05 Jun 1997 23:17:17 -0400
628021Sjoerg * From: Kevin Ruddy <kevin.ruddy@powerdog.com>
728021Sjoerg * To: James FitzGibbon <james@nexis.net>
828021Sjoerg * Subject: Re: Use of your strptime(3) code (fwd)
928021Sjoerg *
1028021Sjoerg * The reason for the "no mod" clause was so that modifications would
1128021Sjoerg * come back and we could integrate them and reissue so that a wider
1228021Sjoerg * audience could use it (thereby spreading the wealth).  This has
1328021Sjoerg * made it possible to get strptime to work on many operating systems.
1428021Sjoerg * I'm not sure why that's "plain unacceptable" to the FreeBSD team.
1528021Sjoerg *
1628021Sjoerg * Anyway, you can change it to "with or without modification" as
1728021Sjoerg * you see fit.  Enjoy.
1828021Sjoerg *
1928021Sjoerg * Kevin Ruddy
2028021Sjoerg * Powerdog Industries, Inc.
2128021Sjoerg */
2228021Sjoerg/*
2328019Sjoerg * Copyright (c) 1994 Powerdog Industries.  All rights reserved.
2428019Sjoerg *
2528021Sjoerg * Redistribution and use in source and binary forms, with or without
2628019Sjoerg * modification, are permitted provided that the following conditions
2728019Sjoerg * are met:
2828019Sjoerg * 1. Redistributions of source code must retain the above copyright
2928019Sjoerg *    notice, this list of conditions and the following disclaimer.
3028019Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
3128019Sjoerg *    notice, this list of conditions and the following disclaimer
3228019Sjoerg *    in the documentation and/or other materials provided with the
3328019Sjoerg *    distribution.
3428019Sjoerg * 3. All advertising materials mentioning features or use of this
3528019Sjoerg *    software must display the following acknowledgement:
3628019Sjoerg *      This product includes software developed by Powerdog Industries.
3728019Sjoerg * 4. The name of Powerdog Industries may not be used to endorse or
3828019Sjoerg *    promote products derived from this software without specific prior
3928019Sjoerg *    written permission.
4028019Sjoerg *
4128019Sjoerg * THIS SOFTWARE IS PROVIDED BY POWERDOG INDUSTRIES ``AS IS'' AND ANY
4228019Sjoerg * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4328019Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4428019Sjoerg * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE POWERDOG INDUSTRIES BE
4528019Sjoerg * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4628019Sjoerg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4728019Sjoerg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
4828019Sjoerg * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
4928019Sjoerg * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
5028019Sjoerg * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
5128019Sjoerg * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5228019Sjoerg */
5328019Sjoerg
5428021Sjoerg#ifdef LIBC_RCS
5528021Sjoergstatic const char rcsid[] =
5650476Speter  "$FreeBSD: head/lib/libc/stdtime/strptime.c 53941 1999-11-30 08:05:09Z ache $";
5728021Sjoerg#endif
5828021Sjoerg
5928019Sjoerg#ifndef lint
6028021Sjoerg#ifndef NOID
6128019Sjoergstatic char copyright[] =
6228019Sjoerg"@(#) Copyright (c) 1994 Powerdog Industries.  All rights reserved.";
6328021Sjoergstatic char sccsid[] = "@(#)strptime.c	0.1 (Powerdog) 94/03/27";
6428021Sjoerg#endif /* !defined NOID */
6528019Sjoerg#endif /* not lint */
6628019Sjoerg
6728019Sjoerg#include <time.h>
6828019Sjoerg#include <ctype.h>
6928019Sjoerg#include <string.h>
7048614Sobrien#ifdef	_THREAD_SAFE
7148614Sobrien#include <pthread.h>
7248614Sobrien#include "pthread_private.h"
7348614Sobrien#endif
7428021Sjoerg#include "timelocal.h"
7528019Sjoerg
7648614Sobrienstatic char * _strptime(const char *, const char *, struct tm *);
7748614Sobrien
7848614Sobrien#ifdef	_THREAD_SAFE
7948614Sobrienstatic struct pthread_mutex	_gotgmt_mutexd = PTHREAD_MUTEX_STATIC_INITIALIZER;
8048614Sobrienstatic pthread_mutex_t		gotgmt_mutex   = &_gotgmt_mutexd;
8148614Sobrien#endif
8248614Sobrienstatic int got_GMT;
8348614Sobrien
8428021Sjoerg#define asizeof(a)	(sizeof (a) / sizeof ((a)[0]))
8528019Sjoerg
8648614Sobrienstatic char *
8748614Sobrien_strptime(const char *buf, const char *fmt, struct tm *tm)
8828019Sjoerg{
8928021Sjoerg	char	c;
9028021Sjoerg	const char *ptr;
9128021Sjoerg	int	i,
9228021Sjoerg		len;
9353941Sache	int Ealternative, Oalternative;
9428019Sjoerg
9528021Sjoerg	ptr = fmt;
9628021Sjoerg	while (*ptr != 0) {
9728021Sjoerg		if (*buf == 0)
9828021Sjoerg			break;
9928019Sjoerg
10028021Sjoerg		c = *ptr++;
10128019Sjoerg
10228021Sjoerg		if (c != '%') {
10328164Sache			if (isspace((unsigned char)c))
10428164Sache				while (*buf != 0 && isspace((unsigned char)*buf))
10528021Sjoerg					buf++;
10628021Sjoerg			else if (c != *buf++)
10728021Sjoerg				return 0;
10828021Sjoerg			continue;
10928021Sjoerg		}
11028019Sjoerg
11153941Sache		Ealternative = 0;
11253941Sache		Oalternative = 0;
11353941Sachelabel:
11428021Sjoerg		c = *ptr++;
11528021Sjoerg		switch (c) {
11628021Sjoerg		case 0:
11728021Sjoerg		case '%':
11828021Sjoerg			if (*buf++ != '%')
11928021Sjoerg				return 0;
12028021Sjoerg			break;
12128019Sjoerg
12253941Sache		case '+':
12348614Sobrien			buf = _strptime(buf, Locale->date_fmt, tm);
12428021Sjoerg			if (buf == 0)
12528021Sjoerg				return 0;
12628021Sjoerg			break;
12728019Sjoerg
12853941Sache		case 'C':
12953941Sache			if (!isdigit((unsigned char)*buf))
13053941Sache				return 0;
13153941Sache
13253941Sache			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
13353941Sache				i *= 10;
13453941Sache				i += *buf - '0';
13553941Sache			}
13653941Sache			if (i < 19)
13753941Sache				return 0;
13853941Sache
13953941Sache			tm->tm_year = i * 100 - 1900;
14053941Sache			break;
14153941Sache
14228021Sjoerg		case 'c':
14353941Sache			buf = _strptime(buf, Locale->c_fmt, tm);
14428021Sjoerg			if (buf == 0)
14528021Sjoerg				return 0;
14628021Sjoerg			break;
14728019Sjoerg
14828021Sjoerg		case 'D':
14948614Sobrien			buf = _strptime(buf, "%m/%d/%y", tm);
15028021Sjoerg			if (buf == 0)
15128021Sjoerg				return 0;
15228021Sjoerg			break;
15328019Sjoerg
15453941Sache		case 'E':
15553941Sache			Ealternative++;
15653941Sache			goto label;
15753941Sache
15853941Sache		case 'O':
15953941Sache			Oalternative++;
16053941Sache			goto label;
16153941Sache
16228021Sjoerg		case 'R':
16348614Sobrien			buf = _strptime(buf, "%H:%M", tm);
16428021Sjoerg			if (buf == 0)
16528021Sjoerg				return 0;
16628021Sjoerg			break;
16728019Sjoerg
16828021Sjoerg		case 'r':
16948614Sobrien			buf = _strptime(buf, "%I:%M:%S %p", tm);
17028021Sjoerg			if (buf == 0)
17128021Sjoerg				return 0;
17228021Sjoerg			break;
17328019Sjoerg
17428021Sjoerg		case 'T':
17548614Sobrien			buf = _strptime(buf, "%H:%M:%S", tm);
17628021Sjoerg			if (buf == 0)
17728021Sjoerg				return 0;
17828021Sjoerg			break;
17928019Sjoerg
18028021Sjoerg		case 'X':
18148614Sobrien			buf = _strptime(buf, Locale->X_fmt, tm);
18228021Sjoerg			if (buf == 0)
18328021Sjoerg				return 0;
18428021Sjoerg			break;
18528019Sjoerg
18628021Sjoerg		case 'x':
18753941Sache			buf = _strptime(buf, Ealternative ? Locale->Ex_fmt : Locale->x_fmt, tm);
18828021Sjoerg			if (buf == 0)
18928021Sjoerg				return 0;
19028021Sjoerg			break;
19128019Sjoerg
19228021Sjoerg		case 'j':
19328164Sache			if (!isdigit((unsigned char)*buf))
19428021Sjoerg				return 0;
19528019Sjoerg
19628164Sache			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
19728021Sjoerg				i *= 10;
19828021Sjoerg				i += *buf - '0';
19928021Sjoerg			}
20053083Ssheldonh			if (i < 1 || i > 366)
20128021Sjoerg				return 0;
20228019Sjoerg
20353083Ssheldonh			tm->tm_yday = i - 1;
20428021Sjoerg			break;
20528019Sjoerg
20628021Sjoerg		case 'M':
20728021Sjoerg		case 'S':
20828164Sache			if (*buf == 0 || isspace((unsigned char)*buf))
20928021Sjoerg				break;
21028019Sjoerg
21128164Sache			if (!isdigit((unsigned char)*buf))
21228021Sjoerg				return 0;
21328019Sjoerg
21428164Sache			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
21528021Sjoerg				i *= 10;
21628021Sjoerg				i += *buf - '0';
21728021Sjoerg			}
21828019Sjoerg
21953083Ssheldonh			if (c == 'M') {
22053083Ssheldonh				if (i > 59)
22153083Ssheldonh					return 0;
22228021Sjoerg				tm->tm_min = i;
22353083Ssheldonh			} else {
22453083Ssheldonh				if (i > 60)
22553083Ssheldonh					return 0;
22628021Sjoerg				tm->tm_sec = i;
22753083Ssheldonh			}
22828019Sjoerg
22928164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
23028164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
23128021Sjoerg					ptr++;
23228021Sjoerg			break;
23328019Sjoerg
23428021Sjoerg		case 'H':
23528021Sjoerg		case 'I':
23628021Sjoerg		case 'k':
23728021Sjoerg		case 'l':
23828164Sache			if (!isdigit((unsigned char)*buf))
23928021Sjoerg				return 0;
24028019Sjoerg
24128164Sache			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
24228021Sjoerg				i *= 10;
24328021Sjoerg				i += *buf - '0';
24428021Sjoerg			}
24528021Sjoerg			if (c == 'H' || c == 'k') {
24628021Sjoerg				if (i > 23)
24728021Sjoerg					return 0;
24828021Sjoerg			} else if (i > 11)
24928021Sjoerg				return 0;
25028019Sjoerg
25128021Sjoerg			tm->tm_hour = i;
25228019Sjoerg
25328164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
25428164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
25528021Sjoerg					ptr++;
25628021Sjoerg			break;
25728019Sjoerg
25828021Sjoerg		case 'p':
25928021Sjoerg			len = strlen(Locale->am);
26028021Sjoerg			if (strncasecmp(buf, Locale->am, len) == 0) {
26128021Sjoerg				if (tm->tm_hour > 12)
26228021Sjoerg					return 0;
26328021Sjoerg				if (tm->tm_hour == 12)
26428021Sjoerg					tm->tm_hour = 0;
26528021Sjoerg				buf += len;
26628021Sjoerg				break;
26728021Sjoerg			}
26828019Sjoerg
26928021Sjoerg			len = strlen(Locale->pm);
27028021Sjoerg			if (strncasecmp(buf, Locale->pm, len) == 0) {
27128021Sjoerg				if (tm->tm_hour > 12)
27228021Sjoerg					return 0;
27328021Sjoerg				if (tm->tm_hour != 12)
27428021Sjoerg					tm->tm_hour += 12;
27528021Sjoerg				buf += len;
27628021Sjoerg				break;
27728021Sjoerg			}
27828019Sjoerg
27928021Sjoerg			return 0;
28028019Sjoerg
28128021Sjoerg		case 'A':
28228021Sjoerg		case 'a':
28328021Sjoerg			for (i = 0; i < asizeof(Locale->weekday); i++) {
28428021Sjoerg				len = strlen(Locale->weekday[i]);
28528021Sjoerg				if (strncasecmp(buf,
28628021Sjoerg						Locale->weekday[i],
28728021Sjoerg						len) == 0)
28828021Sjoerg					break;
28928019Sjoerg
29028021Sjoerg				len = strlen(Locale->wday[i]);
29128021Sjoerg				if (strncasecmp(buf,
29228021Sjoerg						Locale->wday[i],
29328021Sjoerg						len) == 0)
29428021Sjoerg					break;
29528021Sjoerg			}
29628021Sjoerg			if (i == asizeof(Locale->weekday))
29728021Sjoerg				return 0;
29828019Sjoerg
29928021Sjoerg			tm->tm_wday = i;
30028021Sjoerg			buf += len;
30128021Sjoerg			break;
30228019Sjoerg
30353083Ssheldonh		case 'U':
30453083Ssheldonh		case 'W':
30553083Ssheldonh			/*
30653083Ssheldonh			 * XXX This is bogus, as we can not assume any valid
30753083Ssheldonh			 * information present in the tm structure at this
30853083Ssheldonh			 * point to calculate a real value, so just check the
30953083Ssheldonh			 * range for now.
31053083Ssheldonh			 */
31153083Ssheldonh			if (!isdigit((unsigned char)*buf))
31253083Ssheldonh				return 0;
31353083Ssheldonh
31453083Ssheldonh			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
31553083Ssheldonh				i *= 10;
31653083Ssheldonh				i += *buf - '0';
31753083Ssheldonh			}
31853083Ssheldonh			if (i > 53)
31953083Ssheldonh				return 0;
32053083Ssheldonh
32153083Ssheldonh			if (*buf != 0 && isspace((unsigned char)*buf))
32253083Ssheldonh				while (*ptr != 0 && !isspace((unsigned char)*ptr))
32353083Ssheldonh					ptr++;
32453083Ssheldonh			break;
32553083Ssheldonh
32653083Ssheldonh		case 'w':
32753083Ssheldonh			if (!isdigit((unsigned char)*buf))
32853083Ssheldonh				return 0;
32953083Ssheldonh
33053083Ssheldonh			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
33153083Ssheldonh				i *= 10;
33253083Ssheldonh				i += *buf - '0';
33353083Ssheldonh			}
33453083Ssheldonh			if (i > 6)
33553083Ssheldonh				return 0;
33653083Ssheldonh
33753083Ssheldonh			tm->tm_wday = i;
33853083Ssheldonh
33953083Ssheldonh			if (*buf != 0 && isspace((unsigned char)*buf))
34053083Ssheldonh				while (*ptr != 0 && !isspace((unsigned char)*ptr))
34153083Ssheldonh					ptr++;
34253083Ssheldonh			break;
34353083Ssheldonh
34428021Sjoerg		case 'd':
34528021Sjoerg		case 'e':
34628164Sache			if (!isdigit((unsigned char)*buf))
34728021Sjoerg				return 0;
34828019Sjoerg
34928164Sache			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
35028021Sjoerg				i *= 10;
35128021Sjoerg				i += *buf - '0';
35228021Sjoerg			}
35328021Sjoerg			if (i > 31)
35428021Sjoerg				return 0;
35528019Sjoerg
35628021Sjoerg			tm->tm_mday = i;
35728019Sjoerg
35828164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
35928164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
36028021Sjoerg					ptr++;
36128021Sjoerg			break;
36228019Sjoerg
36328021Sjoerg		case 'B':
36428021Sjoerg		case 'b':
36528021Sjoerg		case 'h':
36628021Sjoerg			for (i = 0; i < asizeof(Locale->month); i++) {
36753941Sache				if (Oalternative) {
36853941Sache					if (c == 'B') {
36953941Sache						len = strlen(Locale->alt_month[i]);
37053941Sache						if (strncasecmp(buf,
37153941Sache								Locale->alt_month[i],
37253941Sache								len) == 0)
37353941Sache							break;
37453941Sache					}
37553941Sache				} else {
37653941Sache					if (c == 'B') {
37753941Sache						len = strlen(Locale->month[i]);
37853941Sache						if (strncasecmp(buf,
37953941Sache								Locale->month[i],
38053941Sache								len) == 0)
38153941Sache							break;
38253941Sache					} else {
38353941Sache						len = strlen(Locale->mon[i]);
38453941Sache						if (strncasecmp(buf,
38553941Sache								Locale->mon[i],
38653941Sache								len) == 0)
38753941Sache							break;
38853941Sache					}
38953941Sache				}
39028021Sjoerg			}
39128021Sjoerg			if (i == asizeof(Locale->month))
39228021Sjoerg				return 0;
39328019Sjoerg
39428021Sjoerg			tm->tm_mon = i;
39528021Sjoerg			buf += len;
39628021Sjoerg			break;
39728019Sjoerg
39828021Sjoerg		case 'm':
39928164Sache			if (!isdigit((unsigned char)*buf))
40028021Sjoerg				return 0;
40128019Sjoerg
40228164Sache			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
40328021Sjoerg				i *= 10;
40428021Sjoerg				i += *buf - '0';
40528021Sjoerg			}
40628021Sjoerg			if (i < 1 || i > 12)
40728021Sjoerg				return 0;
40828019Sjoerg
40928021Sjoerg			tm->tm_mon = i - 1;
41028019Sjoerg
41128164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
41228164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
41328021Sjoerg					ptr++;
41428021Sjoerg			break;
41528019Sjoerg
41628021Sjoerg		case 'Y':
41728021Sjoerg		case 'y':
41828164Sache			if (*buf == 0 || isspace((unsigned char)*buf))
41928021Sjoerg				break;
42028019Sjoerg
42128164Sache			if (!isdigit((unsigned char)*buf))
42228021Sjoerg				return 0;
42328019Sjoerg
42428164Sache			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
42528021Sjoerg				i *= 10;
42628021Sjoerg				i += *buf - '0';
42728021Sjoerg			}
42828021Sjoerg			if (c == 'Y')
42928021Sjoerg				i -= 1900;
43046051Swes			if (c == 'y' && i < 69)
43146042Swes				i += 100;
43228021Sjoerg			if (i < 0)
43328021Sjoerg				return 0;
43428019Sjoerg
43528021Sjoerg			tm->tm_year = i;
43628019Sjoerg
43728164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
43828164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
43928021Sjoerg					ptr++;
44028021Sjoerg			break;
44148550Sobrien
44248550Sobrien		case 'Z':
44348550Sobrien			{
44448550Sobrien			const char *cp;
44548550Sobrien			char *zonestr;
44648550Sobrien
44752860Sache			for (cp = buf; *cp && isupper((unsigned char)*cp); ++cp) {/*empty*/}
44848550Sobrien			if (cp - buf) {
44948550Sobrien				zonestr = alloca(cp - buf + 1);
45048550Sobrien				strncpy(zonestr, buf, cp - buf);
45148550Sobrien				zonestr[cp - buf] = '\0';
45248550Sobrien				tzset();
45348550Sobrien				if (0 == strcmp(zonestr, "GMT")) {
45448614Sobrien				    got_GMT = 1;
45548550Sobrien				} else if (0 == strcmp(zonestr, tzname[0])) {
45648614Sobrien				    tm->tm_isdst = 0;
45748550Sobrien				} else if (0 == strcmp(zonestr, tzname[1])) {
45848614Sobrien				    tm->tm_isdst = 1;
45948550Sobrien				} else {
46048614Sobrien				    return 0;
46148550Sobrien				}
46248550Sobrien				buf += cp - buf;
46348550Sobrien			}
46448550Sobrien			}
46548550Sobrien			break;
46628021Sjoerg		}
46728021Sjoerg	}
46848614Sobrien	return (char *)buf;
46948614Sobrien}
47028019Sjoerg
47148614Sobrien
47248614Sobrienchar *
47348614Sobrienstrptime(const char *buf, const char *fmt, struct tm *tm)
47448614Sobrien{
47548614Sobrien	char *ret;
47648614Sobrien
47748614Sobrien#ifdef	_THREAD_SAFE
47848614Sobrien	pthread_mutex_lock(&gotgmt_mutex);
47948614Sobrien#endif
48048614Sobrien
48148614Sobrien	got_GMT = 0;
48248614Sobrien	ret = _strptime(buf, fmt, tm);
48348614Sobrien	if (ret && got_GMT) {
48448550Sobrien		time_t t = timegm(tm);
48548614Sobrien	    localtime_r(&t, tm);
48648614Sobrien		got_GMT = 0;
48748550Sobrien	}
48848614Sobrien
48948614Sobrien#ifdef	_THREAD_SAFE
49048614Sobrien	pthread_mutex_unlock(&gotgmt_mutex);
49148614Sobrien#endif
49248614Sobrien
49348614Sobrien	return ret;
49428019Sjoerg}
495