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.

1009 lines
26KB

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