2 * Copyright (c) 2014 Gary Mills
3 * Copyright 2011, Nexenta Systems, Inc. All rights reserved.
4 * Copyright (c) 1994 Powerdog Industries. All rights reserved.
6 * Copyright (c) 2011 The FreeBSD Foundation
8 * Portions of this software were developed by David Chisnall
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer
18 * in the documentation and/or other materials provided with the
21 * THIS SOFTWARE IS PROVIDED BY POWERDOG INDUSTRIES ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE POWERDOG INDUSTRIES BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * The views and conclusions contained in the software and documentation
34 * are those of the authors and should not be interpreted as representing
35 * official policies, either expressed or implied, of Powerdog Industries.
38 #include <sys/cdefs.h>
41 static char copyright[] __unused =
42 "@(#) Copyright (c) 1994 Powerdog Industries. All rights reserved.";
43 static char sccsid[] __unused = "@(#)strptime.c 0.1 (Powerdog) 94/03/27";
44 #endif /* !defined NOID */
46 __FBSDID("$FreeBSD$");
48 #include "namespace.h"
55 #include "un-namespace.h"
56 #include "libc_private.h"
57 #include "timelocal.h"
60 static char * _strptime(const char *, const char *, struct tm *, int *, locale_t);
62 #define asizeof(a) (sizeof(a) / sizeof((a)[0]))
64 #define FLAG_NONE (1 << 0)
65 #define FLAG_YEAR (1 << 1)
66 #define FLAG_MONTH (1 << 2)
67 #define FLAG_YDAY (1 << 3)
68 #define FLAG_MDAY (1 << 4)
69 #define FLAG_WDAY (1 << 5)
72 * Calculate the week day of the first day of a year. Valid for
73 * the Gregorian calendar, which began Sept 14, 1752 in the UK
74 * and its colonies. Ref:
75 * http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
79 first_wday_of(int year)
81 return (((2 * (3 - (year / 100) % 4)) + (year % 100) +
82 ((year % 100) / 4) + (isleap(year) ? 6 : 0) + 1) % 7);
86 _strptime(const char *buf, const char *fmt, struct tm *tm, int *GMTp,
91 int day_offset = -1, wday_offset;
95 int Ealternative, Oalternative;
96 const struct lc_time_T *tptr = __get_current_time_locale(locale);
97 static int start_of_month[2][13] = {
98 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
99 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
109 if (isspace_l((unsigned char)c, locale))
111 isspace_l((unsigned char)*buf, locale))
113 else if (c != *buf++)
129 buf = _strptime(buf, tptr->date_fmt, tm, GMTp, locale);
132 flags |= FLAG_WDAY | FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
136 if (!isdigit_l((unsigned char)*buf, locale))
139 /* XXX This will break for 3-digit centuries. */
141 for (i = 0; len && *buf != 0 &&
142 isdigit_l((unsigned char)*buf, locale); buf++) {
150 tm->tm_year = i * 100 - TM_YEAR_BASE;
156 buf = _strptime(buf, tptr->c_fmt, tm, GMTp, locale);
159 flags |= FLAG_WDAY | FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
163 buf = _strptime(buf, "%m/%d/%y", tm, GMTp, locale);
166 flags |= FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
170 if (Ealternative || Oalternative)
176 if (Ealternative || Oalternative)
182 buf = _strptime(buf, "%Y-%m-%d", tm, GMTp, locale);
185 flags |= FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
189 buf = _strptime(buf, "%H:%M", tm, GMTp, locale);
195 buf = _strptime(buf, tptr->ampm_fmt, tm, GMTp, locale);
201 buf = _strptime(buf, "%H:%M:%S", tm, GMTp, locale);
207 buf = _strptime(buf, tptr->X_fmt, tm, GMTp, locale);
213 buf = _strptime(buf, tptr->x_fmt, tm, GMTp, locale);
216 flags |= FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
220 if (!isdigit_l((unsigned char)*buf, locale))
224 for (i = 0; len && *buf != 0 &&
225 isdigit_l((unsigned char)*buf, locale); buf++){
230 if (i < 1 || i > 366)
241 isspace_l((unsigned char)*buf, locale))
244 if (!isdigit_l((unsigned char)*buf, locale))
248 for (i = 0; len && *buf != 0 &&
249 isdigit_l((unsigned char)*buf, locale); buf++){
272 * Of these, %l is the only specifier explicitly
273 * documented as not being zero-padded. However,
274 * there is no harm in allowing zero-padding.
276 * XXX The %l specifier may gobble one too many
277 * digits if used incorrectly.
279 if (!isdigit_l((unsigned char)*buf, locale))
283 for (i = 0; len && *buf != 0 &&
284 isdigit_l((unsigned char)*buf, locale); buf++) {
289 if (c == 'H' || c == 'k') {
301 * XXX This is bogus if parsed before hour-related
304 len = strlen(tptr->am);
305 if (strncasecmp_l(buf, tptr->am, len, locale) == 0) {
306 if (tm->tm_hour > 12)
308 if (tm->tm_hour == 12)
314 len = strlen(tptr->pm);
315 if (strncasecmp_l(buf, tptr->pm, len, locale) == 0) {
316 if (tm->tm_hour > 12)
318 if (tm->tm_hour != 12)
328 for (i = 0; i < asizeof(tptr->weekday); i++) {
329 len = strlen(tptr->weekday[i]);
330 if (strncasecmp_l(buf, tptr->weekday[i],
333 len = strlen(tptr->wday[i]);
334 if (strncasecmp_l(buf, tptr->wday[i],
338 if (i == asizeof(tptr->weekday))
349 * XXX This is bogus, as we can not assume any valid
350 * information present in the tm structure at this
351 * point to calculate a real value, so just check the
354 if (!isdigit_l((unsigned char)*buf, locale))
358 for (i = 0; len && *buf != 0 &&
359 isdigit_l((unsigned char)*buf, locale); buf++) {
368 day_offset = TM_SUNDAY;
370 day_offset = TM_MONDAY;
378 if (!isdigit_l((unsigned char)*buf, locale))
392 * With %e format, our strftime(3) adds a blank space
393 * before single digits.
396 isspace_l((unsigned char)*buf, locale))
401 * The %e specifier was once explicitly documented as
402 * not being zero-padded but was later changed to
403 * equivalent to %d. There is no harm in allowing
406 * XXX The %e specifier may gobble one too many
407 * digits if used incorrectly.
409 if (!isdigit_l((unsigned char)*buf, locale))
413 for (i = 0; len && *buf != 0 &&
414 isdigit_l((unsigned char)*buf, locale); buf++) {
430 for (i = 0; i < asizeof(tptr->month); i++) {
433 len = strlen(tptr->alt_month[i]);
434 if (strncasecmp_l(buf,
440 len = strlen(tptr->month[i]);
441 if (strncasecmp_l(buf, tptr->month[i],
447 * Try the abbreviated month name if the full name
448 * wasn't found and Oalternative was not requested.
450 if (i == asizeof(tptr->month) && !Oalternative) {
451 for (i = 0; i < asizeof(tptr->month); i++) {
452 len = strlen(tptr->mon[i]);
453 if (strncasecmp_l(buf, tptr->mon[i],
458 if (i == asizeof(tptr->month))
468 if (!isdigit_l((unsigned char)*buf, locale))
472 for (i = 0; len && *buf != 0 &&
473 isdigit_l((unsigned char)*buf, locale); buf++) {
495 n = strtol_l(buf, &cp, 10, locale);
496 if (errno == ERANGE || (long)(t = n) != n) {
502 if (gmtime_r(&t, tm) == NULL)
505 flags |= FLAG_YDAY | FLAG_WDAY | FLAG_MONTH |
506 FLAG_MDAY | FLAG_YEAR;
513 isspace_l((unsigned char)*buf, locale))
516 if (!isdigit_l((unsigned char)*buf, locale))
519 len = (c == 'Y') ? 4 : 2;
520 for (i = 0; len && *buf != 0 &&
521 isdigit_l((unsigned char)*buf, locale); buf++) {
528 if (c == 'y' && i < 69)
543 for (cp = buf; *cp &&
544 isupper_l((unsigned char)*cp, locale); ++cp) {
547 zonestr = alloca(cp - buf + 1);
548 strncpy(zonestr, buf, cp - buf);
549 zonestr[cp - buf] = '\0';
551 if (0 == strcmp(zonestr, "GMT") ||
552 0 == strcmp(zonestr, "UTC")) {
554 } else if (0 == strcmp(zonestr, tzname[0])) {
556 } else if (0 == strcmp(zonestr, tzname[1])) {
579 for (len = 4; len > 0; len--) {
580 if (isdigit_l((unsigned char)*buf, locale)) {
588 tm->tm_hour -= sign * (i / 100);
589 tm->tm_min -= sign * (i % 100);
596 while (isspace_l((unsigned char)*buf, locale))
605 if (!(flags & FLAG_YDAY) && (flags & FLAG_YEAR)) {
606 if ((flags & (FLAG_MONTH | FLAG_MDAY)) ==
607 (FLAG_MONTH | FLAG_MDAY)) {
608 tm->tm_yday = start_of_month[isleap(tm->tm_year +
609 TM_YEAR_BASE)][tm->tm_mon] + (tm->tm_mday - 1);
611 } else if (day_offset != -1) {
612 /* Set the date to the first Sunday (or Monday)
613 * of the specified week of the year.
615 if (!(flags & FLAG_WDAY)) {
616 tm->tm_wday = day_offset;
620 first_wday_of(tm->tm_year + TM_YEAR_BASE) +
621 day_offset) % 7 + (week_offset - 1) * 7 +
622 tm->tm_wday - day_offset;
627 if ((flags & (FLAG_YEAR | FLAG_YDAY)) == (FLAG_YEAR | FLAG_YDAY)) {
628 if (!(flags & FLAG_MONTH)) {
630 while (tm->tm_yday >=
631 start_of_month[isleap(tm->tm_year +
637 start_of_month[isleap(tm->tm_year +
644 if (!(flags & FLAG_MDAY)) {
645 tm->tm_mday = tm->tm_yday -
646 start_of_month[isleap(tm->tm_year + TM_YEAR_BASE)]
650 if (!(flags & FLAG_WDAY)) {
652 wday_offset = first_wday_of(tm->tm_year);
653 while (i++ <= tm->tm_yday) {
654 if (wday_offset++ >= 6)
657 tm->tm_wday = wday_offset;
662 return ((char *)buf);
666 strptime_l(const char * __restrict buf, const char * __restrict fmt,
667 struct tm * __restrict tm, locale_t loc)
674 ret = _strptime(buf, fmt, tm, &gmt, loc);
676 time_t t = timegm(tm);
685 strptime(const char * __restrict buf, const char * __restrict fmt,
686 struct tm * __restrict tm)
688 return strptime_l(buf, fmt, tm, __get_locale());