1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
22 ***************************************************************************/
24 A brief summary of the date string formats this parser groks:
28 Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
29 Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
30 Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
32 we support dates without week day name:
34 06 Nov 1994 08:49:37 GMT
35 06-Nov-94 08:49:37 GMT
38 without the time zone:
45 1994 Nov 6 08:49:37 (GNU date fails)
46 GMT 08:49:37 06-Nov-94 Sunday
47 94 6 Nov 08:49:37 (GNU date fails)
60 commonly used time zone names:
62 Sun, 06 Nov 1994 08:49:37 CET
63 06 Nov 1994 08:49:37 EST
65 time zones specified using RFC822 style:
67 Sun, 12 Sep 2004 15:05:58 -0700
68 Sat, 11 Sep 2004 21:32:11 +0200
70 compact numerical date strings:
72 20040912 15:05:58 -0700
82 #include <stdlib.h> /* for strtol() */
85 #include <curl/curl.h>
87 static time_t Curl_parsedate(const char *date);
89 static const char * const wkday[] =
90 {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
91 static const char * const weekday[] =
92 { "Monday", "Tuesday", "Wednesday", "Thursday",
93 "Friday", "Saturday", "Sunday" };
94 static const char * const month[]=
95 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
96 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
100 int offset; /* +/- in minutes */
103 /* Here's a bunch of frequently used time zone names. These were supported
104 by the old getdate parser. */
105 static const struct tzinfo tz[]= {
106 {"GMT", 0}, /* Greenwich Mean */
107 {"UTC", 0}, /* Universal (Coordinated) */
108 {"WET", 0}, /* Western European */
109 {"BST", 0}, /* British Summer */
110 {"WAT", 60}, /* West Africa */
111 {"AST", 240}, /* Atlantic Standard */
112 {"ADT", 240}, /* Atlantic Daylight */
113 {"EST", 300}, /* Eastern Standard */
114 {"EDT", 300}, /* Eastern Daylight */
115 {"CST", 360}, /* Central Standard */
116 {"CDT", 360}, /* Central Daylight */
117 {"MST", 420}, /* Mountain Standard */
118 {"MDT", 420}, /* Mountain Daylight */
119 {"PST", 480}, /* Pacific Standard */
120 {"PDT", 480}, /* Pacific Daylight */
121 {"YST", 540}, /* Yukon Standard */
122 {"YDT", 540}, /* Yukon Daylight */
123 {"HST", 600}, /* Hawaii Standard */
124 {"HDT", 600}, /* Hawaii Daylight */
125 {"CAT", 600}, /* Central Alaska */
126 {"AHST", 600}, /* Alaska-Hawaii Standard */
127 {"NT", 660}, /* Nome */
128 {"IDLW", 720}, /* International Date Line West */
129 {"CET", -60}, /* Central European */
130 {"MET", -60}, /* Middle European */
131 {"MEWT", -60}, /* Middle European Winter */
132 {"MEST", -120}, /* Middle European Summer */
133 {"CEST", -120}, /* Central European Summer */
134 {"MESZ", -60}, /* Middle European Summer */
135 {"FWT", -60}, /* French Winter */
136 {"FST", -60}, /* French Summer */
137 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
138 {"WAST", -420}, /* West Australian Standard */
139 {"WADT", -420}, /* West Australian Daylight */
140 {"CCT", -480}, /* China Coast, USSR Zone 7 */
141 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
142 {"EAST", -600}, /* Eastern Australian Standard */
143 {"EADT", -600}, /* Eastern Australian Daylight */
144 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
145 {"NZT", -720}, /* New Zealand */
146 {"NZST", -720}, /* New Zealand Standard */
147 {"NZDT", -720}, /* New Zealand Daylight */
148 {"IDLE", -720}, /* International Date Line East */
156 static int checkday(char *check, size_t len)
159 const char * const *what;
166 if(curl_strequal(check, what[0])) {
175 static int checkmonth(char *check)
178 const char * const *what;
182 for(i=0; i<12; i++) {
183 if(curl_strequal(check, what[0])) {
189 return found?i:-1; /* return the offset or -1, no real offset is -1 */
192 /* return the time zone offset between GMT and the input one, in number
193 of seconds or -1 if the timezone wasn't found/legal */
195 static int checktz(char *check)
198 const struct tzinfo *what;
202 for(i=0; i< sizeof(tz)/sizeof(tz[0]); i++) {
203 if(curl_strequal(check, what->name)) {
209 return found?what->offset*60:-1;
212 static void skip(const char **date)
214 /* skip everything that aren't letters or digits */
215 while(**date && !isalnum((int)**date))
225 static time_t Curl_parsedate(const char *date)
228 int wdaynum=-1; /* day of the week number, 0-6 (mon-sun) */
229 int monnum=-1; /* month of the year number, 0-11 */
230 int mdaynum=-1; /* day of month, 1 - 31 */
237 enum assume dignext = DATE_MDAY;
238 const char *indate = date; /* save the original pointer */
240 int part = 0; /* max 6 parts */
242 while(*date && (part < 6)) {
247 if(isalpha((int)*date)) {
248 /* a name coming up */
251 sscanf(date, "%31[A-Za-z]", buf);
255 wdaynum = checkday(buf, len);
259 if(!found && (monnum == -1)) {
260 monnum = checkmonth(buf);
265 if(!found && (tzoff == -1)) {
266 /* this just must be a time zone string */
267 tzoff = checktz(buf);
273 return -1; /* bad string */
277 else if(isdigit((int)*date)) {
282 (3 == sscanf(date, "%02d:%02d:%02d", &hournum, &minnum, &secnum))) {
288 val = (int)strtol(date, &end, 10);
291 ((end - date) == 4) &&
294 ((date[-1] == '+' || date[-1] == '-'))) {
295 /* four digits and a value less than 1300 and it is preceeded with
296 a plus or minus. This is a time zone indication. */
298 tzoff = (val/100 * 60 + val%100)*60;
300 /* the + and - prefix indicates the local time compared to GMT,
301 this we need ther reversed math to get what we want */
302 tzoff = date[-1]=='+'?-tzoff:tzoff;
305 if(((end - date) == 8) &&
309 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
312 monnum = (val%10000)/100-1; /* month is 0 - 11 */
316 if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) {
317 if((val > 0) && (val<32)) {
324 if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
348 secnum = minnum = hournum = 0; /* no time, make it zero */
350 if((-1 == mdaynum) ||
353 /* lacks vital info, fail */
356 #if SIZEOF_TIME_T < 5
357 /* 32 bit time_t can only hold dates to the beginning of 2038 */
364 tm.tm_hour = hournum;
365 tm.tm_mday = mdaynum;
367 tm.tm_year = yearnum - 1900;
374 /* time zone adjust */
381 /* thread-safe version */
383 gmt = (struct tm *)gmtime_r(&t, &keeptime2);
385 gmt = gmtime(&t); /* use gmtime_r() if available */
388 return -1; /* illegal date/time */
392 /* Add the time zone diff (between the given timezone and GMT) and the
393 diff between the local time zone and GMT. */
394 delta = (tzoff!=-1?tzoff:0) + (t - t2);
396 if((delta>0) && (t + delta < t))
397 return -1; /* time_t overflow */
405 time_t curl_getdate(const char *p, const time_t *now)
408 return Curl_parsedate(p);