1*0a6a1f1dSLionel Sambuc /* $NetBSD: strptime.c,v 1.49 2015/10/09 17:21:45 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * This code was contributed to The NetBSD Foundation by Klaus Klein.
82fe8fb19SBen Gras * Heavily optimised by David Laight
92fe8fb19SBen Gras *
102fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras * are met:
132fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202fe8fb19SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212fe8fb19SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222fe8fb19SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232fe8fb19SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242fe8fb19SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252fe8fb19SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262fe8fb19SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272fe8fb19SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282fe8fb19SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292fe8fb19SBen Gras * POSSIBILITY OF SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
34*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: strptime.c,v 1.49 2015/10/09 17:21:45 christos Exp $");
352fe8fb19SBen Gras #endif
362fe8fb19SBen Gras
372fe8fb19SBen Gras #include "namespace.h"
382fe8fb19SBen Gras #include <sys/localedef.h>
39*0a6a1f1dSLionel Sambuc #include <sys/types.h>
402fe8fb19SBen Gras #include <ctype.h>
412fe8fb19SBen Gras #include <locale.h>
422fe8fb19SBen Gras #include <string.h>
432fe8fb19SBen Gras #include <time.h>
442fe8fb19SBen Gras #include <tzfile.h>
452fe8fb19SBen Gras #include "private.h"
4684d9c625SLionel Sambuc #include "setlocale_local.h"
472fe8fb19SBen Gras
482fe8fb19SBen Gras #ifdef __weak_alias
492fe8fb19SBen Gras __weak_alias(strptime,_strptime)
5084d9c625SLionel Sambuc __weak_alias(strptime_l, _strptime_l)
512fe8fb19SBen Gras #endif
522fe8fb19SBen Gras
53*0a6a1f1dSLionel Sambuc static const u_char *conv_num(const unsigned char *, int *, uint, uint);
54*0a6a1f1dSLionel Sambuc static const u_char *find_string(const u_char *, int *, const char * const *,
55*0a6a1f1dSLionel Sambuc const char * const *, int);
56*0a6a1f1dSLionel Sambuc
5784d9c625SLionel Sambuc #define _TIME_LOCALE(loc) \
5884d9c625SLionel Sambuc ((_TimeLocale *)((loc)->part_impl[(size_t)LC_TIME]))
592fe8fb19SBen Gras
602fe8fb19SBen Gras /*
612fe8fb19SBen Gras * We do not implement alternate representations. However, we always
622fe8fb19SBen Gras * check whether a given modifier is allowed for a certain conversion.
632fe8fb19SBen Gras */
642fe8fb19SBen Gras #define ALT_E 0x01
652fe8fb19SBen Gras #define ALT_O 0x02
662fe8fb19SBen Gras #define LEGAL_ALT(x) { if (alt_format & ~(x)) return NULL; }
672fe8fb19SBen Gras
68*0a6a1f1dSLionel Sambuc #define S_YEAR (1 << 0)
69*0a6a1f1dSLionel Sambuc #define S_MON (1 << 1)
70*0a6a1f1dSLionel Sambuc #define S_YDAY (1 << 2)
71*0a6a1f1dSLionel Sambuc #define S_MDAY (1 << 3)
72*0a6a1f1dSLionel Sambuc #define S_WDAY (1 << 4)
73*0a6a1f1dSLionel Sambuc #define S_HOUR (1 << 5)
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc #define HAVE_MDAY(s) (s & S_MDAY)
76*0a6a1f1dSLionel Sambuc #define HAVE_MON(s) (s & S_MON)
77*0a6a1f1dSLionel Sambuc #define HAVE_WDAY(s) (s & S_WDAY)
78*0a6a1f1dSLionel Sambuc #define HAVE_YDAY(s) (s & S_YDAY)
79*0a6a1f1dSLionel Sambuc #define HAVE_YEAR(s) (s & S_YEAR)
80*0a6a1f1dSLionel Sambuc #define HAVE_HOUR(s) (s & S_HOUR)
81*0a6a1f1dSLionel Sambuc
822fe8fb19SBen Gras static char gmt[] = { "GMT" };
832fe8fb19SBen Gras static char utc[] = { "UTC" };
842fe8fb19SBen Gras /* RFC-822/RFC-2822 */
852fe8fb19SBen Gras static const char * const nast[5] = {
862fe8fb19SBen Gras "EST", "CST", "MST", "PST", "\0\0\0"
872fe8fb19SBen Gras };
882fe8fb19SBen Gras static const char * const nadt[5] = {
892fe8fb19SBen Gras "EDT", "CDT", "MDT", "PDT", "\0\0\0"
902fe8fb19SBen Gras };
912fe8fb19SBen Gras
92*0a6a1f1dSLionel Sambuc /*
93*0a6a1f1dSLionel Sambuc * Table to determine the ordinal date for the start of a month.
94*0a6a1f1dSLionel Sambuc * Ref: http://en.wikipedia.org/wiki/ISO_week_date
95*0a6a1f1dSLionel Sambuc */
96*0a6a1f1dSLionel Sambuc static const int start_of_month[2][13] = {
97*0a6a1f1dSLionel Sambuc /* non-leap year */
98*0a6a1f1dSLionel Sambuc { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
99*0a6a1f1dSLionel Sambuc /* leap year */
100*0a6a1f1dSLionel Sambuc { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
101*0a6a1f1dSLionel Sambuc };
102*0a6a1f1dSLionel Sambuc
103*0a6a1f1dSLionel Sambuc /*
104*0a6a1f1dSLionel Sambuc * Calculate the week day of the first day of a year. Valid for
105*0a6a1f1dSLionel Sambuc * the Gregorian calendar, which began Sept 14, 1752 in the UK
106*0a6a1f1dSLionel Sambuc * and its colonies. Ref:
107*0a6a1f1dSLionel Sambuc * http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
108*0a6a1f1dSLionel Sambuc */
109*0a6a1f1dSLionel Sambuc
110*0a6a1f1dSLionel Sambuc static int
first_wday_of(int yr)111*0a6a1f1dSLionel Sambuc first_wday_of(int yr)
112*0a6a1f1dSLionel Sambuc {
113*0a6a1f1dSLionel Sambuc return ((2 * (3 - (yr / 100) % 4)) + (yr % 100) + ((yr % 100) / 4) +
114*0a6a1f1dSLionel Sambuc (isleap(yr) ? 6 : 0) + 1) % 7;
115*0a6a1f1dSLionel Sambuc }
1162fe8fb19SBen Gras
1172fe8fb19SBen Gras char *
strptime(const char * buf,const char * fmt,struct tm * tm)1182fe8fb19SBen Gras strptime(const char *buf, const char *fmt, struct tm *tm)
1192fe8fb19SBen Gras {
12084d9c625SLionel Sambuc return strptime_l(buf, fmt, tm, _current_locale());
12184d9c625SLionel Sambuc }
12284d9c625SLionel Sambuc
12384d9c625SLionel Sambuc char *
strptime_l(const char * buf,const char * fmt,struct tm * tm,locale_t loc)12484d9c625SLionel Sambuc strptime_l(const char *buf, const char *fmt, struct tm *tm, locale_t loc)
12584d9c625SLionel Sambuc {
1262fe8fb19SBen Gras unsigned char c;
1272fe8fb19SBen Gras const unsigned char *bp, *ep;
128*0a6a1f1dSLionel Sambuc int alt_format, i, split_year = 0, neg = 0, state = 0,
129*0a6a1f1dSLionel Sambuc day_offset = -1, week_offset = 0, offs;
1302fe8fb19SBen Gras const char *new_fmt;
1312fe8fb19SBen Gras
1322fe8fb19SBen Gras bp = (const u_char *)buf;
1332fe8fb19SBen Gras
1342fe8fb19SBen Gras while (bp != NULL && (c = *fmt++) != '\0') {
1352fe8fb19SBen Gras /* Clear `alternate' modifier prior to new conversion. */
1362fe8fb19SBen Gras alt_format = 0;
1372fe8fb19SBen Gras i = 0;
1382fe8fb19SBen Gras
1392fe8fb19SBen Gras /* Eat up white-space. */
1402fe8fb19SBen Gras if (isspace(c)) {
1412fe8fb19SBen Gras while (isspace(*bp))
1422fe8fb19SBen Gras bp++;
1432fe8fb19SBen Gras continue;
1442fe8fb19SBen Gras }
1452fe8fb19SBen Gras
1462fe8fb19SBen Gras if (c != '%')
1472fe8fb19SBen Gras goto literal;
1482fe8fb19SBen Gras
1492fe8fb19SBen Gras
1502fe8fb19SBen Gras again: switch (c = *fmt++) {
1512fe8fb19SBen Gras case '%': /* "%%" is converted to "%". */
1522fe8fb19SBen Gras literal:
1532fe8fb19SBen Gras if (c != *bp++)
1542fe8fb19SBen Gras return NULL;
1552fe8fb19SBen Gras LEGAL_ALT(0);
1562fe8fb19SBen Gras continue;
1572fe8fb19SBen Gras
1582fe8fb19SBen Gras /*
1592fe8fb19SBen Gras * "Alternative" modifiers. Just set the appropriate flag
1602fe8fb19SBen Gras * and start over again.
1612fe8fb19SBen Gras */
1622fe8fb19SBen Gras case 'E': /* "%E?" alternative conversion modifier. */
1632fe8fb19SBen Gras LEGAL_ALT(0);
1642fe8fb19SBen Gras alt_format |= ALT_E;
1652fe8fb19SBen Gras goto again;
1662fe8fb19SBen Gras
1672fe8fb19SBen Gras case 'O': /* "%O?" alternative conversion modifier. */
1682fe8fb19SBen Gras LEGAL_ALT(0);
1692fe8fb19SBen Gras alt_format |= ALT_O;
1702fe8fb19SBen Gras goto again;
1712fe8fb19SBen Gras
1722fe8fb19SBen Gras /*
1732fe8fb19SBen Gras * "Complex" conversion rules, implemented through recursion.
1742fe8fb19SBen Gras */
1752fe8fb19SBen Gras case 'c': /* Date and time, using the locale's format. */
17684d9c625SLionel Sambuc new_fmt = _TIME_LOCALE(loc)->d_t_fmt;
177*0a6a1f1dSLionel Sambuc state |= S_WDAY | S_MON | S_MDAY | S_YEAR;
1782fe8fb19SBen Gras goto recurse;
1792fe8fb19SBen Gras
1802fe8fb19SBen Gras case 'D': /* The date as "%m/%d/%y". */
1812fe8fb19SBen Gras new_fmt = "%m/%d/%y";
1822fe8fb19SBen Gras LEGAL_ALT(0);
183*0a6a1f1dSLionel Sambuc state |= S_MON | S_MDAY | S_YEAR;
1842fe8fb19SBen Gras goto recurse;
1852fe8fb19SBen Gras
1862fe8fb19SBen Gras case 'F': /* The date as "%Y-%m-%d". */
1872fe8fb19SBen Gras new_fmt = "%Y-%m-%d";
1882fe8fb19SBen Gras LEGAL_ALT(0);
189*0a6a1f1dSLionel Sambuc state |= S_MON | S_MDAY | S_YEAR;
1902fe8fb19SBen Gras goto recurse;
1912fe8fb19SBen Gras
1922fe8fb19SBen Gras case 'R': /* The time as "%H:%M". */
1932fe8fb19SBen Gras new_fmt = "%H:%M";
1942fe8fb19SBen Gras LEGAL_ALT(0);
1952fe8fb19SBen Gras goto recurse;
1962fe8fb19SBen Gras
1972fe8fb19SBen Gras case 'r': /* The time in 12-hour clock representation. */
19884d9c625SLionel Sambuc new_fmt = _TIME_LOCALE(loc)->t_fmt_ampm;
1992fe8fb19SBen Gras LEGAL_ALT(0);
2002fe8fb19SBen Gras goto recurse;
2012fe8fb19SBen Gras
2022fe8fb19SBen Gras case 'T': /* The time as "%H:%M:%S". */
2032fe8fb19SBen Gras new_fmt = "%H:%M:%S";
2042fe8fb19SBen Gras LEGAL_ALT(0);
2052fe8fb19SBen Gras goto recurse;
2062fe8fb19SBen Gras
2072fe8fb19SBen Gras case 'X': /* The time, using the locale's format. */
20884d9c625SLionel Sambuc new_fmt = _TIME_LOCALE(loc)->t_fmt;
2092fe8fb19SBen Gras goto recurse;
2102fe8fb19SBen Gras
2112fe8fb19SBen Gras case 'x': /* The date, using the locale's format. */
21284d9c625SLionel Sambuc new_fmt = _TIME_LOCALE(loc)->d_fmt;
213*0a6a1f1dSLionel Sambuc state |= S_MON | S_MDAY | S_YEAR;
2142fe8fb19SBen Gras recurse:
2152fe8fb19SBen Gras bp = (const u_char *)strptime((const char *)bp,
2162fe8fb19SBen Gras new_fmt, tm);
2172fe8fb19SBen Gras LEGAL_ALT(ALT_E);
2182fe8fb19SBen Gras continue;
2192fe8fb19SBen Gras
2202fe8fb19SBen Gras /*
2212fe8fb19SBen Gras * "Elementary" conversion rules.
2222fe8fb19SBen Gras */
2232fe8fb19SBen Gras case 'A': /* The day of week, using the locale's form. */
2242fe8fb19SBen Gras case 'a':
22584d9c625SLionel Sambuc bp = find_string(bp, &tm->tm_wday,
22684d9c625SLionel Sambuc _TIME_LOCALE(loc)->day, _TIME_LOCALE(loc)->abday, 7);
2272fe8fb19SBen Gras LEGAL_ALT(0);
228*0a6a1f1dSLionel Sambuc state |= S_WDAY;
2292fe8fb19SBen Gras continue;
2302fe8fb19SBen Gras
2312fe8fb19SBen Gras case 'B': /* The month, using the locale's form. */
2322fe8fb19SBen Gras case 'b':
2332fe8fb19SBen Gras case 'h':
23484d9c625SLionel Sambuc bp = find_string(bp, &tm->tm_mon,
23584d9c625SLionel Sambuc _TIME_LOCALE(loc)->mon, _TIME_LOCALE(loc)->abmon,
23684d9c625SLionel Sambuc 12);
2372fe8fb19SBen Gras LEGAL_ALT(0);
238*0a6a1f1dSLionel Sambuc state |= S_MON;
2392fe8fb19SBen Gras continue;
2402fe8fb19SBen Gras
2412fe8fb19SBen Gras case 'C': /* The century number. */
2422fe8fb19SBen Gras i = 20;
2432fe8fb19SBen Gras bp = conv_num(bp, &i, 0, 99);
2442fe8fb19SBen Gras
2452fe8fb19SBen Gras i = i * 100 - TM_YEAR_BASE;
2462fe8fb19SBen Gras if (split_year)
2472fe8fb19SBen Gras i += tm->tm_year % 100;
2482fe8fb19SBen Gras split_year = 1;
2492fe8fb19SBen Gras tm->tm_year = i;
2502fe8fb19SBen Gras LEGAL_ALT(ALT_E);
251*0a6a1f1dSLionel Sambuc state |= S_YEAR;
2522fe8fb19SBen Gras continue;
2532fe8fb19SBen Gras
2542fe8fb19SBen Gras case 'd': /* The day of month. */
2552fe8fb19SBen Gras case 'e':
2562fe8fb19SBen Gras bp = conv_num(bp, &tm->tm_mday, 1, 31);
2572fe8fb19SBen Gras LEGAL_ALT(ALT_O);
258*0a6a1f1dSLionel Sambuc state |= S_MDAY;
2592fe8fb19SBen Gras continue;
2602fe8fb19SBen Gras
2612fe8fb19SBen Gras case 'k': /* The hour (24-hour clock representation). */
2622fe8fb19SBen Gras LEGAL_ALT(0);
2632fe8fb19SBen Gras /* FALLTHROUGH */
2642fe8fb19SBen Gras case 'H':
2652fe8fb19SBen Gras bp = conv_num(bp, &tm->tm_hour, 0, 23);
2662fe8fb19SBen Gras LEGAL_ALT(ALT_O);
267*0a6a1f1dSLionel Sambuc state |= S_HOUR;
2682fe8fb19SBen Gras continue;
2692fe8fb19SBen Gras
2702fe8fb19SBen Gras case 'l': /* The hour (12-hour clock representation). */
2712fe8fb19SBen Gras LEGAL_ALT(0);
2722fe8fb19SBen Gras /* FALLTHROUGH */
2732fe8fb19SBen Gras case 'I':
2742fe8fb19SBen Gras bp = conv_num(bp, &tm->tm_hour, 1, 12);
2752fe8fb19SBen Gras if (tm->tm_hour == 12)
2762fe8fb19SBen Gras tm->tm_hour = 0;
2772fe8fb19SBen Gras LEGAL_ALT(ALT_O);
278*0a6a1f1dSLionel Sambuc state |= S_HOUR;
2792fe8fb19SBen Gras continue;
2802fe8fb19SBen Gras
2812fe8fb19SBen Gras case 'j': /* The day of year. */
2822fe8fb19SBen Gras i = 1;
2832fe8fb19SBen Gras bp = conv_num(bp, &i, 1, 366);
2842fe8fb19SBen Gras tm->tm_yday = i - 1;
2852fe8fb19SBen Gras LEGAL_ALT(0);
286*0a6a1f1dSLionel Sambuc state |= S_YDAY;
2872fe8fb19SBen Gras continue;
2882fe8fb19SBen Gras
2892fe8fb19SBen Gras case 'M': /* The minute. */
2902fe8fb19SBen Gras bp = conv_num(bp, &tm->tm_min, 0, 59);
2912fe8fb19SBen Gras LEGAL_ALT(ALT_O);
2922fe8fb19SBen Gras continue;
2932fe8fb19SBen Gras
2942fe8fb19SBen Gras case 'm': /* The month. */
2952fe8fb19SBen Gras i = 1;
2962fe8fb19SBen Gras bp = conv_num(bp, &i, 1, 12);
2972fe8fb19SBen Gras tm->tm_mon = i - 1;
2982fe8fb19SBen Gras LEGAL_ALT(ALT_O);
299*0a6a1f1dSLionel Sambuc state |= S_MON;
3002fe8fb19SBen Gras continue;
3012fe8fb19SBen Gras
3022fe8fb19SBen Gras case 'p': /* The locale's equivalent of AM/PM. */
30384d9c625SLionel Sambuc bp = find_string(bp, &i, _TIME_LOCALE(loc)->am_pm,
30484d9c625SLionel Sambuc NULL, 2);
305*0a6a1f1dSLionel Sambuc if (HAVE_HOUR(state) && tm->tm_hour > 11)
3062fe8fb19SBen Gras return NULL;
3072fe8fb19SBen Gras tm->tm_hour += i * 12;
3082fe8fb19SBen Gras LEGAL_ALT(0);
3092fe8fb19SBen Gras continue;
3102fe8fb19SBen Gras
3112fe8fb19SBen Gras case 'S': /* The seconds. */
3122fe8fb19SBen Gras bp = conv_num(bp, &tm->tm_sec, 0, 61);
3132fe8fb19SBen Gras LEGAL_ALT(ALT_O);
3142fe8fb19SBen Gras continue;
3152fe8fb19SBen Gras
3162fe8fb19SBen Gras #ifndef TIME_MAX
3172fe8fb19SBen Gras #define TIME_MAX INT64_MAX
3182fe8fb19SBen Gras #endif
3192fe8fb19SBen Gras case 's': /* seconds since the epoch */
3202fe8fb19SBen Gras {
3212fe8fb19SBen Gras time_t sse = 0;
3222fe8fb19SBen Gras uint64_t rulim = TIME_MAX;
3232fe8fb19SBen Gras
3242fe8fb19SBen Gras if (*bp < '0' || *bp > '9') {
3252fe8fb19SBen Gras bp = NULL;
3262fe8fb19SBen Gras continue;
3272fe8fb19SBen Gras }
3282fe8fb19SBen Gras
3292fe8fb19SBen Gras do {
3302fe8fb19SBen Gras sse *= 10;
3312fe8fb19SBen Gras sse += *bp++ - '0';
3322fe8fb19SBen Gras rulim /= 10;
3332fe8fb19SBen Gras } while ((sse * 10 <= TIME_MAX) &&
3342fe8fb19SBen Gras rulim && *bp >= '0' && *bp <= '9');
3352fe8fb19SBen Gras
3362fe8fb19SBen Gras if (sse < 0 || (uint64_t)sse > TIME_MAX) {
3372fe8fb19SBen Gras bp = NULL;
3382fe8fb19SBen Gras continue;
3392fe8fb19SBen Gras }
3402fe8fb19SBen Gras
3412fe8fb19SBen Gras if (localtime_r(&sse, tm) == NULL)
3422fe8fb19SBen Gras bp = NULL;
343*0a6a1f1dSLionel Sambuc else
344*0a6a1f1dSLionel Sambuc state |= S_YDAY | S_WDAY |
345*0a6a1f1dSLionel Sambuc S_MON | S_MDAY | S_YEAR;
3462fe8fb19SBen Gras }
3472fe8fb19SBen Gras continue;
3482fe8fb19SBen Gras
3492fe8fb19SBen Gras case 'U': /* The week of year, beginning on sunday. */
3502fe8fb19SBen Gras case 'W': /* The week of year, beginning on monday. */
3512fe8fb19SBen Gras /*
3522fe8fb19SBen Gras * XXX This is bogus, as we can not assume any valid
3532fe8fb19SBen Gras * information present in the tm structure at this
3542fe8fb19SBen Gras * point to calculate a real value, so just check the
3552fe8fb19SBen Gras * range for now.
3562fe8fb19SBen Gras */
3572fe8fb19SBen Gras bp = conv_num(bp, &i, 0, 53);
3582fe8fb19SBen Gras LEGAL_ALT(ALT_O);
359*0a6a1f1dSLionel Sambuc if (c == 'U')
360*0a6a1f1dSLionel Sambuc day_offset = TM_SUNDAY;
361*0a6a1f1dSLionel Sambuc else
362*0a6a1f1dSLionel Sambuc day_offset = TM_MONDAY;
363*0a6a1f1dSLionel Sambuc week_offset = i;
3642fe8fb19SBen Gras continue;
3652fe8fb19SBen Gras
3662fe8fb19SBen Gras case 'w': /* The day of week, beginning on sunday. */
3672fe8fb19SBen Gras bp = conv_num(bp, &tm->tm_wday, 0, 6);
3682fe8fb19SBen Gras LEGAL_ALT(ALT_O);
369*0a6a1f1dSLionel Sambuc state |= S_WDAY;
3702fe8fb19SBen Gras continue;
3712fe8fb19SBen Gras
3722fe8fb19SBen Gras case 'u': /* The day of week, monday = 1. */
3732fe8fb19SBen Gras bp = conv_num(bp, &i, 1, 7);
3742fe8fb19SBen Gras tm->tm_wday = i % 7;
3752fe8fb19SBen Gras LEGAL_ALT(ALT_O);
376*0a6a1f1dSLionel Sambuc state |= S_WDAY;
3772fe8fb19SBen Gras continue;
3782fe8fb19SBen Gras
3792fe8fb19SBen Gras case 'g': /* The year corresponding to the ISO week
3802fe8fb19SBen Gras * number but without the century.
3812fe8fb19SBen Gras */
3822fe8fb19SBen Gras bp = conv_num(bp, &i, 0, 99);
3832fe8fb19SBen Gras continue;
3842fe8fb19SBen Gras
3852fe8fb19SBen Gras case 'G': /* The year corresponding to the ISO week
3862fe8fb19SBen Gras * number with century.
3872fe8fb19SBen Gras */
3882fe8fb19SBen Gras do
3892fe8fb19SBen Gras bp++;
3902fe8fb19SBen Gras while (isdigit(*bp));
3912fe8fb19SBen Gras continue;
3922fe8fb19SBen Gras
3932fe8fb19SBen Gras case 'V': /* The ISO 8601:1988 week number as decimal */
3942fe8fb19SBen Gras bp = conv_num(bp, &i, 0, 53);
3952fe8fb19SBen Gras continue;
3962fe8fb19SBen Gras
3972fe8fb19SBen Gras case 'Y': /* The year. */
3982fe8fb19SBen Gras i = TM_YEAR_BASE; /* just for data sanity... */
3992fe8fb19SBen Gras bp = conv_num(bp, &i, 0, 9999);
4002fe8fb19SBen Gras tm->tm_year = i - TM_YEAR_BASE;
4012fe8fb19SBen Gras LEGAL_ALT(ALT_E);
402*0a6a1f1dSLionel Sambuc state |= S_YEAR;
4032fe8fb19SBen Gras continue;
4042fe8fb19SBen Gras
4052fe8fb19SBen Gras case 'y': /* The year within 100 years of the epoch. */
4062fe8fb19SBen Gras /* LEGAL_ALT(ALT_E | ALT_O); */
4072fe8fb19SBen Gras bp = conv_num(bp, &i, 0, 99);
4082fe8fb19SBen Gras
4092fe8fb19SBen Gras if (split_year)
4102fe8fb19SBen Gras /* preserve century */
4112fe8fb19SBen Gras i += (tm->tm_year / 100) * 100;
4122fe8fb19SBen Gras else {
4132fe8fb19SBen Gras split_year = 1;
4142fe8fb19SBen Gras if (i <= 68)
4152fe8fb19SBen Gras i = i + 2000 - TM_YEAR_BASE;
4162fe8fb19SBen Gras else
4172fe8fb19SBen Gras i = i + 1900 - TM_YEAR_BASE;
4182fe8fb19SBen Gras }
4192fe8fb19SBen Gras tm->tm_year = i;
420*0a6a1f1dSLionel Sambuc state |= S_YEAR;
4212fe8fb19SBen Gras continue;
4222fe8fb19SBen Gras
4232fe8fb19SBen Gras case 'Z':
4242fe8fb19SBen Gras tzset();
425*0a6a1f1dSLionel Sambuc if (strncmp((const char *)bp, gmt, 3) == 0 ||
426*0a6a1f1dSLionel Sambuc strncmp((const char *)bp, utc, 3) == 0) {
4272fe8fb19SBen Gras tm->tm_isdst = 0;
4282fe8fb19SBen Gras #ifdef TM_GMTOFF
4292fe8fb19SBen Gras tm->TM_GMTOFF = 0;
4302fe8fb19SBen Gras #endif
4312fe8fb19SBen Gras #ifdef TM_ZONE
4322fe8fb19SBen Gras tm->TM_ZONE = gmt;
4332fe8fb19SBen Gras #endif
4342fe8fb19SBen Gras bp += 3;
4352fe8fb19SBen Gras } else {
4362fe8fb19SBen Gras ep = find_string(bp, &i,
4372fe8fb19SBen Gras (const char * const *)tzname,
4382fe8fb19SBen Gras NULL, 2);
4392fe8fb19SBen Gras if (ep != NULL) {
4402fe8fb19SBen Gras tm->tm_isdst = i;
4412fe8fb19SBen Gras #ifdef TM_GMTOFF
4422fe8fb19SBen Gras tm->TM_GMTOFF = -(timezone);
4432fe8fb19SBen Gras #endif
4442fe8fb19SBen Gras #ifdef TM_ZONE
4452fe8fb19SBen Gras tm->TM_ZONE = tzname[i];
4462fe8fb19SBen Gras #endif
4472fe8fb19SBen Gras }
4482fe8fb19SBen Gras bp = ep;
4492fe8fb19SBen Gras }
4502fe8fb19SBen Gras continue;
4512fe8fb19SBen Gras
4522fe8fb19SBen Gras case 'z':
4532fe8fb19SBen Gras /*
4542fe8fb19SBen Gras * We recognize all ISO 8601 formats:
4552fe8fb19SBen Gras * Z = Zulu time/UTC
4562fe8fb19SBen Gras * [+-]hhmm
4572fe8fb19SBen Gras * [+-]hh:mm
4582fe8fb19SBen Gras * [+-]hh
4592fe8fb19SBen Gras * We recognize all RFC-822/RFC-2822 formats:
4602fe8fb19SBen Gras * UT|GMT
4612fe8fb19SBen Gras * North American : UTC offsets
4622fe8fb19SBen Gras * E[DS]T = Eastern : -4 | -5
4632fe8fb19SBen Gras * C[DS]T = Central : -5 | -6
4642fe8fb19SBen Gras * M[DS]T = Mountain: -6 | -7
4652fe8fb19SBen Gras * P[DS]T = Pacific : -7 | -8
4662fe8fb19SBen Gras * Military
4672fe8fb19SBen Gras * [A-IL-M] = -1 ... -9 (J not used)
4682fe8fb19SBen Gras * [N-Y] = +1 ... +12
4692fe8fb19SBen Gras */
4702fe8fb19SBen Gras while (isspace(*bp))
4712fe8fb19SBen Gras bp++;
4722fe8fb19SBen Gras
4732fe8fb19SBen Gras switch (*bp++) {
4742fe8fb19SBen Gras case 'G':
4752fe8fb19SBen Gras if (*bp++ != 'M')
4762fe8fb19SBen Gras return NULL;
4772fe8fb19SBen Gras /*FALLTHROUGH*/
4782fe8fb19SBen Gras case 'U':
4792fe8fb19SBen Gras if (*bp++ != 'T')
4802fe8fb19SBen Gras return NULL;
4812fe8fb19SBen Gras /*FALLTHROUGH*/
4822fe8fb19SBen Gras case 'Z':
4832fe8fb19SBen Gras tm->tm_isdst = 0;
4842fe8fb19SBen Gras #ifdef TM_GMTOFF
4852fe8fb19SBen Gras tm->TM_GMTOFF = 0;
4862fe8fb19SBen Gras #endif
4872fe8fb19SBen Gras #ifdef TM_ZONE
4882fe8fb19SBen Gras tm->TM_ZONE = utc;
4892fe8fb19SBen Gras #endif
4902fe8fb19SBen Gras continue;
4912fe8fb19SBen Gras case '+':
4922fe8fb19SBen Gras neg = 0;
4932fe8fb19SBen Gras break;
4942fe8fb19SBen Gras case '-':
4952fe8fb19SBen Gras neg = 1;
4962fe8fb19SBen Gras break;
4972fe8fb19SBen Gras default:
4982fe8fb19SBen Gras --bp;
4992fe8fb19SBen Gras ep = find_string(bp, &i, nast, NULL, 4);
5002fe8fb19SBen Gras if (ep != NULL) {
5012fe8fb19SBen Gras #ifdef TM_GMTOFF
5022fe8fb19SBen Gras tm->TM_GMTOFF = -5 - i;
5032fe8fb19SBen Gras #endif
5042fe8fb19SBen Gras #ifdef TM_ZONE
5052fe8fb19SBen Gras tm->TM_ZONE = __UNCONST(nast[i]);
5062fe8fb19SBen Gras #endif
5072fe8fb19SBen Gras bp = ep;
5082fe8fb19SBen Gras continue;
5092fe8fb19SBen Gras }
5102fe8fb19SBen Gras ep = find_string(bp, &i, nadt, NULL, 4);
5112fe8fb19SBen Gras if (ep != NULL) {
5122fe8fb19SBen Gras tm->tm_isdst = 1;
5132fe8fb19SBen Gras #ifdef TM_GMTOFF
5142fe8fb19SBen Gras tm->TM_GMTOFF = -4 - i;
5152fe8fb19SBen Gras #endif
5162fe8fb19SBen Gras #ifdef TM_ZONE
5172fe8fb19SBen Gras tm->TM_ZONE = __UNCONST(nadt[i]);
5182fe8fb19SBen Gras #endif
5192fe8fb19SBen Gras bp = ep;
5202fe8fb19SBen Gras continue;
5212fe8fb19SBen Gras }
5222fe8fb19SBen Gras
5232fe8fb19SBen Gras if ((*bp >= 'A' && *bp <= 'I') ||
5242fe8fb19SBen Gras (*bp >= 'L' && *bp <= 'Y')) {
5252fe8fb19SBen Gras #ifdef TM_GMTOFF
5262fe8fb19SBen Gras /* Argh! No 'J'! */
5272fe8fb19SBen Gras if (*bp >= 'A' && *bp <= 'I')
5282fe8fb19SBen Gras tm->TM_GMTOFF =
5292fe8fb19SBen Gras ('A' - 1) - (int)*bp;
5302fe8fb19SBen Gras else if (*bp >= 'L' && *bp <= 'M')
5312fe8fb19SBen Gras tm->TM_GMTOFF = 'A' - (int)*bp;
5322fe8fb19SBen Gras else if (*bp >= 'N' && *bp <= 'Y')
5332fe8fb19SBen Gras tm->TM_GMTOFF = (int)*bp - 'M';
5342fe8fb19SBen Gras #endif
5352fe8fb19SBen Gras #ifdef TM_ZONE
536*0a6a1f1dSLionel Sambuc tm->TM_ZONE = utc; /* XXX */
5372fe8fb19SBen Gras #endif
5382fe8fb19SBen Gras bp++;
5392fe8fb19SBen Gras continue;
5402fe8fb19SBen Gras }
5412fe8fb19SBen Gras return NULL;
5422fe8fb19SBen Gras }
5432fe8fb19SBen Gras offs = 0;
5442fe8fb19SBen Gras for (i = 0; i < 4; ) {
5452fe8fb19SBen Gras if (isdigit(*bp)) {
5462fe8fb19SBen Gras offs = offs * 10 + (*bp++ - '0');
5472fe8fb19SBen Gras i++;
5482fe8fb19SBen Gras continue;
5492fe8fb19SBen Gras }
5502fe8fb19SBen Gras if (i == 2 && *bp == ':') {
5512fe8fb19SBen Gras bp++;
5522fe8fb19SBen Gras continue;
5532fe8fb19SBen Gras }
5542fe8fb19SBen Gras break;
5552fe8fb19SBen Gras }
5562fe8fb19SBen Gras switch (i) {
5572fe8fb19SBen Gras case 2:
5582fe8fb19SBen Gras offs *= 100;
5592fe8fb19SBen Gras break;
5602fe8fb19SBen Gras case 4:
5612fe8fb19SBen Gras i = offs % 100;
5622fe8fb19SBen Gras if (i >= 60)
5632fe8fb19SBen Gras return NULL;
5642fe8fb19SBen Gras /* Convert minutes into decimal */
5652fe8fb19SBen Gras offs = (offs / 100) * 100 + (i * 50) / 30;
5662fe8fb19SBen Gras break;
5672fe8fb19SBen Gras default:
5682fe8fb19SBen Gras return NULL;
5692fe8fb19SBen Gras }
5702fe8fb19SBen Gras if (neg)
5712fe8fb19SBen Gras offs = -offs;
5722fe8fb19SBen Gras tm->tm_isdst = 0; /* XXX */
5732fe8fb19SBen Gras #ifdef TM_GMTOFF
5742fe8fb19SBen Gras tm->TM_GMTOFF = offs;
5752fe8fb19SBen Gras #endif
5762fe8fb19SBen Gras #ifdef TM_ZONE
577*0a6a1f1dSLionel Sambuc tm->TM_ZONE = utc; /* XXX */
5782fe8fb19SBen Gras #endif
5792fe8fb19SBen Gras continue;
5802fe8fb19SBen Gras
5812fe8fb19SBen Gras /*
5822fe8fb19SBen Gras * Miscellaneous conversions.
5832fe8fb19SBen Gras */
5842fe8fb19SBen Gras case 'n': /* Any kind of white-space. */
5852fe8fb19SBen Gras case 't':
5862fe8fb19SBen Gras while (isspace(*bp))
5872fe8fb19SBen Gras bp++;
5882fe8fb19SBen Gras LEGAL_ALT(0);
5892fe8fb19SBen Gras continue;
5902fe8fb19SBen Gras
5912fe8fb19SBen Gras
5922fe8fb19SBen Gras default: /* Unknown/unsupported conversion. */
5932fe8fb19SBen Gras return NULL;
5942fe8fb19SBen Gras }
5952fe8fb19SBen Gras }
5962fe8fb19SBen Gras
597*0a6a1f1dSLionel Sambuc if (!HAVE_YDAY(state) && HAVE_YEAR(state)) {
598*0a6a1f1dSLionel Sambuc if (HAVE_MON(state) && HAVE_MDAY(state)) {
599*0a6a1f1dSLionel Sambuc /* calculate day of year (ordinal date) */
600*0a6a1f1dSLionel Sambuc tm->tm_yday = start_of_month[isleap_sum(tm->tm_year,
601*0a6a1f1dSLionel Sambuc TM_YEAR_BASE)][tm->tm_mon] + (tm->tm_mday - 1);
602*0a6a1f1dSLionel Sambuc state |= S_YDAY;
603*0a6a1f1dSLionel Sambuc } else if (day_offset != -1) {
604*0a6a1f1dSLionel Sambuc /*
605*0a6a1f1dSLionel Sambuc * Set the date to the first Sunday (or Monday)
606*0a6a1f1dSLionel Sambuc * of the specified week of the year.
607*0a6a1f1dSLionel Sambuc */
608*0a6a1f1dSLionel Sambuc if (!HAVE_WDAY(state)) {
609*0a6a1f1dSLionel Sambuc tm->tm_wday = day_offset;
610*0a6a1f1dSLionel Sambuc state |= S_WDAY;
611*0a6a1f1dSLionel Sambuc }
612*0a6a1f1dSLionel Sambuc tm->tm_yday = (7 -
613*0a6a1f1dSLionel Sambuc first_wday_of(tm->tm_year + TM_YEAR_BASE) +
614*0a6a1f1dSLionel Sambuc day_offset) % 7 + (week_offset - 1) * 7 +
615*0a6a1f1dSLionel Sambuc tm->tm_wday - day_offset;
616*0a6a1f1dSLionel Sambuc state |= S_YDAY;
617*0a6a1f1dSLionel Sambuc }
618*0a6a1f1dSLionel Sambuc }
619*0a6a1f1dSLionel Sambuc
620*0a6a1f1dSLionel Sambuc if (HAVE_YDAY(state) && HAVE_YEAR(state)) {
621*0a6a1f1dSLionel Sambuc int isleap;
622*0a6a1f1dSLionel Sambuc
623*0a6a1f1dSLionel Sambuc if (!HAVE_MON(state)) {
624*0a6a1f1dSLionel Sambuc /* calculate month of day of year */
625*0a6a1f1dSLionel Sambuc i = 0;
626*0a6a1f1dSLionel Sambuc isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
627*0a6a1f1dSLionel Sambuc while (tm->tm_yday >= start_of_month[isleap][i])
628*0a6a1f1dSLionel Sambuc i++;
629*0a6a1f1dSLionel Sambuc if (i > 12) {
630*0a6a1f1dSLionel Sambuc i = 1;
631*0a6a1f1dSLionel Sambuc tm->tm_yday -= start_of_month[isleap][12];
632*0a6a1f1dSLionel Sambuc tm->tm_year++;
633*0a6a1f1dSLionel Sambuc }
634*0a6a1f1dSLionel Sambuc tm->tm_mon = i - 1;
635*0a6a1f1dSLionel Sambuc state |= S_MON;
636*0a6a1f1dSLionel Sambuc }
637*0a6a1f1dSLionel Sambuc
638*0a6a1f1dSLionel Sambuc if (!HAVE_MDAY(state)) {
639*0a6a1f1dSLionel Sambuc /* calculate day of month */
640*0a6a1f1dSLionel Sambuc isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
641*0a6a1f1dSLionel Sambuc tm->tm_mday = tm->tm_yday -
642*0a6a1f1dSLionel Sambuc start_of_month[isleap][tm->tm_mon] + 1;
643*0a6a1f1dSLionel Sambuc state |= S_MDAY;
644*0a6a1f1dSLionel Sambuc }
645*0a6a1f1dSLionel Sambuc
646*0a6a1f1dSLionel Sambuc if (!HAVE_WDAY(state)) {
647*0a6a1f1dSLionel Sambuc /* calculate day of week */
648*0a6a1f1dSLionel Sambuc i = 0;
649*0a6a1f1dSLionel Sambuc week_offset = first_wday_of(tm->tm_year);
650*0a6a1f1dSLionel Sambuc while (i++ <= tm->tm_yday) {
651*0a6a1f1dSLionel Sambuc if (week_offset++ >= 6)
652*0a6a1f1dSLionel Sambuc week_offset = 0;
653*0a6a1f1dSLionel Sambuc }
654*0a6a1f1dSLionel Sambuc tm->tm_wday = week_offset;
655*0a6a1f1dSLionel Sambuc state |= S_WDAY;
656*0a6a1f1dSLionel Sambuc }
657*0a6a1f1dSLionel Sambuc }
658*0a6a1f1dSLionel Sambuc
6592fe8fb19SBen Gras return __UNCONST(bp);
6602fe8fb19SBen Gras }
6612fe8fb19SBen Gras
6622fe8fb19SBen Gras
6632fe8fb19SBen Gras static const u_char *
conv_num(const unsigned char * buf,int * dest,uint llim,uint ulim)6642fe8fb19SBen Gras conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim)
6652fe8fb19SBen Gras {
6662fe8fb19SBen Gras uint result = 0;
6672fe8fb19SBen Gras unsigned char ch;
6682fe8fb19SBen Gras
6692fe8fb19SBen Gras /* The limit also determines the number of valid digits. */
6702fe8fb19SBen Gras uint rulim = ulim;
6712fe8fb19SBen Gras
6722fe8fb19SBen Gras ch = *buf;
6732fe8fb19SBen Gras if (ch < '0' || ch > '9')
6742fe8fb19SBen Gras return NULL;
6752fe8fb19SBen Gras
6762fe8fb19SBen Gras do {
6772fe8fb19SBen Gras result *= 10;
6782fe8fb19SBen Gras result += ch - '0';
6792fe8fb19SBen Gras rulim /= 10;
6802fe8fb19SBen Gras ch = *++buf;
6812fe8fb19SBen Gras } while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
6822fe8fb19SBen Gras
6832fe8fb19SBen Gras if (result < llim || result > ulim)
6842fe8fb19SBen Gras return NULL;
6852fe8fb19SBen Gras
6862fe8fb19SBen Gras *dest = result;
6872fe8fb19SBen Gras return buf;
6882fe8fb19SBen Gras }
6892fe8fb19SBen Gras
6902fe8fb19SBen Gras static const u_char *
find_string(const u_char * bp,int * tgt,const char * const * n1,const char * const * n2,int c)6912fe8fb19SBen Gras find_string(const u_char *bp, int *tgt, const char * const *n1,
6922fe8fb19SBen Gras const char * const *n2, int c)
6932fe8fb19SBen Gras {
6942fe8fb19SBen Gras int i;
695f14fb602SLionel Sambuc size_t len;
6962fe8fb19SBen Gras
6972fe8fb19SBen Gras /* check full name - then abbreviated ones */
6982fe8fb19SBen Gras for (; n1 != NULL; n1 = n2, n2 = NULL) {
6992fe8fb19SBen Gras for (i = 0; i < c; i++, n1++) {
7002fe8fb19SBen Gras len = strlen(*n1);
7012fe8fb19SBen Gras if (strncasecmp(*n1, (const char *)bp, len) == 0) {
7022fe8fb19SBen Gras *tgt = i;
7032fe8fb19SBen Gras return bp + len;
7042fe8fb19SBen Gras }
7052fe8fb19SBen Gras }
7062fe8fb19SBen Gras }
7072fe8fb19SBen Gras
7082fe8fb19SBen Gras /* Nothing matched */
7092fe8fb19SBen Gras return NULL;
7102fe8fb19SBen Gras }
711