1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2018, 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 https://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 ***************************************************************************/
23 A brief summary of the date string formats this parser groks:
27 Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
28 Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
29 Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
31 we support dates without week day name:
33 06 Nov 1994 08:49:37 GMT
34 06-Nov-94 08:49:37 GMT
37 without the time zone:
44 1994 Nov 6 08:49:37 (GNU date fails)
45 GMT 08:49:37 06-Nov-94 Sunday
46 94 6 Nov 08:49:37 (GNU date fails)
59 commonly used time zone names:
61 Sun, 06 Nov 1994 08:49:37 CET
62 06 Nov 1994 08:49:37 EST
64 time zones specified using RFC822 style:
66 Sun, 12 Sep 2004 15:05:58 -0700
67 Sat, 11 Sep 2004 21:32:11 +0200
69 compact numerical date strings:
71 20040912 15:05:58 -0700
76 #include "curl_setup.h"
80 #include <curl/curl.h>
83 #include "parsedate.h"
85 const char * const Curl_wkday[] =
86 {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
87 static const char * const weekday[] =
88 { "Monday", "Tuesday", "Wednesday", "Thursday",
89 "Friday", "Saturday", "Sunday" };
90 const char * const Curl_month[]=
91 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
92 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
96 int offset; /* +/- in minutes */
104 * PARSEDATE_OK - a fine conversion
105 * PARSEDATE_FAIL - failed to convert
106 * PARSEDATE_LATER - time overflow at the far end of time_t
107 * PARSEDATE_SOONER - time underflow at the low end of time_t
110 static int parsedate(const char *date, time_t *output);
112 #define PARSEDATE_OK 0
113 #define PARSEDATE_FAIL -1
114 #define PARSEDATE_LATER 1
115 #define PARSEDATE_SOONER 2
117 /* Here's a bunch of frequently used time zone names. These were supported
118 by the old getdate parser. */
119 #define tDAYZONE -60 /* offset for daylight savings time */
120 static const struct tzinfo tz[]= {
121 {"GMT", 0}, /* Greenwich Mean */
122 {"UTC", 0}, /* Universal (Coordinated) */
123 {"WET", 0}, /* Western European */
124 {"BST", 0 tDAYZONE}, /* British Summer */
125 {"WAT", 60}, /* West Africa */
126 {"AST", 240}, /* Atlantic Standard */
127 {"ADT", 240 tDAYZONE}, /* Atlantic Daylight */
128 {"EST", 300}, /* Eastern Standard */
129 {"EDT", 300 tDAYZONE}, /* Eastern Daylight */
130 {"CST", 360}, /* Central Standard */
131 {"CDT", 360 tDAYZONE}, /* Central Daylight */
132 {"MST", 420}, /* Mountain Standard */
133 {"MDT", 420 tDAYZONE}, /* Mountain Daylight */
134 {"PST", 480}, /* Pacific Standard */
135 {"PDT", 480 tDAYZONE}, /* Pacific Daylight */
136 {"YST", 540}, /* Yukon Standard */
137 {"YDT", 540 tDAYZONE}, /* Yukon Daylight */
138 {"HST", 600}, /* Hawaii Standard */
139 {"HDT", 600 tDAYZONE}, /* Hawaii Daylight */
140 {"CAT", 600}, /* Central Alaska */
141 {"AHST", 600}, /* Alaska-Hawaii Standard */
142 {"NT", 660}, /* Nome */
143 {"IDLW", 720}, /* International Date Line West */
144 {"CET", -60}, /* Central European */
145 {"MET", -60}, /* Middle European */
146 {"MEWT", -60}, /* Middle European Winter */
147 {"MEST", -60 tDAYZONE}, /* Middle European Summer */
148 {"CEST", -60 tDAYZONE}, /* Central European Summer */
149 {"MESZ", -60 tDAYZONE}, /* Middle European Summer */
150 {"FWT", -60}, /* French Winter */
151 {"FST", -60 tDAYZONE}, /* French Summer */
152 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
153 {"WAST", -420}, /* West Australian Standard */
154 {"WADT", -420 tDAYZONE}, /* West Australian Daylight */
155 {"CCT", -480}, /* China Coast, USSR Zone 7 */
156 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
157 {"EAST", -600}, /* Eastern Australian Standard */
158 {"EADT", -600 tDAYZONE}, /* Eastern Australian Daylight */
159 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
160 {"NZT", -720}, /* New Zealand */
161 {"NZST", -720}, /* New Zealand Standard */
162 {"NZDT", -720 tDAYZONE}, /* New Zealand Daylight */
163 {"IDLE", -720}, /* International Date Line East */
164 /* Next up: Military timezone names. RFC822 allowed these, but (as noted in
165 RFC 1123) had their signs wrong. Here we use the correct signs to match
166 actual military usage.
168 {"A", 1 * 60}, /* Alpha */
169 {"B", 2 * 60}, /* Bravo */
170 {"C", 3 * 60}, /* Charlie */
171 {"D", 4 * 60}, /* Delta */
172 {"E", 5 * 60}, /* Echo */
173 {"F", 6 * 60}, /* Foxtrot */
174 {"G", 7 * 60}, /* Golf */
175 {"H", 8 * 60}, /* Hotel */
176 {"I", 9 * 60}, /* India */
177 /* "J", Juliet is not used as a timezone, to indicate the observer's local
179 {"K", 10 * 60}, /* Kilo */
180 {"L", 11 * 60}, /* Lima */
181 {"M", 12 * 60}, /* Mike */
182 {"N", -1 * 60}, /* November */
183 {"O", -2 * 60}, /* Oscar */
184 {"P", -3 * 60}, /* Papa */
185 {"Q", -4 * 60}, /* Quebec */
186 {"R", -5 * 60}, /* Romeo */
187 {"S", -6 * 60}, /* Sierra */
188 {"T", -7 * 60}, /* Tango */
189 {"U", -8 * 60}, /* Uniform */
190 {"V", -9 * 60}, /* Victor */
191 {"W", -10 * 60}, /* Whiskey */
192 {"X", -11 * 60}, /* X-ray */
193 {"Y", -12 * 60}, /* Yankee */
194 {"Z", 0}, /* Zulu, zero meridian, a.k.a. UTC */
202 static int checkday(const char *check, size_t len)
205 const char * const *what;
210 what = &Curl_wkday[0];
211 for(i = 0; i<7; i++) {
212 if(strcasecompare(check, what[0])) {
221 static int checkmonth(const char *check)
224 const char * const *what;
227 what = &Curl_month[0];
228 for(i = 0; i<12; i++) {
229 if(strcasecompare(check, what[0])) {
235 return found?i:-1; /* return the offset or -1, no real offset is -1 */
238 /* return the time zone offset between GMT and the input one, in number
239 of seconds or -1 if the timezone wasn't found/legal */
241 static int checktz(const char *check)
244 const struct tzinfo *what;
248 for(i = 0; i< sizeof(tz)/sizeof(tz[0]); i++) {
249 if(strcasecompare(check, what->name)) {
255 return found?what->offset*60:-1;
258 static void skip(const char **date)
260 /* skip everything that aren't letters or digits */
261 while(**date && !ISALNUM(**date))
271 /* this is a clone of 'struct tm' but with all fields we don't need or use
279 int tm_year; /* full year */
282 /* struct tm to time since epoch in GMT time zone.
283 * This is similar to the standard mktime function but for GMT only, and
284 * doesn't suffer from the various bugs and portability problems that
285 * some systems' implementations have.
287 * Returns 0 on success, otherwise non-zero.
289 static void my_timegm(struct my_tm *tm, time_t *t)
291 static const int month_days_cumulative [12] =
292 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
293 int month, year, leap_days;
298 year += (11 - month) / 12;
299 month = 11 - (11 - month) % 12;
301 else if(month >= 12) {
306 leap_days = year - (tm->tm_mon <= 1);
307 leap_days = ((leap_days / 4) - (leap_days / 100) + (leap_days / 400)
308 - (1969 / 4) + (1969 / 100) - (1969 / 400));
310 *t = ((((time_t) (year - 1970) * 365
311 + leap_days + month_days_cumulative[month] + tm->tm_mday - 1) * 24
312 + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec;
320 * PARSEDATE_OK - a fine conversion
321 * PARSEDATE_FAIL - failed to convert
322 * PARSEDATE_LATER - time overflow at the far end of time_t
323 * PARSEDATE_SOONER - time underflow at the low end of time_t
326 static int parsedate(const char *date, time_t *output)
329 int wdaynum = -1; /* day of the week number, 0-6 (mon-sun) */
330 int monnum = -1; /* month of the year number, 0-11 */
331 int mdaynum = -1; /* day of month, 1 - 31 */
338 enum assume dignext = DATE_MDAY;
339 const char *indate = date; /* save the original pointer */
340 int part = 0; /* max 6 parts */
342 while(*date && (part < 6)) {
348 /* a name coming up */
351 if(sscanf(date, "%31[ABCDEFGHIJKLMNOPQRSTUVWXYZ"
352 "abcdefghijklmnopqrstuvwxyz]", buf))
358 wdaynum = checkday(buf, len);
362 if(!found && (monnum == -1)) {
363 monnum = checkmonth(buf);
368 if(!found && (tzoff == -1)) {
369 /* this just must be a time zone string */
370 tzoff = checktz(buf);
376 return PARSEDATE_FAIL; /* bad string */
380 else if(ISDIGIT(*date)) {
386 (3 == sscanf(date, "%02d:%02d:%02d%n",
387 &hournum, &minnum, &secnum, &len))) {
391 else if((secnum == -1) &&
392 (2 == sscanf(date, "%02d:%02d%n", &hournum, &minnum, &len))) {
393 /* time stamp without seconds */
404 lval = strtol(date, &end, 10);
406 if(errno != old_errno)
410 return PARSEDATE_FAIL;
412 #if LONG_MAX != INT_MAX
413 if((lval > (long)INT_MAX) || (lval < (long)INT_MIN))
414 return PARSEDATE_FAIL;
417 val = curlx_sltosi(lval);
420 ((end - date) == 4) &&
423 ((date[-1] == '+' || date[-1] == '-'))) {
424 /* four digits and a value less than or equal to 1400 (to take into
425 account all sorts of funny time zone diffs) and it is preceded
426 with a plus or minus. This is a time zone indication. 1400 is
427 picked since +1300 is frequently used and +1400 is mentioned as
428 an edge number in the document "ISO C 200X Proposal: Timezone
429 Functions" at http://david.tribble.com/text/c0xtimezone.html If
430 anyone has a more authoritative source for the exact maximum time
431 zone offsets, please speak up! */
433 tzoff = (val/100 * 60 + val%100)*60;
435 /* the + and - prefix indicates the local time compared to GMT,
436 this we need their reversed math to get what we want */
437 tzoff = date[-1]=='+'?-tzoff:tzoff;
440 if(((end - date) == 8) &&
444 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
447 monnum = (val%10000)/100-1; /* month is 0 - 11 */
451 if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) {
452 if((val > 0) && (val<32)) {
459 if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
473 return PARSEDATE_FAIL;
483 secnum = minnum = hournum = 0; /* no time, make it zero */
485 if((-1 == mdaynum) ||
488 /* lacks vital info, fail */
489 return PARSEDATE_FAIL;
491 #ifdef HAVE_TIME_T_UNSIGNED
493 /* only positive numbers cannot return earlier */
494 *output = TIME_T_MIN;
495 return PARSEDATE_SOONER;
499 #if (SIZEOF_TIME_T < 5)
501 #ifdef HAVE_TIME_T_UNSIGNED
502 /* an unsigned 32 bit time_t can only hold dates to 2106 */
504 *output = TIME_T_MAX;
505 return PARSEDATE_LATER;
508 /* a signed 32 bit time_t can only hold dates to the beginning of 2038 */
510 *output = TIME_T_MAX;
511 return PARSEDATE_LATER;
514 *output = TIME_T_MIN;
515 return PARSEDATE_SOONER;
520 /* The Gregorian calendar was introduced 1582 */
522 return PARSEDATE_FAIL;
525 if((mdaynum > 31) || (monnum > 11) ||
526 (hournum > 23) || (minnum > 59) || (secnum > 60))
527 return PARSEDATE_FAIL; /* clearly an illegal date */
531 tm.tm_hour = hournum;
532 tm.tm_mday = mdaynum;
534 tm.tm_year = yearnum;
536 /* my_timegm() returns a time_t. time_t is often 32 bits, sometimes even on
537 architectures that feature 64 bit 'long' but ultimately time_t is the
538 correct data type to use.
542 /* Add the time zone diff between local time zone and GMT. */
546 if((tzoff > 0) && (t > TIME_T_MAX - tzoff)) {
547 *output = TIME_T_MAX;
548 return PARSEDATE_LATER; /* time_t overflow */
558 time_t curl_getdate(const char *p, const time_t *now)
561 int rc = parsedate(p, &parsed);
562 (void)now; /* legacy argument from the past that we ignore */
564 if(rc == PARSEDATE_OK) {
566 /* avoid returning -1 for a working scenario */
570 /* everything else is fail */
575 * Curl_gmtime() is a gmtime() replacement for portability. Do not use the
576 * gmtime_r() or gmtime() functions anywhere else but here.
580 CURLcode Curl_gmtime(time_t intime, struct tm *store)
584 /* thread-safe version */
585 tm = (struct tm *)gmtime_r(&intime, store);
587 tm = gmtime(&intime);
589 *store = *tm; /* copy the pointed struct to the local copy */
593 return CURLE_BAD_FUNCTION_ARGUMENT;