strptime_l.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /* Copyright (C) 2002-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #ifdef HAVE_CONFIG_H
  15. # include <config.h>
  16. #endif
  17. #include <assert.h>
  18. #include <ctype.h>
  19. #include <langinfo.h>
  20. #include <limits.h>
  21. #include <string.h>
  22. #include <time.h>
  23. #include <stdbool.h>
  24. #ifdef _LIBC
  25. # define HAVE_LOCALTIME_R 0
  26. # include "../locale/localeinfo.h"
  27. #endif
  28. #if ! HAVE_LOCALTIME_R && ! defined localtime_r
  29. # ifdef _LIBC
  30. # define localtime_r __localtime_r
  31. # else
  32. /* Approximate localtime_r as best we can in its absence. */
  33. # define localtime_r my_localtime_r
  34. static struct tm *localtime_r (const time_t *, struct tm *);
  35. static struct tm *
  36. localtime_r (const time_t *t, struct tm *tp)
  37. {
  38. struct tm *l = localtime (t);
  39. if (! l)
  40. return 0;
  41. *tp = *l;
  42. return tp;
  43. }
  44. # endif /* ! _LIBC */
  45. #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
  46. #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
  47. #if defined __GNUC__ && __GNUC__ >= 2
  48. # define match_string(cs1, s2) \
  49. ({ size_t len = strlen (cs1); \
  50. int result = __strncasecmp_l ((cs1), (s2), len, locale) == 0; \
  51. if (result) (s2) += len; \
  52. result; })
  53. #else
  54. /* Oh come on. Get a reasonable compiler. */
  55. # define match_string(cs1, s2) \
  56. (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
  57. #endif
  58. /* We intentionally do not use isdigit() for testing because this will
  59. lead to problems with the wide character version. */
  60. #define get_number(from, to, n) \
  61. do { \
  62. int __n = n; \
  63. val = 0; \
  64. while (ISSPACE (*rp)) \
  65. ++rp; \
  66. if (*rp < '0' || *rp > '9') \
  67. return NULL; \
  68. do { \
  69. val *= 10; \
  70. val += *rp++ - '0'; \
  71. } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
  72. if (val < from || val > to) \
  73. return NULL; \
  74. } while (0)
  75. #ifdef _NL_CURRENT
  76. # define get_alt_number(from, to, n) \
  77. ({ \
  78. __label__ do_normal; \
  79. \
  80. if (s.decided != raw) \
  81. { \
  82. val = _nl_parse_alt_digit (&rp HELPER_LOCALE_ARG); \
  83. if (val == -1 && s.decided != loc) \
  84. { \
  85. s.decided = loc; \
  86. goto do_normal; \
  87. } \
  88. if (val < from || val > to) \
  89. return NULL; \
  90. } \
  91. else \
  92. { \
  93. do_normal: \
  94. get_number (from, to, n); \
  95. } \
  96. 0; \
  97. })
  98. #else
  99. # define get_alt_number(from, to, n) \
  100. /* We don't have the alternate representation. */ \
  101. get_number(from, to, n)
  102. #endif
  103. #define recursive(new_fmt) \
  104. (*(new_fmt) != '\0' \
  105. && (rp = __strptime_internal (rp, (new_fmt), tm, &s LOCALE_ARG)) != NULL)
  106. #ifdef _LIBC
  107. /* This is defined in locale/C-time.c in the GNU libc. */
  108. extern const struct __locale_data _nl_C_LC_TIME attribute_hidden;
  109. # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
  110. # define ab_weekday_name \
  111. (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
  112. # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
  113. # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
  114. # define alt_month_name \
  115. (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ALTMON_1)].string)
  116. # define ab_alt_month_name \
  117. (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (_NL_ABALTMON_1)].string)
  118. # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
  119. # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
  120. # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
  121. # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
  122. # define HERE_T_FMT_AMPM \
  123. (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
  124. # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
  125. # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
  126. #else
  127. static char const weekday_name[][10] =
  128. {
  129. "Sunday", "Monday", "Tuesday", "Wednesday",
  130. "Thursday", "Friday", "Saturday"
  131. };
  132. static char const ab_weekday_name[][4] =
  133. {
  134. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  135. };
  136. static char const month_name[][10] =
  137. {
  138. "January", "February", "March", "April", "May", "June",
  139. "July", "August", "September", "October", "November", "December"
  140. };
  141. static char const ab_month_name[][4] =
  142. {
  143. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  144. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  145. };
  146. # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
  147. # define HERE_D_FMT "%m/%d/%y"
  148. # define HERE_AM_STR "AM"
  149. # define HERE_PM_STR "PM"
  150. # define HERE_T_FMT_AMPM "%I:%M:%S %p"
  151. # define HERE_T_FMT "%H:%M:%S"
  152. static const unsigned short int __mon_yday[2][13] =
  153. {
  154. /* Normal years. */
  155. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  156. /* Leap years. */
  157. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  158. };
  159. #endif
  160. #if defined _LIBC
  161. /* We use this code also for the extended locale handling where the
  162. function gets as an additional argument the locale which has to be
  163. used. To access the values we have to redefine the _NL_CURRENT
  164. macro. */
  165. # define strptime __strptime_l
  166. # undef _NL_CURRENT
  167. # define _NL_CURRENT(category, item) \
  168. (current->values[_NL_ITEM_INDEX (item)].string)
  169. # undef _NL_CURRENT_WORD
  170. # define _NL_CURRENT_WORD(category, item) \
  171. (current->values[_NL_ITEM_INDEX (item)].word)
  172. # define LOCALE_PARAM , locale_t locale
  173. # define LOCALE_ARG , locale
  174. # define HELPER_LOCALE_ARG , current
  175. # define ISSPACE(Ch) __isspace_l (Ch, locale)
  176. #else
  177. # define LOCALE_PARAM
  178. # define LOCALE_ARG
  179. # define HELPER_LOCALE_ARG
  180. # define ISSPACE(Ch) isspace (Ch)
  181. #endif
  182. #ifndef __isleap
  183. /* Nonzero if YEAR is a leap year (every 4 years,
  184. except every 100th isn't, and every 400th is). */
  185. # define __isleap(year) \
  186. ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  187. #endif
  188. /* Compute the day of the week. */
  189. static void
  190. day_of_the_week (struct tm *tm)
  191. {
  192. /* We know that January 1st 1970 was a Thursday (= 4). Compute the
  193. difference between this data in the one on TM and so determine
  194. the weekday. */
  195. int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
  196. int wday = (-473
  197. + (365 * (tm->tm_year - 70))
  198. + (corr_year / 4)
  199. - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
  200. + (((corr_year / 4) / 25) / 4)
  201. + __mon_yday[0][tm->tm_mon]
  202. + tm->tm_mday - 1);
  203. tm->tm_wday = ((wday % 7) + 7) % 7;
  204. }
  205. /* Compute the day of the year. */
  206. static void
  207. day_of_the_year (struct tm *tm)
  208. {
  209. tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
  210. + (tm->tm_mday - 1));
  211. }
  212. #ifdef _LIBC
  213. char *
  214. #else
  215. static char *
  216. #endif
  217. __strptime_internal (const char *rp, const char *fmt, struct tm *tmp,
  218. void *statep LOCALE_PARAM)
  219. {
  220. #ifdef _LIBC
  221. struct __locale_data *const current = locale->__locales[LC_TIME];
  222. #endif
  223. const char *rp_backup;
  224. const char *rp_longest;
  225. int cnt;
  226. int cnt_longest;
  227. size_t val;
  228. size_t num_eras;
  229. struct era_entry *era = NULL;
  230. enum ptime_locale_status { not, loc, raw } decided_longest;
  231. struct __strptime_state
  232. {
  233. unsigned int have_I : 1;
  234. unsigned int have_wday : 1;
  235. unsigned int have_yday : 1;
  236. unsigned int have_mon : 1;
  237. unsigned int have_mday : 1;
  238. unsigned int have_uweek : 1;
  239. unsigned int have_wweek : 1;
  240. unsigned int is_pm : 1;
  241. unsigned int want_century : 1;
  242. unsigned int want_era : 1;
  243. unsigned int want_xday : 1;
  244. enum ptime_locale_status decided : 2;
  245. signed char week_no;
  246. signed char century;
  247. int era_cnt;
  248. } s;
  249. struct tm tmb;
  250. struct tm *tm;
  251. if (statep == NULL)
  252. {
  253. memset (&s, 0, sizeof (s));
  254. s.century = -1;
  255. s.era_cnt = -1;
  256. #ifdef _NL_CURRENT
  257. s.decided = not;
  258. #else
  259. s.decided = raw;
  260. #endif
  261. tm = tmp;
  262. }
  263. else
  264. {
  265. s = *(struct __strptime_state *) statep;
  266. tmb = *tmp;
  267. tm = &tmb;
  268. }
  269. while (*fmt != '\0')
  270. {
  271. /* A white space in the format string matches 0 more or white
  272. space in the input string. */
  273. if (ISSPACE (*fmt))
  274. {
  275. while (ISSPACE (*rp))
  276. ++rp;
  277. ++fmt;
  278. continue;
  279. }
  280. /* Any character but `%' must be matched by the same character
  281. in the input string. */
  282. if (*fmt != '%')
  283. {
  284. match_char (*fmt++, *rp++);
  285. continue;
  286. }
  287. ++fmt;
  288. /* We discard strftime modifiers. */
  289. while (*fmt == '-' || *fmt == '_' || *fmt == '0'
  290. || *fmt == '^' || *fmt == '#')
  291. ++fmt;
  292. /* And field width. */
  293. while (*fmt >= '0' && *fmt <= '9')
  294. ++fmt;
  295. /* In some cases, modifiers are handled by adjusting state and
  296. then restarting the switch statement below. */
  297. start_over:
  298. /* Make back up of current processing pointer. */
  299. rp_backup = rp;
  300. switch (*fmt++)
  301. {
  302. case '%':
  303. /* Match the `%' character itself. */
  304. match_char ('%', *rp++);
  305. break;
  306. case 'a':
  307. case 'A':
  308. /* Match day of week. */
  309. rp_longest = NULL;
  310. decided_longest = s.decided;
  311. cnt_longest = -1;
  312. for (cnt = 0; cnt < 7; ++cnt)
  313. {
  314. const char *trp;
  315. #ifdef _NL_CURRENT
  316. if (s.decided !=raw)
  317. {
  318. trp = rp;
  319. if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), trp)
  320. && trp > rp_longest)
  321. {
  322. rp_longest = trp;
  323. cnt_longest = cnt;
  324. if (s.decided == not
  325. && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
  326. weekday_name[cnt]))
  327. decided_longest = loc;
  328. }
  329. trp = rp;
  330. if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), trp)
  331. && trp > rp_longest)
  332. {
  333. rp_longest = trp;
  334. cnt_longest = cnt;
  335. if (s.decided == not
  336. && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
  337. ab_weekday_name[cnt]))
  338. decided_longest = loc;
  339. }
  340. }
  341. #endif
  342. if (s.decided != loc
  343. && (((trp = rp, match_string (weekday_name[cnt], trp))
  344. && trp > rp_longest)
  345. || ((trp = rp, match_string (ab_weekday_name[cnt], rp))
  346. && trp > rp_longest)))
  347. {
  348. rp_longest = trp;
  349. cnt_longest = cnt;
  350. decided_longest = raw;
  351. }
  352. }
  353. if (rp_longest == NULL)
  354. /* Does not match a weekday name. */
  355. return NULL;
  356. rp = rp_longest;
  357. s.decided = decided_longest;
  358. tm->tm_wday = cnt_longest;
  359. s.have_wday = 1;
  360. break;
  361. case 'b':
  362. case 'B':
  363. case 'h':
  364. /* Match month name. */
  365. rp_longest = NULL;
  366. decided_longest = s.decided;
  367. cnt_longest = -1;
  368. for (cnt = 0; cnt < 12; ++cnt)
  369. {
  370. const char *trp;
  371. #ifdef _NL_CURRENT
  372. if (s.decided !=raw)
  373. {
  374. trp = rp;
  375. if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), trp)
  376. && trp > rp_longest)
  377. {
  378. rp_longest = trp;
  379. cnt_longest = cnt;
  380. if (s.decided == not
  381. && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
  382. month_name[cnt]))
  383. decided_longest = loc;
  384. }
  385. trp = rp;
  386. if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), trp)
  387. && trp > rp_longest)
  388. {
  389. rp_longest = trp;
  390. cnt_longest = cnt;
  391. if (s.decided == not
  392. && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
  393. ab_month_name[cnt]))
  394. decided_longest = loc;
  395. }
  396. #ifdef _LIBC
  397. /* Now check the alt month. */
  398. trp = rp;
  399. if (match_string (_NL_CURRENT (LC_TIME, ALTMON_1 + cnt), trp)
  400. && trp > rp_longest)
  401. {
  402. rp_longest = trp;
  403. cnt_longest = cnt;
  404. if (s.decided == not
  405. && strcmp (_NL_CURRENT (LC_TIME, ALTMON_1 + cnt),
  406. alt_month_name[cnt]))
  407. decided_longest = loc;
  408. }
  409. trp = rp;
  410. if (match_string (_NL_CURRENT (LC_TIME, _NL_ABALTMON_1 + cnt),
  411. trp)
  412. && trp > rp_longest)
  413. {
  414. rp_longest = trp;
  415. cnt_longest = cnt;
  416. if (s.decided == not
  417. && strcmp (_NL_CURRENT (LC_TIME, _NL_ABALTMON_1 + cnt),
  418. alt_month_name[cnt]))
  419. decided_longest = loc;
  420. }
  421. #endif
  422. }
  423. #endif
  424. if (s.decided != loc
  425. && (((trp = rp, match_string (month_name[cnt], trp))
  426. && trp > rp_longest)
  427. || ((trp = rp, match_string (ab_month_name[cnt], trp))
  428. && trp > rp_longest)
  429. #ifdef _LIBC
  430. || ((trp = rp, match_string (alt_month_name[cnt], trp))
  431. && trp > rp_longest)
  432. || ((trp = rp, match_string (ab_alt_month_name[cnt], trp))
  433. && trp > rp_longest)
  434. #endif
  435. ))
  436. {
  437. rp_longest = trp;
  438. cnt_longest = cnt;
  439. decided_longest = raw;
  440. }
  441. }
  442. if (rp_longest == NULL)
  443. /* Does not match a month name. */
  444. return NULL;
  445. rp = rp_longest;
  446. s.decided = decided_longest;
  447. tm->tm_mon = cnt_longest;
  448. s.have_mon = 1;
  449. s.want_xday = 1;
  450. break;
  451. case 'c':
  452. /* Match locale's date and time format. */
  453. #ifdef _NL_CURRENT
  454. if (s.decided != raw)
  455. {
  456. if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
  457. {
  458. if (s.decided == loc)
  459. return NULL;
  460. else
  461. rp = rp_backup;
  462. }
  463. else
  464. {
  465. if (s.decided == not &&
  466. strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
  467. s.decided = loc;
  468. s.want_xday = 1;
  469. break;
  470. }
  471. s.decided = raw;
  472. }
  473. #endif
  474. if (!recursive (HERE_D_T_FMT))
  475. return NULL;
  476. s.want_xday = 1;
  477. break;
  478. case 'C':
  479. /* Match century number. */
  480. match_century:
  481. get_number (0, 99, 2);
  482. s.century = val;
  483. s.want_xday = 1;
  484. break;
  485. case 'd':
  486. case 'e':
  487. /* Match day of month. */
  488. get_number (1, 31, 2);
  489. tm->tm_mday = val;
  490. s.have_mday = 1;
  491. s.want_xday = 1;
  492. break;
  493. case 'F':
  494. if (!recursive ("%Y-%m-%d"))
  495. return NULL;
  496. s.want_xday = 1;
  497. break;
  498. case 'x':
  499. #ifdef _NL_CURRENT
  500. if (s.decided != raw)
  501. {
  502. if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
  503. {
  504. if (s.decided == loc)
  505. return NULL;
  506. else
  507. rp = rp_backup;
  508. }
  509. else
  510. {
  511. if (s.decided == not
  512. && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
  513. s.decided = loc;
  514. s.want_xday = 1;
  515. break;
  516. }
  517. s.decided = raw;
  518. }
  519. #endif
  520. /* Fall through. */
  521. case 'D':
  522. /* Match standard day format. */
  523. if (!recursive (HERE_D_FMT))
  524. return NULL;
  525. s.want_xday = 1;
  526. break;
  527. case 'k':
  528. case 'H':
  529. /* Match hour in 24-hour clock. */
  530. get_number (0, 23, 2);
  531. tm->tm_hour = val;
  532. s.have_I = 0;
  533. break;
  534. case 'l':
  535. /* Match hour in 12-hour clock. GNU extension. */
  536. case 'I':
  537. /* Match hour in 12-hour clock. */
  538. get_number (1, 12, 2);
  539. tm->tm_hour = val % 12;
  540. s.have_I = 1;
  541. break;
  542. case 'j':
  543. /* Match day number of year. */
  544. get_number (1, 366, 3);
  545. tm->tm_yday = val - 1;
  546. s.have_yday = 1;
  547. break;
  548. case 'm':
  549. /* Match number of month. */
  550. get_number (1, 12, 2);
  551. tm->tm_mon = val - 1;
  552. s.have_mon = 1;
  553. s.want_xday = 1;
  554. break;
  555. case 'M':
  556. /* Match minute. */
  557. get_number (0, 59, 2);
  558. tm->tm_min = val;
  559. break;
  560. case 'n':
  561. case 't':
  562. /* Match any white space. */
  563. while (ISSPACE (*rp))
  564. ++rp;
  565. break;
  566. case 'p':
  567. /* Match locale's equivalent of AM/PM. */
  568. #ifdef _NL_CURRENT
  569. if (s.decided != raw)
  570. {
  571. if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
  572. {
  573. if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
  574. s.decided = loc;
  575. s.is_pm = 0;
  576. break;
  577. }
  578. if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
  579. {
  580. if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
  581. s.decided = loc;
  582. s.is_pm = 1;
  583. break;
  584. }
  585. s.decided = raw;
  586. }
  587. #endif
  588. if (!match_string (HERE_AM_STR, rp))
  589. {
  590. if (match_string (HERE_PM_STR, rp))
  591. s.is_pm = 1;
  592. else
  593. return NULL;
  594. }
  595. else
  596. s.is_pm = 0;
  597. break;
  598. case 'r':
  599. #ifdef _NL_CURRENT
  600. if (s.decided != raw)
  601. {
  602. if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
  603. {
  604. if (s.decided == loc)
  605. return NULL;
  606. else
  607. rp = rp_backup;
  608. }
  609. else
  610. {
  611. if (s.decided == not &&
  612. strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
  613. HERE_T_FMT_AMPM))
  614. s.decided = loc;
  615. break;
  616. }
  617. s.decided = raw;
  618. }
  619. #endif
  620. if (!recursive (HERE_T_FMT_AMPM))
  621. return NULL;
  622. break;
  623. case 'R':
  624. if (!recursive ("%H:%M"))
  625. return NULL;
  626. break;
  627. case 's':
  628. {
  629. /* The number of seconds may be very high so we cannot use
  630. the `get_number' macro. Instead read the number
  631. character for character and construct the result while
  632. doing this. */
  633. time_t secs = 0;
  634. if (*rp < '0' || *rp > '9')
  635. /* We need at least one digit. */
  636. return NULL;
  637. do
  638. {
  639. secs *= 10;
  640. secs += *rp++ - '0';
  641. }
  642. while (*rp >= '0' && *rp <= '9');
  643. if (localtime_r (&secs, tm) == NULL)
  644. /* Error in function. */
  645. return NULL;
  646. }
  647. break;
  648. case 'S':
  649. get_number (0, 61, 2);
  650. tm->tm_sec = val;
  651. break;
  652. case 'X':
  653. #ifdef _NL_CURRENT
  654. if (s.decided != raw)
  655. {
  656. if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
  657. {
  658. if (s.decided == loc)
  659. return NULL;
  660. else
  661. rp = rp_backup;
  662. }
  663. else
  664. {
  665. if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
  666. s.decided = loc;
  667. break;
  668. }
  669. s.decided = raw;
  670. }
  671. #endif
  672. /* Fall through. */
  673. case 'T':
  674. if (!recursive (HERE_T_FMT))
  675. return NULL;
  676. break;
  677. case 'u':
  678. get_number (1, 7, 1);
  679. tm->tm_wday = val % 7;
  680. s.have_wday = 1;
  681. break;
  682. case 'g':
  683. get_number (0, 99, 2);
  684. /* XXX This cannot determine any field in TM. */
  685. break;
  686. case 'G':
  687. if (*rp < '0' || *rp > '9')
  688. return NULL;
  689. /* XXX Ignore the number since we would need some more
  690. information to compute a real date. */
  691. do
  692. ++rp;
  693. while (*rp >= '0' && *rp <= '9');
  694. break;
  695. case 'U':
  696. get_number (0, 53, 2);
  697. s.week_no = val;
  698. s.have_uweek = 1;
  699. break;
  700. case 'W':
  701. get_number (0, 53, 2);
  702. s.week_no = val;
  703. s.have_wweek = 1;
  704. break;
  705. case 'V':
  706. get_number (0, 53, 2);
  707. /* XXX This cannot determine any field in TM without some
  708. information. */
  709. break;
  710. case 'w':
  711. /* Match number of weekday. */
  712. get_number (0, 6, 1);
  713. tm->tm_wday = val;
  714. s.have_wday = 1;
  715. break;
  716. case 'y':
  717. match_year_in_century:
  718. /* Match year within century. */
  719. get_number (0, 99, 2);
  720. /* The "Year 2000: The Millennium Rollover" paper suggests that
  721. values in the range 69-99 refer to the twentieth century. */
  722. tm->tm_year = val >= 69 ? val : val + 100;
  723. /* Indicate that we want to use the century, if specified. */
  724. s.want_century = 1;
  725. s.want_xday = 1;
  726. break;
  727. case 'Y':
  728. /* Match year including century number. */
  729. get_number (0, 9999, 4);
  730. tm->tm_year = val - 1900;
  731. s.want_century = 0;
  732. s.want_xday = 1;
  733. break;
  734. case 'Z':
  735. /* Read timezone but perform no conversion. */
  736. while (ISSPACE (*rp))
  737. rp++;
  738. while (!ISSPACE (*rp) && *rp != '\0')
  739. rp++;
  740. break;
  741. case 'z':
  742. /* We recognize four formats:
  743. 1. Two digits specify hours.
  744. 2. Four digits specify hours and minutes.
  745. 3. Two digits, ':', and two digits specify hours and minutes.
  746. 4. 'Z' is equivalent to +0000. */
  747. {
  748. val = 0;
  749. while (ISSPACE (*rp))
  750. ++rp;
  751. if (*rp == 'Z')
  752. {
  753. ++rp;
  754. tm->tm_gmtoff = 0;
  755. break;
  756. }
  757. if (*rp != '+' && *rp != '-')
  758. return NULL;
  759. bool neg = *rp++ == '-';
  760. int n = 0;
  761. while (n < 4 && *rp >= '0' && *rp <= '9')
  762. {
  763. val = val * 10 + *rp++ - '0';
  764. ++n;
  765. if (*rp == ':' && n == 2 && isdigit (*(rp + 1)))
  766. ++rp;
  767. }
  768. if (n == 2)
  769. val *= 100;
  770. else if (n != 4)
  771. /* Only two or four digits recognized. */
  772. return NULL;
  773. else if (val % 100 >= 60)
  774. /* Minutes valid range is 0 through 59. */
  775. return NULL;
  776. tm->tm_gmtoff = (val / 100) * 3600 + (val % 100) * 60;
  777. if (neg)
  778. tm->tm_gmtoff = -tm->tm_gmtoff;
  779. }
  780. break;
  781. case 'E':
  782. #ifdef _NL_CURRENT
  783. switch (*fmt++)
  784. {
  785. case 'c':
  786. /* Match locale's alternate date and time format. */
  787. if (s.decided != raw)
  788. {
  789. const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
  790. if (*fmt == '\0')
  791. fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
  792. if (!recursive (fmt))
  793. {
  794. if (s.decided == loc)
  795. return NULL;
  796. else
  797. rp = rp_backup;
  798. }
  799. else
  800. {
  801. if (strcmp (fmt, HERE_D_T_FMT))
  802. s.decided = loc;
  803. s.want_xday = 1;
  804. break;
  805. }
  806. s.decided = raw;
  807. }
  808. /* The C locale has no era information, so use the
  809. normal representation. */
  810. if (!recursive (HERE_D_T_FMT))
  811. return NULL;
  812. s.want_xday = 1;
  813. break;
  814. case 'C':
  815. if (s.decided != raw)
  816. {
  817. if (s.era_cnt >= 0)
  818. {
  819. era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
  820. if (era != NULL && match_string (era->era_name, rp))
  821. {
  822. s.decided = loc;
  823. break;
  824. }
  825. else
  826. return NULL;
  827. }
  828. num_eras = _NL_CURRENT_WORD (LC_TIME,
  829. _NL_TIME_ERA_NUM_ENTRIES);
  830. for (s.era_cnt = 0; s.era_cnt < (int) num_eras;
  831. ++s.era_cnt, rp = rp_backup)
  832. {
  833. era = _nl_select_era_entry (s.era_cnt
  834. HELPER_LOCALE_ARG);
  835. if (era != NULL && match_string (era->era_name, rp))
  836. {
  837. s.decided = loc;
  838. break;
  839. }
  840. }
  841. if (s.era_cnt != (int) num_eras)
  842. break;
  843. s.era_cnt = -1;
  844. if (s.decided == loc)
  845. return NULL;
  846. s.decided = raw;
  847. }
  848. /* The C locale has no era information, so use the
  849. normal representation. */
  850. goto match_century;
  851. case 'y':
  852. if (s.decided != raw)
  853. {
  854. get_number(0, 9999, 4);
  855. tm->tm_year = val;
  856. s.want_era = 1;
  857. s.want_xday = 1;
  858. s.want_century = 1;
  859. if (s.era_cnt >= 0)
  860. {
  861. assert (s.decided == loc);
  862. era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
  863. bool match = false;
  864. if (era != NULL)
  865. {
  866. int delta = ((tm->tm_year - era->offset)
  867. * era->absolute_direction);
  868. match = (delta >= 0
  869. && delta < (((int64_t) era->stop_date[0]
  870. - (int64_t) era->start_date[0])
  871. * era->absolute_direction));
  872. }
  873. if (! match)
  874. return NULL;
  875. break;
  876. }
  877. num_eras = _NL_CURRENT_WORD (LC_TIME,
  878. _NL_TIME_ERA_NUM_ENTRIES);
  879. for (s.era_cnt = 0; s.era_cnt < (int) num_eras; ++s.era_cnt)
  880. {
  881. era = _nl_select_era_entry (s.era_cnt
  882. HELPER_LOCALE_ARG);
  883. if (era != NULL)
  884. {
  885. int delta = ((tm->tm_year - era->offset)
  886. * era->absolute_direction);
  887. if (delta >= 0
  888. && delta < (((int64_t) era->stop_date[0]
  889. - (int64_t) era->start_date[0])
  890. * era->absolute_direction))
  891. {
  892. s.decided = loc;
  893. break;
  894. }
  895. }
  896. }
  897. if (s.era_cnt != (int) num_eras)
  898. break;
  899. s.era_cnt = -1;
  900. if (s.decided == loc)
  901. return NULL;
  902. s.decided = raw;
  903. }
  904. goto match_year_in_century;
  905. case 'Y':
  906. if (s.decided != raw)
  907. {
  908. num_eras = _NL_CURRENT_WORD (LC_TIME,
  909. _NL_TIME_ERA_NUM_ENTRIES);
  910. for (s.era_cnt = 0; s.era_cnt < (int) num_eras;
  911. ++s.era_cnt, rp = rp_backup)
  912. {
  913. era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
  914. if (era != NULL && recursive (era->era_format))
  915. break;
  916. }
  917. if (s.era_cnt == (int) num_eras)
  918. {
  919. s.era_cnt = -1;
  920. if (s.decided == loc)
  921. return NULL;
  922. else
  923. rp = rp_backup;
  924. }
  925. else
  926. {
  927. s.decided = loc;
  928. break;
  929. }
  930. s.decided = raw;
  931. }
  932. get_number (0, 9999, 4);
  933. tm->tm_year = val - 1900;
  934. s.want_century = 0;
  935. s.want_xday = 1;
  936. break;
  937. case 'x':
  938. if (s.decided != raw)
  939. {
  940. const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
  941. if (*fmt == '\0')
  942. fmt = _NL_CURRENT (LC_TIME, D_FMT);
  943. if (!recursive (fmt))
  944. {
  945. if (s.decided == loc)
  946. return NULL;
  947. else
  948. rp = rp_backup;
  949. }
  950. else
  951. {
  952. if (strcmp (fmt, HERE_D_FMT))
  953. s.decided = loc;
  954. break;
  955. }
  956. s.decided = raw;
  957. }
  958. if (!recursive (HERE_D_FMT))
  959. return NULL;
  960. break;
  961. case 'X':
  962. if (s.decided != raw)
  963. {
  964. const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
  965. if (*fmt == '\0')
  966. fmt = _NL_CURRENT (LC_TIME, T_FMT);
  967. if (!recursive (fmt))
  968. {
  969. if (s.decided == loc)
  970. return NULL;
  971. else
  972. rp = rp_backup;
  973. }
  974. else
  975. {
  976. if (strcmp (fmt, HERE_T_FMT))
  977. s.decided = loc;
  978. break;
  979. }
  980. s.decided = raw;
  981. }
  982. if (!recursive (HERE_T_FMT))
  983. return NULL;
  984. break;
  985. default:
  986. return NULL;
  987. }
  988. break;
  989. #else
  990. /* We have no information about the era format. Just use
  991. the normal format. */
  992. if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
  993. && *fmt != 'x' && *fmt != 'X')
  994. /* This is an illegal format. */
  995. return NULL;
  996. goto start_over;
  997. #endif
  998. case 'O':
  999. switch (*fmt++)
  1000. {
  1001. case 'b':
  1002. case 'B':
  1003. case 'h':
  1004. /* Match month name. Reprocess as plain 'B'. */
  1005. fmt--;
  1006. goto start_over;
  1007. case 'd':
  1008. case 'e':
  1009. /* Match day of month using alternate numeric symbols. */
  1010. get_alt_number (1, 31, 2);
  1011. tm->tm_mday = val;
  1012. s.have_mday = 1;
  1013. s.want_xday = 1;
  1014. break;
  1015. case 'H':
  1016. /* Match hour in 24-hour clock using alternate numeric
  1017. symbols. */
  1018. get_alt_number (0, 23, 2);
  1019. tm->tm_hour = val;
  1020. s.have_I = 0;
  1021. break;
  1022. case 'I':
  1023. /* Match hour in 12-hour clock using alternate numeric
  1024. symbols. */
  1025. get_alt_number (1, 12, 2);
  1026. tm->tm_hour = val % 12;
  1027. s.have_I = 1;
  1028. break;
  1029. case 'm':
  1030. /* Match month using alternate numeric symbols. */
  1031. get_alt_number (1, 12, 2);
  1032. tm->tm_mon = val - 1;
  1033. s.have_mon = 1;
  1034. s.want_xday = 1;
  1035. break;
  1036. case 'M':
  1037. /* Match minutes using alternate numeric symbols. */
  1038. get_alt_number (0, 59, 2);
  1039. tm->tm_min = val;
  1040. break;
  1041. case 'S':
  1042. /* Match seconds using alternate numeric symbols. */
  1043. get_alt_number (0, 61, 2);
  1044. tm->tm_sec = val;
  1045. break;
  1046. case 'U':
  1047. get_alt_number (0, 53, 2);
  1048. s.week_no = val;
  1049. s.have_uweek = 1;
  1050. break;
  1051. case 'W':
  1052. get_alt_number (0, 53, 2);
  1053. s.week_no = val;
  1054. s.have_wweek = 1;
  1055. break;
  1056. case 'V':
  1057. get_alt_number (0, 53, 2);
  1058. /* XXX This cannot determine any field in TM without
  1059. further information. */
  1060. break;
  1061. case 'w':
  1062. /* Match number of weekday using alternate numeric symbols. */
  1063. get_alt_number (0, 6, 1);
  1064. tm->tm_wday = val;
  1065. s.have_wday = 1;
  1066. break;
  1067. case 'y':
  1068. /* Match year within century using alternate numeric symbols. */
  1069. get_alt_number (0, 99, 2);
  1070. tm->tm_year = val >= 69 ? val : val + 100;
  1071. s.want_xday = 1;
  1072. break;
  1073. default:
  1074. return NULL;
  1075. }
  1076. break;
  1077. default:
  1078. return NULL;
  1079. }
  1080. }
  1081. if (statep != NULL)
  1082. {
  1083. /* Recursive invocation, returning success, so
  1084. update parent's struct tm and state. */
  1085. *(struct __strptime_state *) statep = s;
  1086. *tmp = tmb;
  1087. return (char *) rp;
  1088. }
  1089. if (s.have_I && s.is_pm)
  1090. tm->tm_hour += 12;
  1091. if (s.century != -1)
  1092. {
  1093. if (s.want_century)
  1094. tm->tm_year = tm->tm_year % 100 + (s.century - 19) * 100;
  1095. else
  1096. /* Only the century, but not the year. Strange, but so be it. */
  1097. tm->tm_year = (s.century - 19) * 100;
  1098. }
  1099. if (s.era_cnt != -1)
  1100. {
  1101. era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
  1102. if (era == NULL)
  1103. return NULL;
  1104. if (s.want_era)
  1105. tm->tm_year = (era->start_date[0]
  1106. + ((tm->tm_year - era->offset)
  1107. * era->absolute_direction));
  1108. else
  1109. /* Era start year assumed. */
  1110. tm->tm_year = era->start_date[0];
  1111. }
  1112. else
  1113. if (s.want_era)
  1114. {
  1115. /* No era found but we have seen an E modifier. Rectify some
  1116. values. */
  1117. if (s.want_century && s.century == -1 && tm->tm_year < 69)
  1118. tm->tm_year += 100;
  1119. }
  1120. if (s.want_xday && !s.have_wday)
  1121. {
  1122. if ( !(s.have_mon && s.have_mday) && s.have_yday)
  1123. {
  1124. /* We don't have tm_mon and/or tm_mday, compute them. */
  1125. int t_mon = 0;
  1126. while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
  1127. t_mon++;
  1128. if (!s.have_mon)
  1129. tm->tm_mon = t_mon - 1;
  1130. if (!s.have_mday)
  1131. tm->tm_mday =
  1132. (tm->tm_yday
  1133. - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
  1134. s.have_mon = 1;
  1135. s.have_mday = 1;
  1136. }
  1137. /* Don't crash in day_of_the_week if tm_mon is uninitialized. */
  1138. if (s.have_mon || (unsigned) tm->tm_mon <= 11)
  1139. day_of_the_week (tm);
  1140. }
  1141. if (s.want_xday && !s.have_yday && (s.have_mon || (unsigned) tm->tm_mon <= 11))
  1142. day_of_the_year (tm);
  1143. if ((s.have_uweek || s.have_wweek) && s.have_wday)
  1144. {
  1145. int save_wday = tm->tm_wday;
  1146. int save_mday = tm->tm_mday;
  1147. int save_mon = tm->tm_mon;
  1148. int w_offset = s.have_uweek ? 0 : 1;
  1149. tm->tm_mday = 1;
  1150. tm->tm_mon = 0;
  1151. day_of_the_week (tm);
  1152. if (s.have_mday)
  1153. tm->tm_mday = save_mday;
  1154. if (s.have_mon)
  1155. tm->tm_mon = save_mon;
  1156. if (!s.have_yday)
  1157. tm->tm_yday = ((7 - (tm->tm_wday - w_offset)) % 7
  1158. + (s.week_no - 1) * 7
  1159. + (save_wday - w_offset + 7) % 7);
  1160. if (!s.have_mday || !s.have_mon)
  1161. {
  1162. int t_mon = 0;
  1163. while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon]
  1164. <= tm->tm_yday)
  1165. t_mon++;
  1166. if (!s.have_mon)
  1167. tm->tm_mon = t_mon - 1;
  1168. if (!s.have_mday)
  1169. tm->tm_mday =
  1170. (tm->tm_yday
  1171. - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
  1172. }
  1173. tm->tm_wday = save_wday;
  1174. }
  1175. return (char *) rp;
  1176. }
  1177. char *
  1178. strptime (const char *buf, const char *format, struct tm *tm LOCALE_PARAM)
  1179. {
  1180. return __strptime_internal (buf, format, tm, NULL LOCALE_ARG);
  1181. }
  1182. #ifdef _LIBC
  1183. weak_alias (__strptime_l, strptime_l)
  1184. #endif