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. ret = match_string (&buf, full_weekdays);
  209. if (ret < 0)
  210. return NULL;
  211. timeptr->tm_wday = ret;
  212. break;
  213. case 'a' :
  214. ret = match_string (&buf, abb_weekdays);
  215. if (ret < 0)
  216. return NULL;
  217. timeptr->tm_wday = ret;
  218. break;
  219. case 'B' :
  220. ret = match_string (&buf, full_month);
  221. if (ret < 0)
  222. return NULL;
  223. timeptr->tm_mon = ret;
  224. break;
  225. case 'b' :
  226. case 'h' :
  227. ret = match_string (&buf, abb_month);
  228. if (ret < 0)
  229. return NULL;
  230. timeptr->tm_mon = ret;
  231. break;
  232. case 'C' :
  233. ret = strtol (buf, &s, 10);
  234. if (s == buf)
  235. return NULL;
  236. timeptr->tm_year = (ret * 100) - tm_year_base;
  237. buf = s;
  238. break;
  239. case 'c' : /* %a %b %e %H:%M:%S %Y */
  240. s = strptime (buf, "%a %b %e %H:%M:%S %Y", timeptr);
  241. if (s == NULL)
  242. return NULL;
  243. buf = s;
  244. break;
  245. case 'D' : /* %m/%d/%y */
  246. s = strptime (buf, "%m/%d/%y", timeptr);
  247. if (s == NULL)
  248. return NULL;
  249. buf = s;
  250. break;
  251. case 'd' :
  252. case 'e' :
  253. ret = strtol (buf, &s, 10);
  254. if (s == buf)
  255. return NULL;
  256. timeptr->tm_mday = ret;
  257. buf = s;
  258. break;
  259. case 'H' :
  260. case 'k' :
  261. ret = strtol (buf, &s, 10);
  262. if (s == buf)
  263. return NULL;
  264. timeptr->tm_hour = ret;
  265. buf = s;
  266. break;
  267. case 'I' :
  268. case 'l' :
  269. ret = strtol (buf, &s, 10);
  270. if (s == buf)
  271. return NULL;
  272. if (ret == 12)
  273. timeptr->tm_hour = 0;
  274. else
  275. timeptr->tm_hour = ret;
  276. buf = s;
  277. break;
  278. case 'j' :
  279. ret = strtol (buf, &s, 10);
  280. if (s == buf)
  281. return NULL;
  282. timeptr->tm_yday = ret - 1;
  283. buf = s;
  284. break;
  285. case 'm' :
  286. ret = strtol (buf, &s, 10);
  287. if (s == buf)
  288. return NULL;
  289. timeptr->tm_mon = ret - 1;
  290. buf = s;
  291. break;
  292. case 'M' :
  293. ret = strtol (buf, &s, 10);
  294. if (s == buf)
  295. return NULL;
  296. timeptr->tm_min = ret;
  297. buf = s;
  298. break;
  299. case 'n' :
  300. if (*buf == '\n')
  301. ++buf;
  302. else
  303. return NULL;
  304. break;
  305. case 'p' :
  306. ret = match_string (&buf, ampm);
  307. if (ret < 0)
  308. return NULL;
  309. if (timeptr->tm_hour == 0) {
  310. if (ret == 1)
  311. timeptr->tm_hour = 12;
  312. } else
  313. timeptr->tm_hour += 12;
  314. break;
  315. case 'r' : /* %I:%M:%S %p */
  316. s = strptime (buf, "%I:%M:%S %p", timeptr);
  317. if (s == NULL)
  318. return NULL;
  319. buf = s;
  320. break;
  321. case 'R' : /* %H:%M */
  322. s = strptime (buf, "%H:%M", timeptr);
  323. if (s == NULL)
  324. return NULL;
  325. buf = s;
  326. break;
  327. case 'S' :
  328. ret = strtol (buf, &s, 10);
  329. if (s == buf)
  330. return NULL;
  331. timeptr->tm_sec = ret;
  332. buf = s;
  333. break;
  334. case 't' :
  335. if (*buf == '\t')
  336. ++buf;
  337. else
  338. return NULL;
  339. break;
  340. case 'T' : /* %H:%M:%S */
  341. case 'X' :
  342. s = strptime (buf, "%H:%M:%S", timeptr);
  343. if (s == NULL)
  344. return NULL;
  345. buf = s;
  346. break;
  347. case 'u' :
  348. ret = strtol (buf, &s, 10);
  349. if (s == buf)
  350. return NULL;
  351. timeptr->tm_wday = ret - 1;
  352. buf = s;
  353. break;
  354. case 'w' :
  355. ret = strtol (buf, &s, 10);
  356. if (s == buf)
  357. return NULL;
  358. timeptr->tm_wday = ret;
  359. buf = s;
  360. break;
  361. case 'U' :
  362. ret = strtol (buf, &s, 10);
  363. if (s == buf)
  364. return NULL;
  365. set_week_number_sun (timeptr, ret);
  366. buf = s;
  367. break;
  368. case 'V' :
  369. ret = strtol (buf, &s, 10);
  370. if (s == buf)
  371. return NULL;
  372. set_week_number_mon4 (timeptr, ret);
  373. buf = s;
  374. break;
  375. case 'W' :
  376. ret = strtol (buf, &s, 10);
  377. if (s == buf)
  378. return NULL;
  379. set_week_number_mon (timeptr, ret);
  380. buf = s;
  381. break;
  382. case 'x' :
  383. s = strptime (buf, "%Y:%m:%d", timeptr);
  384. if (s == NULL)
  385. return NULL;
  386. buf = s;
  387. break;
  388. case 'y' :
  389. ret = strtol (buf, &s, 10);
  390. if (s == buf)
  391. return NULL;
  392. if (ret < 70)
  393. timeptr->tm_year = 100 + ret;
  394. else
  395. timeptr->tm_year = ret;
  396. buf = s;
  397. break;
  398. case 'Y' :
  399. ret = strtol (buf, &s, 10);
  400. if (s == buf)
  401. return NULL;
  402. timeptr->tm_year = ret - tm_year_base;
  403. buf = s;
  404. break;
  405. case 'Z' :
  406. /* source: cygwin-1.5.24-2-src/cygwin-1.5.24-2/winsup/cygwin/libc/strptime.cc */
  407. {
  408. const char *cp;
  409. char *zonestr;
  410. for (cp = buf; *cp && isupper((unsigned char)*cp); ++cp) {/*empty*/}
  411. if (cp - buf) {
  412. zonestr = (char *) alloca(cp - buf + 1);
  413. strncpy(zonestr, buf, cp - buf);
  414. zonestr[cp - buf] = '\0';
  415. tzset();
  416. if (0 == strcmp(zonestr, "GMT")) {
  417. *gmt = 1;
  418. } else if (0 == strcmp(zonestr, tzname[0])) {
  419. timeptr->tm_isdst = 0;
  420. } else if (0 == strcmp(zonestr, tzname[1])) {
  421. timeptr->tm_isdst = 1;
  422. } else {
  423. return 0;
  424. }
  425. buf += cp - buf;
  426. }
  427. }
  428. break;
  429. case '\0' :
  430. --format;
  431. /* FALLTHROUGH */
  432. case '%' :
  433. if (*buf == '%')
  434. ++buf;
  435. else
  436. return NULL;
  437. break;
  438. default :
  439. if (*buf == '%' || *++buf == c)
  440. ++buf;
  441. else
  442. return NULL;
  443. break;
  444. }
  445. } else {
  446. if (*buf == c)
  447. ++buf;
  448. else
  449. return NULL;
  450. }
  451. }
  452. return (char *)buf;
  453. }
  454. char *
  455. strptime (const char *buf, const char *format, struct tm *timeptr)
  456. {
  457. char *ret;
  458. int gmt;
  459. gmt = 0;
  460. ret = _strptime(buf, format, timeptr, &gmt);
  461. if (ret && gmt) {
  462. time_t t = timegm(timeptr);
  463. localtime_r(&t, timeptr);
  464. }
  465. return (ret);
  466. }