parsedate.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /*
  23. A brief summary of the date string formats this parser groks:
  24. RFC 2616 3.3.1
  25. Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
  26. Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
  27. Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
  28. we support dates without week day name:
  29. 06 Nov 1994 08:49:37 GMT
  30. 06-Nov-94 08:49:37 GMT
  31. Nov 6 08:49:37 1994
  32. without the time zone:
  33. 06 Nov 1994 08:49:37
  34. 06-Nov-94 08:49:37
  35. weird order:
  36. 1994 Nov 6 08:49:37 (GNU date fails)
  37. GMT 08:49:37 06-Nov-94 Sunday
  38. 94 6 Nov 08:49:37 (GNU date fails)
  39. time left out:
  40. 1994 Nov 6
  41. 06-Nov-94
  42. Sun Nov 6 94
  43. unusual separators:
  44. 1994.Nov.6
  45. Sun/Nov/6/94/GMT
  46. commonly used time zone names:
  47. Sun, 06 Nov 1994 08:49:37 CET
  48. 06 Nov 1994 08:49:37 EST
  49. time zones specified using RFC822 style:
  50. Sun, 12 Sep 2004 15:05:58 -0700
  51. Sat, 11 Sep 2004 21:32:11 +0200
  52. compact numerical date strings:
  53. 20040912 15:05:58 -0700
  54. 20040911 +0200
  55. */
  56. #include "setup.h"
  57. #include <curl/curl.h>
  58. #include "rawstr.h"
  59. #include "warnless.h"
  60. #include "parsedate.h"
  61. const char * const Curl_wkday[] =
  62. {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
  63. static const char * const weekday[] =
  64. { "Monday", "Tuesday", "Wednesday", "Thursday",
  65. "Friday", "Saturday", "Sunday" };
  66. const char * const Curl_month[]=
  67. { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  68. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  69. struct tzinfo {
  70. char name[5];
  71. int offset; /* +/- in minutes */
  72. };
  73. /*
  74. * parsedate()
  75. *
  76. * Returns:
  77. *
  78. * PARSEDATE_OK - a fine conversion
  79. * PARSEDATE_FAIL - failed to convert
  80. * PARSEDATE_LATER - time overflow at the far end of time_t
  81. * PARSEDATE_SOONER - time underflow at the low end of time_t
  82. */
  83. static int parsedate(const char *date, time_t *output);
  84. #define PARSEDATE_OK 0
  85. #define PARSEDATE_FAIL -1
  86. #define PARSEDATE_LATER 1
  87. #define PARSEDATE_SOONER 2
  88. /* Here's a bunch of frequently used time zone names. These were supported
  89. by the old getdate parser. */
  90. #define tDAYZONE -60 /* offset for daylight savings time */
  91. static const struct tzinfo tz[]= {
  92. {"GMT", 0}, /* Greenwich Mean */
  93. {"UTC", 0}, /* Universal (Coordinated) */
  94. {"WET", 0}, /* Western European */
  95. {"BST", 0 tDAYZONE}, /* British Summer */
  96. {"WAT", 60}, /* West Africa */
  97. {"AST", 240}, /* Atlantic Standard */
  98. {"ADT", 240 tDAYZONE}, /* Atlantic Daylight */
  99. {"EST", 300}, /* Eastern Standard */
  100. {"EDT", 300 tDAYZONE}, /* Eastern Daylight */
  101. {"CST", 360}, /* Central Standard */
  102. {"CDT", 360 tDAYZONE}, /* Central Daylight */
  103. {"MST", 420}, /* Mountain Standard */
  104. {"MDT", 420 tDAYZONE}, /* Mountain Daylight */
  105. {"PST", 480}, /* Pacific Standard */
  106. {"PDT", 480 tDAYZONE}, /* Pacific Daylight */
  107. {"YST", 540}, /* Yukon Standard */
  108. {"YDT", 540 tDAYZONE}, /* Yukon Daylight */
  109. {"HST", 600}, /* Hawaii Standard */
  110. {"HDT", 600 tDAYZONE}, /* Hawaii Daylight */
  111. {"CAT", 600}, /* Central Alaska */
  112. {"AHST", 600}, /* Alaska-Hawaii Standard */
  113. {"NT", 660}, /* Nome */
  114. {"IDLW", 720}, /* International Date Line West */
  115. {"CET", -60}, /* Central European */
  116. {"MET", -60}, /* Middle European */
  117. {"MEWT", -60}, /* Middle European Winter */
  118. {"MEST", -60 tDAYZONE}, /* Middle European Summer */
  119. {"CEST", -60 tDAYZONE}, /* Central European Summer */
  120. {"MESZ", -60 tDAYZONE}, /* Middle European Summer */
  121. {"FWT", -60}, /* French Winter */
  122. {"FST", -60 tDAYZONE}, /* French Summer */
  123. {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
  124. {"WAST", -420}, /* West Australian Standard */
  125. {"WADT", -420 tDAYZONE}, /* West Australian Daylight */
  126. {"CCT", -480}, /* China Coast, USSR Zone 7 */
  127. {"JST", -540}, /* Japan Standard, USSR Zone 8 */
  128. {"EAST", -600}, /* Eastern Australian Standard */
  129. {"EADT", -600 tDAYZONE}, /* Eastern Australian Daylight */
  130. {"GST", -600}, /* Guam Standard, USSR Zone 9 */
  131. {"NZT", -720}, /* New Zealand */
  132. {"NZST", -720}, /* New Zealand Standard */
  133. {"NZDT", -720 tDAYZONE}, /* New Zealand Daylight */
  134. {"IDLE", -720}, /* International Date Line East */
  135. /* Next up: Military timezone names. RFC822 allowed these, but (as noted in
  136. RFC 1123) had their signs wrong. Here we use the correct signs to match
  137. actual military usage.
  138. */
  139. {"A", +1 * 60}, /* Alpha */
  140. {"B", +2 * 60}, /* Bravo */
  141. {"C", +3 * 60}, /* Charlie */
  142. {"D", +4 * 60}, /* Delta */
  143. {"E", +5 * 60}, /* Echo */
  144. {"F", +6 * 60}, /* Foxtrot */
  145. {"G", +7 * 60}, /* Golf */
  146. {"H", +8 * 60}, /* Hotel */
  147. {"I", +9 * 60}, /* India */
  148. /* "J", Juliet is not used as a timezone, to indicate the observer's local
  149. time */
  150. {"K", +10 * 60}, /* Kilo */
  151. {"L", +11 * 60}, /* Lima */
  152. {"M", +12 * 60}, /* Mike */
  153. {"N", -1 * 60}, /* November */
  154. {"O", -2 * 60}, /* Oscar */
  155. {"P", -3 * 60}, /* Papa */
  156. {"Q", -4 * 60}, /* Quebec */
  157. {"R", -5 * 60}, /* Romeo */
  158. {"S", -6 * 60}, /* Sierra */
  159. {"T", -7 * 60}, /* Tango */
  160. {"U", -8 * 60}, /* Uniform */
  161. {"V", -9 * 60}, /* Victor */
  162. {"W", -10 * 60}, /* Whiskey */
  163. {"X", -11 * 60}, /* X-ray */
  164. {"Y", -12 * 60}, /* Yankee */
  165. {"Z", 0}, /* Zulu, zero meridian, a.k.a. UTC */
  166. };
  167. /* returns:
  168. -1 no day
  169. 0 monday - 6 sunday
  170. */
  171. static int checkday(const char *check, size_t len)
  172. {
  173. int i;
  174. const char * const *what;
  175. bool found= FALSE;
  176. if(len > 3)
  177. what = &weekday[0];
  178. else
  179. what = &Curl_wkday[0];
  180. for(i=0; i<7; i++) {
  181. if(Curl_raw_equal(check, what[0])) {
  182. found=TRUE;
  183. break;
  184. }
  185. what++;
  186. }
  187. return found?i:-1;
  188. }
  189. static int checkmonth(const char *check)
  190. {
  191. int i;
  192. const char * const *what;
  193. bool found= FALSE;
  194. what = &Curl_month[0];
  195. for(i=0; i<12; i++) {
  196. if(Curl_raw_equal(check, what[0])) {
  197. found=TRUE;
  198. break;
  199. }
  200. what++;
  201. }
  202. return found?i:-1; /* return the offset or -1, no real offset is -1 */
  203. }
  204. /* return the time zone offset between GMT and the input one, in number
  205. of seconds or -1 if the timezone wasn't found/legal */
  206. static int checktz(const char *check)
  207. {
  208. unsigned int i;
  209. const struct tzinfo *what;
  210. bool found= FALSE;
  211. what = tz;
  212. for(i=0; i< sizeof(tz)/sizeof(tz[0]); i++) {
  213. if(Curl_raw_equal(check, what->name)) {
  214. found=TRUE;
  215. break;
  216. }
  217. what++;
  218. }
  219. return found?what->offset*60:-1;
  220. }
  221. static void skip(const char **date)
  222. {
  223. /* skip everything that aren't letters or digits */
  224. while(**date && !ISALNUM(**date))
  225. (*date)++;
  226. }
  227. enum assume {
  228. DATE_MDAY,
  229. DATE_YEAR,
  230. DATE_TIME
  231. };
  232. /* this is a clone of 'struct tm' but with all fields we don't need or use
  233. cut out */
  234. struct my_tm {
  235. int tm_sec;
  236. int tm_min;
  237. int tm_hour;
  238. int tm_mday;
  239. int tm_mon;
  240. int tm_year;
  241. };
  242. /* struct tm to time since epoch in GMT time zone.
  243. * This is similar to the standard mktime function but for GMT only, and
  244. * doesn't suffer from the various bugs and portability problems that
  245. * some systems' implementations have.
  246. */
  247. static time_t my_timegm(struct my_tm *tm)
  248. {
  249. static const int month_days_cumulative [12] =
  250. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  251. int month, year, leap_days;
  252. if(tm->tm_year < 70)
  253. /* we don't support years before 1970 as they will cause this function
  254. to return a negative value */
  255. return -1;
  256. year = tm->tm_year + 1900;
  257. month = tm->tm_mon;
  258. if(month < 0) {
  259. year += (11 - month) / 12;
  260. month = 11 - (11 - month) % 12;
  261. }
  262. else if(month >= 12) {
  263. year -= month / 12;
  264. month = month % 12;
  265. }
  266. leap_days = year - (tm->tm_mon <= 1);
  267. leap_days = ((leap_days / 4) - (leap_days / 100) + (leap_days / 400)
  268. - (1969 / 4) + (1969 / 100) - (1969 / 400));
  269. return ((((time_t) (year - 1970) * 365
  270. + leap_days + month_days_cumulative [month] + tm->tm_mday - 1) * 24
  271. + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec;
  272. }
  273. /*
  274. * parsedate()
  275. *
  276. * Returns:
  277. *
  278. * PARSEDATE_OK - a fine conversion
  279. * PARSEDATE_FAIL - failed to convert
  280. * PARSEDATE_LATER - time overflow at the far end of time_t
  281. * PARSEDATE_SOONER - time underflow at the low end of time_t
  282. */
  283. static int parsedate(const char *date, time_t *output)
  284. {
  285. time_t t = 0;
  286. int wdaynum=-1; /* day of the week number, 0-6 (mon-sun) */
  287. int monnum=-1; /* month of the year number, 0-11 */
  288. int mdaynum=-1; /* day of month, 1 - 31 */
  289. int hournum=-1;
  290. int minnum=-1;
  291. int secnum=-1;
  292. int yearnum=-1;
  293. int tzoff=-1;
  294. struct my_tm tm;
  295. enum assume dignext = DATE_MDAY;
  296. const char *indate = date; /* save the original pointer */
  297. int part = 0; /* max 6 parts */
  298. while(*date && (part < 6)) {
  299. bool found=FALSE;
  300. skip(&date);
  301. if(ISALPHA(*date)) {
  302. /* a name coming up */
  303. char buf[32]="";
  304. size_t len;
  305. sscanf(date, "%31[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]",
  306. buf);
  307. len = strlen(buf);
  308. if(wdaynum == -1) {
  309. wdaynum = checkday(buf, len);
  310. if(wdaynum != -1)
  311. found = TRUE;
  312. }
  313. if(!found && (monnum == -1)) {
  314. monnum = checkmonth(buf);
  315. if(monnum != -1)
  316. found = TRUE;
  317. }
  318. if(!found && (tzoff == -1)) {
  319. /* this just must be a time zone string */
  320. tzoff = checktz(buf);
  321. if(tzoff != -1)
  322. found = TRUE;
  323. }
  324. if(!found)
  325. return PARSEDATE_FAIL; /* bad string */
  326. date += len;
  327. }
  328. else if(ISDIGIT(*date)) {
  329. /* a digit */
  330. int val;
  331. char *end;
  332. if((secnum == -1) &&
  333. (3 == sscanf(date, "%02d:%02d:%02d", &hournum, &minnum, &secnum))) {
  334. /* time stamp! */
  335. date += 8;
  336. }
  337. else if((secnum == -1) &&
  338. (2 == sscanf(date, "%02d:%02d", &hournum, &minnum))) {
  339. /* time stamp without seconds */
  340. date += 5;
  341. secnum = 0;
  342. }
  343. else {
  344. val = curlx_sltosi(strtol(date, &end, 10));
  345. if((tzoff == -1) &&
  346. ((end - date) == 4) &&
  347. (val <= 1400) &&
  348. (indate< date) &&
  349. ((date[-1] == '+' || date[-1] == '-'))) {
  350. /* four digits and a value less than or equal to 1400 (to take into
  351. account all sorts of funny time zone diffs) and it is preceded
  352. with a plus or minus. This is a time zone indication. 1400 is
  353. picked since +1300 is frequently used and +1400 is mentioned as
  354. an edge number in the document "ISO C 200X Proposal: Timezone
  355. Functions" at http://david.tribble.com/text/c0xtimezone.html If
  356. anyone has a more authoritative source for the exact maximum time
  357. zone offsets, please speak up! */
  358. found = TRUE;
  359. tzoff = (val/100 * 60 + val%100)*60;
  360. /* the + and - prefix indicates the local time compared to GMT,
  361. this we need ther reversed math to get what we want */
  362. tzoff = date[-1]=='+'?-tzoff:tzoff;
  363. }
  364. if(((end - date) == 8) &&
  365. (yearnum == -1) &&
  366. (monnum == -1) &&
  367. (mdaynum == -1)) {
  368. /* 8 digits, no year, month or day yet. This is YYYYMMDD */
  369. found = TRUE;
  370. yearnum = val/10000;
  371. monnum = (val%10000)/100-1; /* month is 0 - 11 */
  372. mdaynum = val%100;
  373. }
  374. if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) {
  375. if((val > 0) && (val<32)) {
  376. mdaynum = val;
  377. found = TRUE;
  378. }
  379. dignext = DATE_YEAR;
  380. }
  381. if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
  382. yearnum = val;
  383. found = TRUE;
  384. if(yearnum < 1900) {
  385. if(yearnum > 70)
  386. yearnum += 1900;
  387. else
  388. yearnum += 2000;
  389. }
  390. if(mdaynum == -1)
  391. dignext = DATE_MDAY;
  392. }
  393. if(!found)
  394. return PARSEDATE_FAIL;
  395. date = end;
  396. }
  397. }
  398. part++;
  399. }
  400. if(-1 == secnum)
  401. secnum = minnum = hournum = 0; /* no time, make it zero */
  402. if((-1 == mdaynum) ||
  403. (-1 == monnum) ||
  404. (-1 == yearnum))
  405. /* lacks vital info, fail */
  406. return PARSEDATE_FAIL;
  407. #if SIZEOF_TIME_T < 5
  408. /* 32 bit time_t can only hold dates to the beginning of 2038 */
  409. if(yearnum > 2037) {
  410. *output = 0x7fffffff;
  411. return PARSEDATE_LATER;
  412. }
  413. #endif
  414. if(yearnum < 1970) {
  415. *output = 0;
  416. return PARSEDATE_SOONER;
  417. }
  418. if((mdaynum > 31) || (monnum > 11) ||
  419. (hournum > 23) || (minnum > 59) || (secnum > 60))
  420. return PARSEDATE_FAIL; /* clearly an illegal date */
  421. tm.tm_sec = secnum;
  422. tm.tm_min = minnum;
  423. tm.tm_hour = hournum;
  424. tm.tm_mday = mdaynum;
  425. tm.tm_mon = monnum;
  426. tm.tm_year = yearnum - 1900;
  427. /* my_timegm() returns a time_t. time_t is often 32 bits, even on many
  428. architectures that feature 64 bit 'long'.
  429. Some systems have 64 bit time_t and deal with years beyond 2038. However,
  430. even on some of the systems with 64 bit time_t mktime() returns -1 for
  431. dates beyond 03:14:07 UTC, January 19, 2038. (Such as AIX 5100-06)
  432. */
  433. t = my_timegm(&tm);
  434. /* time zone adjust (cast t to int to compare to negative one) */
  435. if(-1 != (int)t) {
  436. /* Add the time zone diff between local time zone and GMT. */
  437. long delta = (long)(tzoff!=-1?tzoff:0);
  438. if((delta>0) && (t + delta < t))
  439. return -1; /* time_t overflow */
  440. t += delta;
  441. }
  442. *output = t;
  443. return PARSEDATE_OK;
  444. }
  445. time_t curl_getdate(const char *p, const time_t *now)
  446. {
  447. time_t parsed;
  448. int rc = parsedate(p, &parsed);
  449. (void)now; /* legacy argument from the past that we ignore */
  450. switch(rc) {
  451. case PARSEDATE_OK:
  452. case PARSEDATE_LATER:
  453. case PARSEDATE_SOONER:
  454. return parsed;
  455. }
  456. /* everything else is fail */
  457. return -1;
  458. }
  459. /*
  460. * Curl_gmtime() is a gmtime() replacement for portability. Do not use the
  461. * gmtime_r() or gmtime() functions anywhere else but here.
  462. *
  463. * To make sure no such function calls slip in, we define them to cause build
  464. * errors, which is why we use the name within parentheses in this function.
  465. *
  466. */
  467. CURLcode Curl_gmtime(time_t intime, struct tm *store)
  468. {
  469. const struct tm *tm;
  470. #ifdef HAVE_GMTIME_R
  471. /* thread-safe version */
  472. tm = (struct tm *)gmtime_r(&intime, store);
  473. #else
  474. tm = gmtime(&intime);
  475. if(tm)
  476. *store = *tm; /* copy the pointed struct to the local copy */
  477. #endif
  478. if(!tm)
  479. return CURLE_BAD_FUNCTION_ARGUMENT;
  480. return CURLE_OK;
  481. }