strptime.h 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. #ifndef __P
  32. # if defined (__GNUC__) || (defined (__STDC__) && __STDC__)
  33. # define __P(args) args
  34. # else
  35. # define __P(args) ()
  36. # endif /* GCC. */
  37. #endif /* Not __P. */
  38. #if ! HAVE_LOCALTIME_R && ! defined localtime_r
  39. # ifdef _LIBC
  40. # define localtime_r __localtime_r
  41. # else
  42. /* Approximate localtime_r as best we can in its absence. */
  43. # define localtime_r my_localtime_r
  44. static struct tm *localtime_r __P ((const time_t *, struct tm *));
  45. static struct tm *
  46. localtime_r (t, tp)
  47. const time_t *t;
  48. struct tm *tp;
  49. {
  50. struct tm *l = localtime (t);
  51. if (! l)
  52. return 0;
  53. *tp = *l;
  54. return tp;
  55. }
  56. # endif /* ! _LIBC */
  57. #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
  58. #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
  59. #if defined __GNUC__ && __GNUC__ >= 2
  60. # define match_string(cs1, s2) \
  61. ({ size_t len = strlen (cs1); \
  62. int result = strncasecmp ((cs1), (s2), len) == 0; \
  63. if (result) (s2) += len; \
  64. result; })
  65. #else
  66. /* Oh come on. Get a reasonable compiler. */
  67. # define match_string(cs1, s2) \
  68. (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
  69. #endif
  70. /* We intentionally do not use isdigit() for testing because this will
  71. lead to problems with the wide character version. */
  72. #define get_number(from, to, n) \
  73. do { \
  74. int __n = n; \
  75. val = 0; \
  76. while (*rp == ' ') \
  77. ++rp; \
  78. if (*rp < '0' || *rp > '9') \
  79. return NULL; \
  80. do { \
  81. val *= 10; \
  82. val += *rp++ - '0'; \
  83. } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
  84. if (val < from || val > to) \
  85. return NULL; \
  86. } while (0)
  87. #ifdef _NL_CURRENT
  88. # define get_alt_number(from, to, n) \
  89. ({ \
  90. __label__ do_normal; \
  91. if (*decided != raw) \
  92. { \
  93. const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
  94. int __n = n; \
  95. int any = 0; \
  96. while (*rp == ' ') \
  97. ++rp; \
  98. val = 0; \
  99. do { \
  100. val *= 10; \
  101. while (*alts != '\0') \
  102. { \
  103. size_t len = strlen (alts); \
  104. if (strncasecmp (alts, rp, len) == 0) \
  105. break; \
  106. alts += len + 1; \
  107. ++val; \
  108. } \
  109. if (*alts == '\0') \
  110. { \
  111. if (*decided == not && ! any) \
  112. goto do_normal; \
  113. /* If we haven't read anything it's an error. */ \
  114. if (! any) \
  115. return NULL; \
  116. /* Correct the premature multiplication. */ \
  117. val /= 10; \
  118. break; \
  119. } \
  120. else \
  121. *decided = loc; \
  122. } while (--__n > 0 && val * 10 <= to); \
  123. if (val < from || val > to) \
  124. return NULL; \
  125. } \
  126. else \
  127. { \
  128. do_normal: \
  129. get_number (from, to, n); \
  130. } \
  131. 0; \
  132. })
  133. #else
  134. # define get_alt_number(from, to, n) \
  135. /* We don't have the alternate representation. */ \
  136. get_number(from, to, n)
  137. #endif
  138. #define recursive(new_fmt) \
  139. (*(new_fmt) != '\0' \
  140. && (rp = strptime_internal (rp, (new_fmt), tm, decided, era_cnt)) != NULL)
  141. #ifdef _LIBC
  142. /* This is defined in locale/C-time.c in the GNU libc. */
  143. extern const struct locale_data _nl_C_LC_TIME;
  144. extern const unsigned short int __mon_yday[2][13];
  145. # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
  146. # define ab_weekday_name \
  147. (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
  148. # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
  149. # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
  150. # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
  151. # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
  152. # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
  153. # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
  154. # define HERE_T_FMT_AMPM \
  155. (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
  156. # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
  157. # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
  158. #else
  159. static char const weekday_name[][10] =
  160. {
  161. "Sunday", "Monday", "Tuesday", "Wednesday",
  162. "Thursday", "Friday", "Saturday"
  163. };
  164. static char const ab_weekday_name[][4] =
  165. {
  166. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  167. };
  168. static char const month_name[][10] =
  169. {
  170. "January", "February", "March", "April", "May", "June",
  171. "July", "August", "September", "October", "November", "December"
  172. };
  173. static char const ab_month_name[][4] =
  174. {
  175. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  176. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  177. };
  178. # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
  179. # define HERE_D_FMT "%m/%d/%y"
  180. # define HERE_AM_STR "AM"
  181. # define HERE_PM_STR "PM"
  182. # define HERE_T_FMT_AMPM "%I:%M:%S %p"
  183. # define HERE_T_FMT "%H:%M:%S"
  184. const unsigned short int __mon_yday[2][13] =
  185. {
  186. /* Normal years. */
  187. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  188. /* Leap years. */
  189. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  190. };
  191. #endif
  192. /* Status of lookup: do we use the locale data or the raw data? */
  193. enum locale_status { not, loc, raw };
  194. #ifndef __isleap
  195. /* Nonzero if YEAR is a leap year (every 4 years,
  196. except every 100th isn't, and every 400th is). */
  197. # define __isleap(year) \
  198. ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  199. #endif
  200. /* Compute the day of the week. */
  201. static void
  202. day_of_the_week (struct tm *tm)
  203. {
  204. /* We know that January 1st 1970 was a Thursday (= 4). Compute the
  205. the difference between this data in the one on TM and so determine
  206. the weekday. */
  207. int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
  208. int wday = (-473
  209. + (365 * (tm->tm_year - 70))
  210. + (corr_year / 4)
  211. - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
  212. + (((corr_year / 4) / 25) / 4)
  213. + __mon_yday[0][tm->tm_mon]
  214. + tm->tm_mday - 1);
  215. tm->tm_wday = ((wday % 7) + 7) % 7;
  216. }
  217. /* Compute the day of the year. */
  218. static void
  219. day_of_the_year (struct tm *tm)
  220. {
  221. tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
  222. + (tm->tm_mday - 1));
  223. }
  224. static char *
  225. #ifdef _LIBC
  226. internal_function
  227. #endif
  228. strptime_internal __P ((const char *rp, const char *fmt, struct tm *tm,
  229. enum locale_status *decided, int era_cnt));
  230. static char *
  231. #ifdef _LIBC
  232. internal_function
  233. #endif
  234. strptime_internal (rp, fmt, tm, decided, era_cnt)
  235. const char *rp;
  236. const char *fmt;
  237. struct tm *tm;
  238. enum locale_status *decided;
  239. int era_cnt;
  240. {
  241. const char *rp_backup;
  242. int cnt;
  243. size_t val;
  244. int have_I, is_pm;
  245. int century, want_century;
  246. int want_era;
  247. int have_wday, want_xday;
  248. int have_yday;
  249. int have_mon, have_mday;
  250. #ifdef _NL_CURRENT
  251. size_t num_eras;
  252. #endif
  253. struct era_entry *era;
  254. have_I = is_pm = 0;
  255. century = -1;
  256. want_century = 0;
  257. want_era = 0;
  258. era = NULL;
  259. have_wday = want_xday = have_yday = have_mon = have_mday = 0;
  260. while (*fmt != '\0')
  261. {
  262. /* A white space in the format string matches 0 more or white
  263. space in the input string. */
  264. if (isspace (*fmt))
  265. {
  266. while (isspace (*rp))
  267. ++rp;
  268. ++fmt;
  269. continue;
  270. }
  271. /* Any character but `%' must be matched by the same character
  272. in the input string. */
  273. if (*fmt != '%')
  274. {
  275. match_char (*fmt++, *rp++);
  276. continue;
  277. }
  278. ++fmt;
  279. #ifndef _NL_CURRENT
  280. /* We need this for handling the `E' modifier. */
  281. start_over:
  282. #endif
  283. /* Make back up of current processing pointer. */
  284. rp_backup = rp;
  285. switch (*fmt++)
  286. {
  287. case '%':
  288. /* Match the `%' character itself. */
  289. match_char ('%', *rp++);
  290. break;
  291. case 'a':
  292. case 'A':
  293. /* Match day of week. */
  294. for (cnt = 0; cnt < 7; ++cnt)
  295. {
  296. #ifdef _NL_CURRENT
  297. if (*decided !=raw)
  298. {
  299. if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), rp))
  300. {
  301. if (*decided == not
  302. && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
  303. weekday_name[cnt]))
  304. *decided = loc;
  305. break;
  306. }
  307. if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), rp))
  308. {
  309. if (*decided == not
  310. && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
  311. ab_weekday_name[cnt]))
  312. *decided = loc;
  313. break;
  314. }
  315. }
  316. #endif
  317. if (*decided != loc
  318. && (match_string (weekday_name[cnt], rp)
  319. || match_string (ab_weekday_name[cnt], rp)))
  320. {
  321. *decided = raw;
  322. break;
  323. }
  324. }
  325. if (cnt == 7)
  326. /* Does not match a weekday name. */
  327. return NULL;
  328. tm->tm_wday = cnt;
  329. have_wday = 1;
  330. break;
  331. case 'b':
  332. case 'B':
  333. case 'h':
  334. /* Match month name. */
  335. for (cnt = 0; cnt < 12; ++cnt)
  336. {
  337. #ifdef _NL_CURRENT
  338. if (*decided !=raw)
  339. {
  340. if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), rp))
  341. {
  342. if (*decided == not
  343. && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
  344. month_name[cnt]))
  345. *decided = loc;
  346. break;
  347. }
  348. if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), rp))
  349. {
  350. if (*decided == not
  351. && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
  352. ab_month_name[cnt]))
  353. *decided = loc;
  354. break;
  355. }
  356. }
  357. #endif
  358. if (match_string (month_name[cnt], rp)
  359. || match_string (ab_month_name[cnt], rp))
  360. {
  361. *decided = raw;
  362. break;
  363. }
  364. }
  365. if (cnt == 12)
  366. /* Does not match a month name. */
  367. return NULL;
  368. tm->tm_mon = cnt;
  369. want_xday = 1;
  370. break;
  371. case 'c':
  372. /* Match locale's date and time format. */
  373. #ifdef _NL_CURRENT
  374. if (*decided != raw)
  375. {
  376. if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
  377. {
  378. if (*decided == loc)
  379. return NULL;
  380. else
  381. rp = rp_backup;
  382. }
  383. else
  384. {
  385. if (*decided == not &&
  386. strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
  387. *decided = loc;
  388. want_xday = 1;
  389. break;
  390. }
  391. *decided = raw;
  392. }
  393. #endif
  394. if (!recursive (HERE_D_T_FMT))
  395. return NULL;
  396. want_xday = 1;
  397. break;
  398. case 'C':
  399. /* Match century number. */
  400. #ifdef _NL_CURRENT
  401. match_century:
  402. #endif
  403. get_number (0, 99, 2);
  404. century = val;
  405. want_xday = 1;
  406. break;
  407. case 'd':
  408. case 'e':
  409. /* Match day of month. */
  410. get_number (1, 31, 2);
  411. tm->tm_mday = val;
  412. have_mday = 1;
  413. want_xday = 1;
  414. break;
  415. case 'F':
  416. if (!recursive ("%Y-%m-%d"))
  417. return NULL;
  418. want_xday = 1;
  419. break;
  420. case 'x':
  421. #ifdef _NL_CURRENT
  422. if (*decided != raw)
  423. {
  424. if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
  425. {
  426. if (*decided == loc)
  427. return NULL;
  428. else
  429. rp = rp_backup;
  430. }
  431. else
  432. {
  433. if (*decided == not
  434. && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
  435. *decided = loc;
  436. want_xday = 1;
  437. break;
  438. }
  439. *decided = raw;
  440. }
  441. #endif
  442. /* Fall through. */
  443. case 'D':
  444. /* Match standard day format. */
  445. if (!recursive (HERE_D_FMT))
  446. return NULL;
  447. want_xday = 1;
  448. break;
  449. case 'k':
  450. case 'H':
  451. /* Match hour in 24-hour clock. */
  452. get_number (0, 23, 2);
  453. tm->tm_hour = val;
  454. have_I = 0;
  455. break;
  456. case 'I':
  457. /* Match hour in 12-hour clock. */
  458. get_number (1, 12, 2);
  459. tm->tm_hour = val % 12;
  460. have_I = 1;
  461. break;
  462. case 'j':
  463. /* Match day number of year. */
  464. get_number (1, 366, 3);
  465. tm->tm_yday = val - 1;
  466. have_yday = 1;
  467. break;
  468. case 'm':
  469. /* Match number of month. */
  470. get_number (1, 12, 2);
  471. tm->tm_mon = val - 1;
  472. have_mon = 1;
  473. want_xday = 1;
  474. break;
  475. case 'M':
  476. /* Match minute. */
  477. get_number (0, 59, 2);
  478. tm->tm_min = val;
  479. break;
  480. case 'n':
  481. case 't':
  482. /* Match any white space. */
  483. while (isspace (*rp))
  484. ++rp;
  485. break;
  486. case 'p':
  487. /* Match locale's equivalent of AM/PM. */
  488. #ifdef _NL_CURRENT
  489. if (*decided != raw)
  490. {
  491. if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
  492. {
  493. if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
  494. *decided = loc;
  495. break;
  496. }
  497. if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
  498. {
  499. if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
  500. *decided = loc;
  501. is_pm = 1;
  502. break;
  503. }
  504. *decided = raw;
  505. }
  506. #endif
  507. if (!match_string (HERE_AM_STR, rp)) {
  508. if (match_string (HERE_PM_STR, rp))
  509. is_pm = 1;
  510. else
  511. return NULL;
  512. }
  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. }