fix compiler warning: external definition with no prior declaration
[platform/upstream/curl.git] / lib / parsedate.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, 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
26   RFC 2616 3.3.1
27
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
31
32   we support dates without week day name:
33
34   06 Nov 1994 08:49:37 GMT
35   06-Nov-94 08:49:37 GMT
36   Nov  6 08:49:37 1994
37
38   without the time zone:
39
40   06 Nov 1994 08:49:37
41   06-Nov-94 08:49:37
42
43   weird order:
44
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)
48
49   time left out:
50
51   1994 Nov 6
52   06-Nov-94
53   Sun Nov 6 94
54
55   unusual separators:
56
57   1994.Nov.6
58   Sun/Nov/6/94/GMT
59
60   commonly used time zone names:
61
62   Sun, 06 Nov 1994 08:49:37 CET
63   06 Nov 1994 08:49:37 EST
64
65   time zones specified using RFC822 style:
66
67   Sun, 12 Sep 2004 15:05:58 -0700
68   Sat, 11 Sep 2004 21:32:11 +0200
69
70   compact numerical date strings:
71
72   20040912 15:05:58 -0700
73   20040911 +0200
74
75 */
76 #include "setup.h"
77 #include <stdio.h>
78 #include <ctype.h>
79 #include <string.h>
80
81 #ifdef HAVE_STDLIB_H
82 #include <stdlib.h> /* for strtol() */
83 #endif
84
85 #include <curl/curl.h>
86 #include "parsedate.h"
87
88 const char * const Curl_wkday[] =
89 {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
90 static const char * const weekday[] =
91 { "Monday", "Tuesday", "Wednesday", "Thursday",
92   "Friday", "Saturday", "Sunday" };
93 const char * const Curl_month[]=
94 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
95   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
96
97 struct tzinfo {
98   const char *name;
99   int offset; /* +/- in minutes */
100 };
101
102 /* Here's a bunch of frequently used time zone names. These were supported
103    by the old getdate parser. */
104 #define tDAYZONE -60       /* offset for daylight savings time */
105 static const struct tzinfo tz[]= {
106   {"GMT", 0},              /* Greenwich Mean */
107   {"UTC", 0},              /* Universal (Coordinated) */
108   {"WET", 0},              /* Western European */
109   {"BST", 0 tDAYZONE},     /* British Summer */
110   {"WAT", 60},             /* West Africa */
111   {"AST", 240},            /* Atlantic Standard */
112   {"ADT", 240 tDAYZONE},   /* Atlantic Daylight */
113   {"EST", 300},            /* Eastern Standard */
114   {"EDT", 300 tDAYZONE},   /* Eastern Daylight */
115   {"CST", 360},            /* Central Standard */
116   {"CDT", 360 tDAYZONE},   /* Central Daylight */
117   {"MST", 420},            /* Mountain Standard */
118   {"MDT", 420 tDAYZONE},   /* Mountain Daylight */
119   {"PST", 480},            /* Pacific Standard */
120   {"PDT", 480 tDAYZONE},   /* Pacific Daylight */
121   {"YST", 540},            /* Yukon Standard */
122   {"YDT", 540 tDAYZONE},   /* Yukon Daylight */
123   {"HST", 600},            /* Hawaii Standard */
124   {"HDT", 600 tDAYZONE},   /* 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", -60 tDAYZONE},  /* Middle European Summer */
133   {"CEST", -60 tDAYZONE},  /* Central European Summer */
134   {"MESZ", -60 tDAYZONE},  /* Middle European Summer */
135   {"FWT", -60},            /* French Winter */
136   {"FST", -60 tDAYZONE},   /* French Summer */
137   {"EET", -120},           /* Eastern Europe, USSR Zone 1 */
138   {"WAST", -420},          /* West Australian Standard */
139   {"WADT", -420 tDAYZONE}, /* 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 tDAYZONE}, /* 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 tDAYZONE}, /* New Zealand Daylight */
148   {"IDLE", -720},          /* International Date Line East */
149 };
150
151 /* returns:
152    -1 no day
153    0 monday - 6 sunday
154 */
155
156 static int checkday(const char *check, size_t len)
157 {
158   int i;
159   const char * const *what;
160   bool found= FALSE;
161   if(len > 3)
162     what = &weekday[0];
163   else
164     what = &Curl_wkday[0];
165   for(i=0; i<7; i++) {
166     if(curl_strequal(check, what[0])) {
167       found=TRUE;
168       break;
169     }
170     what++;
171   }
172   return found?i:-1;
173 }
174
175 static int checkmonth(const char *check)
176 {
177   int i;
178   const char * const *what;
179   bool found= FALSE;
180
181   what = &Curl_month[0];
182   for(i=0; i<12; i++) {
183     if(curl_strequal(check, what[0])) {
184       found=TRUE;
185       break;
186     }
187     what++;
188   }
189   return found?i:-1; /* return the offset or -1, no real offset is -1 */
190 }
191
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 */
194
195 static int checktz(const char *check)
196 {
197   unsigned int i;
198   const struct tzinfo *what;
199   bool found= FALSE;
200
201   what = tz;
202   for(i=0; i< sizeof(tz)/sizeof(tz[0]); i++) {
203     if(curl_strequal(check, what->name)) {
204       found=TRUE;
205       break;
206     }
207     what++;
208   }
209   return found?what->offset*60:-1;
210 }
211
212 static void skip(const char **date)
213 {
214   /* skip everything that aren't letters or digits */
215   while(**date && !ISALNUM(**date))
216     (*date)++;
217 }
218
219 enum assume {
220   DATE_MDAY,
221   DATE_YEAR,
222   DATE_TIME
223 };
224
225 static time_t parsedate(const char *date)
226 {
227   time_t t = 0;
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 */
231   int hournum=-1;
232   int minnum=-1;
233   int secnum=-1;
234   int yearnum=-1;
235   int tzoff=-1;
236   struct tm tm;
237   enum assume dignext = DATE_MDAY;
238   const char *indate = date; /* save the original pointer */
239   int part = 0; /* max 6 parts */
240
241   while(*date && (part < 6)) {
242     bool found=FALSE;
243
244     skip(&date);
245
246     if(ISALPHA(*date)) {
247       /* a name coming up */
248       char buf[32]="";
249       size_t len;
250       sscanf(date, "%31[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]", buf);
251       len = strlen(buf);
252
253       if(wdaynum == -1) {
254         wdaynum = checkday(buf, len);
255         if(wdaynum != -1)
256           found = TRUE;
257       }
258       if(!found && (monnum == -1)) {
259         monnum = checkmonth(buf);
260         if(monnum != -1)
261           found = TRUE;
262       }
263
264       if(!found && (tzoff == -1)) {
265         /* this just must be a time zone string */
266         tzoff = checktz(buf);
267         if(tzoff != -1)
268           found = TRUE;
269       }
270
271       if(!found)
272         return -1; /* bad string */
273
274       date += len;
275     }
276     else if(ISDIGIT(*date)) {
277       /* a digit */
278       int val;
279       char *end;
280       if((secnum == -1) &&
281          (3 == sscanf(date, "%02d:%02d:%02d", &hournum, &minnum, &secnum))) {
282         /* time stamp! */
283         date += 8;
284         found = TRUE;
285       }
286       else {
287         val = (int)strtol(date, &end, 10);
288
289         if((tzoff == -1) &&
290            ((end - date) == 4) &&
291            (val <= 1400) &&
292            (indate< date) &&
293            ((date[-1] == '+' || date[-1] == '-'))) {
294           /* four digits and a value less than or equal to 1400 (to take into
295              account all sorts of funny time zone diffs) and it is preceeded
296              with a plus or minus. This is a time zone indication.  1400 is
297              picked since +1300 is frequently used and +1400 is mentioned as
298              an edge number in the document "ISO C 200X Proposal: Timezone
299              Functions" at http://david.tribble.com/text/c0xtimezone.html If
300              anyone has a more authoritative source for the exact maximum time
301              zone offsets, please speak up! */
302           found = TRUE;
303           tzoff = (val/100 * 60 + val%100)*60;
304
305           /* the + and - prefix indicates the local time compared to GMT,
306              this we need ther reversed math to get what we want */
307           tzoff = date[-1]=='+'?-tzoff:tzoff;
308         }
309
310         if(((end - date) == 8) &&
311            (yearnum == -1) &&
312            (monnum == -1) &&
313            (mdaynum == -1)) {
314           /* 8 digits, no year, month or day yet. This is YYYYMMDD */
315           found = TRUE;
316           yearnum = val/10000;
317           monnum = (val%10000)/100-1; /* month is 0 - 11 */
318           mdaynum = val%100;
319         }
320
321         if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) {
322           if((val > 0) && (val<32)) {
323             mdaynum = val;
324             found = TRUE;
325           }
326           dignext = DATE_YEAR;
327         }
328
329         if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
330           yearnum = val;
331           found = TRUE;
332           if(yearnum < 1900) {
333             if(yearnum > 70)
334               yearnum += 1900;
335             else
336               yearnum += 2000;
337           }
338           if(mdaynum == -1)
339             dignext = DATE_MDAY;
340         }
341
342         if(!found)
343           return -1;
344
345         date = end;
346       }
347     }
348
349     part++;
350   }
351
352   if(-1 == secnum)
353     secnum = minnum = hournum = 0; /* no time, make it zero */
354
355   if((-1 == mdaynum) ||
356      (-1 == monnum) ||
357      (-1 == yearnum))
358     /* lacks vital info, fail */
359     return -1;
360
361 #if SIZEOF_TIME_T < 5
362   /* 32 bit time_t can only hold dates to the beginning of 2038 */
363   if(yearnum > 2037)
364     return 0x7fffffff;
365 #endif
366
367   tm.tm_sec = secnum;
368   tm.tm_min = minnum;
369   tm.tm_hour = hournum;
370   tm.tm_mday = mdaynum;
371   tm.tm_mon = monnum;
372   tm.tm_year = yearnum - 1900;
373   tm.tm_wday = 0;
374   tm.tm_yday = 0;
375   tm.tm_isdst = 0;
376
377   /* mktime() returns a time_t. time_t is often 32 bits, even on many
378      architectures that feature 64 bit 'long'.
379
380      Some systems have 64 bit time_t and deal with years beyond 2038. However,
381      even some of the systems with 64 bit time_t returns -1 for dates beyond
382      03:14:07 UTC, January 19, 2038. (Such as AIX 5100-06)
383   */
384   t = mktime(&tm);
385
386   /* time zone adjust (cast t to int to compare to negative one) */
387   if(-1 != (int)t) {
388     struct tm *gmt;
389     long delta;
390     time_t t2;
391
392 #ifdef HAVE_GMTIME_R
393     /* thread-safe version */
394     struct tm keeptime2;
395     gmt = (struct tm *)gmtime_r(&t, &keeptime2);
396     if(!gmt)
397       return -1; /* illegal date/time */
398     t2 = mktime(gmt);
399 #else
400     /* It seems that at least the MSVC version of mktime() doesn't work
401        properly if it gets the 'gmt' pointer passed in (which is a pointer
402        returned from gmtime() pointing to static memory), so instead we copy
403        the tm struct to a local struct and pass a pointer to that struct as
404        input to mktime(). */
405     struct tm gmt2;
406     gmt = gmtime(&t); /* use gmtime_r() if available */
407     if(!gmt)
408       return -1; /* illegal date/time */
409     gmt2 = *gmt;
410     t2 = mktime(&gmt2);
411 #endif
412
413     /* Add the time zone diff (between the given timezone and GMT) and the
414        diff between the local time zone and GMT. */
415     delta = (long)((tzoff!=-1?tzoff:0) + (t - t2));
416
417     if((delta>0) && (t + delta < t))
418       return -1; /* time_t overflow */
419
420     t += delta;
421   }
422
423   return t;
424 }
425
426 time_t curl_getdate(const char *p, const time_t *now)
427 {
428   (void)now;
429   return parsedate(p);
430 }