parsedate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2006, 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. static time_t Curl_parsedate(const char *date);
  66. const char * const Curl_wkday[] =
  67. {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
  68. static const char * const weekday[] =
  69. { "Monday", "Tuesday", "Wednesday", "Thursday",
  70. "Friday", "Saturday", "Sunday" };
  71. const char * const Curl_month[]=
  72. { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  73. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  74. struct tzinfo {
  75. const char *name;
  76. int offset; /* +/- in minutes */
  77. };
  78. /* Here's a bunch of frequently used time zone names. These were supported
  79. by the old getdate parser. */
  80. #define tDAYZONE -60 /* offset for daylight savings time */
  81. static const struct tzinfo tz[]= {
  82. {"GMT", 0}, /* Greenwich Mean */
  83. {"UTC", 0}, /* Universal (Coordinated) */
  84. {"WET", 0}, /* Western European */
  85. {"BST", 0 tDAYZONE}, /* British Summer */
  86. {"WAT", 60}, /* West Africa */
  87. {"AST", 240}, /* Atlantic Standard */
  88. {"ADT", 240 tDAYZONE}, /* Atlantic Daylight */
  89. {"EST", 300}, /* Eastern Standard */
  90. {"EDT", 300 tDAYZONE}, /* Eastern Daylight */
  91. {"CST", 360}, /* Central Standard */
  92. {"CDT", 360 tDAYZONE}, /* Central Daylight */
  93. {"MST", 420}, /* Mountain Standard */
  94. {"MDT", 420 tDAYZONE}, /* Mountain Daylight */
  95. {"PST", 480}, /* Pacific Standard */
  96. {"PDT", 480 tDAYZONE}, /* Pacific Daylight */
  97. {"YST", 540}, /* Yukon Standard */
  98. {"YDT", 540 tDAYZONE}, /* Yukon Daylight */
  99. {"HST", 600}, /* Hawaii Standard */
  100. {"HDT", 600 tDAYZONE}, /* Hawaii Daylight */
  101. {"CAT", 600}, /* Central Alaska */
  102. {"AHST", 600}, /* Alaska-Hawaii Standard */
  103. {"NT", 660}, /* Nome */
  104. {"IDLW", 720}, /* International Date Line West */
  105. {"CET", -60}, /* Central European */
  106. {"MET", -60}, /* Middle European */
  107. {"MEWT", -60}, /* Middle European Winter */
  108. {"MEST", -60 tDAYZONE}, /* Middle European Summer */
  109. {"CEST", -60 tDAYZONE}, /* Central European Summer */
  110. {"MESZ", -60 tDAYZONE}, /* Middle European Summer */
  111. {"FWT", -60}, /* French Winter */
  112. {"FST", -60 tDAYZONE}, /* French Summer */
  113. {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
  114. {"WAST", -420}, /* West Australian Standard */
  115. {"WADT", -420 tDAYZONE}, /* West Australian Daylight */
  116. {"CCT", -480}, /* China Coast, USSR Zone 7 */
  117. {"JST", -540}, /* Japan Standard, USSR Zone 8 */
  118. {"EAST", -600}, /* Eastern Australian Standard */
  119. {"EADT", -600 tDAYZONE}, /* Eastern Australian Daylight */
  120. {"GST", -600}, /* Guam Standard, USSR Zone 9 */
  121. {"NZT", -720}, /* New Zealand */
  122. {"NZST", -720}, /* New Zealand Standard */
  123. {"NZDT", -720 tDAYZONE}, /* New Zealand Daylight */
  124. {"IDLE", -720}, /* International Date Line East */
  125. };
  126. /* returns:
  127. -1 no day
  128. 0 monday - 6 sunday
  129. */
  130. static int checkday(char *check, size_t len)
  131. {
  132. int i;
  133. const char * const *what;
  134. bool found= FALSE;
  135. if(len > 3)
  136. what = &weekday[0];
  137. else
  138. what = &Curl_wkday[0];
  139. for(i=0; i<7; i++) {
  140. if(curl_strequal(check, what[0])) {
  141. found=TRUE;
  142. break;
  143. }
  144. what++;
  145. }
  146. return found?i:-1;
  147. }
  148. static int checkmonth(char *check)
  149. {
  150. int i;
  151. const char * const *what;
  152. bool found= FALSE;
  153. what = &Curl_month[0];
  154. for(i=0; i<12; i++) {
  155. if(curl_strequal(check, what[0])) {
  156. found=TRUE;
  157. break;
  158. }
  159. what++;
  160. }
  161. return found?i:-1; /* return the offset or -1, no real offset is -1 */
  162. }
  163. /* return the time zone offset between GMT and the input one, in number
  164. of seconds or -1 if the timezone wasn't found/legal */
  165. static int checktz(char *check)
  166. {
  167. unsigned int i;
  168. const struct tzinfo *what;
  169. bool found= FALSE;
  170. what = tz;
  171. for(i=0; i< sizeof(tz)/sizeof(tz[0]); i++) {
  172. if(curl_strequal(check, what->name)) {
  173. found=TRUE;
  174. break;
  175. }
  176. what++;
  177. }
  178. return found?what->offset*60:-1;
  179. }
  180. static void skip(const char **date)
  181. {
  182. /* skip everything that aren't letters or digits */
  183. while(**date && !ISALNUM(**date))
  184. (*date)++;
  185. }
  186. enum assume {
  187. DATE_MDAY,
  188. DATE_YEAR,
  189. DATE_TIME
  190. };
  191. static time_t Curl_parsedate(const char *date)
  192. {
  193. time_t t = 0;
  194. int wdaynum=-1; /* day of the week number, 0-6 (mon-sun) */
  195. int monnum=-1; /* month of the year number, 0-11 */
  196. int mdaynum=-1; /* day of month, 1 - 31 */
  197. int hournum=-1;
  198. int minnum=-1;
  199. int secnum=-1;
  200. int yearnum=-1;
  201. int tzoff=-1;
  202. struct tm tm;
  203. enum assume dignext = DATE_MDAY;
  204. const char *indate = date; /* save the original pointer */
  205. int part = 0; /* max 6 parts */
  206. while(*date && (part < 6)) {
  207. bool found=FALSE;
  208. skip(&date);
  209. if(ISALPHA(*date)) {
  210. /* a name coming up */
  211. char buf[32]="";
  212. size_t len;
  213. sscanf(date, "%31[A-Za-z]", buf);
  214. len = strlen(buf);
  215. if(wdaynum == -1) {
  216. wdaynum = checkday(buf, len);
  217. if(wdaynum != -1)
  218. found = TRUE;
  219. }
  220. if(!found && (monnum == -1)) {
  221. monnum = checkmonth(buf);
  222. if(monnum != -1)
  223. found = TRUE;
  224. }
  225. if(!found && (tzoff == -1)) {
  226. /* this just must be a time zone string */
  227. tzoff = checktz(buf);
  228. if(tzoff != -1)
  229. found = TRUE;
  230. }
  231. if(!found)
  232. return -1; /* bad string */
  233. date += len;
  234. }
  235. else if(ISDIGIT(*date)) {
  236. /* a digit */
  237. int val;
  238. char *end;
  239. if((secnum == -1) &&
  240. (3 == sscanf(date, "%02d:%02d:%02d", &hournum, &minnum, &secnum))) {
  241. /* time stamp! */
  242. date += 8;
  243. found = TRUE;
  244. }
  245. else {
  246. val = (int)strtol(date, &end, 10);
  247. if((tzoff == -1) &&
  248. ((end - date) == 4) &&
  249. (val < 1300) &&
  250. (indate< date) &&
  251. ((date[-1] == '+' || date[-1] == '-'))) {
  252. /* four digits and a value less than 1300 and it is preceeded with
  253. a plus or minus. This is a time zone indication. */
  254. found = TRUE;
  255. tzoff = (val/100 * 60 + val%100)*60;
  256. /* the + and - prefix indicates the local time compared to GMT,
  257. this we need ther reversed math to get what we want */
  258. tzoff = date[-1]=='+'?-tzoff:tzoff;
  259. }
  260. if(((end - date) == 8) &&
  261. (yearnum == -1) &&
  262. (monnum == -1) &&
  263. (mdaynum == -1)) {
  264. /* 8 digits, no year, month or day yet. This is YYYYMMDD */
  265. found = TRUE;
  266. yearnum = val/10000;
  267. monnum = (val%10000)/100-1; /* month is 0 - 11 */
  268. mdaynum = val%100;
  269. }
  270. if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) {
  271. if((val > 0) && (val<32)) {
  272. mdaynum = val;
  273. found = TRUE;
  274. }
  275. dignext = DATE_YEAR;
  276. }
  277. if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
  278. yearnum = val;
  279. found = TRUE;
  280. if(yearnum < 1900) {
  281. if (yearnum > 70)
  282. yearnum += 1900;
  283. else
  284. yearnum += 2000;
  285. }
  286. if(mdaynum == -1)
  287. dignext = DATE_MDAY;
  288. }
  289. if(!found)
  290. return -1;
  291. date = end;
  292. }
  293. }
  294. part++;
  295. }
  296. if(-1 == secnum)
  297. secnum = minnum = hournum = 0; /* no time, make it zero */
  298. if((-1 == mdaynum) ||
  299. (-1 == monnum) ||
  300. (-1 == yearnum))
  301. /* lacks vital info, fail */
  302. return -1;
  303. #if SIZEOF_TIME_T < 5
  304. /* 32 bit time_t can only hold dates to the beginning of 2038 */
  305. if(yearnum > 2037)
  306. return 0x7fffffff;
  307. #endif
  308. tm.tm_sec = secnum;
  309. tm.tm_min = minnum;
  310. tm.tm_hour = hournum;
  311. tm.tm_mday = mdaynum;
  312. tm.tm_mon = monnum;
  313. tm.tm_year = yearnum - 1900;
  314. tm.tm_wday = 0;
  315. tm.tm_yday = 0;
  316. tm.tm_isdst = 0;
  317. /* mktime() returns a time_t. time_t is often 32 bits, even on many
  318. architectures that feature 64 bit 'long'.
  319. Some systems have 64 bit time_t and deal with years beyond 2038. However,
  320. even some of the systems with 64 bit time_t returns -1 for dates beyond
  321. 03:14:07 UTC, January 19, 2038. (Such as AIX 5100-06)
  322. */
  323. t = mktime(&tm);
  324. /* time zone adjust (cast t to int to compare to negative one) */
  325. if(-1 != (int)t) {
  326. struct tm *gmt;
  327. long delta;
  328. time_t t2;
  329. #ifdef HAVE_GMTIME_R
  330. /* thread-safe version */
  331. struct tm keeptime2;
  332. gmt = (struct tm *)gmtime_r(&t, &keeptime2);
  333. if(!gmt)
  334. return -1; /* illegal date/time */
  335. t2 = mktime(gmt);
  336. #else
  337. /* It seems that at least the MSVC version of mktime() doesn't work
  338. properly if it gets the 'gmt' pointer passed in (which is a pointer
  339. returned from gmtime() pointing to static memory), so instead we copy
  340. the tm struct to a local struct and pass a pointer to that struct as
  341. input to mktime(). */
  342. struct tm gmt2;
  343. gmt = gmtime(&t); /* use gmtime_r() if available */
  344. if(!gmt)
  345. return -1; /* illegal date/time */
  346. gmt2 = *gmt;
  347. t2 = mktime(&gmt2);
  348. #endif
  349. /* Add the time zone diff (between the given timezone and GMT) and the
  350. diff between the local time zone and GMT. */
  351. delta = (long)((tzoff!=-1?tzoff:0) + (t - t2));
  352. if((delta>0) && (t + delta < t))
  353. return -1; /* time_t overflow */
  354. t += delta;
  355. }
  356. return t;
  357. }
  358. time_t curl_getdate(const char *p, const time_t *now)
  359. {
  360. (void)now;
  361. return Curl_parsedate(p);
  362. }