strptime.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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[] = {"Sun", "Mon", "Tue", "Wed",
  55. "Thu", "Fri", "Sat", NULL};
  56. static const char* full_weekdays[] = {"Sunday", "Monday", "Tuesday",
  57. "Wednesday", "Thursday", "Friday",
  58. "Saturday", NULL};
  59. static const char* abb_month[] = {"Jan", "Feb", "Mar", "Apr", "May",
  60. "Jun", "Jul", "Aug", "Sep", "Oct",
  61. "Nov", "Dec", NULL};
  62. static const char* full_month[] = {
  63. "January", "February", "March", "April", "May", "June", "July",
  64. "August", "September", "October", "November", "December", NULL,
  65. };
  66. static const char* ampm[] = {"am", "pm", NULL};
  67. /*
  68. * tm_year is relative this year
  69. */
  70. const int tm_year_base = 1900;
  71. /*
  72. * Return TRUE iff `year' was a leap year.
  73. * Needed for strptime.
  74. */
  75. static int is_leap_year(int year)
  76. {
  77. return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
  78. }
  79. /* Needed for strptime. */
  80. static int match_string(const char** buf, const char** strs)
  81. {
  82. int i = 0;
  83. for (i = 0; strs[i] != NULL; ++i) {
  84. int len = strlen(strs[i]);
  85. if (strncasecmp(*buf, strs[i], len) == 0) {
  86. *buf += len;
  87. return i;
  88. }
  89. }
  90. return -1;
  91. }
  92. /* Needed for strptime. */
  93. static int first_day(int year)
  94. {
  95. int ret = 4;
  96. for (; year > 1970; --year)
  97. ret = (ret + 365 + is_leap_year(year) ? 1 : 0) % 7;
  98. return ret;
  99. }
  100. /*
  101. * Set `timeptr' given `wnum' (week number [0, 53])
  102. * Needed for strptime
  103. */
  104. static void set_week_number_sun(struct tm* timeptr, int wnum)
  105. {
  106. int fday = first_day(timeptr->tm_year + tm_year_base);
  107. timeptr->tm_yday = wnum * 7 + timeptr->tm_wday - fday;
  108. if (timeptr->tm_yday < 0) {
  109. timeptr->tm_wday = fday;
  110. timeptr->tm_yday = 0;
  111. }
  112. }
  113. /*
  114. * Set `timeptr' given `wnum' (week number [0, 53])
  115. * Needed for strptime
  116. */
  117. static void set_week_number_mon(struct tm* timeptr, int wnum)
  118. {
  119. int fday = (first_day(timeptr->tm_year + tm_year_base) + 6) % 7;
  120. timeptr->tm_yday = wnum * 7 + (timeptr->tm_wday + 6) % 7 - fday;
  121. if (timeptr->tm_yday < 0) {
  122. timeptr->tm_wday = (fday + 1) % 7;
  123. timeptr->tm_yday = 0;
  124. }
  125. }
  126. /*
  127. * Set `timeptr' given `wnum' (week number [0, 53])
  128. * Needed for strptime
  129. */
  130. static void set_week_number_mon4(struct tm* timeptr, int wnum)
  131. {
  132. int fday = (first_day(timeptr->tm_year + tm_year_base) + 6) % 7;
  133. int offset = 0;
  134. if (fday < 4)
  135. offset += 7;
  136. timeptr->tm_yday = offset + (wnum - 1) * 7 + timeptr->tm_wday - fday;
  137. if (timeptr->tm_yday < 0) {
  138. timeptr->tm_wday = fday;
  139. timeptr->tm_yday = 0;
  140. }
  141. }
  142. /* strptime: roken */
  143. // extern "C"
  144. static char* _strptime(const char* buf, const char* format, struct tm* timeptr,
  145. int* gmt)
  146. {
  147. char c;
  148. for (; (c = *format) != '\0'; ++format) {
  149. char* s;
  150. int ret;
  151. if (isspace(c)) {
  152. while (isspace(*buf))
  153. ++buf;
  154. }
  155. else if (c == '%' && format[1] != '\0') {
  156. c = *++format;
  157. if (c == 'E' || c == 'O')
  158. c = *++format;
  159. switch (c) {
  160. case 'A':
  161. case 'a':
  162. ret = match_string(&buf, full_weekdays);
  163. if (ret < 0)
  164. ret = match_string(&buf, abb_weekdays);
  165. if (ret < 0)
  166. return NULL;
  167. timeptr->tm_wday = ret;
  168. break;
  169. case 'B':
  170. case 'b':
  171. case 'h':
  172. ret = match_string(&buf, full_month);
  173. if (ret < 0)
  174. ret = match_string(&buf, abb_month);
  175. if (ret < 0)
  176. return NULL;
  177. timeptr->tm_mon = ret;
  178. break;
  179. case 'C':
  180. ret = strtol(buf, &s, 10);
  181. if (s == buf)
  182. return NULL;
  183. timeptr->tm_year = (ret * 100) - tm_year_base;
  184. buf = s;
  185. break;
  186. case 'c': /* %a %b %e %H:%M:%S %Y */
  187. s = strptime(buf, "%a %b %e %H:%M:%S %Y", timeptr);
  188. if (s == NULL)
  189. return NULL;
  190. buf = s;
  191. break;
  192. case 'D': /* %m/%d/%y */
  193. s = strptime(buf, "%m/%d/%y", timeptr);
  194. if (s == NULL)
  195. return NULL;
  196. buf = s;
  197. break;
  198. case 'd':
  199. case 'e':
  200. ret = strtol(buf, &s, 10);
  201. if (s == buf)
  202. return NULL;
  203. timeptr->tm_mday = ret;
  204. buf = s;
  205. break;
  206. case 'H':
  207. case 'k':
  208. ret = strtol(buf, &s, 10);
  209. if (s == buf)
  210. return NULL;
  211. timeptr->tm_hour = ret;
  212. buf = s;
  213. break;
  214. case 'I':
  215. case 'l':
  216. ret = strtol(buf, &s, 10);
  217. if (s == buf)
  218. return NULL;
  219. if (ret == 12)
  220. timeptr->tm_hour = 0;
  221. else
  222. timeptr->tm_hour = ret;
  223. buf = s;
  224. break;
  225. case 'j':
  226. ret = strtol(buf, &s, 10);
  227. if (s == buf)
  228. return NULL;
  229. timeptr->tm_yday = ret - 1;
  230. buf = s;
  231. break;
  232. case 'm':
  233. ret = strtol(buf, &s, 10);
  234. if (s == buf)
  235. return NULL;
  236. timeptr->tm_mon = ret - 1;
  237. buf = s;
  238. break;
  239. case 'M':
  240. ret = strtol(buf, &s, 10);
  241. if (s == buf)
  242. return NULL;
  243. timeptr->tm_min = ret;
  244. buf = s;
  245. break;
  246. case 'n':
  247. if (*buf == '\n')
  248. ++buf;
  249. else
  250. return NULL;
  251. break;
  252. case 'p':
  253. ret = match_string(&buf, ampm);
  254. if (ret < 0)
  255. return NULL;
  256. if (timeptr->tm_hour == 0) {
  257. if (ret == 1)
  258. timeptr->tm_hour = 12;
  259. }
  260. else
  261. timeptr->tm_hour += 12;
  262. break;
  263. case 'r': /* %I:%M:%S %p */
  264. s = strptime(buf, "%I:%M:%S %p", timeptr);
  265. if (s == NULL)
  266. return NULL;
  267. buf = s;
  268. break;
  269. case 'R': /* %H:%M */
  270. s = strptime(buf, "%H:%M", timeptr);
  271. if (s == NULL)
  272. return NULL;
  273. buf = s;
  274. break;
  275. case 'S':
  276. ret = strtol(buf, &s, 10);
  277. if (s == buf)
  278. return NULL;
  279. timeptr->tm_sec = ret;
  280. buf = s;
  281. break;
  282. case 't':
  283. if (*buf == '\t')
  284. ++buf;
  285. else
  286. return NULL;
  287. break;
  288. case 'T': /* %H:%M:%S */
  289. case 'X':
  290. s = strptime(buf, "%H:%M:%S", timeptr);
  291. if (s == NULL)
  292. return NULL;
  293. buf = s;
  294. break;
  295. case 'u':
  296. ret = strtol(buf, &s, 10);
  297. if (s == buf)
  298. return NULL;
  299. timeptr->tm_wday = ret - 1;
  300. buf = s;
  301. break;
  302. case 'w':
  303. ret = strtol(buf, &s, 10);
  304. if (s == buf)
  305. return NULL;
  306. timeptr->tm_wday = ret;
  307. buf = s;
  308. break;
  309. case 'U':
  310. ret = strtol(buf, &s, 10);
  311. if (s == buf)
  312. return NULL;
  313. set_week_number_sun(timeptr, ret);
  314. buf = s;
  315. break;
  316. case 'V':
  317. ret = strtol(buf, &s, 10);
  318. if (s == buf)
  319. return NULL;
  320. set_week_number_mon4(timeptr, ret);
  321. buf = s;
  322. break;
  323. case 'W':
  324. ret = strtol(buf, &s, 10);
  325. if (s == buf)
  326. return NULL;
  327. set_week_number_mon(timeptr, ret);
  328. buf = s;
  329. break;
  330. case 'x':
  331. s = strptime(buf, "%Y:%m:%d", timeptr);
  332. if (s == NULL)
  333. return NULL;
  334. buf = s;
  335. break;
  336. case 'y':
  337. ret = strtol(buf, &s, 10);
  338. if (s == buf)
  339. return NULL;
  340. /*
  341. * y represents stricty 2 digits, raise error if more than 3
  342. * digits are parsed.
  343. */
  344. if (ret > 99) {
  345. return NULL;
  346. }
  347. /*
  348. * The value in the range 69-99 refer to years in 20th century.
  349. * The value in the range 00-68 refer to years in 21st century.
  350. */
  351. if (ret < 69)
  352. timeptr->tm_year = 100 + ret;
  353. else
  354. timeptr->tm_year = ret;
  355. buf = s;
  356. break;
  357. case 'Y':
  358. ret = strtol(buf, &s, 10);
  359. if (s == buf)
  360. return NULL;
  361. timeptr->tm_year = ret - tm_year_base;
  362. buf = s;
  363. break;
  364. case 'Z':
  365. /* source:
  366. * cygwin-1.5.24-2-src/cygwin-1.5.24-2/winsup/cygwin/libc/strptime.cc */
  367. {
  368. const char* cp;
  369. char* zonestr;
  370. for (cp = buf; *cp && isupper((unsigned char)*cp); ++cp) { /*empty*/
  371. }
  372. if (cp - buf) {
  373. zonestr = (char*)alloca(cp - buf + 1);
  374. strncpy(zonestr, buf, cp - buf);
  375. zonestr[cp - buf] = '\0';
  376. tzset();
  377. if (0 == strcmp(zonestr, "GMT")) {
  378. *gmt = 1;
  379. }
  380. else if (0 == strcmp(zonestr, tzname[0])) {
  381. timeptr->tm_isdst = 0;
  382. }
  383. else if (0 == strcmp(zonestr, tzname[1])) {
  384. timeptr->tm_isdst = 1;
  385. }
  386. else {
  387. return 0;
  388. }
  389. buf += cp - buf;
  390. }
  391. }
  392. break;
  393. case '\0':
  394. --format;
  395. /* FALLTHROUGH */
  396. case '%':
  397. if (*buf == '%')
  398. ++buf;
  399. else
  400. return NULL;
  401. break;
  402. default:
  403. if (*buf == '%' || *++buf == c)
  404. ++buf;
  405. else
  406. return NULL;
  407. break;
  408. }
  409. }
  410. else {
  411. if (*buf == c)
  412. ++buf;
  413. else
  414. return NULL;
  415. }
  416. }
  417. return (char*)buf;
  418. }
  419. char* strptime(const char* buf, const char* format, struct tm* timeptr)
  420. {
  421. char* ret;
  422. int gmt;
  423. gmt = 0;
  424. ret = _strptime(buf, format, timeptr, &gmt);
  425. return (ret);
  426. }