Mark elf/tst-protected1[ab] as XFAIL.
[glibc.git] / time / strptime_l.c
blob4203ad81a0f3b36c6d5feafac9eb42de49b472ee
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 two formats: if two digits are given, these
753 specify hours. If fours digits are used, minutes are
754 also specified. */
756 val = 0;
757 while (ISSPACE (*rp))
758 ++rp;
759 if (*rp != '+' && *rp != '-')
760 return NULL;
761 bool neg = *rp++ == '-';
762 int n = 0;
763 while (n < 4 && *rp >= '0' && *rp <= '9')
765 val = val * 10 + *rp++ - '0';
766 ++n;
768 if (n == 2)
769 val *= 100;
770 else if (n != 4)
771 /* Only two or four digits recognized. */
772 return NULL;
773 else if (val % 100 >= 60)
774 /* Minutes valid range is 0 through 59. */
775 return NULL;
776 tm->tm_gmtoff = (val / 100) * 3600 + (val % 100) * 60;
777 if (neg)
778 tm->tm_gmtoff = -tm->tm_gmtoff;
780 break;
781 case 'E':
782 #ifdef _NL_CURRENT
783 switch (*fmt++)
785 case 'c':
786 /* Match locale's alternate date and time format. */
787 if (s.decided != raw)
789 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
791 if (*fmt == '\0')
792 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
794 if (!recursive (fmt))
796 if (s.decided == loc)
797 return NULL;
798 else
799 rp = rp_backup;
801 else
803 if (strcmp (fmt, HERE_D_T_FMT))
804 s.decided = loc;
805 s.want_xday = 1;
806 break;
808 s.decided = raw;
810 /* The C locale has no era information, so use the
811 normal representation. */
812 if (!recursive (HERE_D_T_FMT))
813 return NULL;
814 s.want_xday = 1;
815 break;
816 case 'C':
817 if (s.decided != raw)
819 if (s.era_cnt >= 0)
821 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
822 if (era != NULL && match_string (era->era_name, rp))
824 s.decided = loc;
825 break;
827 else
828 return NULL;
831 num_eras = _NL_CURRENT_WORD (LC_TIME,
832 _NL_TIME_ERA_NUM_ENTRIES);
833 for (s.era_cnt = 0; s.era_cnt < (int) num_eras;
834 ++s.era_cnt, rp = rp_backup)
836 era = _nl_select_era_entry (s.era_cnt
837 HELPER_LOCALE_ARG);
838 if (era != NULL && match_string (era->era_name, rp))
840 s.decided = loc;
841 break;
844 if (s.era_cnt != (int) num_eras)
845 break;
847 s.era_cnt = -1;
848 if (s.decided == loc)
849 return NULL;
851 s.decided = raw;
853 /* The C locale has no era information, so use the
854 normal representation. */
855 goto match_century;
856 case 'y':
857 if (s.decided != raw)
859 get_number(0, 9999, 4);
860 tm->tm_year = val;
861 s.want_era = 1;
862 s.want_xday = 1;
863 s.want_century = 1;
865 if (s.era_cnt >= 0)
867 assert (s.decided == loc);
869 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
870 bool match = false;
871 if (era != NULL)
873 int delta = ((tm->tm_year - era->offset)
874 * era->absolute_direction);
875 match = (delta >= 0
876 && delta < (((int64_t) era->stop_date[0]
877 - (int64_t) era->start_date[0])
878 * era->absolute_direction));
880 if (! match)
881 return NULL;
883 break;
886 num_eras = _NL_CURRENT_WORD (LC_TIME,
887 _NL_TIME_ERA_NUM_ENTRIES);
888 for (s.era_cnt = 0; s.era_cnt < (int) num_eras; ++s.era_cnt)
890 era = _nl_select_era_entry (s.era_cnt
891 HELPER_LOCALE_ARG);
892 if (era != NULL)
894 int delta = ((tm->tm_year - era->offset)
895 * era->absolute_direction);
896 if (delta >= 0
897 && delta < (((int64_t) era->stop_date[0]
898 - (int64_t) era->start_date[0])
899 * era->absolute_direction))
901 s.decided = loc;
902 break;
906 if (s.era_cnt != (int) num_eras)
907 break;
909 s.era_cnt = -1;
910 if (s.decided == loc)
911 return NULL;
913 s.decided = raw;
916 goto match_year_in_century;
917 case 'Y':
918 if (s.decided != raw)
920 num_eras = _NL_CURRENT_WORD (LC_TIME,
921 _NL_TIME_ERA_NUM_ENTRIES);
922 for (s.era_cnt = 0; s.era_cnt < (int) num_eras;
923 ++s.era_cnt, rp = rp_backup)
925 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
926 if (era != NULL && recursive (era->era_format))
927 break;
929 if (s.era_cnt == (int) num_eras)
931 s.era_cnt = -1;
932 if (s.decided == loc)
933 return NULL;
934 else
935 rp = rp_backup;
937 else
939 s.decided = loc;
940 break;
943 s.decided = raw;
945 get_number (0, 9999, 4);
946 tm->tm_year = val - 1900;
947 s.want_century = 0;
948 s.want_xday = 1;
949 break;
950 case 'x':
951 if (s.decided != raw)
953 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
955 if (*fmt == '\0')
956 fmt = _NL_CURRENT (LC_TIME, D_FMT);
958 if (!recursive (fmt))
960 if (s.decided == loc)
961 return NULL;
962 else
963 rp = rp_backup;
965 else
967 if (strcmp (fmt, HERE_D_FMT))
968 s.decided = loc;
969 break;
971 s.decided = raw;
973 if (!recursive (HERE_D_FMT))
974 return NULL;
975 break;
976 case 'X':
977 if (s.decided != raw)
979 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
981 if (*fmt == '\0')
982 fmt = _NL_CURRENT (LC_TIME, T_FMT);
984 if (!recursive (fmt))
986 if (s.decided == loc)
987 return NULL;
988 else
989 rp = rp_backup;
991 else
993 if (strcmp (fmt, HERE_T_FMT))
994 s.decided = loc;
995 break;
997 s.decided = raw;
999 if (!recursive (HERE_T_FMT))
1000 return NULL;
1001 break;
1002 default:
1003 return NULL;
1005 break;
1006 #else
1007 /* We have no information about the era format. Just use
1008 the normal format. */
1009 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
1010 && *fmt != 'x' && *fmt != 'X')
1011 /* This is an illegal format. */
1012 return NULL;
1014 goto start_over;
1015 #endif
1016 case 'O':
1017 switch (*fmt++)
1019 case 'd':
1020 case 'e':
1021 /* Match day of month using alternate numeric symbols. */
1022 get_alt_number (1, 31, 2);
1023 tm->tm_mday = val;
1024 s.have_mday = 1;
1025 s.want_xday = 1;
1026 break;
1027 case 'H':
1028 /* Match hour in 24-hour clock using alternate numeric
1029 symbols. */
1030 get_alt_number (0, 23, 2);
1031 tm->tm_hour = val;
1032 s.have_I = 0;
1033 break;
1034 case 'I':
1035 /* Match hour in 12-hour clock using alternate numeric
1036 symbols. */
1037 get_alt_number (1, 12, 2);
1038 tm->tm_hour = val % 12;
1039 s.have_I = 1;
1040 break;
1041 case 'm':
1042 /* Match month using alternate numeric symbols. */
1043 get_alt_number (1, 12, 2);
1044 tm->tm_mon = val - 1;
1045 s.have_mon = 1;
1046 s.want_xday = 1;
1047 break;
1048 case 'M':
1049 /* Match minutes using alternate numeric symbols. */
1050 get_alt_number (0, 59, 2);
1051 tm->tm_min = val;
1052 break;
1053 case 'S':
1054 /* Match seconds using alternate numeric symbols. */
1055 get_alt_number (0, 61, 2);
1056 tm->tm_sec = val;
1057 break;
1058 case 'U':
1059 get_alt_number (0, 53, 2);
1060 s.week_no = val;
1061 s.have_uweek = 1;
1062 break;
1063 case 'W':
1064 get_alt_number (0, 53, 2);
1065 s.week_no = val;
1066 s.have_wweek = 1;
1067 break;
1068 case 'V':
1069 get_alt_number (0, 53, 2);
1070 /* XXX This cannot determine any field in TM without
1071 further information. */
1072 break;
1073 case 'w':
1074 /* Match number of weekday using alternate numeric symbols. */
1075 get_alt_number (0, 6, 1);
1076 tm->tm_wday = val;
1077 s.have_wday = 1;
1078 break;
1079 case 'y':
1080 /* Match year within century using alternate numeric symbols. */
1081 get_alt_number (0, 99, 2);
1082 tm->tm_year = val >= 69 ? val : val + 100;
1083 s.want_xday = 1;
1084 break;
1085 default:
1086 return NULL;
1088 break;
1089 default:
1090 return NULL;
1094 if (statep != NULL)
1096 /* Recursive invocation, returning success, so
1097 update parent's struct tm and state. */
1098 *(struct __strptime_state *) statep = s;
1099 *tmp = tmb;
1100 return (char *) rp;
1103 if (s.have_I && s.is_pm)
1104 tm->tm_hour += 12;
1106 if (s.century != -1)
1108 if (s.want_century)
1109 tm->tm_year = tm->tm_year % 100 + (s.century - 19) * 100;
1110 else
1111 /* Only the century, but not the year. Strange, but so be it. */
1112 tm->tm_year = (s.century - 19) * 100;
1115 if (s.era_cnt != -1)
1117 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
1118 if (era == NULL)
1119 return NULL;
1120 if (s.want_era)
1121 tm->tm_year = (era->start_date[0]
1122 + ((tm->tm_year - era->offset)
1123 * era->absolute_direction));
1124 else
1125 /* Era start year assumed. */
1126 tm->tm_year = era->start_date[0];
1128 else
1129 if (s.want_era)
1131 /* No era found but we have seen an E modifier. Rectify some
1132 values. */
1133 if (s.want_century && s.century == -1 && tm->tm_year < 69)
1134 tm->tm_year += 100;
1137 if (s.want_xday && !s.have_wday)
1139 if ( !(s.have_mon && s.have_mday) && s.have_yday)
1141 /* We don't have tm_mon and/or tm_mday, compute them. */
1142 int t_mon = 0;
1143 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
1144 t_mon++;
1145 if (!s.have_mon)
1146 tm->tm_mon = t_mon - 1;
1147 if (!s.have_mday)
1148 tm->tm_mday =
1149 (tm->tm_yday
1150 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
1151 s.have_mon = 1;
1152 s.have_mday = 1;
1154 /* Don't crash in day_of_the_week if tm_mon is uninitialized. */
1155 if (s.have_mon || (unsigned) tm->tm_mon <= 11)
1156 day_of_the_week (tm);
1159 if (s.want_xday && !s.have_yday && (s.have_mon || (unsigned) tm->tm_mon <= 11))
1160 day_of_the_year (tm);
1162 if ((s.have_uweek || s.have_wweek) && s.have_wday)
1164 int save_wday = tm->tm_wday;
1165 int save_mday = tm->tm_mday;
1166 int save_mon = tm->tm_mon;
1167 int w_offset = s.have_uweek ? 0 : 1;
1169 tm->tm_mday = 1;
1170 tm->tm_mon = 0;
1171 day_of_the_week (tm);
1172 if (s.have_mday)
1173 tm->tm_mday = save_mday;
1174 if (s.have_mon)
1175 tm->tm_mon = save_mon;
1177 if (!s.have_yday)
1178 tm->tm_yday = ((7 - (tm->tm_wday - w_offset)) % 7
1179 + (s.week_no - 1) * 7
1180 + (save_wday - w_offset + 7) % 7);
1182 if (!s.have_mday || !s.have_mon)
1184 int t_mon = 0;
1185 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon]
1186 <= tm->tm_yday)
1187 t_mon++;
1188 if (!s.have_mon)
1189 tm->tm_mon = t_mon - 1;
1190 if (!s.have_mday)
1191 tm->tm_mday =
1192 (tm->tm_yday
1193 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
1196 tm->tm_wday = save_wday;
1199 return (char *) rp;
1203 char *
1204 strptime (buf, format, tm LOCALE_PARAM)
1205 const char *buf;
1206 const char *format;
1207 struct tm *tm;
1208 LOCALE_PARAM_DECL
1210 return __strptime_internal (buf, format, tm, NULL LOCALE_ARG);
1213 #ifdef _LIBC
1214 weak_alias (__strptime_l, strptime_l)
1215 #endif