2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014 Gary Mills
5 * Copyright 2011, Nexenta Systems, Inc. All rights reserved.
6 * Copyright (c) 1994 Powerdog Industries. All rights reserved.
8 * Copyright (c) 2011 The FreeBSD Foundation
10 * Portions of this software were developed by David Chisnall
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer
20 * in the documentation and/or other materials provided with the
23 * THIS SOFTWARE IS PROVIDED BY POWERDOG INDUSTRIES ``AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE POWERDOG INDUSTRIES BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * The views and conclusions contained in the software and documentation
36 * are those of the authors and should not be interpreted as representing
37 * official policies, either expressed or implied, of Powerdog Industries.
40 #include <sys/cdefs.h>
43 static char copyright[] __unused =
44 "@(#) Copyright (c) 1994 Powerdog Industries. All rights reserved.";
45 static char sccsid[] __unused = "@(#)strptime.c 0.1 (Powerdog) 94/03/27";
46 #endif /* !defined NOID */
48 __FBSDID("$FreeBSD$");
50 #include "namespace.h"
57 #include "un-namespace.h"
58 #include "libc_private.h"
59 #include "timelocal.h"
62 static char * _strptime(const char *, const char *, struct tm *, int *, locale_t);
64 #define asizeof(a) (sizeof(a) / sizeof((a)[0]))
66 #define FLAG_NONE (1 << 0)
67 #define FLAG_YEAR (1 << 1)
68 #define FLAG_MONTH (1 << 2)
69 #define FLAG_YDAY (1 << 3)
70 #define FLAG_MDAY (1 << 4)
71 #define FLAG_WDAY (1 << 5)
74 * Calculate the week day of the first day of a year. Valid for
75 * the Gregorian calendar, which began Sept 14, 1752 in the UK
76 * and its colonies. Ref:
77 * http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
81 first_wday_of(int year)
83 return (((2 * (3 - (year / 100) % 4)) + (year % 100) +
84 ((year % 100) / 4) + (isleap(year) ? 6 : 0) + 1) % 7);
88 _strptime(const char *buf, const char *fmt, struct tm *tm, int *GMTp,
93 int day_offset = -1, wday_offset;
97 int Ealternative, Oalternative;
99 const struct lc_time_T *tptr = __get_current_time_locale(locale);
100 static int start_of_month[2][13] = {
101 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
102 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
114 if (isspace_l((unsigned char)c, locale))
116 isspace_l((unsigned char)*buf, locale))
118 else if (c != *buf++)
134 buf = _strptime(buf, tptr->date_fmt, tm, GMTp, locale);
137 flags |= FLAG_WDAY | FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
141 if (!isdigit_l((unsigned char)*buf, locale))
144 /* XXX This will break for 3-digit centuries. */
146 for (i = 0; len && *buf != 0 &&
147 isdigit_l((unsigned char)*buf, locale); buf++) {
159 buf = _strptime(buf, tptr->c_fmt, tm, GMTp, locale);
162 flags |= FLAG_WDAY | FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
166 buf = _strptime(buf, "%m/%d/%y", tm, GMTp, locale);
169 flags |= FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
173 if (Ealternative || Oalternative)
179 if (Ealternative || Oalternative)
185 buf = _strptime(buf, "%Y-%m-%d", tm, GMTp, locale);
188 flags |= FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
192 buf = _strptime(buf, "%H:%M", tm, GMTp, locale);
198 buf = _strptime(buf, tptr->ampm_fmt, tm, GMTp, locale);
204 buf = _strptime(buf, "%H:%M:%S", tm, GMTp, locale);
210 buf = _strptime(buf, tptr->X_fmt, tm, GMTp, locale);
216 buf = _strptime(buf, tptr->x_fmt, tm, GMTp, locale);
219 flags |= FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
223 if (!isdigit_l((unsigned char)*buf, locale))
227 for (i = 0; len && *buf != 0 &&
228 isdigit_l((unsigned char)*buf, locale); buf++){
233 if (i < 1 || i > 366)
244 isspace_l((unsigned char)*buf, locale))
247 if (!isdigit_l((unsigned char)*buf, locale))
251 for (i = 0; len && *buf != 0 &&
252 isdigit_l((unsigned char)*buf, locale); buf++){
275 * %k and %l specifiers are documented as being
276 * blank-padded. However, there is no harm in
277 * allowing zero-padding.
279 * XXX %k and %l specifiers may gobble one too many
280 * digits if used incorrectly.
284 if ((c == 'k' || c == 'l') &&
285 isblank_l((unsigned char)*buf, locale)) {
290 if (!isdigit_l((unsigned char)*buf, locale))
293 for (i = 0; len && *buf != 0 &&
294 isdigit_l((unsigned char)*buf, locale); buf++) {
299 if (c == 'H' || c == 'k') {
302 } else if (i == 0 || i > 12)
311 * XXX This is bogus if parsed before hour-related
314 if (tm->tm_hour > 12)
317 len = strlen(tptr->am);
318 if (strncasecmp_l(buf, tptr->am, len, locale) == 0) {
319 if (tm->tm_hour == 12)
325 len = strlen(tptr->pm);
326 if (strncasecmp_l(buf, tptr->pm, len, locale) == 0) {
327 if (tm->tm_hour != 12)
337 for (i = 0; i < asizeof(tptr->weekday); i++) {
338 len = strlen(tptr->weekday[i]);
339 if (strncasecmp_l(buf, tptr->weekday[i],
342 len = strlen(tptr->wday[i]);
343 if (strncasecmp_l(buf, tptr->wday[i],
347 if (i == asizeof(tptr->weekday))
358 * XXX This is bogus, as we can not assume any valid
359 * information present in the tm structure at this
360 * point to calculate a real value, so just check the
363 if (!isdigit_l((unsigned char)*buf, locale))
367 for (i = 0; len && *buf != 0 &&
368 isdigit_l((unsigned char)*buf, locale); buf++) {
377 day_offset = TM_SUNDAY;
379 day_offset = TM_MONDAY;
388 if (!isdigit_l((unsigned char)*buf, locale))
392 if (i < 0 || i > 7 || (c == 'u' && i < 1) ||
403 * With %e format, our strftime(3) adds a blank space
404 * before single digits.
407 isspace_l((unsigned char)*buf, locale))
412 * The %e specifier was once explicitly documented as
413 * not being zero-padded but was later changed to
414 * equivalent to %d. There is no harm in allowing
417 * XXX The %e specifier may gobble one too many
418 * digits if used incorrectly.
420 if (!isdigit_l((unsigned char)*buf, locale))
424 for (i = 0; len && *buf != 0 &&
425 isdigit_l((unsigned char)*buf, locale); buf++) {
430 if (i == 0 || i > 31)
441 for (i = 0; i < asizeof(tptr->month); i++) {
444 len = strlen(tptr->alt_month[i]);
445 if (strncasecmp_l(buf,
451 len = strlen(tptr->month[i]);
452 if (strncasecmp_l(buf, tptr->month[i],
458 * Try the abbreviated month name if the full name
459 * wasn't found and Oalternative was not requested.
461 if (i == asizeof(tptr->month) && !Oalternative) {
462 for (i = 0; i < asizeof(tptr->month); i++) {
463 len = strlen(tptr->mon[i]);
464 if (strncasecmp_l(buf, tptr->mon[i],
469 if (i == asizeof(tptr->month))
479 if (!isdigit_l((unsigned char)*buf, locale))
483 for (i = 0; len && *buf != 0 &&
484 isdigit_l((unsigned char)*buf, locale); buf++) {
506 n = strtol_l(buf, &cp, 10, locale);
507 if (errno == ERANGE || (long)(t = n) != n) {
513 if (gmtime_r(&t, tm) == NULL)
516 flags |= FLAG_YDAY | FLAG_WDAY | FLAG_MONTH |
517 FLAG_MDAY | FLAG_YEAR;
524 isspace_l((unsigned char)*buf, locale))
527 if (!isdigit_l((unsigned char)*buf, locale))
530 len = (c == 'Y') ? 4 : 2;
531 for (i = 0; len && *buf != 0 &&
532 isdigit_l((unsigned char)*buf, locale); buf++) {
550 for (cp = buf; *cp &&
551 isupper_l((unsigned char)*cp, locale); ++cp) {
554 zonestr = alloca(cp - buf + 1);
555 strncpy(zonestr, buf, cp - buf);
556 zonestr[cp - buf] = '\0';
558 if (0 == strcmp(zonestr, "GMT") ||
559 0 == strcmp(zonestr, "UTC")) {
561 } else if (0 == strcmp(zonestr, tzname[0])) {
563 } else if (0 == strcmp(zonestr, tzname[1])) {
586 for (len = 4; len > 0; len--) {
587 if (isdigit_l((unsigned char)*buf, locale)) {
591 } else if (len == 2) {
598 if (i > 1400 || (sign == -1 && i > 1200) ||
601 tm->tm_hour -= sign * (i / 100);
602 tm->tm_min -= sign * (i % 100);
609 while (isspace_l((unsigned char)*buf, locale))
618 if (century != -1 || year != -1) {
625 year += century * 100 - TM_YEAR_BASE;
629 if (!(flags & FLAG_YDAY) && (flags & FLAG_YEAR)) {
630 if ((flags & (FLAG_MONTH | FLAG_MDAY)) ==
631 (FLAG_MONTH | FLAG_MDAY)) {
632 tm->tm_yday = start_of_month[isleap(tm->tm_year +
633 TM_YEAR_BASE)][tm->tm_mon] + (tm->tm_mday - 1);
635 } else if (day_offset != -1) {
636 int tmpwday, tmpyday, fwo;
638 fwo = first_wday_of(tm->tm_year + TM_YEAR_BASE);
639 /* No incomplete week (week 0). */
640 if (week_offset == 0 && fwo == day_offset)
643 /* Set the date to the first Sunday (or Monday)
644 * of the specified week of the year.
646 tmpwday = (flags & FLAG_WDAY) ? tm->tm_wday :
648 tmpyday = (7 - fwo + day_offset) % 7 +
649 (week_offset - 1) * 7 +
650 (tmpwday - day_offset + 7) % 7;
651 /* Impossible yday for incomplete week (week 0). */
653 if (flags & FLAG_WDAY)
657 tm->tm_yday = tmpyday;
662 if ((flags & (FLAG_YEAR | FLAG_YDAY)) == (FLAG_YEAR | FLAG_YDAY)) {
663 if (!(flags & FLAG_MONTH)) {
665 while (tm->tm_yday >=
666 start_of_month[isleap(tm->tm_year +
672 start_of_month[isleap(tm->tm_year +
679 if (!(flags & FLAG_MDAY)) {
680 tm->tm_mday = tm->tm_yday -
681 start_of_month[isleap(tm->tm_year + TM_YEAR_BASE)]
685 if (!(flags & FLAG_WDAY)) {
687 wday_offset = first_wday_of(tm->tm_year);
688 while (i++ <= tm->tm_yday) {
689 if (wday_offset++ >= 6)
692 tm->tm_wday = wday_offset;
697 return ((char *)buf);
701 strptime_l(const char * __restrict buf, const char * __restrict fmt,
702 struct tm * __restrict tm, locale_t loc)
709 ret = _strptime(buf, fmt, tm, &gmt, loc);
711 time_t t = timegm(tm);
720 strptime(const char * __restrict buf, const char * __restrict fmt,
721 struct tm * __restrict tm)
723 return strptime_l(buf, fmt, tm, __get_locale());