parsedate.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, 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. * $Id$
  22. ***************************************************************************/
  23. /*
  24. A brief summary of the date string formats this parser groks:
  25. RFC 2616 3.3.1
  26. Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
  27. Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
  28. Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
  29. we support dates without week day name:
  30. 06 Nov 1994 08:49:37 GMT
  31. 06-Nov-94 08:49:37 GMT
  32. Nov 6 08:49:37 1994
  33. without the time zone:
  34. 06 Nov 1994 08:49:37
  35. 06-Nov-94 08:49:37
  36. weird order:
  37. 1994 Nov 6 08:49:37 (GNU date fails)
  38. GMT 08:49:37 06-Nov-94 Sunday
  39. 94 6 Nov 08:49:37 (GNU date fails)
  40. time left out:
  41. 1994 Nov 6
  42. 06-Nov-94
  43. Sun Nov 6 94
  44. unusual separators:
  45. 1994.Nov.6
  46. Sun/Nov/6/94/GMT
  47. commonly used time zone names:
  48. Sun, 06 Nov 1994 08:49:37 CET
  49. 06 Nov 1994 08:49:37 EST
  50. time zones specified using RFC822 style:
  51. Sun, 12 Sep 2004 15:05:58 -0700
  52. Sat, 11 Sep 2004 21:32:11 +0200
  53. compact numerical date strings:
  54. 20040912 15:05:58 -0700
  55. 20040911 +0200
  56. */
  57. #include "setup.h"
  58. #include <stdio.h>
  59. #include <ctype.h>
  60. #include <string.h>
  61. #ifdef HAVE_STDLIB_H
  62. #include <stdlib.h> /* for strtol() */
  63. #endif
  64. #include <curl/curl.h>
  65. #include "rawstr.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. found = TRUE;
  327. }
  328. else {
  329. val = (int)strtol(date, &end, 10);
  330. if((tzoff == -1) &&
  331. ((end - date) == 4) &&
  332. (val <= 1400) &&
  333. (indate< date) &&
  334. ((date[-1] == '+' || date[-1] == '-'))) {
  335. /* four digits and a value less than or equal to 1400 (to take into
  336. account all sorts of funny time zone diffs) and it is preceeded
  337. with a plus or minus. This is a time zone indication. 1400 is
  338. picked since +1300 is frequently used and +1400 is mentioned as
  339. an edge number in the document "ISO C 200X Proposal: Timezone
  340. Functions" at http://david.tribble.com/text/c0xtimezone.html If
  341. anyone has a more authoritative source for the exact maximum time
  342. zone offsets, please speak up! */
  343. found = TRUE;
  344. tzoff = (val/100 * 60 + val%100)*60;
  345. /* the + and - prefix indicates the local time compared to GMT,
  346. this we need ther reversed math to get what we want */
  347. tzoff = date[-1]=='+'?-tzoff:tzoff;
  348. }
  349. if(((end - date) == 8) &&
  350. (yearnum == -1) &&
  351. (monnum == -1) &&
  352. (mdaynum == -1)) {
  353. /* 8 digits, no year, month or day yet. This is YYYYMMDD */
  354. found = TRUE;
  355. yearnum = val/10000;
  356. monnum = (val%10000)/100-1; /* month is 0 - 11 */
  357. mdaynum = val%100;
  358. }
  359. if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) {
  360. if((val > 0) && (val<32)) {
  361. mdaynum = val;
  362. found = TRUE;
  363. }
  364. dignext = DATE_YEAR;
  365. }
  366. if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
  367. yearnum = val;
  368. found = TRUE;
  369. if(yearnum < 1900) {
  370. if(yearnum > 70)
  371. yearnum += 1900;
  372. else
  373. yearnum += 2000;
  374. }
  375. if(mdaynum == -1)
  376. dignext = DATE_MDAY;
  377. }
  378. if(!found)
  379. return PARSEDATE_FAIL;
  380. date = end;
  381. }
  382. }
  383. part++;
  384. }
  385. if(-1 == secnum)
  386. secnum = minnum = hournum = 0; /* no time, make it zero */
  387. if((-1 == mdaynum) ||
  388. (-1 == monnum) ||
  389. (-1 == yearnum))
  390. /* lacks vital info, fail */
  391. return PARSEDATE_FAIL;
  392. #if SIZEOF_TIME_T < 5
  393. /* 32 bit time_t can only hold dates to the beginning of 2038 */
  394. if(yearnum > 2037) {
  395. *output = 0x7fffffff;
  396. return PARSEDATE_LATER;
  397. }
  398. #endif
  399. if(yearnum < 1970) {
  400. *output = 0;
  401. return PARSEDATE_SOONER;
  402. }
  403. tm.tm_sec = secnum;
  404. tm.tm_min = minnum;
  405. tm.tm_hour = hournum;
  406. tm.tm_mday = mdaynum;
  407. tm.tm_mon = monnum;
  408. tm.tm_year = yearnum - 1900;
  409. /* my_timegm() returns a time_t. time_t is often 32 bits, even on many
  410. architectures that feature 64 bit 'long'.
  411. Some systems have 64 bit time_t and deal with years beyond 2038. However,
  412. even on some of the systems with 64 bit time_t mktime() returns -1 for
  413. dates beyond 03:14:07 UTC, January 19, 2038. (Such as AIX 5100-06)
  414. */
  415. t = my_timegm(&tm);
  416. /* time zone adjust (cast t to int to compare to negative one) */
  417. if(-1 != (int)t) {
  418. /* Add the time zone diff between local time zone and GMT. */
  419. long delta = (long)(tzoff!=-1?tzoff:0);
  420. if((delta>0) && (t + delta < t))
  421. return -1; /* time_t overflow */
  422. t += delta;
  423. }
  424. *output = t;
  425. return PARSEDATE_OK;
  426. }
  427. time_t curl_getdate(const char *p, const time_t *now)
  428. {
  429. time_t parsed;
  430. int rc = Curl_parsedate(p, &parsed);
  431. (void)now; /* legacy argument from the past that we ignore */
  432. switch(rc) {
  433. case PARSEDATE_OK:
  434. case PARSEDATE_LATER:
  435. case PARSEDATE_SOONER:
  436. return parsed;
  437. }
  438. /* everything else is fail */
  439. return -1;
  440. }