Fix implicit declaration
[glibc.git] / time / strptime_l.c
blob8fe623d5792b137ffe191c3a15b5dcce100fce7a
1 /* Copyright (C) 2002-2014 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 # include "../locale/localeinfo.h"
32 #endif
35 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
36 # ifdef _LIBC
37 # define localtime_r __localtime_r
38 # else
39 /* Approximate localtime_r as best we can in its absence. */
40 # define localtime_r my_localtime_r
41 static struct tm *localtime_r (const time_t *, struct tm *);
42 static struct tm *
43 localtime_r (t, tp)
44 const time_t *t;
45 struct tm *tp;
47 struct tm *l = localtime (t);
48 if (! l)
49 return 0;
50 *tp = *l;
51 return tp;
53 # endif /* ! _LIBC */
54 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
57 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
58 #if defined __GNUC__ && __GNUC__ >= 2
59 # define match_string(cs1, s2) \
60 ({ size_t len = strlen (cs1); \
61 int result = __strncasecmp_l ((cs1), (s2), len, locale) == 0; \
62 if (result) (s2) += len; \
63 result; })
64 #else
65 /* Oh come on. Get a reasonable compiler. */
66 # define match_string(cs1, s2) \
67 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
68 #endif
69 /* We intentionally do not use isdigit() for testing because this will
70 lead to problems with the wide character version. */
71 #define get_number(from, to, n) \
72 do { \
73 int __n = n; \
74 val = 0; \
75 while (ISSPACE (*rp)) \
76 ++rp; \
77 if (*rp < '0' || *rp > '9') \
78 return NULL; \
79 do { \
80 val *= 10; \
81 val += *rp++ - '0'; \
82 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
83 if (val < from || val > to) \
84 return NULL; \
85 } while (0)
86 #ifdef _NL_CURRENT
87 # define get_alt_number(from, to, n) \
88 ({ \
89 __label__ do_normal; \
91 if (s.decided != raw) \
92 { \
93 val = _nl_parse_alt_digit (&rp HELPER_LOCALE_ARG); \
94 if (val == -1 && s.decided != loc) \
95 { \
96 s.decided = loc; \
97 goto do_normal; \
98 } \
99 if (val < from || val > to) \
100 return NULL; \
102 else \
104 do_normal: \
105 get_number (from, to, n); \
107 0; \
109 #else
110 # define get_alt_number(from, to, n) \
111 /* We don't have the alternate representation. */ \
112 get_number(from, to, n)
113 #endif
114 #define recursive(new_fmt) \
115 (*(new_fmt) != '\0' \
116 && (rp = __strptime_internal (rp, (new_fmt), tm, &s LOCALE_ARG)) != NULL)
119 #ifdef _LIBC
120 /* This is defined in locale/C-time.c in the GNU libc. */
121 extern const struct __locale_data _nl_C_LC_TIME attribute_hidden;
123 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
124 # define ab_weekday_name \
125 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
126 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
127 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
128 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
129 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
130 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
131 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
132 # define HERE_T_FMT_AMPM \
133 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
134 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
136 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
137 #else
138 static char const weekday_name[][10] =
140 "Sunday", "Monday", "Tuesday", "Wednesday",
141 "Thursday", "Friday", "Saturday"
143 static char const ab_weekday_name[][4] =
145 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
147 static char const month_name[][10] =
149 "January", "February", "March", "April", "May", "June",
150 "July", "August", "September", "October", "November", "December"
152 static char const ab_month_name[][4] =
154 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
155 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
157 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
158 # define HERE_D_FMT "%m/%d/%y"
159 # define HERE_AM_STR "AM"
160 # define HERE_PM_STR "PM"
161 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
162 # define HERE_T_FMT "%H:%M:%S"
164 static const unsigned short int __mon_yday[2][13] =
166 /* Normal years. */
167 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
168 /* Leap years. */
169 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
171 #endif
173 #if defined _LIBC
174 /* We use this code also for the extended locale handling where the
175 function gets as an additional argument the locale which has to be
176 used. To access the values we have to redefine the _NL_CURRENT
177 macro. */
178 # define strptime __strptime_l
179 # undef _NL_CURRENT
180 # define _NL_CURRENT(category, item) \
181 (current->values[_NL_ITEM_INDEX (item)].string)
182 # undef _NL_CURRENT_WORD
183 # define _NL_CURRENT_WORD(category, item) \
184 (current->values[_NL_ITEM_INDEX (item)].word)
185 # define LOCALE_PARAM , locale
186 # define LOCALE_ARG , locale
187 # define LOCALE_PARAM_PROTO , __locale_t locale
188 # define LOCALE_PARAM_DECL __locale_t locale;
189 # define HELPER_LOCALE_ARG , current
190 # define ISSPACE(Ch) __isspace_l (Ch, locale)
191 #else
192 # define LOCALE_PARAM
193 # define LOCALE_ARG
194 # define LOCALE_PARAM_DECL
195 # define LOCALE_PARAM_PROTO
196 # define HELPER_LOCALE_ARG
197 # define ISSPACE(Ch) isspace (Ch)
198 #endif
203 #ifndef __isleap
204 /* Nonzero if YEAR is a leap year (every 4 years,
205 except every 100th isn't, and every 400th is). */
206 # define __isleap(year) \
207 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
208 #endif
210 /* Compute the day of the week. */
211 static void
212 day_of_the_week (struct tm *tm)
214 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
215 difference between this data in the one on TM and so determine
216 the weekday. */
217 int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
218 int wday = (-473
219 + (365 * (tm->tm_year - 70))
220 + (corr_year / 4)
221 - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
222 + (((corr_year / 4) / 25) / 4)
223 + __mon_yday[0][tm->tm_mon]
224 + tm->tm_mday - 1);
225 tm->tm_wday = ((wday % 7) + 7) % 7;
228 /* Compute the day of the year. */
229 static void
230 day_of_the_year (struct tm *tm)
232 tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
233 + (tm->tm_mday - 1));
237 #ifdef _LIBC
238 char *
239 internal_function
240 #else
241 static char *
242 #endif
243 __strptime_internal (rp, fmt, tmp, statep LOCALE_PARAM)
244 const char *rp;
245 const char *fmt;
246 struct tm *tmp;
247 void *statep;
248 LOCALE_PARAM_DECL
250 #ifdef _LIBC
251 struct __locale_data *const current = locale->__locales[LC_TIME];
252 #endif
254 const char *rp_backup;
255 const char *rp_longest;
256 int cnt;
257 int cnt_longest;
258 size_t val;
259 size_t num_eras;
260 struct era_entry *era = NULL;
261 enum ptime_locale_status { not, loc, raw } decided_longest;
262 struct __strptime_state
264 unsigned int have_I : 1;
265 unsigned int have_wday : 1;
266 unsigned int have_yday : 1;
267 unsigned int have_mon : 1;
268 unsigned int have_mday : 1;
269 unsigned int have_uweek : 1;
270 unsigned int have_wweek : 1;
271 unsigned int is_pm : 1;
272 unsigned int want_century : 1;
273 unsigned int want_era : 1;
274 unsigned int want_xday : 1;
275 enum ptime_locale_status decided : 2;
276 signed char week_no;
277 signed char century;
278 int era_cnt;
279 } s;
280 struct tm tmb;
281 struct tm *tm;
283 if (statep == NULL)
285 memset (&s, 0, sizeof (s));
286 s.century = -1;
287 s.era_cnt = -1;
288 #ifdef _NL_CURRENT
289 s.decided = not;
290 #else
291 s.decided = raw;
292 #endif
293 tm = tmp;
295 else
297 s = *(struct __strptime_state *) statep;
298 tmb = *tmp;
299 tm = &tmb;
302 while (*fmt != '\0')
304 /* A white space in the format string matches 0 more or white
305 space in the input string. */
306 if (ISSPACE (*fmt))
308 while (ISSPACE (*rp))
309 ++rp;
310 ++fmt;
311 continue;
314 /* Any character but `%' must be matched by the same character
315 in the iput string. */
316 if (*fmt != '%')
318 match_char (*fmt++, *rp++);
319 continue;
322 ++fmt;
323 /* We discard strftime modifiers. */
324 while (*fmt == '-' || *fmt == '_' || *fmt == '0'
325 || *fmt == '^' || *fmt == '#')
326 ++fmt;
328 /* And field width. */
329 while (*fmt >= '0' && *fmt <= '9')
330 ++fmt;
332 #ifndef _NL_CURRENT
333 /* We need this for handling the `E' modifier. */
334 start_over:
335 #endif
337 /* Make back up of current processing pointer. */
338 rp_backup = rp;
340 switch (*fmt++)
342 case '%':
343 /* Match the `%' character itself. */
344 match_char ('%', *rp++);
345 break;
346 case 'a':
347 case 'A':
348 /* Match day of week. */
349 rp_longest = NULL;
350 decided_longest = s.decided;
351 cnt_longest = -1;
352 for (cnt = 0; cnt < 7; ++cnt)
354 const char *trp;
355 #ifdef _NL_CURRENT
356 if (s.decided !=raw)
358 trp = rp;
359 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), trp)
360 && trp > rp_longest)
362 rp_longest = trp;
363 cnt_longest = cnt;
364 if (s.decided == not
365 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
366 weekday_name[cnt]))
367 decided_longest = loc;
369 trp = rp;
370 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), trp)
371 && trp > rp_longest)
373 rp_longest = trp;
374 cnt_longest = cnt;
375 if (s.decided == not
376 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
377 ab_weekday_name[cnt]))
378 decided_longest = loc;
381 #endif
382 if (s.decided != loc
383 && (((trp = rp, match_string (weekday_name[cnt], trp))
384 && trp > rp_longest)
385 || ((trp = rp, match_string (ab_weekday_name[cnt], rp))
386 && trp > rp_longest)))
388 rp_longest = trp;
389 cnt_longest = cnt;
390 decided_longest = raw;
393 if (rp_longest == NULL)
394 /* Does not match a weekday name. */
395 return NULL;
396 rp = rp_longest;
397 s.decided = decided_longest;
398 tm->tm_wday = cnt_longest;
399 s.have_wday = 1;
400 break;
401 case 'b':
402 case 'B':
403 case 'h':
404 /* Match month name. */
405 rp_longest = NULL;
406 decided_longest = s.decided;
407 cnt_longest = -1;
408 for (cnt = 0; cnt < 12; ++cnt)
410 const char *trp;
411 #ifdef _NL_CURRENT
412 if (s.decided !=raw)
414 trp = rp;
415 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), trp)
416 && trp > rp_longest)
418 rp_longest = trp;
419 cnt_longest = cnt;
420 if (s.decided == not
421 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
422 month_name[cnt]))
423 decided_longest = loc;
425 trp = rp;
426 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), trp)
427 && trp > rp_longest)
429 rp_longest = trp;
430 cnt_longest = cnt;
431 if (s.decided == not
432 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
433 ab_month_name[cnt]))
434 decided_longest = loc;
437 #endif
438 if (s.decided != loc
439 && (((trp = rp, match_string (month_name[cnt], trp))
440 && trp > rp_longest)
441 || ((trp = rp, match_string (ab_month_name[cnt], trp))
442 && trp > rp_longest)))
444 rp_longest = trp;
445 cnt_longest = cnt;
446 decided_longest = raw;
449 if (rp_longest == NULL)
450 /* Does not match a month name. */
451 return NULL;
452 rp = rp_longest;
453 s.decided = decided_longest;
454 tm->tm_mon = cnt_longest;
455 s.have_mon = 1;
456 s.want_xday = 1;
457 break;
458 case 'c':
459 /* Match locale's date and time format. */
460 #ifdef _NL_CURRENT
461 if (s.decided != raw)
463 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
465 if (s.decided == loc)
466 return NULL;
467 else
468 rp = rp_backup;
470 else
472 if (s.decided == not &&
473 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
474 s.decided = loc;
475 s.want_xday = 1;
476 break;
478 s.decided = raw;
480 #endif
481 if (!recursive (HERE_D_T_FMT))
482 return NULL;
483 s.want_xday = 1;
484 break;
485 case 'C':
486 /* Match century number. */
487 match_century:
488 get_number (0, 99, 2);
489 s.century = val;
490 s.want_xday = 1;
491 break;
492 case 'd':
493 case 'e':
494 /* Match day of month. */
495 get_number (1, 31, 2);
496 tm->tm_mday = val;
497 s.have_mday = 1;
498 s.want_xday = 1;
499 break;
500 case 'F':
501 if (!recursive ("%Y-%m-%d"))
502 return NULL;
503 s.want_xday = 1;
504 break;
505 case 'x':
506 #ifdef _NL_CURRENT
507 if (s.decided != raw)
509 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
511 if (s.decided == loc)
512 return NULL;
513 else
514 rp = rp_backup;
516 else
518 if (s.decided == not
519 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
520 s.decided = loc;
521 s.want_xday = 1;
522 break;
524 s.decided = raw;
526 #endif
527 /* Fall through. */
528 case 'D':
529 /* Match standard day format. */
530 if (!recursive (HERE_D_FMT))
531 return NULL;
532 s.want_xday = 1;
533 break;
534 case 'k':
535 case 'H':
536 /* Match hour in 24-hour clock. */
537 get_number (0, 23, 2);
538 tm->tm_hour = val;
539 s.have_I = 0;
540 break;
541 case 'l':
542 /* Match hour in 12-hour clock. GNU extension. */
543 case 'I':
544 /* Match hour in 12-hour clock. */
545 get_number (1, 12, 2);
546 tm->tm_hour = val % 12;
547 s.have_I = 1;
548 break;
549 case 'j':
550 /* Match day number of year. */
551 get_number (1, 366, 3);
552 tm->tm_yday = val - 1;
553 s.have_yday = 1;
554 break;
555 case 'm':
556 /* Match number of month. */
557 get_number (1, 12, 2);
558 tm->tm_mon = val - 1;
559 s.have_mon = 1;
560 s.want_xday = 1;
561 break;
562 case 'M':
563 /* Match minute. */
564 get_number (0, 59, 2);
565 tm->tm_min = val;
566 break;
567 case 'n':
568 case 't':
569 /* Match any white space. */
570 while (ISSPACE (*rp))
571 ++rp;
572 break;
573 case 'p':
574 /* Match locale's equivalent of AM/PM. */
575 #ifdef _NL_CURRENT
576 if (s.decided != raw)
578 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
580 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
581 s.decided = loc;
582 s.is_pm = 0;
583 break;
585 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
587 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
588 s.decided = loc;
589 s.is_pm = 1;
590 break;
592 s.decided = raw;
594 #endif
595 if (!match_string (HERE_AM_STR, rp))
597 if (match_string (HERE_PM_STR, rp))
598 s.is_pm = 1;
599 else
600 return NULL;
602 else
603 s.is_pm = 0;
604 break;
605 case 'r':
606 #ifdef _NL_CURRENT
607 if (s.decided != raw)
609 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
611 if (s.decided == loc)
612 return NULL;
613 else
614 rp = rp_backup;
616 else
618 if (s.decided == not &&
619 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
620 HERE_T_FMT_AMPM))
621 s.decided = loc;
622 break;
624 s.decided = raw;
626 #endif
627 if (!recursive (HERE_T_FMT_AMPM))
628 return NULL;
629 break;
630 case 'R':
631 if (!recursive ("%H:%M"))
632 return NULL;
633 break;
634 case 's':
636 /* The number of seconds may be very high so we cannot use
637 the `get_number' macro. Instead read the number
638 character for character and construct the result while
639 doing this. */
640 time_t secs = 0;
641 if (*rp < '0' || *rp > '9')
642 /* We need at least one digit. */
643 return NULL;
647 secs *= 10;
648 secs += *rp++ - '0';
650 while (*rp >= '0' && *rp <= '9');
652 if (localtime_r (&secs, tm) == NULL)
653 /* Error in function. */
654 return NULL;
656 break;
657 case 'S':
658 get_number (0, 61, 2);
659 tm->tm_sec = val;
660 break;
661 case 'X':
662 #ifdef _NL_CURRENT
663 if (s.decided != raw)
665 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
667 if (s.decided == loc)
668 return NULL;
669 else
670 rp = rp_backup;
672 else
674 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
675 s.decided = loc;
676 break;
678 s.decided = raw;
680 #endif
681 /* Fall through. */
682 case 'T':
683 if (!recursive (HERE_T_FMT))
684 return NULL;
685 break;
686 case 'u':
687 get_number (1, 7, 1);
688 tm->tm_wday = val % 7;
689 s.have_wday = 1;
690 break;
691 case 'g':
692 get_number (0, 99, 2);
693 /* XXX This cannot determine any field in TM. */
694 break;
695 case 'G':
696 if (*rp < '0' || *rp > '9')
697 return NULL;
698 /* XXX Ignore the number since we would need some more
699 information to compute a real date. */
701 ++rp;
702 while (*rp >= '0' && *rp <= '9');
703 break;
704 case 'U':
705 get_number (0, 53, 2);
706 s.week_no = val;
707 s.have_uweek = 1;
708 break;
709 case 'W':
710 get_number (0, 53, 2);
711 s.week_no = val;
712 s.have_wweek = 1;
713 break;
714 case 'V':
715 get_number (0, 53, 2);
716 /* XXX This cannot determine any field in TM without some
717 information. */
718 break;
719 case 'w':
720 /* Match number of weekday. */
721 get_number (0, 6, 1);
722 tm->tm_wday = val;
723 s.have_wday = 1;
724 break;
725 case 'y':
726 match_year_in_century:
727 /* Match year within century. */
728 get_number (0, 99, 2);
729 /* The "Year 2000: The Millennium Rollover" paper suggests that
730 values in the range 69-99 refer to the twentieth century. */
731 tm->tm_year = val >= 69 ? val : val + 100;
732 /* Indicate that we want to use the century, if specified. */
733 s.want_century = 1;
734 s.want_xday = 1;
735 break;
736 case 'Y':
737 /* Match year including century number. */
738 get_number (0, 9999, 4);
739 tm->tm_year = val - 1900;
740 s.want_century = 0;
741 s.want_xday = 1;
742 break;
743 case 'Z':
744 /* Read timezone but perform no conversion. */
745 while (ISSPACE (*rp))
746 rp++;
747 while (!ISSPACE (*rp) && *rp != '\0')
748 rp++;
749 break;
750 case 'z':
751 /* We recognize two formats: if two digits are given, these
752 specify hours. If fours digits are used, minutes are
753 also specified. */
755 val = 0;
756 while (ISSPACE (*rp))
757 ++rp;
758 if (*rp != '+' && *rp != '-')
759 return NULL;
760 bool neg = *rp++ == '-';
761 int n = 0;
762 while (n < 4 && *rp >= '0' && *rp <= '9')
764 val = val * 10 + *rp++ - '0';
765 ++n;
767 if (n == 2)
768 val *= 100;
769 else if (n != 4)
770 /* Only two or four digits recognized. */
771 return NULL;
772 else
774 /* We have to convert the minutes into decimal. */
775 if (val % 100 >= 60)
776 return NULL;
777 val = (val / 100) * 100 + ((val % 100) * 50) / 30;
779 if (val > 1200)
780 return NULL;
781 tm->tm_gmtoff = (val * 3600) / 100;
782 if (neg)
783 tm->tm_gmtoff = -tm->tm_gmtoff;
785 break;
786 case 'E':
787 #ifdef _NL_CURRENT
788 switch (*fmt++)
790 case 'c':
791 /* Match locale's alternate date and time format. */
792 if (s.decided != raw)
794 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
796 if (*fmt == '\0')
797 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
799 if (!recursive (fmt))
801 if (s.decided == loc)
802 return NULL;
803 else
804 rp = rp_backup;
806 else
808 if (strcmp (fmt, HERE_D_T_FMT))
809 s.decided = loc;
810 s.want_xday = 1;
811 break;
813 s.decided = raw;
815 /* The C locale has no era information, so use the
816 normal representation. */
817 if (!recursive (HERE_D_T_FMT))
818 return NULL;
819 s.want_xday = 1;
820 break;
821 case 'C':
822 if (s.decided != raw)
824 if (s.era_cnt >= 0)
826 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
827 if (era != NULL && match_string (era->era_name, rp))
829 s.decided = loc;
830 break;
832 else
833 return NULL;
836 num_eras = _NL_CURRENT_WORD (LC_TIME,
837 _NL_TIME_ERA_NUM_ENTRIES);
838 for (s.era_cnt = 0; s.era_cnt < (int) num_eras;
839 ++s.era_cnt, rp = rp_backup)
841 era = _nl_select_era_entry (s.era_cnt
842 HELPER_LOCALE_ARG);
843 if (era != NULL && match_string (era->era_name, rp))
845 s.decided = loc;
846 break;
849 if (s.era_cnt != (int) num_eras)
850 break;
852 s.era_cnt = -1;
853 if (s.decided == loc)
854 return NULL;
856 s.decided = raw;
858 /* The C locale has no era information, so use the
859 normal representation. */
860 goto match_century;
861 case 'y':
862 if (s.decided != raw)
864 get_number(0, 9999, 4);
865 tm->tm_year = val;
866 s.want_era = 1;
867 s.want_xday = 1;
868 s.want_century = 1;
870 if (s.era_cnt >= 0)
872 assert (s.decided == loc);
874 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
875 bool match = false;
876 if (era != NULL)
878 int delta = ((tm->tm_year - era->offset)
879 * era->absolute_direction);
880 match = (delta >= 0
881 && delta < (((int64_t) era->stop_date[0]
882 - (int64_t) era->start_date[0])
883 * era->absolute_direction));
885 if (! match)
886 return NULL;
888 break;
891 num_eras = _NL_CURRENT_WORD (LC_TIME,
892 _NL_TIME_ERA_NUM_ENTRIES);
893 for (s.era_cnt = 0; s.era_cnt < (int) num_eras; ++s.era_cnt)
895 era = _nl_select_era_entry (s.era_cnt
896 HELPER_LOCALE_ARG);
897 if (era != NULL)
899 int delta = ((tm->tm_year - era->offset)
900 * era->absolute_direction);
901 if (delta >= 0
902 && delta < (((int64_t) era->stop_date[0]
903 - (int64_t) era->start_date[0])
904 * era->absolute_direction))
906 s.decided = loc;
907 break;
911 if (s.era_cnt != (int) num_eras)
912 break;
914 s.era_cnt = -1;
915 if (s.decided == loc)
916 return NULL;
918 s.decided = raw;
921 goto match_year_in_century;
922 case 'Y':
923 if (s.decided != raw)
925 num_eras = _NL_CURRENT_WORD (LC_TIME,
926 _NL_TIME_ERA_NUM_ENTRIES);
927 for (s.era_cnt = 0; s.era_cnt < (int) num_eras;
928 ++s.era_cnt, rp = rp_backup)
930 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
931 if (era != NULL && recursive (era->era_format))
932 break;
934 if (s.era_cnt == (int) num_eras)
936 s.era_cnt = -1;
937 if (s.decided == loc)
938 return NULL;
939 else
940 rp = rp_backup;
942 else
944 s.decided = loc;
945 break;
948 s.decided = raw;
950 get_number (0, 9999, 4);
951 tm->tm_year = val - 1900;
952 s.want_century = 0;
953 s.want_xday = 1;
954 break;
955 case 'x':
956 if (s.decided != raw)
958 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
960 if (*fmt == '\0')
961 fmt = _NL_CURRENT (LC_TIME, D_FMT);
963 if (!recursive (fmt))
965 if (s.decided == loc)
966 return NULL;
967 else
968 rp = rp_backup;
970 else
972 if (strcmp (fmt, HERE_D_FMT))
973 s.decided = loc;
974 break;
976 s.decided = raw;
978 if (!recursive (HERE_D_FMT))
979 return NULL;
980 break;
981 case 'X':
982 if (s.decided != raw)
984 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
986 if (*fmt == '\0')
987 fmt = _NL_CURRENT (LC_TIME, T_FMT);
989 if (!recursive (fmt))
991 if (s.decided == loc)
992 return NULL;
993 else
994 rp = rp_backup;
996 else
998 if (strcmp (fmt, HERE_T_FMT))
999 s.decided = loc;
1000 break;
1002 s.decided = raw;
1004 if (!recursive (HERE_T_FMT))
1005 return NULL;
1006 break;
1007 default:
1008 return NULL;
1010 break;
1011 #else
1012 /* We have no information about the era format. Just use
1013 the normal format. */
1014 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
1015 && *fmt != 'x' && *fmt != 'X')
1016 /* This is an illegal format. */
1017 return NULL;
1019 goto start_over;
1020 #endif
1021 case 'O':
1022 switch (*fmt++)
1024 case 'd':
1025 case 'e':
1026 /* Match day of month using alternate numeric symbols. */
1027 get_alt_number (1, 31, 2);
1028 tm->tm_mday = val;
1029 s.have_mday = 1;
1030 s.want_xday = 1;
1031 break;
1032 case 'H':
1033 /* Match hour in 24-hour clock using alternate numeric
1034 symbols. */
1035 get_alt_number (0, 23, 2);
1036 tm->tm_hour = val;
1037 s.have_I = 0;
1038 break;
1039 case 'I':
1040 /* Match hour in 12-hour clock using alternate numeric
1041 symbols. */
1042 get_alt_number (1, 12, 2);
1043 tm->tm_hour = val % 12;
1044 s.have_I = 1;
1045 break;
1046 case 'm':
1047 /* Match month using alternate numeric symbols. */
1048 get_alt_number (1, 12, 2);
1049 tm->tm_mon = val - 1;
1050 s.have_mon = 1;
1051 s.want_xday = 1;
1052 break;
1053 case 'M':
1054 /* Match minutes using alternate numeric symbols. */
1055 get_alt_number (0, 59, 2);
1056 tm->tm_min = val;
1057 break;
1058 case 'S':
1059 /* Match seconds using alternate numeric symbols. */
1060 get_alt_number (0, 61, 2);
1061 tm->tm_sec = val;
1062 break;
1063 case 'U':
1064 get_alt_number (0, 53, 2);
1065 s.week_no = val;
1066 s.have_uweek = 1;
1067 break;
1068 case 'W':
1069 get_alt_number (0, 53, 2);
1070 s.week_no = val;
1071 s.have_wweek = 1;
1072 break;
1073 case 'V':
1074 get_alt_number (0, 53, 2);
1075 /* XXX This cannot determine any field in TM without
1076 further information. */
1077 break;
1078 case 'w':
1079 /* Match number of weekday using alternate numeric symbols. */
1080 get_alt_number (0, 6, 1);
1081 tm->tm_wday = val;
1082 s.have_wday = 1;
1083 break;
1084 case 'y':
1085 /* Match year within century using alternate numeric symbols. */
1086 get_alt_number (0, 99, 2);
1087 tm->tm_year = val >= 69 ? val : val + 100;
1088 s.want_xday = 1;
1089 break;
1090 default:
1091 return NULL;
1093 break;
1094 default:
1095 return NULL;
1099 if (statep != NULL)
1101 /* Recursive invocation, returning success, so
1102 update parent's struct tm and state. */
1103 *(struct __strptime_state *) statep = s;
1104 *tmp = tmb;
1105 return (char *) rp;
1108 if (s.have_I && s.is_pm)
1109 tm->tm_hour += 12;
1111 if (s.century != -1)
1113 if (s.want_century)
1114 tm->tm_year = tm->tm_year % 100 + (s.century - 19) * 100;
1115 else
1116 /* Only the century, but not the year. Strange, but so be it. */
1117 tm->tm_year = (s.century - 19) * 100;
1120 if (s.era_cnt != -1)
1122 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
1123 if (era == NULL)
1124 return NULL;
1125 if (s.want_era)
1126 tm->tm_year = (era->start_date[0]
1127 + ((tm->tm_year - era->offset)
1128 * era->absolute_direction));
1129 else
1130 /* Era start year assumed. */
1131 tm->tm_year = era->start_date[0];
1133 else
1134 if (s.want_era)
1136 /* No era found but we have seen an E modifier. Rectify some
1137 values. */
1138 if (s.want_century && s.century == -1 && tm->tm_year < 69)
1139 tm->tm_year += 100;
1142 if (s.want_xday && !s.have_wday)
1144 if ( !(s.have_mon && s.have_mday) && s.have_yday)
1146 /* We don't have tm_mon and/or tm_mday, compute them. */
1147 int t_mon = 0;
1148 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
1149 t_mon++;
1150 if (!s.have_mon)
1151 tm->tm_mon = t_mon - 1;
1152 if (!s.have_mday)
1153 tm->tm_mday =
1154 (tm->tm_yday
1155 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
1156 s.have_mon = 1;
1157 s.have_mday = 1;
1159 /* Don't crash in day_of_the_week if tm_mon is uninitialized. */
1160 if (s.have_mon || (unsigned) tm->tm_mon <= 11)
1161 day_of_the_week (tm);
1164 if (s.want_xday && !s.have_yday && (s.have_mon || (unsigned) tm->tm_mon <= 11))
1165 day_of_the_year (tm);
1167 if ((s.have_uweek || s.have_wweek) && s.have_wday)
1169 int save_wday = tm->tm_wday;
1170 int save_mday = tm->tm_mday;
1171 int save_mon = tm->tm_mon;
1172 int w_offset = s.have_uweek ? 0 : 1;
1174 tm->tm_mday = 1;
1175 tm->tm_mon = 0;
1176 day_of_the_week (tm);
1177 if (s.have_mday)
1178 tm->tm_mday = save_mday;
1179 if (s.have_mon)
1180 tm->tm_mon = save_mon;
1182 if (!s.have_yday)
1183 tm->tm_yday = ((7 - (tm->tm_wday - w_offset)) % 7
1184 + (s.week_no - 1) * 7
1185 + (save_wday - w_offset + 7) % 7);
1187 if (!s.have_mday || !s.have_mon)
1189 int t_mon = 0;
1190 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon]
1191 <= tm->tm_yday)
1192 t_mon++;
1193 if (!s.have_mon)
1194 tm->tm_mon = t_mon - 1;
1195 if (!s.have_mday)
1196 tm->tm_mday =
1197 (tm->tm_yday
1198 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
1201 tm->tm_wday = save_wday;
1204 return (char *) rp;
1208 char *
1209 strptime (buf, format, tm LOCALE_PARAM)
1210 const char *buf;
1211 const char *format;
1212 struct tm *tm;
1213 LOCALE_PARAM_DECL
1215 return __strptime_internal (buf, format, tm, NULL LOCALE_ARG);
1218 #ifdef _LIBC
1219 weak_alias (__strptime_l, strptime_l)
1220 #endif