parsedate.c 14 KB

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