strptime.c revision 71579
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 71579 2001-01-24 13:01:12Z deischen $"; 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 6771579Sdeischen#include "namespace.h" 6828019Sjoerg#include <time.h> 6928019Sjoerg#include <ctype.h> 7028019Sjoerg#include <string.h> 7148614Sobrien#include <pthread.h> 7271579Sdeischen#include "un-namespace.h" 7371579Sdeischen#include "libc_private.h" 7428021Sjoerg#include "timelocal.h" 7528019Sjoerg 7648614Sobrienstatic char * _strptime(const char *, const char *, struct tm *); 7748614Sobrien 7871579Sdeischenstatic pthread_mutex_t gotgmt_mutex = PTHREAD_MUTEX_INITIALIZER; 7948614Sobrienstatic int got_GMT; 8048614Sobrien 8128021Sjoerg#define asizeof(a) (sizeof (a) / sizeof ((a)[0])) 8228019Sjoerg 8348614Sobrienstatic char * 8448614Sobrien_strptime(const char *buf, const char *fmt, struct tm *tm) 8528019Sjoerg{ 8628021Sjoerg char c; 8728021Sjoerg const char *ptr; 8828021Sjoerg int i, 8928021Sjoerg len; 9053941Sache int Ealternative, Oalternative; 9128019Sjoerg 9228021Sjoerg ptr = fmt; 9328021Sjoerg while (*ptr != 0) { 9428021Sjoerg if (*buf == 0) 9528021Sjoerg break; 9628019Sjoerg 9728021Sjoerg c = *ptr++; 9828019Sjoerg 9928021Sjoerg if (c != '%') { 10028164Sache if (isspace((unsigned char)c)) 10128164Sache while (*buf != 0 && isspace((unsigned char)*buf)) 10228021Sjoerg buf++; 10328021Sjoerg else if (c != *buf++) 10428021Sjoerg return 0; 10528021Sjoerg continue; 10628021Sjoerg } 10728019Sjoerg 10853941Sache Ealternative = 0; 10953941Sache Oalternative = 0; 11053941Sachelabel: 11128021Sjoerg c = *ptr++; 11228021Sjoerg switch (c) { 11328021Sjoerg case 0: 11428021Sjoerg case '%': 11528021Sjoerg if (*buf++ != '%') 11628021Sjoerg return 0; 11728021Sjoerg break; 11828019Sjoerg 11953941Sache case '+': 12048614Sobrien buf = _strptime(buf, Locale->date_fmt, tm); 12128021Sjoerg if (buf == 0) 12228021Sjoerg return 0; 12328021Sjoerg break; 12428019Sjoerg 12553941Sache case 'C': 12653941Sache if (!isdigit((unsigned char)*buf)) 12753941Sache return 0; 12853941Sache 12954316Ssheldonh /* XXX This will break for 3-digit centuries. */ 13054316Ssheldonh len = 2; 13154316Ssheldonh for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { 13253941Sache i *= 10; 13353941Sache i += *buf - '0'; 13454316Ssheldonh len--; 13553941Sache } 13653941Sache if (i < 19) 13753941Sache return 0; 13853941Sache 13953941Sache tm->tm_year = i * 100 - 1900; 14053941Sache break; 14153941Sache 14228021Sjoerg case 'c': 14367632Sache /* NOTE: c_fmt is intentionally ignored */ 14467632Sache buf = _strptime(buf, "%a %Ef %T %Y", tm); 14528021Sjoerg if (buf == 0) 14628021Sjoerg return 0; 14728021Sjoerg break; 14828019Sjoerg 14928021Sjoerg case 'D': 15048614Sobrien buf = _strptime(buf, "%m/%d/%y", tm); 15128021Sjoerg if (buf == 0) 15228021Sjoerg return 0; 15328021Sjoerg break; 15428019Sjoerg 15553941Sache case 'E': 15653960Sache if (Ealternative || Oalternative) 15753960Sache break; 15853941Sache Ealternative++; 15953941Sache goto label; 16053941Sache 16153941Sache case 'O': 16253960Sache if (Ealternative || Oalternative) 16353960Sache break; 16453941Sache Oalternative++; 16553941Sache goto label; 16653941Sache 16753960Sache case 'F': 16853960Sache case 'f': 16953960Sache if (!Ealternative) 17053960Sache break; 17153960Sache buf = _strptime(buf, (c == 'f') ? Locale->Ef_fmt : Locale->EF_fmt, tm); 17253960Sache if (buf == 0) 17353960Sache return 0; 17453960Sache break; 17553960Sache 17628021Sjoerg case 'R': 17748614Sobrien buf = _strptime(buf, "%H:%M", tm); 17828021Sjoerg if (buf == 0) 17928021Sjoerg return 0; 18028021Sjoerg break; 18128019Sjoerg 18228021Sjoerg case 'r': 18348614Sobrien buf = _strptime(buf, "%I:%M:%S %p", tm); 18428021Sjoerg if (buf == 0) 18528021Sjoerg return 0; 18628021Sjoerg break; 18728019Sjoerg 18828021Sjoerg case 'T': 18948614Sobrien buf = _strptime(buf, "%H:%M:%S", tm); 19028021Sjoerg if (buf == 0) 19128021Sjoerg return 0; 19228021Sjoerg break; 19328019Sjoerg 19428021Sjoerg case 'X': 19548614Sobrien buf = _strptime(buf, Locale->X_fmt, tm); 19628021Sjoerg if (buf == 0) 19728021Sjoerg return 0; 19828021Sjoerg break; 19928019Sjoerg 20028021Sjoerg case 'x': 20153960Sache buf = _strptime(buf, Locale->x_fmt, tm); 20228021Sjoerg if (buf == 0) 20328021Sjoerg return 0; 20428021Sjoerg break; 20528019Sjoerg 20628021Sjoerg case 'j': 20728164Sache if (!isdigit((unsigned char)*buf)) 20828021Sjoerg return 0; 20928019Sjoerg 21054316Ssheldonh len = 3; 21154316Ssheldonh for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { 21228021Sjoerg i *= 10; 21328021Sjoerg i += *buf - '0'; 21454316Ssheldonh len--; 21528021Sjoerg } 21653083Ssheldonh if (i < 1 || i > 366) 21728021Sjoerg return 0; 21828019Sjoerg 21953083Ssheldonh tm->tm_yday = i - 1; 22028021Sjoerg break; 22128019Sjoerg 22228021Sjoerg case 'M': 22328021Sjoerg case 'S': 22428164Sache if (*buf == 0 || isspace((unsigned char)*buf)) 22528021Sjoerg break; 22628019Sjoerg 22728164Sache if (!isdigit((unsigned char)*buf)) 22828021Sjoerg return 0; 22928019Sjoerg 23054316Ssheldonh len = 2; 23154316Ssheldonh for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { 23228021Sjoerg i *= 10; 23328021Sjoerg i += *buf - '0'; 23454316Ssheldonh len--; 23528021Sjoerg } 23628019Sjoerg 23753083Ssheldonh if (c == 'M') { 23853083Ssheldonh if (i > 59) 23953083Ssheldonh return 0; 24028021Sjoerg tm->tm_min = i; 24153083Ssheldonh } else { 24253083Ssheldonh if (i > 60) 24353083Ssheldonh return 0; 24428021Sjoerg tm->tm_sec = i; 24553083Ssheldonh } 24628019Sjoerg 24728164Sache if (*buf != 0 && isspace((unsigned char)*buf)) 24828164Sache while (*ptr != 0 && !isspace((unsigned char)*ptr)) 24928021Sjoerg ptr++; 25028021Sjoerg break; 25128019Sjoerg 25228021Sjoerg case 'H': 25328021Sjoerg case 'I': 25428021Sjoerg case 'k': 25528021Sjoerg case 'l': 25654316Ssheldonh /* 25754316Ssheldonh * Of these, %l is the only specifier explicitly 25854316Ssheldonh * documented as not being zero-padded. However, 25954316Ssheldonh * there is no harm in allowing zero-padding. 26054316Ssheldonh * 26154316Ssheldonh * XXX The %l specifier may gobble one too many 26254316Ssheldonh * digits if used incorrectly. 26354316Ssheldonh */ 26428164Sache if (!isdigit((unsigned char)*buf)) 26528021Sjoerg return 0; 26628019Sjoerg 26754316Ssheldonh len = 2; 26854316Ssheldonh for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { 26928021Sjoerg i *= 10; 27028021Sjoerg i += *buf - '0'; 27154316Ssheldonh len--; 27228021Sjoerg } 27328021Sjoerg if (c == 'H' || c == 'k') { 27428021Sjoerg if (i > 23) 27528021Sjoerg return 0; 27654301Ssheldonh } else if (i > 12) 27728021Sjoerg return 0; 27828019Sjoerg 27928021Sjoerg tm->tm_hour = i; 28028019Sjoerg 28128164Sache if (*buf != 0 && isspace((unsigned char)*buf)) 28228164Sache while (*ptr != 0 && !isspace((unsigned char)*ptr)) 28328021Sjoerg ptr++; 28428021Sjoerg break; 28528019Sjoerg 28628021Sjoerg case 'p': 28754316Ssheldonh /* 28854316Ssheldonh * XXX This is bogus if parsed before hour-related 28954316Ssheldonh * specifiers. 29054316Ssheldonh */ 29128021Sjoerg len = strlen(Locale->am); 29228021Sjoerg if (strncasecmp(buf, Locale->am, len) == 0) { 29328021Sjoerg if (tm->tm_hour > 12) 29428021Sjoerg return 0; 29528021Sjoerg if (tm->tm_hour == 12) 29628021Sjoerg tm->tm_hour = 0; 29728021Sjoerg buf += len; 29828021Sjoerg break; 29928021Sjoerg } 30028019Sjoerg 30128021Sjoerg len = strlen(Locale->pm); 30228021Sjoerg if (strncasecmp(buf, Locale->pm, len) == 0) { 30328021Sjoerg if (tm->tm_hour > 12) 30428021Sjoerg return 0; 30528021Sjoerg if (tm->tm_hour != 12) 30628021Sjoerg tm->tm_hour += 12; 30728021Sjoerg buf += len; 30828021Sjoerg break; 30928021Sjoerg } 31028019Sjoerg 31128021Sjoerg return 0; 31228019Sjoerg 31328021Sjoerg case 'A': 31428021Sjoerg case 'a': 31528021Sjoerg for (i = 0; i < asizeof(Locale->weekday); i++) { 31653942Sache if (c == 'A') { 31753942Sache len = strlen(Locale->weekday[i]); 31853942Sache if (strncasecmp(buf, 31953942Sache Locale->weekday[i], 32053942Sache len) == 0) 32153942Sache break; 32253942Sache } else { 32353942Sache len = strlen(Locale->wday[i]); 32453942Sache if (strncasecmp(buf, 32553942Sache Locale->wday[i], 32653942Sache len) == 0) 32753942Sache break; 32853942Sache } 32928021Sjoerg } 33028021Sjoerg if (i == asizeof(Locale->weekday)) 33128021Sjoerg return 0; 33228019Sjoerg 33328021Sjoerg tm->tm_wday = i; 33428021Sjoerg buf += len; 33528021Sjoerg break; 33628019Sjoerg 33753083Ssheldonh case 'U': 33853083Ssheldonh case 'W': 33953083Ssheldonh /* 34053083Ssheldonh * XXX This is bogus, as we can not assume any valid 34153083Ssheldonh * information present in the tm structure at this 34253083Ssheldonh * point to calculate a real value, so just check the 34353083Ssheldonh * range for now. 34453083Ssheldonh */ 34553083Ssheldonh if (!isdigit((unsigned char)*buf)) 34653083Ssheldonh return 0; 34753083Ssheldonh 34854316Ssheldonh len = 2; 34954316Ssheldonh for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { 35053083Ssheldonh i *= 10; 35153083Ssheldonh i += *buf - '0'; 35254316Ssheldonh len--; 35353083Ssheldonh } 35453083Ssheldonh if (i > 53) 35553083Ssheldonh return 0; 35653083Ssheldonh 35753083Ssheldonh if (*buf != 0 && isspace((unsigned char)*buf)) 35853083Ssheldonh while (*ptr != 0 && !isspace((unsigned char)*ptr)) 35953083Ssheldonh ptr++; 36053083Ssheldonh break; 36153083Ssheldonh 36253083Ssheldonh case 'w': 36353083Ssheldonh if (!isdigit((unsigned char)*buf)) 36453083Ssheldonh return 0; 36553083Ssheldonh 36654316Ssheldonh i = *buf - '0'; 36753083Ssheldonh if (i > 6) 36853083Ssheldonh return 0; 36953083Ssheldonh 37053083Ssheldonh tm->tm_wday = i; 37153083Ssheldonh 37253083Ssheldonh if (*buf != 0 && isspace((unsigned char)*buf)) 37353083Ssheldonh while (*ptr != 0 && !isspace((unsigned char)*ptr)) 37453083Ssheldonh ptr++; 37553083Ssheldonh break; 37653083Ssheldonh 37728021Sjoerg case 'd': 37828021Sjoerg case 'e': 37954316Ssheldonh /* 38054316Ssheldonh * The %e specifier is explicitly documented as not 38154316Ssheldonh * being zero-padded but there is no harm in allowing 38254316Ssheldonh * such padding. 38354316Ssheldonh * 38454316Ssheldonh * XXX The %e specifier may gobble one too many 38554316Ssheldonh * digits if used incorrectly. 38654316Ssheldonh */ 38728164Sache if (!isdigit((unsigned char)*buf)) 38828021Sjoerg return 0; 38928019Sjoerg 39054316Ssheldonh len = 2; 39154316Ssheldonh for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { 39228021Sjoerg i *= 10; 39328021Sjoerg i += *buf - '0'; 39454316Ssheldonh len--; 39528021Sjoerg } 39628021Sjoerg if (i > 31) 39728021Sjoerg return 0; 39828019Sjoerg 39928021Sjoerg tm->tm_mday = i; 40028019Sjoerg 40128164Sache if (*buf != 0 && isspace((unsigned char)*buf)) 40228164Sache while (*ptr != 0 && !isspace((unsigned char)*ptr)) 40328021Sjoerg ptr++; 40428021Sjoerg break; 40528019Sjoerg 40628021Sjoerg case 'B': 40728021Sjoerg case 'b': 40828021Sjoerg case 'h': 40928021Sjoerg for (i = 0; i < asizeof(Locale->month); i++) { 41053941Sache if (Oalternative) { 41153941Sache if (c == 'B') { 41253941Sache len = strlen(Locale->alt_month[i]); 41353941Sache if (strncasecmp(buf, 41453941Sache Locale->alt_month[i], 41553941Sache len) == 0) 41653941Sache break; 41753941Sache } 41853941Sache } else { 41953941Sache if (c == 'B') { 42053941Sache len = strlen(Locale->month[i]); 42153941Sache if (strncasecmp(buf, 42253941Sache Locale->month[i], 42353941Sache len) == 0) 42453941Sache break; 42553941Sache } else { 42653941Sache len = strlen(Locale->mon[i]); 42753941Sache if (strncasecmp(buf, 42853941Sache Locale->mon[i], 42953941Sache len) == 0) 43053941Sache break; 43153941Sache } 43253941Sache } 43328021Sjoerg } 43428021Sjoerg if (i == asizeof(Locale->month)) 43528021Sjoerg return 0; 43628019Sjoerg 43728021Sjoerg tm->tm_mon = i; 43828021Sjoerg buf += len; 43928021Sjoerg break; 44028019Sjoerg 44128021Sjoerg case 'm': 44228164Sache if (!isdigit((unsigned char)*buf)) 44328021Sjoerg return 0; 44428019Sjoerg 44554316Ssheldonh len = 2; 44654316Ssheldonh for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { 44728021Sjoerg i *= 10; 44828021Sjoerg i += *buf - '0'; 44954316Ssheldonh len--; 45028021Sjoerg } 45128021Sjoerg if (i < 1 || i > 12) 45228021Sjoerg return 0; 45328019Sjoerg 45428021Sjoerg tm->tm_mon = i - 1; 45528019Sjoerg 45628164Sache if (*buf != 0 && isspace((unsigned char)*buf)) 45728164Sache while (*ptr != 0 && !isspace((unsigned char)*ptr)) 45828021Sjoerg ptr++; 45928021Sjoerg break; 46028019Sjoerg 46128021Sjoerg case 'Y': 46228021Sjoerg case 'y': 46328164Sache if (*buf == 0 || isspace((unsigned char)*buf)) 46428021Sjoerg break; 46528019Sjoerg 46628164Sache if (!isdigit((unsigned char)*buf)) 46728021Sjoerg return 0; 46828019Sjoerg 46954316Ssheldonh len = (c == 'Y') ? 4 : 2; 47054316Ssheldonh for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { 47128021Sjoerg i *= 10; 47228021Sjoerg i += *buf - '0'; 47354316Ssheldonh len--; 47428021Sjoerg } 47528021Sjoerg if (c == 'Y') 47628021Sjoerg i -= 1900; 47746051Swes if (c == 'y' && i < 69) 47846042Swes i += 100; 47928021Sjoerg if (i < 0) 48028021Sjoerg return 0; 48128019Sjoerg 48228021Sjoerg tm->tm_year = i; 48328019Sjoerg 48428164Sache if (*buf != 0 && isspace((unsigned char)*buf)) 48528164Sache while (*ptr != 0 && !isspace((unsigned char)*ptr)) 48628021Sjoerg ptr++; 48728021Sjoerg break; 48848550Sobrien 48948550Sobrien case 'Z': 49048550Sobrien { 49148550Sobrien const char *cp; 49248550Sobrien char *zonestr; 49348550Sobrien 49452860Sache for (cp = buf; *cp && isupper((unsigned char)*cp); ++cp) {/*empty*/} 49548550Sobrien if (cp - buf) { 49648550Sobrien zonestr = alloca(cp - buf + 1); 49748550Sobrien strncpy(zonestr, buf, cp - buf); 49848550Sobrien zonestr[cp - buf] = '\0'; 49948550Sobrien tzset(); 50048550Sobrien if (0 == strcmp(zonestr, "GMT")) { 50148614Sobrien got_GMT = 1; 50248550Sobrien } else if (0 == strcmp(zonestr, tzname[0])) { 50348614Sobrien tm->tm_isdst = 0; 50448550Sobrien } else if (0 == strcmp(zonestr, tzname[1])) { 50548614Sobrien tm->tm_isdst = 1; 50648550Sobrien } else { 50748614Sobrien return 0; 50848550Sobrien } 50948550Sobrien buf += cp - buf; 51048550Sobrien } 51148550Sobrien } 51248550Sobrien break; 51328021Sjoerg } 51428021Sjoerg } 51548614Sobrien return (char *)buf; 51648614Sobrien} 51728019Sjoerg 51848614Sobrien 51948614Sobrienchar * 52048614Sobrienstrptime(const char *buf, const char *fmt, struct tm *tm) 52148614Sobrien{ 52248614Sobrien char *ret; 52348614Sobrien 52471579Sdeischen if (__isthreaded) 52571579Sdeischen _pthread_mutex_lock(&gotgmt_mutex); 52648614Sobrien 52748614Sobrien got_GMT = 0; 52848614Sobrien ret = _strptime(buf, fmt, tm); 52948614Sobrien if (ret && got_GMT) { 53048550Sobrien time_t t = timegm(tm); 53171579Sdeischen localtime_r(&t, tm); 53248614Sobrien got_GMT = 0; 53348550Sobrien } 53448614Sobrien 53571579Sdeischen if (__isthreaded) 53671579Sdeischen _pthread_mutex_unlock(&gotgmt_mutex); 53748614Sobrien 53848614Sobrien return ret; 53928019Sjoerg} 540