Remove -static-libgcc configure test.
[glibc.git] / time / strptime_l.c
blobc3ce50fe10ab1735b8bb3d3790786f334cdf495e
1 /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <assert.h>
23 #include <ctype.h>
24 #include <langinfo.h>
25 #include <limits.h>
26 #include <string.h>
27 #include <time.h>
28 #include <stdbool.h>
30 #ifdef _LIBC
31 # define HAVE_LOCALTIME_R 0
32 # include "../locale/localeinfo.h"
33 #endif
36 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
37 # ifdef _LIBC
38 # define localtime_r __localtime_r
39 # else
40 /* Approximate localtime_r as best we can in its absence. */
41 # define localtime_r my_localtime_r
42 static struct tm *localtime_r (const time_t *, struct tm *);
43 static struct tm *
44 localtime_r (t, tp)
45 const time_t *t;
46 struct tm *tp;
48 struct tm *l = localtime (t);
49 if (! l)
50 return 0;
51 *tp = *l;
52 return tp;
54 # endif /* ! _LIBC */
55 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
58 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
59 #if defined __GNUC__ && __GNUC__ >= 2
60 # define match_string(cs1, s2) \
61 ({ size_t len = strlen (cs1); \
62 int result = __strncasecmp_l ((cs1), (s2), len, locale) == 0; \
63 if (result) (s2) += len; \
64 result; })
65 #else
66 /* Oh come on. Get a reasonable compiler. */
67 # define match_string(cs1, s2) \
68 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
69 #endif
70 /* We intentionally do not use isdigit() for testing because this will
71 lead to problems with the wide character version. */
72 #define get_number(from, to, n) \
73 do { \
74 int __n = n; \
75 val = 0; \
76 while (ISSPACE (*rp)) \
77 ++rp; \
78 if (*rp < '0' || *rp > '9') \
79 return NULL; \
80 do { \
81 val *= 10; \
82 val += *rp++ - '0'; \
83 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
84 if (val < from || val > to) \
85 return NULL; \
86 } while (0)
87 #ifdef _NL_CURRENT
88 # define get_alt_number(from, to, n) \
89 ({ \
90 __label__ do_normal; \
92 if (s.decided != raw) \
93 { \
94 val = _nl_parse_alt_digit (&rp HELPER_LOCALE_ARG); \
95 if (val == -1 && s.decided != loc) \
96 { \
97 s.decided = loc; \
98 goto do_normal; \
99 } \
100 if (val < from || val > to) \
101 return NULL; \
103 else \
105 do_normal: \
106 get_number (from, to, n); \
108 0; \
110 #else
111 # define get_alt_number(from, to, n) \
112 /* We don't have the alternate representation. */ \
113 get_number(from, to, n)
114 #endif
115 #define recursive(new_fmt) \
116 (*(new_fmt) != '\0' \
117 && (rp = __strptime_internal (rp, (new_fmt), tm, &s LOCALE_ARG)) != NULL)
120 #ifdef _LIBC
121 /* This is defined in locale/C-time.c in the GNU libc. */
122 extern const struct __locale_data _nl_C_LC_TIME attribute_hidden;
124 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
125 # define ab_weekday_name \
126 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
127 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
128 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
129 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
130 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
131 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
132 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
133 # define HERE_T_FMT_AMPM \
134 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
135 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
137 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
138 #else
139 static char const weekday_name[][10] =
141 "Sunday", "Monday", "Tuesday", "Wednesday",
142 "Thursday", "Friday", "Saturday"
144 static char const ab_weekday_name[][4] =
146 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
148 static char const month_name[][10] =
150 "January", "February", "March", "April", "May", "June",
151 "July", "August", "September", "October", "November", "December"
153 static char const ab_month_name[][4] =
155 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
156 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
158 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
159 # define HERE_D_FMT "%m/%d/%y"
160 # define HERE_AM_STR "AM"
161 # define HERE_PM_STR "PM"
162 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
163 # define HERE_T_FMT "%H:%M:%S"
165 static const unsigned short int __mon_yday[2][13] =
167 /* Normal years. */
168 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
169 /* Leap years. */
170 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
172 #endif
174 #if defined _LIBC
175 /* We use this code also for the extended locale handling where the
176 function gets as an additional argument the locale which has to be
177 used. To access the values we have to redefine the _NL_CURRENT
178 macro. */
179 # define strptime __strptime_l
180 # undef _NL_CURRENT
181 # define _NL_CURRENT(category, item) \
182 (current->values[_NL_ITEM_INDEX (item)].string)
183 # undef _NL_CURRENT_WORD
184 # define _NL_CURRENT_WORD(category, item) \
185 (current->values[_NL_ITEM_INDEX (item)].word)
186 # define LOCALE_PARAM , locale
187 # define LOCALE_ARG , locale
188 # define LOCALE_PARAM_PROTO , __locale_t locale
189 # define LOCALE_PARAM_DECL __locale_t locale;
190 # define HELPER_LOCALE_ARG , current
191 # define ISSPACE(Ch) __isspace_l (Ch, locale)
192 #else
193 # define LOCALE_PARAM
194 # define LOCALE_ARG
195 # define LOCALE_PARAM_DECL
196 # define LOCALE_PARAM_PROTO
197 # define HELPER_LOCALE_ARG
198 # define ISSPACE(Ch) isspace (Ch)
199 #endif
204 #ifndef __isleap
205 /* Nonzero if YEAR is a leap year (every 4 years,
206 except every 100th isn't, and every 400th is). */
207 # define __isleap(year) \
208 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
209 #endif
211 /* Compute the day of the week. */
212 static void
213 day_of_the_week (struct tm *tm)
215 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
216 difference between this data in the one on TM and so determine
217 the weekday. */
218 int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
219 int wday = (-473
220 + (365 * (tm->tm_year - 70))
221 + (corr_year / 4)
222 - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
223 + (((corr_year / 4) / 25) / 4)
224 + __mon_yday[0][tm->tm_mon]
225 + tm->tm_mday - 1);
226 tm->tm_wday = ((wday % 7) + 7) % 7;
229 /* Compute the day of the year. */
230 static void
231 day_of_the_year (struct tm *tm)
233 tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
234 + (tm->tm_mday - 1));
238 #ifdef _LIBC
239 char *
240 internal_function
241 #else
242 static char *
243 #endif
244 __strptime_internal (rp, fmt, tmp, statep LOCALE_PARAM)
245 const char *rp;
246 const char *fmt;
247 struct tm *tmp;
248 void *statep;
249 LOCALE_PARAM_DECL
251 #ifdef _LIBC
252 struct __locale_data *const current = locale->__locales[LC_TIME];
253 #endif
255 const char *rp_backup;
256 const char *rp_longest;
257 int cnt;
258 int cnt_longest;
259 size_t val;
260 size_t num_eras;
261 struct era_entry *era = NULL;
262 enum ptime_locale_status { not, loc, raw } decided_longest;
263 struct __strptime_state
265 unsigned int have_I : 1;
266 unsigned int have_wday : 1;
267 unsigned int have_yday : 1;
268 unsigned int have_mon : 1;
269 unsigned int have_mday : 1;
270 unsigned int have_uweek : 1;
271 unsigned int have_wweek : 1;
272 unsigned int is_pm : 1;
273 unsigned int want_century : 1;
274 unsigned int want_era : 1;
275 unsigned int want_xday : 1;
276 enum ptime_locale_status decided : 2;
277 signed char week_no;
278 signed char century;
279 int era_cnt;
280 } s;
281 struct tm tmb;
282 struct tm *tm;
284 if (statep == NULL)
286 memset (&s, 0, sizeof (s));
287 s.century = -1;
288 s.era_cnt = -1;
289 #ifdef _NL_CURRENT
290 s.decided = not;
291 #else
292 s.decided = raw;
293 #endif
294 tm = tmp;
296 else
298 s = *(struct __strptime_state *) statep;
299 tmb = *tmp;
300 tm = &tmb;
303 while (*fmt != '\0')
305 /* A white space in the format string matches 0 more or white
306 space in the input string. */
307 if (ISSPACE (*fmt))
309 while (ISSPACE (*rp))
310 ++rp;
311 ++fmt;
312 continue;
315 /* Any character but `%' must be matched by the same character
316 in the iput string. */
317 if (*fmt != '%')
319 match_char (*fmt++, *rp++);
320 continue;
323 ++fmt;
324 /* We discard strftime modifiers. */
325 while (*fmt == '-' || *fmt == '_' || *fmt == '0'
326 || *fmt == '^' || *fmt == '#')
327 ++fmt;
329 /* And field width. */
330 while (*fmt >= '0' && *fmt <= '9')
331 ++fmt;
333 #ifndef _NL_CURRENT
334 /* We need this for handling the `E' modifier. */
335 start_over:
336 #endif
338 /* Make back up of current processing pointer. */
339 rp_backup = rp;
341 switch (*fmt++)
343 case '%':
344 /* Match the `%' character itself. */
345 match_char ('%', *rp++);
346 break;
347 case 'a':
348 case 'A':
349 /* Match day of week. */
350 rp_longest = NULL;
351 decided_longest = s.decided;
352 cnt_longest = -1;
353 for (cnt = 0; cnt < 7; ++cnt)
355 const char *trp;
356 #ifdef _NL_CURRENT
357 if (s.decided !=raw)
359 trp = rp;
360 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), trp)
361 && trp > rp_longest)
363 rp_longest = trp;
364 cnt_longest = cnt;
365 if (s.decided == not
366 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
367 weekday_name[cnt]))
368 decided_longest = loc;
370 trp = rp;
371 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), trp)
372 && trp > rp_longest)
374 rp_longest = trp;
375 cnt_longest = cnt;
376 if (s.decided == not
377 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
378 ab_weekday_name[cnt]))
379 decided_longest = loc;
382 #endif
383 if (s.decided != loc
384 && (((trp = rp, match_string (weekday_name[cnt], trp))
385 && trp > rp_longest)
386 || ((trp = rp, match_string (ab_weekday_name[cnt], rp))
387 && trp > rp_longest)))
389 rp_longest = trp;
390 cnt_longest = cnt;
391 decided_longest = raw;
394 if (rp_longest == NULL)
395 /* Does not match a weekday name. */
396 return NULL;
397 rp = rp_longest;
398 s.decided = decided_longest;
399 tm->tm_wday = cnt_longest;
400 s.have_wday = 1;
401 break;
402 case 'b':
403 case 'B':
404 case 'h':
405 /* Match month name. */
406 rp_longest = NULL;
407 decided_longest = s.decided;
408 cnt_longest = -1;
409 for (cnt = 0; cnt < 12; ++cnt)
411 const char *trp;
412 #ifdef _NL_CURRENT
413 if (s.decided !=raw)
415 trp = rp;
416 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), trp)
417 && trp > rp_longest)
419 rp_longest = trp;
420 cnt_longest = cnt;
421 if (s.decided == not
422 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
423 month_name[cnt]))
424 decided_longest = loc;
426 trp = rp;
427 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), trp)
428 && trp > rp_longest)
430 rp_longest = trp;
431 cnt_longest = cnt;
432 if (s.decided == not
433 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
434 ab_month_name[cnt]))
435 decided_longest = loc;
438 #endif
439 if (s.decided != loc
440 && (((trp = rp, match_string (month_name[cnt], trp))
441 && trp > rp_longest)
442 || ((trp = rp, match_string (ab_month_name[cnt], trp))
443 && trp > rp_longest)))
445 rp_longest = trp;
446 cnt_longest = cnt;
447 decided_longest = raw;
450 if (rp_longest == NULL)
451 /* Does not match a month name. */
452 return NULL;
453 rp = rp_longest;
454 s.decided = decided_longest;
455 tm->tm_mon = cnt_longest;
456 s.have_mon = 1;
457 s.want_xday = 1;
458 break;
459 case 'c':
460 /* Match locale's date and time format. */
461 #ifdef _NL_CURRENT
462 if (s.decided != raw)
464 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
466 if (s.decided == loc)
467 return NULL;
468 else
469 rp = rp_backup;
471 else
473 if (s.decided == not &&
474 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
475 s.decided = loc;
476 s.want_xday = 1;
477 break;
479 s.decided = raw;
481 #endif
482 if (!recursive (HERE_D_T_FMT))
483 return NULL;
484 s.want_xday = 1;
485 break;
486 case 'C':
487 /* Match century number. */
488 match_century:
489 get_number (0, 99, 2);
490 s.century = val;
491 s.want_xday = 1;
492 break;
493 case 'd':
494 case 'e':
495 /* Match day of month. */
496 get_number (1, 31, 2);
497 tm->tm_mday = val;
498 s.have_mday = 1;
499 s.want_xday = 1;
500 break;
501 case 'F':
502 if (!recursive ("%Y-%m-%d"))
503 return NULL;
504 s.want_xday = 1;
505 break;
506 case 'x':
507 #ifdef _NL_CURRENT
508 if (s.decided != raw)
510 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
512 if (s.decided == loc)
513 return NULL;
514 else
515 rp = rp_backup;
517 else
519 if (s.decided == not
520 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
521 s.decided = loc;
522 s.want_xday = 1;
523 break;
525 s.decided = raw;
527 #endif
528 /* Fall through. */
529 case 'D':
530 /* Match standard day format. */
531 if (!recursive (HERE_D_FMT))
532 return NULL;
533 s.want_xday = 1;
534 break;
535 case 'k':
536 case 'H':
537 /* Match hour in 24-hour clock. */
538 get_number (0, 23, 2);
539 tm->tm_hour = val;
540 s.have_I = 0;
541 break;
542 case 'l':
543 /* Match hour in 12-hour clock. GNU extension. */
544 case 'I':
545 /* Match hour in 12-hour clock. */
546 get_number (1, 12, 2);
547 tm->tm_hour = val % 12;
548 s.have_I = 1;
549 break;
550 case 'j':
551 /* Match day number of year. */
552 get_number (1, 366, 3);
553 tm->tm_yday = val - 1;
554 s.have_yday = 1;
555 break;
556 case 'm':
557 /* Match number of month. */
558 get_number (1, 12, 2);
559 tm->tm_mon = val - 1;
560 s.have_mon = 1;
561 s.want_xday = 1;
562 break;
563 case 'M':
564 /* Match minute. */
565 get_number (0, 59, 2);
566 tm->tm_min = val;
567 break;
568 case 'n':
569 case 't':
570 /* Match any white space. */
571 while (ISSPACE (*rp))
572 ++rp;
573 break;
574 case 'p':
575 /* Match locale's equivalent of AM/PM. */
576 #ifdef _NL_CURRENT
577 if (s.decided != raw)
579 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
581 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
582 s.decided = loc;
583 s.is_pm = 0;
584 break;
586 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
588 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
589 s.decided = loc;
590 s.is_pm = 1;
591 break;
593 s.decided = raw;
595 #endif
596 if (!match_string (HERE_AM_STR, rp))
598 if (match_string (HERE_PM_STR, rp))
599 s.is_pm = 1;
600 else
601 return NULL;
603 else
604 s.is_pm = 0;
605 break;
606 case 'r':
607 #ifdef _NL_CURRENT
608 if (s.decided != raw)
610 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
612 if (s.decided == loc)
613 return NULL;
614 else
615 rp = rp_backup;
617 else
619 if (s.decided == not &&
620 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
621 HERE_T_FMT_AMPM))
622 s.decided = loc;
623 break;
625 s.decided = raw;
627 #endif
628 if (!recursive (HERE_T_FMT_AMPM))
629 return NULL;
630 break;
631 case 'R':
632 if (!recursive ("%H:%M"))
633 return NULL;
634 break;
635 case 's':
637 /* The number of seconds may be very high so we cannot use
638 the `get_number' macro. Instead read the number
639 character for character and construct the result while
640 doing this. */
641 time_t secs = 0;
642 if (*rp < '0' || *rp > '9')
643 /* We need at least one digit. */
644 return NULL;
648 secs *= 10;
649 secs += *rp++ - '0';
651 while (*rp >= '0' && *rp <= '9');
653 if (localtime_r (&secs, tm) == NULL)
654 /* Error in function. */
655 return NULL;
657 break;
658 case 'S':
659 get_number (0, 61, 2);
660 tm->tm_sec = val;
661 break;
662 case 'X':
663 #ifdef _NL_CURRENT
664 if (s.decided != raw)
666 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
668 if (s.decided == loc)
669 return NULL;
670 else
671 rp = rp_backup;
673 else
675 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
676 s.decided = loc;
677 break;
679 s.decided = raw;
681 #endif
682 /* Fall through. */
683 case 'T':
684 if (!recursive (HERE_T_FMT))
685 return NULL;
686 break;
687 case 'u':
688 get_number (1, 7, 1);
689 tm->tm_wday = val % 7;
690 s.have_wday = 1;
691 break;
692 case 'g':
693 get_number (0, 99, 2);
694 /* XXX This cannot determine any field in TM. */
695 break;
696 case 'G':
697 if (*rp < '0' || *rp > '9')
698 return NULL;
699 /* XXX Ignore the number since we would need some more
700 information to compute a real date. */
702 ++rp;
703 while (*rp >= '0' && *rp <= '9');
704 break;
705 case 'U':
706 get_number (0, 53, 2);
707 s.week_no = val;
708 s.have_uweek = 1;
709 break;
710 case 'W':
711 get_number (0, 53, 2);
712 s.week_no = val;
713 s.have_wweek = 1;
714 break;
715 case 'V':
716 get_number (0, 53, 2);
717 /* XXX This cannot determine any field in TM without some
718 information. */
719 break;
720 case 'w':
721 /* Match number of weekday. */
722 get_number (0, 6, 1);
723 tm->tm_wday = val;
724 s.have_wday = 1;
725 break;
726 case 'y':
727 match_year_in_century:
728 /* Match year within century. */
729 get_number (0, 99, 2);
730 /* The "Year 2000: The Millennium Rollover" paper suggests that
731 values in the range 69-99 refer to the twentieth century. */
732 tm->tm_year = val >= 69 ? val : val + 100;
733 /* Indicate that we want to use the century, if specified. */
734 s.want_century = 1;
735 s.want_xday = 1;
736 break;
737 case 'Y':
738 /* Match year including century number. */
739 get_number (0, 9999, 4);
740 tm->tm_year = val - 1900;
741 s.want_century = 0;
742 s.want_xday = 1;
743 break;
744 case 'Z':
745 /* Read timezone but perform no conversion. */
746 while (ISSPACE (*rp))
747 rp++;
748 while (!ISSPACE (*rp) && *rp != '\0')
749 rp++;
750 break;
751 case 'z':
752 /* We recognize four formats:
753 1. Two digits specify hours.
754 2. Four digits specify hours and minutes.
755 3. Two digits, ':', and two digits specify hours and minutes.
756 4. 'Z' is equivalent to +0000. */
758 val = 0;
759 while (ISSPACE (*rp))
760 ++rp;
761 if (*rp == 'Z')
763 ++rp;
764 tm->tm_gmtoff = 0;
765 break;
767 if (*rp != '+' && *rp != '-')
768 return NULL;
769 bool neg = *rp++ == '-';
770 int n = 0;
771 while (n < 4 && *rp >= '0' && *rp <= '9')
773 val = val * 10 + *rp++ - '0';
774 ++n;
775 if (*rp == ':' && n == 2 && isdigit (*(rp + 1)))
776 ++rp;
778 if (n == 2)
779 val *= 100;
780 else if (n != 4)
781 /* Only two or four digits recognized. */
782 return NULL;
783 else if (val % 100 >= 60)
784 /* Minutes valid range is 0 through 59. */
785 return NULL;
786 tm->tm_gmtoff = (val / 100) * 3600 + (val % 100) * 60;
787 if (neg)
788 tm->tm_gmtoff = -tm->tm_gmtoff;
790 break;
791 case 'E':
792 #ifdef _NL_CURRENT
793 switch (*fmt++)
795 case 'c':
796 /* Match locale's alternate date and time format. */
797 if (s.decided != raw)
799 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
801 if (*fmt == '\0')
802 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
804 if (!recursive (fmt))
806 if (s.decided == loc)
807 return NULL;
808 else
809 rp = rp_backup;
811 else
813 if (strcmp (fmt, HERE_D_T_FMT))
814 s.decided = loc;
815 s.want_xday = 1;
816 break;
818 s.decided = raw;
820 /* The C locale has no era information, so use the
821 normal representation. */
822 if (!recursive (HERE_D_T_FMT))
823 return NULL;
824 s.want_xday = 1;
825 break;
826 case 'C':
827 if (s.decided != raw)
829 if (s.era_cnt >= 0)
831 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
832 if (era != NULL && match_string (era->era_name, rp))
834 s.decided = loc;
835 break;
837 else
838 return NULL;
841 num_eras = _NL_CURRENT_WORD (LC_TIME,
842 _NL_TIME_ERA_NUM_ENTRIES);
843 for (s.era_cnt = 0; s.era_cnt < (int) num_eras;
844 ++s.era_cnt, rp = rp_backup)
846 era = _nl_select_era_entry (s.era_cnt
847 HELPER_LOCALE_ARG);
848 if (era != NULL && match_string (era->era_name, rp))
850 s.decided = loc;
851 break;
854 if (s.era_cnt != (int) num_eras)
855 break;
857 s.era_cnt = -1;
858 if (s.decided == loc)
859 return NULL;
861 s.decided = raw;
863 /* The C locale has no era information, so use the
864 normal representation. */
865 goto match_century;
866 case 'y':
867 if (s.decided != raw)
869 get_number(0, 9999, 4);
870 tm->tm_year = val;
871 s.want_era = 1;
872 s.want_xday = 1;
873 s.want_century = 1;
875 if (s.era_cnt >= 0)
877 assert (s.decided == loc);
879 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
880 bool match = false;
881 if (era != NULL)
883 int delta = ((tm->tm_year - era->offset)
884 * era->absolute_direction);
885 match = (delta >= 0
886 && delta < (((int64_t) era->stop_date[0]
887 - (int64_t) era->start_date[0])
888 * era->absolute_direction));
890 if (! match)
891 return NULL;
893 break;
896 num_eras = _NL_CURRENT_WORD (LC_TIME,
897 _NL_TIME_ERA_NUM_ENTRIES);
898 for (s.era_cnt = 0; s.era_cnt < (int) num_eras; ++s.era_cnt)
900 era = _nl_select_era_entry (s.era_cnt
901 HELPER_LOCALE_ARG);
902 if (era != NULL)
904 int delta = ((tm->tm_year - era->offset)
905 * era->absolute_direction);
906 if (delta >= 0
907 && delta < (((int64_t) era->stop_date[0]
908 - (int64_t) era->start_date[0])
909 * era->absolute_direction))
911 s.decided = loc;
912 break;
916 if (s.era_cnt != (int) num_eras)
917 break;
919 s.era_cnt = -1;
920 if (s.decided == loc)
921 return NULL;
923 s.decided = raw;
926 goto match_year_in_century;
927 case 'Y':
928 if (s.decided != raw)
930 num_eras = _NL_CURRENT_WORD (LC_TIME,
931 _NL_TIME_ERA_NUM_ENTRIES);
932 for (s.era_cnt = 0; s.era_cnt < (int) num_eras;
933 ++s.era_cnt, rp = rp_backup)
935 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
936 if (era != NULL && recursive (era->era_format))
937 break;
939 if (s.era_cnt == (int) num_eras)
941 s.era_cnt = -1;
942 if (s.decided == loc)
943 return NULL;
944 else
945 rp = rp_backup;
947 else
949 s.decided = loc;
950 break;
953 s.decided = raw;
955 get_number (0, 9999, 4);
956 tm->tm_year = val - 1900;
957 s.want_century = 0;
958 s.want_xday = 1;
959 break;
960 case 'x':
961 if (s.decided != raw)
963 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
965 if (*fmt == '\0')
966 fmt = _NL_CURRENT (LC_TIME, D_FMT);
968 if (!recursive (fmt))
970 if (s.decided == loc)
971 return NULL;
972 else
973 rp = rp_backup;
975 else
977 if (strcmp (fmt, HERE_D_FMT))
978 s.decided = loc;
979 break;
981 s.decided = raw;
983 if (!recursive (HERE_D_FMT))
984 return NULL;
985 break;
986 case 'X':
987 if (s.decided != raw)
989 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
991 if (*fmt == '\0')
992 fmt = _NL_CURRENT (LC_TIME, T_FMT);
994 if (!recursive (fmt))
996 if (s.decided == loc)
997 return NULL;
998 else
999 rp = rp_backup;
1001 else
1003 if (strcmp (fmt, HERE_T_FMT))
1004 s.decided = loc;
1005 break;
1007 s.decided = raw;
1009 if (!recursive (HERE_T_FMT))
1010 return NULL;
1011 break;
1012 default:
1013 return NULL;
1015 break;
1016 #else
1017 /* We have no information about the era format. Just use
1018 the normal format. */
1019 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
1020 && *fmt != 'x' && *fmt != 'X')
1021 /* This is an illegal format. */
1022 return NULL;
1024 goto start_over;
1025 #endif
1026 case 'O':
1027 switch (*fmt++)
1029 case 'd':
1030 case 'e':
1031 /* Match day of month using alternate numeric symbols. */
1032 get_alt_number (1, 31, 2);
1033 tm->tm_mday = val;
1034 s.have_mday = 1;
1035 s.want_xday = 1;
1036 break;
1037 case 'H':
1038 /* Match hour in 24-hour clock using alternate numeric
1039 symbols. */
1040 get_alt_number (0, 23, 2);
1041 tm->tm_hour = val;
1042 s.have_I = 0;
1043 break;
1044 case 'I':
1045 /* Match hour in 12-hour clock using alternate numeric
1046 symbols. */
1047 get_alt_number (1, 12, 2);
1048 tm->tm_hour = val % 12;
1049 s.have_I = 1;
1050 break;
1051 case 'm':
1052 /* Match month using alternate numeric symbols. */
1053 get_alt_number (1, 12, 2);
1054 tm->tm_mon = val - 1;
1055 s.have_mon = 1;
1056 s.want_xday = 1;
1057 break;
1058 case 'M':
1059 /* Match minutes using alternate numeric symbols. */
1060 get_alt_number (0, 59, 2);
1061 tm->tm_min = val;
1062 break;
1063 case 'S':
1064 /* Match seconds using alternate numeric symbols. */
1065 get_alt_number (0, 61, 2);
1066 tm->tm_sec = val;
1067 break;
1068 case 'U':
1069 get_alt_number (0, 53, 2);
1070 s.week_no = val;
1071 s.have_uweek = 1;
1072 break;
1073 case 'W':
1074 get_alt_number (0, 53, 2);
1075 s.week_no = val;
1076 s.have_wweek = 1;
1077 break;
1078 case 'V':
1079 get_alt_number (0, 53, 2);
1080 /* XXX This cannot determine any field in TM without
1081 further information. */
1082 break;
1083 case 'w':
1084 /* Match number of weekday using alternate numeric symbols. */
1085 get_alt_number (0, 6, 1);
1086 tm->tm_wday = val;
1087 s.have_wday = 1;
1088 break;
1089 case 'y':
1090 /* Match year within century using alternate numeric symbols. */
1091 get_alt_number (0, 99, 2);
1092 tm->tm_year = val >= 69 ? val : val + 100;
1093 s.want_xday = 1;
1094 break;
1095 default:
1096 return NULL;
1098 break;
1099 default:
1100 return NULL;
1104 if (statep != NULL)
1106 /* Recursive invocation, returning success, so
1107 update parent's struct tm and state. */
1108 *(struct __strptime_state *) statep = s;
1109 *tmp = tmb;
1110 return (char *) rp;
1113 if (s.have_I && s.is_pm)
1114 tm->tm_hour += 12;
1116 if (s.century != -1)
1118 if (s.want_century)
1119 tm->tm_year = tm->tm_year % 100 + (s.century - 19) * 100;
1120 else
1121 /* Only the century, but not the year. Strange, but so be it. */
1122 tm->tm_year = (s.century - 19) * 100;
1125 if (s.era_cnt != -1)
1127 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
1128 if (era == NULL)
1129 return NULL;
1130 if (s.want_era)
1131 tm->tm_year = (era->start_date[0]
1132 + ((tm->tm_year - era->offset)
1133 * era->absolute_direction));
1134 else
1135 /* Era start year assumed. */
1136 tm->tm_year = era->start_date[0];
1138 else
1139 if (s.want_era)
1141 /* No era found but we have seen an E modifier. Rectify some
1142 values. */
1143 if (s.want_century && s.century == -1 && tm->tm_year < 69)
1144 tm->tm_year += 100;
1147 if (s.want_xday && !s.have_wday)
1149 if ( !(s.have_mon && s.have_mday) && s.have_yday)
1151 /* We don't have tm_mon and/or tm_mday, compute them. */
1152 int t_mon = 0;
1153 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
1154 t_mon++;
1155 if (!s.have_mon)
1156 tm->tm_mon = t_mon - 1;
1157 if (!s.have_mday)
1158 tm->tm_mday =
1159 (tm->tm_yday
1160 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
1161 s.have_mon = 1;
1162 s.have_mday = 1;
1164 /* Don't crash in day_of_the_week if tm_mon is uninitialized. */
1165 if (s.have_mon || (unsigned) tm->tm_mon <= 11)
1166 day_of_the_week (tm);
1169 if (s.want_xday && !s.have_yday && (s.have_mon || (unsigned) tm->tm_mon <= 11))
1170 day_of_the_year (tm);
1172 if ((s.have_uweek || s.have_wweek) && s.have_wday)
1174 int save_wday = tm->tm_wday;
1175 int save_mday = tm->tm_mday;
1176 int save_mon = tm->tm_mon;
1177 int w_offset = s.have_uweek ? 0 : 1;
1179 tm->tm_mday = 1;
1180 tm->tm_mon = 0;
1181 day_of_the_week (tm);
1182 if (s.have_mday)
1183 tm->tm_mday = save_mday;
1184 if (s.have_mon)
1185 tm->tm_mon = save_mon;
1187 if (!s.have_yday)
1188 tm->tm_yday = ((7 - (tm->tm_wday - w_offset)) % 7
1189 + (s.week_no - 1) * 7
1190 + (save_wday - w_offset + 7) % 7);
1192 if (!s.have_mday || !s.have_mon)
1194 int t_mon = 0;
1195 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon]
1196 <= tm->tm_yday)
1197 t_mon++;
1198 if (!s.have_mon)
1199 tm->tm_mon = t_mon - 1;
1200 if (!s.have_mday)
1201 tm->tm_mday =
1202 (tm->tm_yday
1203 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
1206 tm->tm_wday = save_wday;
1209 return (char *) rp;
1213 char *
1214 strptime (buf, format, tm LOCALE_PARAM)
1215 const char *buf;
1216 const char *format;
1217 struct tm *tm;
1218 LOCALE_PARAM_DECL
1220 return __strptime_internal (buf, format, tm, NULL LOCALE_ARG);
1223 #ifdef _LIBC
1224 weak_alias (__strptime_l, strptime_l)
1225 #endif