You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

984 lines
23KB

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