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 (curl_getdate() and GNU date fails)
46 GMT 08:49:37 06-Nov-94 Sunday
47 94 6 Nov 08:49:37 (curl_getdate() and 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
77 #include <stdlib.h> /* for strtol() */
80 #include <curl/curl.h>
83 #include "parsedate.h"
85 static const char *wkday[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
86 static const char *weekday[] = { "Monday", "Tuesday", "Wednesday", "Thursday",
87 "Friday", "Saturday", "Sunday" };
88 const char *Curl_month[]= { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
89 "Aug", "Sep", "Oct", "Nov", "Dec" };
93 int offset; /* +/- in minutes */
96 /* Here's a bunch of frequently used time zone names. These were supported
97 by the old getdate parser. */
98 static const struct tzinfo tz[]= {
99 {"GMT", 0}, /* Greenwich Mean */
100 {"UTC", 0}, /* Universal (Coordinated) */
101 {"WET", 0}, /* Western European */
102 {"BST", 0}, /* British Summer */
103 {"WAT", 60}, /* West Africa */
104 {"AST", 240}, /* Atlantic Standard */
105 {"ADT", 240}, /* Atlantic Daylight */
106 {"EST", 300}, /* Eastern Standard */
107 {"EDT", 300}, /* Eastern Daylight */
108 {"CST", 360}, /* Central Standard */
109 {"CDT", 360}, /* Central Daylight */
110 {"MST", 420}, /* Mountain Standard */
111 {"MDT", 420}, /* Mountain Daylight */
112 {"PST", 480}, /* Pacific Standard */
113 {"PDT", 480}, /* Pacific Daylight */
114 {"YST", 540}, /* Yukon Standard */
115 {"YDT", 540}, /* Yukon Daylight */
116 {"HST", 600}, /* Hawaii Standard */
117 {"HDT", 600}, /* Hawaii Daylight */
118 {"CAT", 600}, /* Central Alaska */
119 {"AHST", 600}, /* Alaska-Hawaii Standard */
120 {"NT", 660}, /* Nome */
121 {"IDLW", 720}, /* International Date Line West */
122 {"CET", -60}, /* Central European */
123 {"MET", -60}, /* Middle European */
124 {"MEWT", -60}, /* Middle European Winter */
125 {"MEST", -120}, /* Middle European Summer */
126 {"CEST", -120}, /* Central European Summer */
127 {"MESZ", -60}, /* Middle European Summer */
128 {"FWT", -60}, /* French Winter */
129 {"FST", -60}, /* French Summer */
130 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
131 {"WAST", -420}, /* West Australian Standard */
132 {"WADT", -420}, /* West Australian Daylight */
133 {"CCT", -480}, /* China Coast, USSR Zone 7 */
134 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
135 {"EAST", -600}, /* Eastern Australian Standard */
136 {"EADT", -600}, /* Eastern Australian Daylight */
137 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
138 {"NZT", -720}, /* New Zealand */
139 {"NZST", -720}, /* New Zealand Standard */
140 {"NZDT", -720}, /* New Zealand Daylight */
141 {"IDLE", -720}, /* International Date Line East */
149 static int checkday(char *check, size_t len)
159 if(curl_strequal(check, what[0])) {
168 static int checkmonth(char *check)
174 what = &Curl_month[0];
175 for(i=0; i<12; i++) {
176 if(curl_strequal(check, what[0])) {
182 return found?i:-1; /* return the offset or -1, no real offset is -1 */
185 /* return the time zone offset between GMT and the input one, in number
186 of seconds or -1 if the timezone wasn't found/legal */
188 static int checktz(char *check)
191 const struct tzinfo *what;
195 for(i=0; i< sizeof(tz)/sizeof(tz[0]); i++) {
196 if(curl_strequal(check, what->name)) {
202 return found?what->offset*60:-1;
205 static void skip(const char **date)
207 /* skip everything that aren't letters or digits */
208 while(**date && !isalnum((int)**date))
213 #define TM_YEAR_ORIGIN 1900
215 /* Yield A - B, measured in seconds. (from getdate.y) */
217 difftm (struct tm *a, struct tm *b)
219 int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
220 int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
222 /* difference in day of year */
223 a->tm_yday - b->tm_yday
224 /* + intervening leap days */
225 + ((ay >> 2) - (by >> 2))
226 - (ay / 100 - by / 100)
227 + ((ay / 100 >> 2) - (by / 100 >> 2))
228 /* + difference in years * 365 */
229 + (long) (ay - by) * 365
231 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
232 + (a->tm_min - b->tm_min))
233 + (a->tm_sec - b->tm_sec));
243 time_t Curl_parsedate(const char *date)
246 int wdaynum=-1; /* day of the week number, 0-6 (mon-sun) */
247 int monnum=-1; /* month of the year number, 0-11 */
248 long mdaynum=-1; /* day of month, 1 - 31 */
255 enum assume dignext = DATE_MDAY;
256 const char *indate = date; /* save the original pointer */
258 int part = 0; /* max 6 parts */
260 while(*date && (part < 6)) {
265 if(isalpha((int)*date)) {
266 /* a name coming up */
269 sscanf(date, "%31[A-Za-z]", buf);
273 wdaynum = checkday(buf, len);
277 if(!found && (monnum == -1)) {
278 monnum = checkmonth(buf);
283 if(!found && (tzoff == -1)) {
284 /* this just must be a time zone string */
285 tzoff = checktz(buf);
291 return -1; /* bad string */
295 else if(isdigit((int)*date)) {
300 (3 == sscanf(date, "%02d:%02d:%02d", &hournum, &minnum, &secnum))) {
306 val = strtol(date, &end, 10);
308 if( ((end - date) == 4) &&
311 ((date[-1] == '+' || date[-1] == '-'))) {
312 /* four digits and a value less than 1300 and it is preceeded with
313 a plus or minus. This is a time zone indication. */
315 tzoff = (val/100 * 60 + val%100)*60;
317 /* the + and - prefix indicates the local time compared to GMT,
318 this we need ther reversed math to get what we want */
319 tzoff = date[-1]=='+'?-tzoff:tzoff;
322 if((dignext == DATE_MDAY) && (mdaynum == -1)) {
323 if((val > 0) && (val<32)) {
330 if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
354 secnum = minnum = hournum = 0; /* no time, make it zero */
356 if((-1 == mdaynum) ||
359 /* lacks vital info, fail */
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 /* previous involved version (that bugs?) */
389 delta = difftm(&tm, gmt);
394 /* Add the time zone diff (between the given timezone and GMT) and the
395 diff between the local time zone and GMT. */
396 delta = (tzoff!=-1?tzoff:0) + (t - t2);
398 if((delta>0) && (t + delta < t))
399 return -1; /* time_t overflow */
407 time_t curl_getdate(const char *p, const time_t *now)
410 return Curl_parsedate(p);