strptime.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Copyright (c) 1999 Kungliga Tekniska Högskolan
  3. * (Royal Institute of Technology, Stockholm, Sweden).
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * 3. Neither the name of KTH nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software without
  19. * specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
  22. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  32. #ifndef HAVE_CONFIG_H
  33. # include "config.h"
  34. #endif // HAVE_CONFIG_H
  35. #ifndef HAVE_LOCALTIME_R
  36. # include "localtime_r.h"
  37. #endif // HAVE_LOCALTIME_R
  38. #ifndef HAVE_TIMEGM
  39. # include "timegm.h"
  40. #endif // HAVE_TIMEGM
  41. #include <stddef.h>
  42. #include <stdio.h>
  43. #include <time.h>
  44. #include <string.h>
  45. #include <ctype.h>
  46. #include <stdlib.h>
  47. #ifdef HAVE_ALLOCA_H
  48. # include <alloca.h>
  49. #endif // HAVE_ALLOCA_H
  50. #ifdef HAVE_MALLOC_H
  51. # include <malloc.h>
  52. #endif // HAVE_MALLOC_H
  53. #include "strptime.h"
  54. static const char *abb_weekdays[] = {
  55. "Sun",
  56. "Mon",
  57. "Tue",
  58. "Wed",
  59. "Thu",
  60. "Fri",
  61. "Sat",
  62. NULL
  63. };
  64. static const char *full_weekdays[] = {
  65. "Sunday",
  66. "Monday",
  67. "Tuesday",
  68. "Wednesday",
  69. "Thursday",
  70. "Friday",
  71. "Saturday",
  72. NULL
  73. };
  74. static const char *abb_month[] = {
  75. "Jan",
  76. "Feb",
  77. "Mar",
  78. "Apr",
  79. "May",
  80. "Jun",
  81. "Jul",
  82. "Aug",
  83. "Sep",
  84. "Oct",
  85. "Nov",
  86. "Dec",
  87. NULL
  88. };
  89. static const char *full_month[] = {
  90. "January",
  91. "February",
  92. "March",
  93. "April",
  94. "May",
  95. "June",
  96. "July",
  97. "August",
  98. "September",
  99. "October",
  100. "November",
  101. "December",
  102. NULL,
  103. };
  104. static const char *ampm[] = {
  105. "am",
  106. "pm",
  107. NULL
  108. };
  109. /*
  110. * tm_year is relative this year
  111. */
  112. const int tm_year_base = 1900;
  113. /*
  114. * Return TRUE iff `year' was a leap year.
  115. * Needed for strptime.
  116. */
  117. static int
  118. is_leap_year (int year)
  119. {
  120. return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
  121. }
  122. /* Needed for strptime. */
  123. static int
  124. match_string (const char **buf, const char **strs)
  125. {
  126. int i = 0;
  127. for (i = 0; strs[i] != NULL; ++i) {
  128. int len = strlen (strs[i]);
  129. if (strncasecmp (*buf, strs[i], len) == 0) {
  130. *buf += len;
  131. return i;
  132. }
  133. }
  134. return -1;
  135. }
  136. /* Needed for strptime. */
  137. static int
  138. first_day (int year)
  139. {
  140. int ret = 4;
  141. for (; year > 1970; --year)
  142. ret = (ret + 365 + is_leap_year (year) ? 1 : 0) % 7;
  143. return ret;
  144. }
  145. /*
  146. * Set `timeptr' given `wnum' (week number [0, 53])
  147. * Needed for strptime
  148. */
  149. static void
  150. set_week_number_sun (struct tm *timeptr, int wnum)
  151. {
  152. int fday = first_day (timeptr->tm_year + tm_year_base);
  153. timeptr->tm_yday = wnum * 7 + timeptr->tm_wday - fday;
  154. if (timeptr->tm_yday < 0) {
  155. timeptr->tm_wday = fday;
  156. timeptr->tm_yday = 0;
  157. }
  158. }
  159. /*
  160. * Set `timeptr' given `wnum' (week number [0, 53])
  161. * Needed for strptime
  162. */
  163. static void
  164. set_week_number_mon (struct tm *timeptr, int wnum)
  165. {
  166. int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
  167. timeptr->tm_yday = wnum * 7 + (timeptr->tm_wday + 6) % 7 - fday;
  168. if (timeptr->tm_yday < 0) {
  169. timeptr->tm_wday = (fday + 1) % 7;
  170. timeptr->tm_yday = 0;
  171. }
  172. }
  173. /*
  174. * Set `timeptr' given `wnum' (week number [0, 53])
  175. * Needed for strptime
  176. */
  177. static void
  178. set_week_number_mon4 (struct tm *timeptr, int wnum)
  179. {
  180. int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
  181. int offset = 0;
  182. if (fday < 4)
  183. offset += 7;
  184. timeptr->tm_yday = offset + (wnum - 1) * 7 + timeptr->tm_wday - fday;
  185. if (timeptr->tm_yday < 0) {
  186. timeptr->tm_wday = fday;
  187. timeptr->tm_yday = 0;
  188. }
  189. }
  190. /* strptime: roken */
  191. //extern "C"
  192. static char *
  193. _strptime (const char *buf, const char *format, struct tm *timeptr, int *gmt)
  194. {
  195. char c;
  196. for (; (c = *format) != '\0'; ++format) {
  197. char *s;
  198. int ret;
  199. if (isspace (c)) {
  200. while (isspace (*buf))
  201. ++buf;
  202. } else if (c == '%' && format[1] != '\0') {
  203. c = *++format;
  204. if (c == 'E' || c == 'O')
  205. c = *++format;
  206. switch (c) {
  207. case 'A' :
  208. case 'a' :
  209. ret = match_string (&buf, full_weekdays);
  210. if (ret < 0)
  211. ret = match_string (&buf, abb_weekdays);
  212. if (ret < 0)
  213. return NULL;
  214. timeptr->tm_wday = ret;
  215. break;
  216. case 'B' :
  217. case 'b' :
  218. case 'h' :
  219. ret = match_string (&buf, full_month);
  220. if (ret < 0)
  221. ret = match_string (&buf, abb_month);
  222. if (ret < 0)
  223. return NULL;
  224. timeptr->tm_mon = ret;
  225. break;
  226. case 'C' :
  227. ret = strtol (buf, &s, 10);
  228. if (s == buf)
  229. return NULL;
  230. timeptr->tm_year = (ret * 100) - tm_year_base;
  231. buf = s;
  232. break;
  233. case 'c' : /* %a %b %e %H:%M:%S %Y */
  234. s = strptime (buf, "%a %b %e %H:%M:%S %Y", timeptr);
  235. if (s == NULL)
  236. return NULL;
  237. buf = s;
  238. break;
  239. case 'D' : /* %m/%d/%y */
  240. s = strptime (buf, "%m/%d/%y", timeptr);
  241. if (s == NULL)
  242. return NULL;
  243. buf = s;
  244. break;
  245. case 'd' :
  246. case 'e' :
  247. ret = strtol (buf, &s, 10);
  248. if (s == buf)
  249. return NULL;
  250. timeptr->tm_mday = ret;
  251. buf = s;
  252. break;
  253. case 'H' :
  254. case 'k' :
  255. ret = strtol (buf, &s, 10);
  256. if (s == buf)
  257. return NULL;
  258. timeptr->tm_hour = ret;
  259. buf = s;
  260. break;
  261. case 'I' :
  262. case 'l' :
  263. ret = strtol (buf, &s, 10);
  264. if (s == buf)
  265. return NULL;
  266. if (ret == 12)
  267. timeptr->tm_hour = 0;
  268. else
  269. timeptr->tm_hour = ret;
  270. buf = s;
  271. break;
  272. case 'j' :
  273. ret = strtol (buf, &s, 10);
  274. if (s == buf)
  275. return NULL;
  276. timeptr->tm_yday = ret - 1;
  277. buf = s;
  278. break;
  279. case 'm' :
  280. ret = strtol (buf, &s, 10);
  281. if (s == buf)
  282. return NULL;
  283. timeptr->tm_mon = ret - 1;
  284. buf = s;
  285. break;
  286. case 'M' :
  287. ret = strtol (buf, &s, 10);
  288. if (s == buf)
  289. return NULL;
  290. timeptr->tm_min = ret;
  291. buf = s;
  292. break;
  293. case 'n' :
  294. if (*buf == '\n')
  295. ++buf;
  296. else
  297. return NULL;
  298. break;
  299. case 'p' :
  300. ret = match_string (&buf, ampm);
  301. if (ret < 0)
  302. return NULL;
  303. if (timeptr->tm_hour == 0) {
  304. if (ret == 1)
  305. timeptr->tm_hour = 12;
  306. } else
  307. timeptr->tm_hour += 12;
  308. break;
  309. case 'r' : /* %I:%M:%S %p */
  310. s = strptime (buf, "%I:%M:%S %p", timeptr);
  311. if (s == NULL)
  312. return NULL;
  313. buf = s;
  314. break;
  315. case 'R' : /* %H:%M */
  316. s = strptime (buf, "%H:%M", timeptr);
  317. if (s == NULL)
  318. return NULL;
  319. buf = s;
  320. break;
  321. case 'S' :
  322. ret = strtol (buf, &s, 10);
  323. if (s == buf)
  324. return NULL;
  325. timeptr->tm_sec = ret;
  326. buf = s;
  327. break;
  328. case 't' :
  329. if (*buf == '\t')
  330. ++buf;
  331. else
  332. return NULL;
  333. break;
  334. case 'T' : /* %H:%M:%S */
  335. case 'X' :
  336. s = strptime (buf, "%H:%M:%S", timeptr);
  337. if (s == NULL)
  338. return NULL;
  339. buf = s;
  340. break;
  341. case 'u' :
  342. ret = strtol (buf, &s, 10);
  343. if (s == buf)
  344. return NULL;
  345. timeptr->tm_wday = ret - 1;
  346. buf = s;
  347. break;
  348. case 'w' :
  349. ret = strtol (buf, &s, 10);
  350. if (s == buf)
  351. return NULL;
  352. timeptr->tm_wday = ret;
  353. buf = s;
  354. break;
  355. case 'U' :
  356. ret = strtol (buf, &s, 10);
  357. if (s == buf)
  358. return NULL;
  359. set_week_number_sun (timeptr, ret);
  360. buf = s;
  361. break;
  362. case 'V' :
  363. ret = strtol (buf, &s, 10);
  364. if (s == buf)
  365. return NULL;
  366. set_week_number_mon4 (timeptr, ret);
  367. buf = s;
  368. break;
  369. case 'W' :
  370. ret = strtol (buf, &s, 10);
  371. if (s == buf)
  372. return NULL;
  373. set_week_number_mon (timeptr, ret);
  374. buf = s;
  375. break;
  376. case 'x' :
  377. s = strptime (buf, "%Y:%m:%d", timeptr);
  378. if (s == NULL)
  379. return NULL;
  380. buf = s;
  381. break;
  382. case 'y' :
  383. ret = strtol (buf, &s, 10);
  384. if (s == buf)
  385. return NULL;
  386. /*
  387. * y represents stricty 2 digits, raise error if more than 3
  388. * digits are parsed.
  389. */
  390. if (ret > 99) {
  391. return NULL;
  392. }
  393. /*
  394. * The value in the range 69-99 refer to years in 20th century.
  395. * The value in the range 00-68 refer to years in 21st century.
  396. */
  397. if (ret < 69)
  398. timeptr->tm_year = 100 + ret;
  399. else
  400. timeptr->tm_year = ret;
  401. buf = s;
  402. break;
  403. case 'Y' :
  404. ret = strtol (buf, &s, 10);
  405. if (s == buf)
  406. return NULL;
  407. timeptr->tm_year = ret - tm_year_base;
  408. buf = s;
  409. break;
  410. case 'Z' :
  411. /* source: cygwin-1.5.24-2-src/cygwin-1.5.24-2/winsup/cygwin/libc/strptime.cc */
  412. {
  413. const char *cp;
  414. char *zonestr;
  415. for (cp = buf; *cp && isupper((unsigned char)*cp); ++cp) {/*empty*/}
  416. if (cp - buf) {
  417. zonestr = (char *) alloca(cp - buf + 1);
  418. strncpy(zonestr, buf, cp - buf);
  419. zonestr[cp - buf] = '\0';
  420. tzset();
  421. if (0 == strcmp(zonestr, "GMT")) {
  422. *gmt = 1;
  423. } else if (0 == strcmp(zonestr, tzname[0])) {
  424. timeptr->tm_isdst = 0;
  425. } else if (0 == strcmp(zonestr, tzname[1])) {
  426. timeptr->tm_isdst = 1;
  427. } else {
  428. return 0;
  429. }
  430. buf += cp - buf;
  431. }
  432. }
  433. break;
  434. case '\0' :
  435. --format;
  436. /* FALLTHROUGH */
  437. case '%' :
  438. if (*buf == '%')
  439. ++buf;
  440. else
  441. return NULL;
  442. break;
  443. default :
  444. if (*buf == '%' || *++buf == c)
  445. ++buf;
  446. else
  447. return NULL;
  448. break;
  449. }
  450. } else {
  451. if (*buf == c)
  452. ++buf;
  453. else
  454. return NULL;
  455. }
  456. }
  457. return (char *)buf;
  458. }
  459. char *
  460. strptime (const char *buf, const char *format, struct tm *timeptr)
  461. {
  462. char *ret;
  463. int gmt;
  464. gmt = 0;
  465. ret = _strptime(buf, format, timeptr, &gmt);
  466. return (ret);
  467. }