Debian Codegraph @

compat/strptime.c

raw symbols SCIP
1 /* Convert a string representation of time to a time value.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <[email protected]>, 1996.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 3 of the
9 License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 see <http://www.gnu.org/licenses/>. */
19
20 /* XXX This version of the implementation is not really complete.
21 Some of the fields cannot add information alone. But if seeing
22 some of them in the same format (such as year, week and weekday)
23 this is enough information for determining the date. */
24
25 #include <string.h>
26 #include <ctype.h>
27 #include "compat.h"
28
29 #ifndef __P
30 # if defined (__GNUC__) || (defined (__STDC__) && __STDC__)
31 # define __P(args) args
32 # else
33 # define __P(args) ()
34 # endif /* GCC. */
35 #endif /* Not __P. */
36
37 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
38 # ifdef _LIBC
39 # define localtime_r __localtime_r
40 # else
41 /* Approximate localtime_r as best we can in its absence. */
42 # define localtime_r my_localtime_r
43 static struct tm *localtime_r __P ((const time_t *, struct tm *));
44 static struct tm *
45 localtime_r (t, tp)
46 const time_t *t;
47 struct tm *tp;
48 {
49 struct tm *l = localtime (t);
50 if (! l)
51 return 0;
52 *tp = *l;
53 return tp;
54 }
55 # endif /* ! _LIBC */
56 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
57
58
59 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
60 #if defined __GNUC__ && __GNUC__ >= 2
61 # define match_string(cs1, s2) \
62 ({ size_t len = strlen (cs1); \
63 int result = strncasecmp ((cs1), (s2), len) == 0; \
64 if (result) (s2) += len; \
65 result; })
66 #elif defined _MSC_VER
67 # define match_string(cs1, s2) \
68 (_strnicmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
69 #else
70 /* Oh come on. Get a reasonable compiler. */
71 # define match_string(cs1, s2) \
72 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
73 #endif
74 /* We intentionally do not use isdigit() for testing because this will
75 lead to problems with the wide character version. */
76 #define get_number(from, to, n) \
77 do { \
78 int __n = n; \
79 val = 0; \
80 while (*rp == ' ') \
81 ++rp; \
82 if (*rp < '0' || *rp > '9') \
83 return NULL; \
84 do { \
85 val *= 10; \
86 val += *rp++ - '0'; \
87 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
88 if (val < from || val > to) \
89 return NULL; \
90 } while (0)
91 #ifdef _NL_CURRENT
92 # define get_alt_number(from, to, n) \
93 ({ \
94 __label__ do_normal; \
95 if (*decided != raw) \
96 { \
97 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
98 int __n = n; \
99 int any = 0; \
100 while (*rp == ' ') \
101 ++rp; \
102 val = 0; \
103 do { \
104 val *= 10; \
105 while (*alts != '\0') \
106 { \
107 size_t len = strlen (alts); \
108 if (strncasecmp (alts, rp, len) == 0) \
109 break; \
110 alts += len + 1; \
111 ++val; \
112 } \
113 if (*alts == '\0') \
114 { \
115 if (*decided == not && ! any) \
116 goto do_normal; \
117 /* If we haven't read anything it's an error. */ \
118 if (! any) \
119 return NULL; \
120 /* Correct the premature multiplication. */ \
121 val /= 10; \
122 break; \
123 } \
124 else \
125 *decided = loc; \
126 } while (--__n > 0 && val * 10 <= to); \
127 if (val < from || val > to) \
128 return NULL; \
129 } \
130 else \
131 { \
132 do_normal: \
133 get_number (from, to, n); \
134 } \
135 0; \
136 })
137 #else
138 # define get_alt_number(from, to, n) \
139 /* We don't have the alternate representation. */ \
140 get_number(from, to, n)
141 #endif
142 #define recursive(new_fmt) \
143 (*(new_fmt) != '\0' \
144 && (rp = strptime_internal (rp, (new_fmt), tm, decided, era_cnt)) != NULL)
145
146
147 #ifdef _LIBC
148 /* This is defined in locale/C-time.c in the GNU libc. */
149 extern const struct locale_data _nl_C_LC_TIME;
150 extern const unsigned short int __mon_yday[2][13];
151
152 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
153 # define ab_weekday_name \
154 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
155 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
156 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
157 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
158 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
159 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
160 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
161 # define HERE_T_FMT_AMPM \
162 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
163 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
164
165 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
166 #else
167 static char const weekday_name[][10] =
168 {
169 "Sunday", "Monday", "Tuesday", "Wednesday",
170 "Thursday", "Friday", "Saturday"
171 };
172 static char const ab_weekday_name[][4] =
173 {
174 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
175 };
176 static char const month_name[][10] =
177 {
178 "January", "February", "March", "April", "May", "June",
179 "July", "August", "September", "October", "November", "December"
180 };
181 static char const ab_month_name[][4] =
182 {
183 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
184 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
185 };
186 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
187 # define HERE_D_FMT "%m/%d/%y"
188 # define HERE_AM_STR "AM"
189 # define HERE_PM_STR "PM"
190 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
191 # define HERE_T_FMT "%H:%M:%S"
192
193 static const unsigned short int __mon_yday[2][13] =
194 {
195 /* Normal years. */
196 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
197 /* Leap years. */
198 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
199 };
200 #endif
201
202 /* Status of lookup: do we use the locale data or the raw data? */
203 enum locale_status { not, loc, raw };
204
205
206 #ifndef __isleap
207 /* Nonzero if YEAR is a leap year (every 4 years,
208 except every 100th isn't, and every 400th is). */
209 # define __isleap(year) \
210 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
211 #endif
212
213 /* Compute the day of the week. */
214 static void
215 day_of_the_week (struct tm *tm)
216 {
217 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
218 the difference between this data in the one on TM and so determine
219 the weekday. */
220 int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
221 int wday = (-473
222 + (365 * (tm->tm_year - 70))
223 + (corr_year / 4)
224 - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
225 + (((corr_year / 4) / 25) / 4)
226 + __mon_yday[0][tm->tm_mon]
227 + tm->tm_mday - 1);
228 tm->tm_wday = ((wday % 7) + 7) % 7;
229 }
230
231 /* Compute the day of the year. */
232 static void
233 day_of_the_year (struct tm *tm)
234 {
235 tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
236 + (tm->tm_mday - 1));
237 }
238
239 static char *
240 #ifdef _LIBC
241 internal_function
242 #endif
243 strptime_internal __P ((const char *rp, const char *fmt, struct tm *tm,
244 enum locale_status *decided, int era_cnt));
245
246 static char *
247 #ifdef _LIBC
248 internal_function
249 #endif
250 strptime_internal (rp, fmt, tm, decided, era_cnt)
251 const char *rp;
252 const char *fmt;
253 struct tm *tm;
254 enum locale_status *decided;
255 int era_cnt;
256 {
257 int cnt;
258 size_t val;
259 int have_I, is_pm;
260 int century, want_century;
261 int want_era;
262 int have_wday, want_xday;
263 int have_yday;
264 int have_mon, have_mday;
265 #ifdef _NL_CURRENT
266 const char *rp_backup;
267 size_t num_eras;
268 struct era_entry *era;
269
270 era = NULL;
271 #endif
272
273 have_I = is_pm = 0;
274 century = -1;
275 want_century = 0;
276 want_era = 0;
277
278 have_wday = want_xday = have_yday = have_mon = have_mday = 0;
279
280 while (*fmt != '\0')
281 {
282 /* A white space in the format string matches 0 more or white
283 space in the input string. */
284 if (isspace (*fmt))
285 {
286 while (isspace (*rp))
287 ++rp;
288 ++fmt;
289 continue;
290 }
291
292 /* Any character but `%' must be matched by the same character
293 in the iput string. */
294 if (*fmt != '%')
295 {
296 match_char (*fmt++, *rp++);
297 continue;
298 }
299
300 ++fmt;
301 #ifndef _NL_CURRENT
302 /* We need this for handling the `E' modifier. */
303 start_over:
304 #endif
305
306 #ifdef _NL_CURRENT
307 /* Make back up of current processing pointer. */
308 rp_backup = rp;
309 #endif
310
311 switch (*fmt++)
312 {
313 case '%':
314 /* Match the `%' character itself. */
315 match_char ('%', *rp++);
316 break;
317 case 'a':
318 case 'A':
319 /* Match day of week. */
320 for (cnt = 0; cnt < 7; ++cnt)
321 {
322 #ifdef _NL_CURRENT
323 if (*decided !=raw)
324 {
325 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), rp))
326 {
327 if (*decided == not
328 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
329 weekday_name[cnt]))
330 *decided = loc;
331 break;
332 }
333 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), rp))
334 {
335 if (*decided == not
336 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
337 ab_weekday_name[cnt]))
338 *decided = loc;
339 break;
340 }
341 }
342 #endif
343 if (*decided != loc
344 && (match_string (weekday_name[cnt], rp)
345 || match_string (ab_weekday_name[cnt], rp)))
346 {
347 *decided = raw;
348 break;
349 }
350 }
351 if (cnt == 7)
352 /* Does not match a weekday name. */
353 return NULL;
354 tm->tm_wday = cnt;
355 have_wday = 1;
356 break;
357 case 'b':
358 case 'B':
359 case 'h':
360 /* Match month name. */
361 for (cnt = 0; cnt < 12; ++cnt)
362 {
363 #ifdef _NL_CURRENT
364 if (*decided !=raw)
365 {
366 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), rp))
367 {
368 if (*decided == not
369 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
370 month_name[cnt]))
371 *decided = loc;
372 break;
373 }
374 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), rp))
375 {
376 if (*decided == not
377 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
378 ab_month_name[cnt]))
379 *decided = loc;
380 break;
381 }
382 }
383 #endif
384 if (match_string (month_name[cnt], rp)
385 || match_string (ab_month_name[cnt], rp))
386 {
387 *decided = raw;
388 break;
389 }
390 }
391 if (cnt == 12)
392 /* Does not match a month name. */
393 return NULL;
394 tm->tm_mon = cnt;
395 want_xday = 1;
396 break;
397 case 'c':
398 /* Match locale's date and time format. */
399 #ifdef _NL_CURRENT
400 if (*decided != raw)
401 {
402 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
403 {
404 if (*decided == loc)
405 return NULL;
406 else
407 rp = rp_backup;
408 }
409 else
410 {
411 if (*decided == not &&
412 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
413 *decided = loc;
414 want_xday = 1;
415 break;
416 }
417 *decided = raw;
418 }
419 #endif
420 if (!recursive (HERE_D_T_FMT))
421 return NULL;
422 want_xday = 1;
423 break;
424 case 'C':
425 /* Match century number. */
426 #ifdef _NL_CURRENT
427 match_century:
428 #endif
429 get_number (0, 99, 2);
430 century = val;
431 want_xday = 1;
432 break;
433 case 'd':
434 case 'e':
435 /* Match day of month. */
436 get_number (1, 31, 2);
437 tm->tm_mday = val;
438 have_mday = 1;
439 want_xday = 1;
440 break;
441 case 'F':
442 if (!recursive ("%Y-%m-%d"))
443 return NULL;
444 want_xday = 1;
445 break;
446 case 'x':
447 #ifdef _NL_CURRENT
448 if (*decided != raw)
449 {
450 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
451 {
452 if (*decided == loc)
453 return NULL;
454 else
455 rp = rp_backup;
456 }
457 else
458 {
459 if (*decided == not
460 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
461 *decided = loc;
462 want_xday = 1;
463 break;
464 }
465 *decided = raw;
466 }
467 #endif
468 /* Fall through. */
469 case 'D':
470 /* Match standard day format. */
471 if (!recursive (HERE_D_FMT))
472 return NULL;
473 want_xday = 1;
474 break;
475 case 'k':
476 case 'H':
477 /* Match hour in 24-hour clock. */
478 get_number (0, 23, 2);
479 tm->tm_hour = val;
480 have_I = 0;
481 break;
482 case 'I':
483 /* Match hour in 12-hour clock. */
484 get_number (1, 12, 2);
485 tm->tm_hour = val % 12;
486 have_I = 1;
487 break;
488 case 'j':
489 /* Match day number of year. */
490 get_number (1, 366, 3);
491 tm->tm_yday = val - 1;
492 have_yday = 1;
493 break;
494 case 'm':
495 /* Match number of month. */
496 get_number (1, 12, 2);
497 tm->tm_mon = val - 1;
498 have_mon = 1;
499 want_xday = 1;
500 break;
501 case 'M':
502 /* Match minute. */
503 get_number (0, 59, 2);
504 tm->tm_min = val;
505 break;
506 case 'n':
507 case 't':
508 /* Match any white space. */
509 while (isspace (*rp))
510 ++rp;
511 break;
512 case 'p':
513 /* Match locale's equivalent of AM/PM. */
514 #ifdef _NL_CURRENT
515 if (*decided != raw)
516 {
517 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
518 {
519 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
520 *decided = loc;
521 break;
522 }
523 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
524 {
525 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
526 *decided = loc;
527 is_pm = 1;
528 break;
529 }
530 *decided = raw;
531 }
532 #endif
533 if (!match_string (HERE_AM_STR, rp)) {
534 if (match_string (HERE_PM_STR, rp)) {
535 is_pm = 1;
536 } else {
537 return NULL;
538 }
539 }
540 break;
541 case 'r':
542 #ifdef _NL_CURRENT
543 if (*decided != raw)
544 {
545 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
546 {
547 if (*decided == loc)
548 return NULL;
549 else
550 rp = rp_backup;
551 }
552 else
553 {
554 if (*decided == not &&
555 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
556 HERE_T_FMT_AMPM))
557 *decided = loc;
558 break;
559 }
560 *decided = raw;
561 }
562 #endif
563 if (!recursive (HERE_T_FMT_AMPM))
564 return NULL;
565 break;
566 case 'R':
567 if (!recursive ("%H:%M"))
568 return NULL;
569 break;
570 case 's':
571 {
572 /* The number of seconds may be very high so we cannot use
573 the `get_number' macro. Instead read the number
574 character for character and construct the result while
575 doing this. */
576 time_t secs = 0;
577 if (*rp < '0' || *rp > '9')
578 /* We need at least one digit. */
579 return NULL;
580
581 do
582 {
583 secs *= 10;
584 secs += *rp++ - '0';
585 }
586 while (*rp >= '0' && *rp <= '9');
587
588 if (localtime_r (&secs, tm) == NULL)
589 /* Error in function. */
590 return NULL;
591 }
592 break;
593 case 'S':
594 get_number (0, 61, 2);
595 tm->tm_sec = val;
596 break;
597 case 'X':
598 #ifdef _NL_CURRENT
599 if (*decided != raw)
600 {
601 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
602 {
603 if (*decided == loc)
604 return NULL;
605 else
606 rp = rp_backup;
607 }
608 else
609 {
610 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
611 *decided = loc;
612 break;
613 }
614 *decided = raw;
615 }
616 #endif
617 /* Fall through. */
618 case 'T':
619 if (!recursive (HERE_T_FMT))
620 return NULL;
621 break;
622 case 'u':
623 get_number (1, 7, 1);
624 tm->tm_wday = val % 7;
625 have_wday = 1;
626 break;
627 case 'g':
628 get_number (0, 99, 2);
629 /* XXX This cannot determine any field in TM. */
630 break;
631 case 'G':
632 if (*rp < '0' || *rp > '9')
633 return NULL;
634 /* XXX Ignore the number since we would need some more
635 information to compute a real date. */
636 do
637 ++rp;
638 while (*rp >= '0' && *rp <= '9');
639 break;
640 case 'U':
641 case 'V':
642 case 'W':
643 get_number (0, 53, 2);
644 /* XXX This cannot determine any field in TM without some
645 information. */
646 break;
647 case 'w':
648 /* Match number of weekday. */
649 get_number (0, 6, 1);
650 tm->tm_wday = val;
651 have_wday = 1;
652 break;
653 case 'y':
654 #ifdef _NL_CURRENT
655 match_year_in_century:
656 #endif
657 /* Match year within century. */
658 get_number (0, 99, 2);
659 /* The "Year 2000: The Millennium Rollover" paper suggests that
660 values in the range 69-99 refer to the twentieth century. */
661 tm->tm_year = val >= 69 ? val : val + 100;
662 /* Indicate that we want to use the century, if specified. */
663 want_century = 1;
664 want_xday = 1;
665 break;
666 case 'Y':
667 /* Match year including century number. */
668 get_number (0, 9999, 4);
669 tm->tm_year = val - 1900;
670 want_century = 0;
671 want_xday = 1;
672 break;
673 case 'Z':
674 /* XXX How to handle this? */
675 break;
676 case 'E':
677 #ifdef _NL_CURRENT
678 switch (*fmt++)
679 {
680 case 'c':
681 /* Match locale's alternate date and time format. */
682 if (*decided != raw)
683 {
684 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
685
686 if (*fmt == '\0')
687 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
688
689 if (!recursive (fmt))
690 {
691 if (*decided == loc)
692 return NULL;
693 else
694 rp = rp_backup;
695 }
696 else
697 {
698 if (strcmp (fmt, HERE_D_T_FMT))
699 *decided = loc;
700 want_xday = 1;
701 break;
702 }
703 *decided = raw;
704 }
705 /* The C locale has no era information, so use the
706 normal representation. */
707 if (!recursive (HERE_D_T_FMT))
708 return NULL;
709 want_xday = 1;
710 break;
711 case 'C':
712 if (*decided != raw)
713 {
714 if (era_cnt >= 0)
715 {
716 era = _nl_select_era_entry (era_cnt);
717 if (match_string (era->era_name, rp))
718 {
719 *decided = loc;
720 break;
721 }
722 else
723 return NULL;
724 }
725 else
726 {
727 num_eras = _NL_CURRENT_WORD (LC_TIME,
728 _NL_TIME_ERA_NUM_ENTRIES);
729 for (era_cnt = 0; era_cnt < (int) num_eras;
730 ++era_cnt, rp = rp_backup)
731 {
732 era = _nl_select_era_entry (era_cnt);
733 if (match_string (era->era_name, rp))
734 {
735 *decided = loc;
736 break;
737 }
738 }
739 if (era_cnt == (int) num_eras)
740 {
741 era_cnt = -1;
742 if (*decided == loc)
743 return NULL;
744 }
745 else
746 break;
747 }
748
749 *decided = raw;
750 }
751 /* The C locale has no era information, so use the
752 normal representation. */
753 goto match_century;
754 case 'y':
755 if (*decided == raw)
756 goto match_year_in_century;
757
758 get_number(0, 9999, 4);
759 tm->tm_year = val;
760 want_era = 1;
761 want_xday = 1;
762 break;
763 case 'Y':
764 if (*decided != raw)
765 {
766 num_eras = _NL_CURRENT_WORD (LC_TIME,
767 _NL_TIME_ERA_NUM_ENTRIES);
768 for (era_cnt = 0; era_cnt < (int) num_eras;
769 ++era_cnt, rp = rp_backup)
770 {
771 era = _nl_select_era_entry (era_cnt);
772 if (recursive (era->era_format))
773 break;
774 }
775 if (era_cnt == (int) num_eras)
776 {
777 era_cnt = -1;
778 if (*decided == loc)
779 return NULL;
780 else
781 rp = rp_backup;
782 }
783 else
784 {
785 *decided = loc;
786 era_cnt = -1;
787 break;
788 }
789
790 *decided = raw;
791 }
792 get_number (0, 9999, 4);
793 tm->tm_year = val - 1900;
794 want_century = 0;
795 want_xday = 1;
796 break;
797 case 'x':
798 if (*decided != raw)
799 {
800 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
801
802 if (*fmt == '\0')
803 fmt = _NL_CURRENT (LC_TIME, D_FMT);
804
805 if (!recursive (fmt))
806 {
807 if (*decided == loc)
808 return NULL;
809 else
810 rp = rp_backup;
811 }
812 else
813 {
814 if (strcmp (fmt, HERE_D_FMT))
815 *decided = loc;
816 break;
817 }
818 *decided = raw;
819 }
820 if (!recursive (HERE_D_FMT))
821 return NULL;
822 break;
823 case 'X':
824 if (*decided != raw)
825 {
826 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
827
828 if (*fmt == '\0')
829 fmt = _NL_CURRENT (LC_TIME, T_FMT);
830
831 if (!recursive (fmt))
832 {
833 if (*decided == loc)
834 return NULL;
835 else
836 rp = rp_backup;
837 }
838 else
839 {
840 if (strcmp (fmt, HERE_T_FMT))
841 *decided = loc;
842 break;
843 }
844 *decided = raw;
845 }
846 if (!recursive (HERE_T_FMT))
847 return NULL;
848 break;
849 default:
850 return NULL;
851 }
852 break;
853 #else
854 /* We have no information about the era format. Just use
855 the normal format. */
856 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
857 && *fmt != 'x' && *fmt != 'X')
858 /* This is an illegal format. */
859 return NULL;
860
861 goto start_over;
862 #endif
863 case 'O':
864 switch (*fmt++)
865 {
866 case 'd':
867 case 'e':
868 /* Match day of month using alternate numeric symbols. */
869 get_alt_number (1, 31, 2);
870 tm->tm_mday = val;
871 have_mday = 1;
872 want_xday = 1;
873 break;
874 case 'H':
875 /* Match hour in 24-hour clock using alternate numeric
876 symbols. */
877 get_alt_number (0, 23, 2);
878 tm->tm_hour = val;
879 have_I = 0;
880 break;
881 case 'I':
882 /* Match hour in 12-hour clock using alternate numeric
883 symbols. */
884 get_alt_number (1, 12, 2);
885 tm->tm_hour = val - 1;
886 have_I = 1;
887 break;
888 case 'm':
889 /* Match month using alternate numeric symbols. */
890 get_alt_number (1, 12, 2);
891 tm->tm_mon = val - 1;
892 have_mon = 1;
893 want_xday = 1;
894 break;
895 case 'M':
896 /* Match minutes using alternate numeric symbols. */
897 get_alt_number (0, 59, 2);
898 tm->tm_min = val;
899 break;
900 case 'S':
901 /* Match seconds using alternate numeric symbols. */
902 get_alt_number (0, 61, 2);
903 tm->tm_sec = val;
904 break;
905 case 'U':
906 case 'V':
907 case 'W':
908 get_alt_number (0, 53, 2);
909 /* XXX This cannot determine any field in TM without
910 further information. */
911 break;
912 case 'w':
913 /* Match number of weekday using alternate numeric symbols. */
914 get_alt_number (0, 6, 1);
915 tm->tm_wday = val;
916 have_wday = 1;
917 break;
918 case 'y':
919 /* Match year within century using alternate numeric symbols. */
920 get_alt_number (0, 99, 2);
921 tm->tm_year = val >= 69 ? val : val + 100;
922 want_xday = 1;
923 break;
924 default:
925 return NULL;
926 }
927 break;
928 default:
929 return NULL;
930 }
931 }
932
933 if (have_I && is_pm)
934 tm->tm_hour += 12;
935
936 if (century != -1)
937 {
938 if (want_century)
939 tm->tm_year = tm->tm_year % 100 + (century - 19) * 100;
940 else
941 /* Only the century, but not the year. Strange, but so be it. */
942 tm->tm_year = (century - 19) * 100;
943 }
944
945 #ifdef _NL_CURRENT
946 if (era_cnt != -1)
947 {
948 era = _nl_select_era_entry(era_cnt);
949 if (want_era)
950 tm->tm_year = (era->start_date[0]
951 + ((tm->tm_year - era->offset)
952 * era->absolute_direction));
953 else
954 /* Era start year assumed. */
955 tm->tm_year = era->start_date[0];
956 }
957 else
958 #endif
959 if (want_era)
960 return NULL;
961
962 if (want_xday && !have_wday)
963 {
964 if ( !(have_mon && have_mday) && have_yday)
965 {
966 /* We don't have tm_mon and/or tm_mday, compute them. */
967 int t_mon = 0;
968 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
969 t_mon++;
970 if (!have_mon)
971 tm->tm_mon = t_mon - 1;
972 if (!have_mday)
973 tm->tm_mday =
974 (tm->tm_yday
975 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
976 }
977 day_of_the_week (tm);
978 }
979 if (want_xday && !have_yday)
980 day_of_the_year (tm);
981
982 return rp;
983 }
984
985
986 char *strptime(const char *buf, const char *format, struct tm *tm)
987 {
988 enum locale_status decided;
989
990 #ifdef _NL_CURRENT
991 decided = not;
992 #else
993 decided = raw;
994 #endif
995 return strptime_internal (buf, format, tm, &decided, -1);
996 }