added more examples/docs in the top comment
[platform/upstream/curl.git] / lib / parsedate.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2004, 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  (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)
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 */
71 #include "setup.h"
72 #include <stdio.h>
73 #include <ctype.h>
74 #include <string.h>
75
76 #ifdef HAVE_STDLIB_H
77 #include <stdlib.h> /* for strtol() */
78 #endif
79
80 #include <curl/curl.h>
81
82
83 #include "parsedate.h"
84
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" };
90
91 struct tzinfo {
92   const char *name;
93   int offset; /* +/- in minutes */
94 };
95
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 */
142 };
143
144 /* returns:
145    -1 no day
146    0 monday - 6 sunday
147 */
148
149 static int checkday(char *check, size_t len)
150 {
151   int i;
152   const char **what;
153   bool found= FALSE;
154   if(len > 3)
155     what = &weekday[0];
156   else
157     what = &wkday[0];
158   for(i=0; i<7; i++) {
159     if(curl_strequal(check, what[0])) {
160       found=TRUE;
161       break;
162     }
163     what++;
164   }
165   return found?i:-1;
166 }
167
168 static int checkmonth(char *check)
169 {
170   int i;
171   const char **what;
172   bool found= FALSE;
173
174   what = &Curl_month[0];
175   for(i=0; i<12; i++) {
176     if(curl_strequal(check, what[0])) {
177       found=TRUE;
178       break;
179     }
180     what++;
181   }
182   return found?i:-1; /* return the offset or -1, no real offset is -1 */
183 }
184
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 */
187
188 static int checktz(char *check)
189 {
190   unsigned int i;
191   const struct tzinfo *what;
192   bool found= FALSE;
193
194   what = tz;
195   for(i=0; i< sizeof(tz)/sizeof(tz[0]); i++) {
196     if(curl_strequal(check, what->name)) {
197       found=TRUE;
198       break;
199     }
200     what++;
201   }
202   return found?what->offset*60:-1;
203 }
204
205 static void skip(const char **date)
206 {
207   /* skip everything that aren't letters or digits */
208   while(**date && !isalnum((int)**date))
209     (*date)++;
210 }
211
212 #if 0
213 #define TM_YEAR_ORIGIN 1900
214
215 /* Yield A - B, measured in seconds. (from getdate.y)  */
216 static long
217 difftm (struct tm *a, struct tm *b)
218 {
219   int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
220   int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
221   long days = (
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
230   );
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));
234 }
235 #endif
236
237 enum assume {
238   DATE_MDAY,
239   DATE_YEAR,
240   DATE_TIME
241 };
242
243 time_t Curl_parsedate(const char *date)
244 {
245   time_t t = 0;
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 */
249   int hournum=-1;
250   int minnum=-1;
251   int secnum=-1;
252   long yearnum=-1;
253   int tzoff=-1;
254   struct tm tm;
255   enum assume dignext = DATE_MDAY;
256   const char *indate = date; /* save the original pointer */
257
258   int part = 0; /* max 6 parts */
259
260   while(*date && (part < 6)) {
261     bool found=FALSE;
262
263     skip(&date);
264
265     if(isalpha((int)*date)) {
266       /* a name coming up */
267       char buf[32]="";
268       size_t len;
269       sscanf(date, "%31[A-Za-z]", buf);
270       len = strlen(buf);
271
272       if(wdaynum == -1) {
273         wdaynum = checkday(buf, len);
274         if(wdaynum != -1)
275           found = TRUE;
276       }
277       if(!found && (monnum == -1)) {
278         monnum = checkmonth(buf);
279         if(monnum != -1)
280           found = TRUE;
281       }
282
283       if(!found && (tzoff == -1)) {
284         /* this just must be a time zone string */
285         tzoff = checktz(buf);
286         if(tzoff != -1)
287           found = TRUE;
288       }
289
290       if(!found)
291         return -1; /* bad string */
292
293       date += len;
294     }
295     else if(isdigit((int)*date)) {
296       /* a digit */
297       long val;
298       char *end;
299       if((secnum == -1) &&
300          (3 == sscanf(date, "%02d:%02d:%02d", &hournum, &minnum, &secnum))) {
301         /* time stamp! */
302         date += 8;
303         found = TRUE;
304       }
305       else {
306         val = strtol(date, &end, 10);
307
308         if( ((end - date) == 4) &&
309             (val < 1300) &&
310             (indate< date) &&
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. */
314           found = TRUE;
315           tzoff = (val/100 * 60 + val%100)*60;
316
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;
320         }
321
322         if((dignext == DATE_MDAY) && (mdaynum == -1)) {
323           if((val > 0) && (val<32)) {
324             mdaynum = val;
325             found = TRUE;
326           }
327           dignext = DATE_YEAR;
328         }
329
330         if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
331           yearnum = val;
332           found = TRUE;
333           if(yearnum < 1900) {
334             if (yearnum > 70)
335               yearnum += 1900;
336             else
337               yearnum += 2000;
338           }
339           if(mdaynum == -1)
340             dignext = DATE_MDAY;
341         }
342
343         if(!found)
344           return -1;
345
346         date = end;
347       }
348     }
349
350     part++;
351   }
352
353   if(-1 == secnum)
354     secnum = minnum = hournum = 0; /* no time, make it zero */
355
356   if((-1 == mdaynum) ||
357      (-1 == monnum) ||
358      (-1 == yearnum))
359     /* lacks vital info, fail */
360     return -1;
361
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;
371
372   t = mktime(&tm);
373
374   /* time zone adjust */
375   {
376     struct tm *gmt;
377     long delta;
378     time_t t2;
379
380 #ifdef HAVE_GMTIME_R
381     /* thread-safe version */
382     struct tm keeptime2;
383     gmt = (struct tm *)gmtime_r(&t, &keeptime2);
384 #else
385     gmt = gmtime(&t); /* use gmtime_r() if available */
386 #endif
387 #if 0
388     /* previous involved version (that bugs?) */
389     delta = difftm(&tm, gmt);
390 #endif
391
392     t2 = mktime(gmt);
393
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);
397
398     if((delta>0) && (t + delta < t))
399       return -1; /* time_t overflow */
400
401     t += delta;
402   }
403
404   return t;
405 }
406
407 time_t curl_getdate(const char *p, const time_t *now)
408 {
409   (void)now;
410   return Curl_parsedate(p);
411 }