Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / lib / parsedate.c
blob44f1f0660776626ef5801ac1c19ea089fb52b2a6
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, 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.
21 * $Id: parsedate.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
24 A brief summary of the date string formats this parser groks:
26 RFC 2616 3.3.1
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
36 Nov 6 08:49:37 1994
38 without the time zone:
40 06 Nov 1994 08:49:37
41 06-Nov-94 08:49:37
43 weird order:
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)
49 time left out:
51 1994 Nov 6
52 06-Nov-94
53 Sun Nov 6 94
55 unusual separators:
57 1994.Nov.6
58 Sun/Nov/6/94/GMT
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
73 20040911 +0200
76 #include "setup.h"
77 #include <stdio.h>
78 #include <ctype.h>
79 #include <string.h>
81 #ifdef HAVE_STDLIB_H
82 #include <stdlib.h> /* for strtol() */
83 #endif
85 #include <curl/curl.h>
87 const char * const Curl_wkday[] =
88 {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
89 static const char * const weekday[] =
90 { "Monday", "Tuesday", "Wednesday", "Thursday",
91 "Friday", "Saturday", "Sunday" };
92 const char * const Curl_month[]=
93 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
94 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
96 struct tzinfo {
97 const char *name;
98 int offset; /* +/- in minutes */
101 /* Here's a bunch of frequently used time zone names. These were supported
102 by the old getdate parser. */
103 #define tDAYZONE -60 /* offset for daylight savings time */
104 static const struct tzinfo tz[]= {
105 {"GMT", 0}, /* Greenwich Mean */
106 {"UTC", 0}, /* Universal (Coordinated) */
107 {"WET", 0}, /* Western European */
108 {"BST", 0 tDAYZONE}, /* British Summer */
109 {"WAT", 60}, /* West Africa */
110 {"AST", 240}, /* Atlantic Standard */
111 {"ADT", 240 tDAYZONE}, /* Atlantic Daylight */
112 {"EST", 300}, /* Eastern Standard */
113 {"EDT", 300 tDAYZONE}, /* Eastern Daylight */
114 {"CST", 360}, /* Central Standard */
115 {"CDT", 360 tDAYZONE}, /* Central Daylight */
116 {"MST", 420}, /* Mountain Standard */
117 {"MDT", 420 tDAYZONE}, /* Mountain Daylight */
118 {"PST", 480}, /* Pacific Standard */
119 {"PDT", 480 tDAYZONE}, /* Pacific Daylight */
120 {"YST", 540}, /* Yukon Standard */
121 {"YDT", 540 tDAYZONE}, /* Yukon Daylight */
122 {"HST", 600}, /* Hawaii Standard */
123 {"HDT", 600 tDAYZONE}, /* Hawaii Daylight */
124 {"CAT", 600}, /* Central Alaska */
125 {"AHST", 600}, /* Alaska-Hawaii Standard */
126 {"NT", 660}, /* Nome */
127 {"IDLW", 720}, /* International Date Line West */
128 {"CET", -60}, /* Central European */
129 {"MET", -60}, /* Middle European */
130 {"MEWT", -60}, /* Middle European Winter */
131 {"MEST", -60 tDAYZONE}, /* Middle European Summer */
132 {"CEST", -60 tDAYZONE}, /* Central European Summer */
133 {"MESZ", -60 tDAYZONE}, /* Middle European Summer */
134 {"FWT", -60}, /* French Winter */
135 {"FST", -60 tDAYZONE}, /* French Summer */
136 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
137 {"WAST", -420}, /* West Australian Standard */
138 {"WADT", -420 tDAYZONE}, /* West Australian Daylight */
139 {"CCT", -480}, /* China Coast, USSR Zone 7 */
140 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
141 {"EAST", -600}, /* Eastern Australian Standard */
142 {"EADT", -600 tDAYZONE}, /* Eastern Australian Daylight */
143 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
144 {"NZT", -720}, /* New Zealand */
145 {"NZST", -720}, /* New Zealand Standard */
146 {"NZDT", -720 tDAYZONE}, /* New Zealand Daylight */
147 {"IDLE", -720}, /* International Date Line East */
150 /* returns:
151 -1 no day
152 0 monday - 6 sunday
155 static int checkday(const char *check, size_t len)
157 int i;
158 const char * const *what;
159 bool found= FALSE;
160 if(len > 3)
161 what = &weekday[0];
162 else
163 what = &Curl_wkday[0];
164 for(i=0; i<7; i++) {
165 if(curl_strequal(check, what[0])) {
166 found=TRUE;
167 break;
169 what++;
171 return found?i:-1;
174 static int checkmonth(const char *check)
176 int i;
177 const char * const *what;
178 bool found= FALSE;
180 what = &Curl_month[0];
181 for(i=0; i<12; i++) {
182 if(curl_strequal(check, what[0])) {
183 found=TRUE;
184 break;
186 what++;
188 return found?i:-1; /* return the offset or -1, no real offset is -1 */
191 /* return the time zone offset between GMT and the input one, in number
192 of seconds or -1 if the timezone wasn't found/legal */
194 static int checktz(const char *check)
196 unsigned int i;
197 const struct tzinfo *what;
198 bool found= FALSE;
200 what = tz;
201 for(i=0; i< sizeof(tz)/sizeof(tz[0]); i++) {
202 if(curl_strequal(check, what->name)) {
203 found=TRUE;
204 break;
206 what++;
208 return found?what->offset*60:-1;
211 static void skip(const char **date)
213 /* skip everything that aren't letters or digits */
214 while(**date && !ISALNUM(**date))
215 (*date)++;
218 enum assume {
219 DATE_MDAY,
220 DATE_YEAR,
221 DATE_TIME
224 static time_t parsedate(const char *date)
226 time_t t = 0;
227 int wdaynum=-1; /* day of the week number, 0-6 (mon-sun) */
228 int monnum=-1; /* month of the year number, 0-11 */
229 int mdaynum=-1; /* day of month, 1 - 31 */
230 int hournum=-1;
231 int minnum=-1;
232 int secnum=-1;
233 int yearnum=-1;
234 int tzoff=-1;
235 struct tm tm;
236 enum assume dignext = DATE_MDAY;
237 const char *indate = date; /* save the original pointer */
238 int part = 0; /* max 6 parts */
240 while(*date && (part < 6)) {
241 bool found=FALSE;
243 skip(&date);
245 if(ISALPHA(*date)) {
246 /* a name coming up */
247 char buf[32]="";
248 size_t len;
249 sscanf(date, "%31[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]", buf);
250 len = strlen(buf);
252 if(wdaynum == -1) {
253 wdaynum = checkday(buf, len);
254 if(wdaynum != -1)
255 found = TRUE;
257 if(!found && (monnum == -1)) {
258 monnum = checkmonth(buf);
259 if(monnum != -1)
260 found = TRUE;
263 if(!found && (tzoff == -1)) {
264 /* this just must be a time zone string */
265 tzoff = checktz(buf);
266 if(tzoff != -1)
267 found = TRUE;
270 if(!found)
271 return -1; /* bad string */
273 date += len;
275 else if(ISDIGIT(*date)) {
276 /* a digit */
277 int val;
278 char *end;
279 if((secnum == -1) &&
280 (3 == sscanf(date, "%02d:%02d:%02d", &hournum, &minnum, &secnum))) {
281 /* time stamp! */
282 date += 8;
283 found = TRUE;
285 else {
286 val = (int)strtol(date, &end, 10);
288 if((tzoff == -1) &&
289 ((end - date) == 4) &&
290 (val <= 1400) &&
291 (indate< date) &&
292 ((date[-1] == '+' || date[-1] == '-'))) {
293 /* four digits and a value less than or equal to 1400 (to take into
294 account all sorts of funny time zone diffs) and it is preceeded
295 with a plus or minus. This is a time zone indication. 1400 is
296 picked since +1300 is frequently used and +1400 is mentioned as
297 an edge number in the document "ISO C 200X Proposal: Timezone
298 Functions" at http://david.tribble.com/text/c0xtimezone.html If
299 anyone has a more authoritative source for the exact maximum time
300 zone offsets, please speak up! */
301 found = TRUE;
302 tzoff = (val/100 * 60 + val%100)*60;
304 /* the + and - prefix indicates the local time compared to GMT,
305 this we need ther reversed math to get what we want */
306 tzoff = date[-1]=='+'?-tzoff:tzoff;
309 if(((end - date) == 8) &&
310 (yearnum == -1) &&
311 (monnum == -1) &&
312 (mdaynum == -1)) {
313 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
314 found = TRUE;
315 yearnum = val/10000;
316 monnum = (val%10000)/100-1; /* month is 0 - 11 */
317 mdaynum = val%100;
320 if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) {
321 if((val > 0) && (val<32)) {
322 mdaynum = val;
323 found = TRUE;
325 dignext = DATE_YEAR;
328 if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
329 yearnum = val;
330 found = TRUE;
331 if(yearnum < 1900) {
332 if(yearnum > 70)
333 yearnum += 1900;
334 else
335 yearnum += 2000;
337 if(mdaynum == -1)
338 dignext = DATE_MDAY;
341 if(!found)
342 return -1;
344 date = end;
348 part++;
351 if(-1 == secnum)
352 secnum = minnum = hournum = 0; /* no time, make it zero */
354 if((-1 == mdaynum) ||
355 (-1 == monnum) ||
356 (-1 == yearnum))
357 /* lacks vital info, fail */
358 return -1;
360 #if SIZEOF_TIME_T < 5
361 /* 32 bit time_t can only hold dates to the beginning of 2038 */
362 if(yearnum > 2037)
363 return 0x7fffffff;
364 #endif
366 tm.tm_sec = secnum;
367 tm.tm_min = minnum;
368 tm.tm_hour = hournum;
369 tm.tm_mday = mdaynum;
370 tm.tm_mon = monnum;
371 tm.tm_year = yearnum - 1900;
372 tm.tm_wday = 0;
373 tm.tm_yday = 0;
374 tm.tm_isdst = 0;
376 /* mktime() returns a time_t. time_t is often 32 bits, even on many
377 architectures that feature 64 bit 'long'.
379 Some systems have 64 bit time_t and deal with years beyond 2038. However,
380 even some of the systems with 64 bit time_t returns -1 for dates beyond
381 03:14:07 UTC, January 19, 2038. (Such as AIX 5100-06)
383 t = mktime(&tm);
385 /* time zone adjust (cast t to int to compare to negative one) */
386 if(-1 != (int)t) {
387 struct tm *gmt;
388 long delta;
389 time_t t2;
391 #ifdef HAVE_GMTIME_R
392 /* thread-safe version */
393 struct tm keeptime2;
394 gmt = (struct tm *)gmtime_r(&t, &keeptime2);
395 if(!gmt)
396 return -1; /* illegal date/time */
397 t2 = mktime(gmt);
398 #else
399 /* It seems that at least the MSVC version of mktime() doesn't work
400 properly if it gets the 'gmt' pointer passed in (which is a pointer
401 returned from gmtime() pointing to static memory), so instead we copy
402 the tm struct to a local struct and pass a pointer to that struct as
403 input to mktime(). */
404 struct tm gmt2;
405 gmt = gmtime(&t); /* use gmtime_r() if available */
406 if(!gmt)
407 return -1; /* illegal date/time */
408 gmt2 = *gmt;
409 t2 = mktime(&gmt2);
410 #endif
412 /* Add the time zone diff (between the given timezone and GMT) and the
413 diff between the local time zone and GMT. */
414 delta = (long)((tzoff!=-1?tzoff:0) + (t - t2));
416 if((delta>0) && (t + delta < t))
417 return -1; /* time_t overflow */
419 t += delta;
422 return t;
425 time_t curl_getdate(const char *p, const time_t *now)
427 (void)now;
428 return parsedate(p);