strptime.c 23 KB

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