strptime.c 23 KB

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