parsedate.c 12 KB

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