Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl / parsedate.c
blob4771aa07e41214abe7779a523de0f8711dd1ac0b
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2006, 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.2 2007/03/15 19:22:13 andy 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 static time_t Curl_parsedate(const char *date);
89 const char * const Curl_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 const char * const Curl_month[]=
95 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
96 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
98 struct tzinfo {
99 const char *name;
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 #define tDAYZONE -60 /* offset for daylight savings time */
106 static const struct tzinfo tz[]= {
107 {"GMT", 0}, /* Greenwich Mean */
108 {"UTC", 0}, /* Universal (Coordinated) */
109 {"WET", 0}, /* Western European */
110 {"BST", 0 tDAYZONE}, /* British Summer */
111 {"WAT", 60}, /* West Africa */
112 {"AST", 240}, /* Atlantic Standard */
113 {"ADT", 240 tDAYZONE}, /* Atlantic Daylight */
114 {"EST", 300}, /* Eastern Standard */
115 {"EDT", 300 tDAYZONE}, /* Eastern Daylight */
116 {"CST", 360}, /* Central Standard */
117 {"CDT", 360 tDAYZONE}, /* Central Daylight */
118 {"MST", 420}, /* Mountain Standard */
119 {"MDT", 420 tDAYZONE}, /* Mountain Daylight */
120 {"PST", 480}, /* Pacific Standard */
121 {"PDT", 480 tDAYZONE}, /* Pacific Daylight */
122 {"YST", 540}, /* Yukon Standard */
123 {"YDT", 540 tDAYZONE}, /* Yukon Daylight */
124 {"HST", 600}, /* Hawaii Standard */
125 {"HDT", 600 tDAYZONE}, /* Hawaii Daylight */
126 {"CAT", 600}, /* Central Alaska */
127 {"AHST", 600}, /* Alaska-Hawaii Standard */
128 {"NT", 660}, /* Nome */
129 {"IDLW", 720}, /* International Date Line West */
130 {"CET", -60}, /* Central European */
131 {"MET", -60}, /* Middle European */
132 {"MEWT", -60}, /* Middle European Winter */
133 {"MEST", -60 tDAYZONE}, /* Middle European Summer */
134 {"CEST", -60 tDAYZONE}, /* Central European Summer */
135 {"MESZ", -60 tDAYZONE}, /* Middle European Summer */
136 {"FWT", -60}, /* French Winter */
137 {"FST", -60 tDAYZONE}, /* French Summer */
138 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
139 {"WAST", -420}, /* West Australian Standard */
140 {"WADT", -420 tDAYZONE}, /* West Australian Daylight */
141 {"CCT", -480}, /* China Coast, USSR Zone 7 */
142 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
143 {"EAST", -600}, /* Eastern Australian Standard */
144 {"EADT", -600 tDAYZONE}, /* Eastern Australian Daylight */
145 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
146 {"NZT", -720}, /* New Zealand */
147 {"NZST", -720}, /* New Zealand Standard */
148 {"NZDT", -720 tDAYZONE}, /* New Zealand Daylight */
149 {"IDLE", -720}, /* International Date Line East */
152 /* returns:
153 -1 no day
154 0 monday - 6 sunday
157 static int checkday(char *check, size_t len)
159 int i;
160 const char * const *what;
161 bool found= FALSE;
162 if(len > 3)
163 what = &weekday[0];
164 else
165 what = &Curl_wkday[0];
166 for(i=0; i<7; i++) {
167 if(curl_strequal(check, what[0])) {
168 found=TRUE;
169 break;
171 what++;
173 return found?i:-1;
176 static int checkmonth(char *check)
178 int i;
179 const char * const *what;
180 bool found= FALSE;
182 what = &Curl_month[0];
183 for(i=0; i<12; i++) {
184 if(curl_strequal(check, what[0])) {
185 found=TRUE;
186 break;
188 what++;
190 return found?i:-1; /* return the offset or -1, no real offset is -1 */
193 /* return the time zone offset between GMT and the input one, in number
194 of seconds or -1 if the timezone wasn't found/legal */
196 static int checktz(char *check)
198 unsigned int i;
199 const struct tzinfo *what;
200 bool found= FALSE;
202 what = tz;
203 for(i=0; i< sizeof(tz)/sizeof(tz[0]); i++) {
204 if(curl_strequal(check, what->name)) {
205 found=TRUE;
206 break;
208 what++;
210 return found?what->offset*60:-1;
213 static void skip(const char **date)
215 /* skip everything that aren't letters or digits */
216 while(**date && !ISALNUM(**date))
217 (*date)++;
220 enum assume {
221 DATE_MDAY,
222 DATE_YEAR,
223 DATE_TIME
226 static time_t Curl_parsedate(const char *date)
228 time_t t = 0;
229 int wdaynum=-1; /* day of the week number, 0-6 (mon-sun) */
230 int monnum=-1; /* month of the year number, 0-11 */
231 int mdaynum=-1; /* day of month, 1 - 31 */
232 int hournum=-1;
233 int minnum=-1;
234 int secnum=-1;
235 int yearnum=-1;
236 int tzoff=-1;
237 struct tm tm;
238 enum assume dignext = DATE_MDAY;
239 const char *indate = date; /* save the original pointer */
240 int part = 0; /* max 6 parts */
242 while(*date && (part < 6)) {
243 bool found=FALSE;
245 skip(&date);
247 if(ISALPHA(*date)) {
248 /* a name coming up */
249 char buf[32]="";
250 size_t len;
251 sscanf(date, "%31[A-Za-z]", buf);
252 len = strlen(buf);
254 if(wdaynum == -1) {
255 wdaynum = checkday(buf, len);
256 if(wdaynum != -1)
257 found = TRUE;
259 if(!found && (monnum == -1)) {
260 monnum = checkmonth(buf);
261 if(monnum != -1)
262 found = TRUE;
265 if(!found && (tzoff == -1)) {
266 /* this just must be a time zone string */
267 tzoff = checktz(buf);
268 if(tzoff != -1)
269 found = TRUE;
272 if(!found)
273 return -1; /* bad string */
275 date += len;
277 else if(ISDIGIT(*date)) {
278 /* a digit */
279 int val;
280 char *end;
281 if((secnum == -1) &&
282 (3 == sscanf(date, "%02d:%02d:%02d", &hournum, &minnum, &secnum))) {
283 /* time stamp! */
284 date += 8;
285 found = TRUE;
287 else {
288 val = (int)strtol(date, &end, 10);
290 if((tzoff == -1) &&
291 ((end - date) == 4) &&
292 (val < 1300) &&
293 (indate< date) &&
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. */
297 found = TRUE;
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) &&
306 (yearnum == -1) &&
307 (monnum == -1) &&
308 (mdaynum == -1)) {
309 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
310 found = TRUE;
311 yearnum = val/10000;
312 monnum = (val%10000)/100-1; /* month is 0 - 11 */
313 mdaynum = val%100;
316 if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) {
317 if((val > 0) && (val<32)) {
318 mdaynum = val;
319 found = TRUE;
321 dignext = DATE_YEAR;
324 if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
325 yearnum = val;
326 found = TRUE;
327 if(yearnum < 1900) {
328 if (yearnum > 70)
329 yearnum += 1900;
330 else
331 yearnum += 2000;
333 if(mdaynum == -1)
334 dignext = DATE_MDAY;
337 if(!found)
338 return -1;
340 date = end;
344 part++;
347 if(-1 == secnum)
348 secnum = minnum = hournum = 0; /* no time, make it zero */
350 if((-1 == mdaynum) ||
351 (-1 == monnum) ||
352 (-1 == yearnum))
353 /* lacks vital info, fail */
354 return -1;
356 #if SIZEOF_TIME_T < 5
357 /* 32 bit time_t can only hold dates to the beginning of 2038 */
358 if(yearnum > 2037)
359 return 0x7fffffff;
360 #endif
362 tm.tm_sec = secnum;
363 tm.tm_min = minnum;
364 tm.tm_hour = hournum;
365 tm.tm_mday = mdaynum;
366 tm.tm_mon = monnum;
367 tm.tm_year = yearnum - 1900;
368 tm.tm_wday = 0;
369 tm.tm_yday = 0;
370 tm.tm_isdst = 0;
372 /* mktime() returns a time_t. time_t is often 32 bits, even on many
373 architectures that feature 64 bit 'long'.
375 Some systems have 64 bit time_t and deal with years beyond 2038. However,
376 even some of the systems with 64 bit time_t returns -1 for dates beyond
377 03:14:07 UTC, January 19, 2038. (Such as AIX 5100-06)
379 t = mktime(&tm);
381 /* time zone adjust (cast t to int to compare to negative one) */
382 if(-1 != (int)t) {
383 struct tm *gmt;
384 long delta;
385 time_t t2;
387 #ifdef HAVE_GMTIME_R
388 /* thread-safe version */
389 struct tm keeptime2;
390 gmt = (struct tm *)gmtime_r(&t, &keeptime2);
391 if(!gmt)
392 return -1; /* illegal date/time */
393 t2 = mktime(gmt);
394 #else
395 /* It seems that at least the MSVC version of mktime() doesn't work
396 properly if it gets the 'gmt' pointer passed in (which is a pointer
397 returned from gmtime() pointing to static memory), so instead we copy
398 the tm struct to a local struct and pass a pointer to that struct as
399 input to mktime(). */
400 struct tm gmt2;
401 gmt = gmtime(&t); /* use gmtime_r() if available */
402 if(!gmt)
403 return -1; /* illegal date/time */
404 gmt2 = *gmt;
405 t2 = mktime(&gmt2);
406 #endif
408 /* Add the time zone diff (between the given timezone and GMT) and the
409 diff between the local time zone and GMT. */
410 delta = (long)((tzoff!=-1?tzoff:0) + (t - t2));
412 if((delta>0) && (t + delta < t))
413 return -1; /* time_t overflow */
415 t += delta;
418 return t;
421 time_t curl_getdate(const char *p, const time_t *now)
423 (void)now;
424 return Curl_parsedate(p);