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.

1005 lines
24KB

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