2004-09-11 17:24:02 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2004-09-11 17:24:02 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2004-09-11 17:24:02 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2004-09-11 17:24:02 +08:00
|
|
|
***************************************************************************/
|
|
|
|
/*
|
2004-09-12 03:16:34 +08:00
|
|
|
A brief summary of the date string formats this parser groks:
|
|
|
|
|
2004-09-11 17:24:02 +08:00
|
|
|
RFC 2616 3.3.1
|
|
|
|
|
|
|
|
Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
|
|
|
|
Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
|
|
|
|
Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
|
|
|
|
|
|
|
|
we support dates without week day name:
|
|
|
|
|
|
|
|
06 Nov 1994 08:49:37 GMT
|
|
|
|
06-Nov-94 08:49:37 GMT
|
|
|
|
Nov 6 08:49:37 1994
|
|
|
|
|
2004-09-13 15:57:12 +08:00
|
|
|
without the time zone:
|
2004-09-11 17:24:02 +08:00
|
|
|
|
|
|
|
06 Nov 1994 08:49:37
|
|
|
|
06-Nov-94 08:49:37
|
|
|
|
|
2004-09-12 03:16:34 +08:00
|
|
|
weird order:
|
2004-09-11 17:24:02 +08:00
|
|
|
|
2004-09-14 04:40:27 +08:00
|
|
|
1994 Nov 6 08:49:37 (GNU date fails)
|
2004-09-12 03:16:34 +08:00
|
|
|
GMT 08:49:37 06-Nov-94 Sunday
|
2004-09-14 04:40:27 +08:00
|
|
|
94 6 Nov 08:49:37 (GNU date fails)
|
2004-09-11 17:24:02 +08:00
|
|
|
|
2004-09-12 03:16:34 +08:00
|
|
|
time left out:
|
|
|
|
|
|
|
|
1994 Nov 6
|
|
|
|
06-Nov-94
|
|
|
|
Sun Nov 6 94
|
|
|
|
|
2004-09-13 15:57:12 +08:00
|
|
|
unusual separators:
|
2004-09-12 03:16:34 +08:00
|
|
|
|
|
|
|
1994.Nov.6
|
|
|
|
Sun/Nov/6/94/GMT
|
|
|
|
|
2004-09-13 15:57:12 +08:00
|
|
|
commonly used time zone names:
|
|
|
|
|
|
|
|
Sun, 06 Nov 1994 08:49:37 CET
|
|
|
|
06 Nov 1994 08:49:37 EST
|
|
|
|
|
|
|
|
time zones specified using RFC822 style:
|
|
|
|
|
|
|
|
Sun, 12 Sep 2004 15:05:58 -0700
|
|
|
|
Sat, 11 Sep 2004 21:32:11 +0200
|
|
|
|
|
2004-09-15 15:28:04 +08:00
|
|
|
compact numerical date strings:
|
2004-09-14 04:40:27 +08:00
|
|
|
|
|
|
|
20040912 15:05:58 -0700
|
|
|
|
20040911 +0200
|
|
|
|
|
2004-09-11 17:24:02 +08:00
|
|
|
*/
|
|
|
|
|
2013-01-07 02:06:49 +08:00
|
|
|
#include "curl_setup.h"
|
2004-09-11 17:24:02 +08:00
|
|
|
|
2012-03-22 22:53:03 +08:00
|
|
|
#include <limits.h>
|
|
|
|
|
2004-09-11 17:24:02 +08:00
|
|
|
#include <curl/curl.h>
|
2016-10-01 00:54:02 +08:00
|
|
|
#include "strcase.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "warnless.h"
|
|
|
|
#include "parsedate.h"
|
2004-09-11 17:24:02 +08:00
|
|
|
|
2011-06-04 01:31:32 +08:00
|
|
|
/*
|
|
|
|
* parsedate()
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
*
|
|
|
|
* PARSEDATE_OK - a fine conversion
|
|
|
|
* PARSEDATE_FAIL - failed to convert
|
|
|
|
* PARSEDATE_LATER - time overflow at the far end of time_t
|
|
|
|
* PARSEDATE_SOONER - time underflow at the low end of time_t
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int parsedate(const char *date, time_t *output);
|
|
|
|
|
|
|
|
#define PARSEDATE_OK 0
|
|
|
|
#define PARSEDATE_FAIL -1
|
|
|
|
#define PARSEDATE_LATER 1
|
|
|
|
#define PARSEDATE_SOONER 2
|
|
|
|
|
2019-09-13 05:28:00 +08:00
|
|
|
#if !defined(CURL_DISABLE_PARSEDATE) || !defined(CURL_DISABLE_FTP) || \
|
|
|
|
!defined(CURL_DISABLE_FILE)
|
|
|
|
/* These names are also used by FTP and FILE code */
|
2019-05-05 23:08:21 +08:00
|
|
|
const char * const Curl_wkday[] =
|
|
|
|
{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
|
|
|
|
const char * const Curl_month[]=
|
|
|
|
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
|
2019-09-13 05:28:00 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CURL_DISABLE_PARSEDATE
|
|
|
|
static const char * const weekday[] =
|
|
|
|
{ "Monday", "Tuesday", "Wednesday", "Thursday",
|
|
|
|
"Friday", "Saturday", "Sunday" };
|
2019-05-05 23:08:21 +08:00
|
|
|
|
|
|
|
struct tzinfo {
|
|
|
|
char name[5];
|
|
|
|
int offset; /* +/- in minutes */
|
|
|
|
};
|
|
|
|
|
2004-09-13 15:45:19 +08:00
|
|
|
/* Here's a bunch of frequently used time zone names. These were supported
|
|
|
|
by the old getdate parser. */
|
2005-08-10 05:59:31 +08:00
|
|
|
#define tDAYZONE -60 /* offset for daylight savings time */
|
2004-09-13 15:45:19 +08:00
|
|
|
static const struct tzinfo tz[]= {
|
2005-08-10 05:59:31 +08:00
|
|
|
{"GMT", 0}, /* Greenwich Mean */
|
2018-03-19 21:42:36 +08:00
|
|
|
{"UT", 0}, /* Universal Time */
|
2005-08-10 05:59:31 +08:00
|
|
|
{"UTC", 0}, /* Universal (Coordinated) */
|
|
|
|
{"WET", 0}, /* Western European */
|
|
|
|
{"BST", 0 tDAYZONE}, /* British Summer */
|
|
|
|
{"WAT", 60}, /* West Africa */
|
|
|
|
{"AST", 240}, /* Atlantic Standard */
|
|
|
|
{"ADT", 240 tDAYZONE}, /* Atlantic Daylight */
|
|
|
|
{"EST", 300}, /* Eastern Standard */
|
|
|
|
{"EDT", 300 tDAYZONE}, /* Eastern Daylight */
|
|
|
|
{"CST", 360}, /* Central Standard */
|
|
|
|
{"CDT", 360 tDAYZONE}, /* Central Daylight */
|
|
|
|
{"MST", 420}, /* Mountain Standard */
|
|
|
|
{"MDT", 420 tDAYZONE}, /* Mountain Daylight */
|
|
|
|
{"PST", 480}, /* Pacific Standard */
|
|
|
|
{"PDT", 480 tDAYZONE}, /* Pacific Daylight */
|
|
|
|
{"YST", 540}, /* Yukon Standard */
|
|
|
|
{"YDT", 540 tDAYZONE}, /* Yukon Daylight */
|
|
|
|
{"HST", 600}, /* Hawaii Standard */
|
|
|
|
{"HDT", 600 tDAYZONE}, /* Hawaii Daylight */
|
|
|
|
{"CAT", 600}, /* Central Alaska */
|
|
|
|
{"AHST", 600}, /* Alaska-Hawaii Standard */
|
|
|
|
{"NT", 660}, /* Nome */
|
|
|
|
{"IDLW", 720}, /* International Date Line West */
|
|
|
|
{"CET", -60}, /* Central European */
|
|
|
|
{"MET", -60}, /* Middle European */
|
|
|
|
{"MEWT", -60}, /* Middle European Winter */
|
2005-10-05 02:15:33 +08:00
|
|
|
{"MEST", -60 tDAYZONE}, /* Middle European Summer */
|
|
|
|
{"CEST", -60 tDAYZONE}, /* Central European Summer */
|
2005-08-10 05:59:31 +08:00
|
|
|
{"MESZ", -60 tDAYZONE}, /* Middle European Summer */
|
|
|
|
{"FWT", -60}, /* French Winter */
|
|
|
|
{"FST", -60 tDAYZONE}, /* French Summer */
|
|
|
|
{"EET", -120}, /* Eastern Europe, USSR Zone 1 */
|
|
|
|
{"WAST", -420}, /* West Australian Standard */
|
|
|
|
{"WADT", -420 tDAYZONE}, /* West Australian Daylight */
|
|
|
|
{"CCT", -480}, /* China Coast, USSR Zone 7 */
|
|
|
|
{"JST", -540}, /* Japan Standard, USSR Zone 8 */
|
|
|
|
{"EAST", -600}, /* Eastern Australian Standard */
|
|
|
|
{"EADT", -600 tDAYZONE}, /* Eastern Australian Daylight */
|
|
|
|
{"GST", -600}, /* Guam Standard, USSR Zone 9 */
|
|
|
|
{"NZT", -720}, /* New Zealand */
|
|
|
|
{"NZST", -720}, /* New Zealand Standard */
|
|
|
|
{"NZDT", -720 tDAYZONE}, /* New Zealand Daylight */
|
|
|
|
{"IDLE", -720}, /* International Date Line East */
|
2009-10-19 05:56:19 +08:00
|
|
|
/* Next up: Military timezone names. RFC822 allowed these, but (as noted in
|
|
|
|
RFC 1123) had their signs wrong. Here we use the correct signs to match
|
|
|
|
actual military usage.
|
|
|
|
*/
|
2017-09-10 05:55:08 +08:00
|
|
|
{"A", 1 * 60}, /* Alpha */
|
|
|
|
{"B", 2 * 60}, /* Bravo */
|
|
|
|
{"C", 3 * 60}, /* Charlie */
|
|
|
|
{"D", 4 * 60}, /* Delta */
|
|
|
|
{"E", 5 * 60}, /* Echo */
|
|
|
|
{"F", 6 * 60}, /* Foxtrot */
|
|
|
|
{"G", 7 * 60}, /* Golf */
|
|
|
|
{"H", 8 * 60}, /* Hotel */
|
|
|
|
{"I", 9 * 60}, /* India */
|
2011-04-20 21:17:42 +08:00
|
|
|
/* "J", Juliet is not used as a timezone, to indicate the observer's local
|
|
|
|
time */
|
2017-09-10 05:55:08 +08:00
|
|
|
{"K", 10 * 60}, /* Kilo */
|
|
|
|
{"L", 11 * 60}, /* Lima */
|
|
|
|
{"M", 12 * 60}, /* Mike */
|
2009-10-19 05:56:19 +08:00
|
|
|
{"N", -1 * 60}, /* November */
|
|
|
|
{"O", -2 * 60}, /* Oscar */
|
|
|
|
{"P", -3 * 60}, /* Papa */
|
|
|
|
{"Q", -4 * 60}, /* Quebec */
|
|
|
|
{"R", -5 * 60}, /* Romeo */
|
|
|
|
{"S", -6 * 60}, /* Sierra */
|
|
|
|
{"T", -7 * 60}, /* Tango */
|
|
|
|
{"U", -8 * 60}, /* Uniform */
|
|
|
|
{"V", -9 * 60}, /* Victor */
|
|
|
|
{"W", -10 * 60}, /* Whiskey */
|
|
|
|
{"X", -11 * 60}, /* X-ray */
|
|
|
|
{"Y", -12 * 60}, /* Yankee */
|
|
|
|
{"Z", 0}, /* Zulu, zero meridian, a.k.a. UTC */
|
2004-09-13 15:45:19 +08:00
|
|
|
};
|
2004-09-11 17:24:02 +08:00
|
|
|
|
|
|
|
/* returns:
|
|
|
|
-1 no day
|
|
|
|
0 monday - 6 sunday
|
|
|
|
*/
|
|
|
|
|
2007-11-07 00:20:04 +08:00
|
|
|
static int checkday(const char *check, size_t len)
|
2004-09-11 17:24:02 +08:00
|
|
|
{
|
|
|
|
int i;
|
2004-12-21 02:23:43 +08:00
|
|
|
const char * const *what;
|
2004-09-11 17:24:02 +08:00
|
|
|
if(len > 3)
|
|
|
|
what = &weekday[0];
|
2023-02-17 17:04:31 +08:00
|
|
|
else if(len == 3)
|
2005-02-11 08:03:49 +08:00
|
|
|
what = &Curl_wkday[0];
|
2023-02-17 17:04:31 +08:00
|
|
|
else
|
|
|
|
return -1; /* too short */
|
2017-09-10 05:09:06 +08:00
|
|
|
for(i = 0; i<7; i++) {
|
2023-02-17 17:04:31 +08:00
|
|
|
size_t ilen = strlen(what[0]);
|
|
|
|
if((ilen == len) &&
|
|
|
|
strncasecompare(check, what[0], len))
|
|
|
|
return i;
|
2004-09-11 17:24:02 +08:00
|
|
|
what++;
|
|
|
|
}
|
2023-02-17 17:04:31 +08:00
|
|
|
return -1;
|
2004-09-11 17:24:02 +08:00
|
|
|
}
|
|
|
|
|
2023-02-17 17:04:31 +08:00
|
|
|
static int checkmonth(const char *check, size_t len)
|
2004-09-11 17:24:02 +08:00
|
|
|
{
|
|
|
|
int i;
|
2023-02-17 17:04:31 +08:00
|
|
|
const char * const *what = &Curl_month[0];
|
|
|
|
if(len != 3)
|
|
|
|
return -1; /* not a month */
|
2004-09-11 17:24:02 +08:00
|
|
|
|
2017-09-10 05:09:06 +08:00
|
|
|
for(i = 0; i<12; i++) {
|
2023-02-17 17:04:31 +08:00
|
|
|
if(strncasecompare(check, what[0], 3))
|
|
|
|
return i;
|
2004-09-11 17:24:02 +08:00
|
|
|
what++;
|
|
|
|
}
|
2023-02-17 17:04:31 +08:00
|
|
|
return -1; /* return the offset or -1, no real offset is -1 */
|
2004-09-11 17:24:02 +08:00
|
|
|
}
|
|
|
|
|
2004-09-13 15:45:19 +08:00
|
|
|
/* return the time zone offset between GMT and the input one, in number
|
|
|
|
of seconds or -1 if the timezone wasn't found/legal */
|
|
|
|
|
2023-02-17 17:04:31 +08:00
|
|
|
static int checktz(const char *check, size_t len)
|
2004-09-11 17:24:02 +08:00
|
|
|
{
|
2004-09-13 15:45:19 +08:00
|
|
|
unsigned int i;
|
2023-02-17 17:04:31 +08:00
|
|
|
const struct tzinfo *what = tz;
|
|
|
|
if(len > 4) /* longer than any valid timezone */
|
|
|
|
return -1;
|
2004-09-11 17:24:02 +08:00
|
|
|
|
2017-09-10 05:09:06 +08:00
|
|
|
for(i = 0; i< sizeof(tz)/sizeof(tz[0]); i++) {
|
2023-02-17 17:04:31 +08:00
|
|
|
size_t ilen = strlen(what->name);
|
|
|
|
if((ilen == len) &&
|
|
|
|
strncasecompare(check, what->name, len))
|
|
|
|
return what->offset*60;
|
2004-09-11 17:24:02 +08:00
|
|
|
what++;
|
|
|
|
}
|
2023-02-17 17:04:31 +08:00
|
|
|
return -1;
|
2004-09-11 17:24:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void skip(const char **date)
|
|
|
|
{
|
|
|
|
/* skip everything that aren't letters or digits */
|
2006-10-18 05:32:56 +08:00
|
|
|
while(**date && !ISALNUM(**date))
|
2004-09-11 17:24:02 +08:00
|
|
|
(*date)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum assume {
|
|
|
|
DATE_MDAY,
|
|
|
|
DATE_YEAR,
|
|
|
|
DATE_TIME
|
|
|
|
};
|
|
|
|
|
2020-09-21 18:55:38 +08:00
|
|
|
/*
|
|
|
|
* time2epoch: time stamp to seconds since epoch in GMT time zone. Similar to
|
|
|
|
* mktime but for GMT only.
|
2008-09-30 07:22:41 +08:00
|
|
|
*/
|
2020-09-21 18:55:38 +08:00
|
|
|
static time_t time2epoch(int sec, int min, int hour,
|
|
|
|
int mday, int mon, int year)
|
2008-09-23 19:00:01 +08:00
|
|
|
{
|
2008-09-30 07:22:41 +08:00
|
|
|
static const int month_days_cumulative [12] =
|
2008-09-23 19:00:01 +08:00
|
|
|
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
|
2020-09-21 18:55:38 +08:00
|
|
|
int leap_days = year - (mon <= 1);
|
2008-09-23 19:00:01 +08:00
|
|
|
leap_days = ((leap_days / 4) - (leap_days / 100) + (leap_days / 400)
|
|
|
|
- (1969 / 4) + (1969 / 100) - (1969 / 400));
|
2020-09-21 18:55:38 +08:00
|
|
|
return ((((time_t) (year - 1970) * 365
|
|
|
|
+ leap_days + month_days_cumulative[mon] + mday - 1) * 24
|
|
|
|
+ hour) * 60 + min) * 60 + sec;
|
2008-09-23 19:00:01 +08:00
|
|
|
}
|
|
|
|
|
2023-02-17 18:23:12 +08:00
|
|
|
/* Returns the value of a single-digit or two-digit decimal number, return
|
|
|
|
then pointer to after the number. The 'date' pointer is known to point to a
|
|
|
|
digit. */
|
|
|
|
static int oneortwodigit(const char *date, const char **endp)
|
|
|
|
{
|
|
|
|
int num = date[0] - '0';
|
|
|
|
if(ISDIGIT(date[1])) {
|
|
|
|
*endp = &date[2];
|
|
|
|
return num*10 + (date[1] - '0');
|
|
|
|
}
|
|
|
|
*endp = &date[1];
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* HH:MM:SS or HH:MM and accept single-digits too */
|
|
|
|
static bool match_time(const char *date,
|
|
|
|
int *h, int *m, int *s, char **endp)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
int hh, mm, ss = 0;
|
|
|
|
hh = oneortwodigit(date, &p);
|
|
|
|
if((hh < 24) && (*p == ':') && ISDIGIT(p[1])) {
|
|
|
|
mm = oneortwodigit(&p[1], &p);
|
|
|
|
if(mm < 60) {
|
|
|
|
if((*p == ':') && ISDIGIT(p[1])) {
|
|
|
|
ss = oneortwodigit(&p[1], &p);
|
|
|
|
if(ss <= 60) {
|
|
|
|
/* valid HH:MM:SS */
|
|
|
|
goto match;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* valid HH:MM */
|
|
|
|
goto match;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE; /* not a time string */
|
2023-05-18 12:54:18 +08:00
|
|
|
match:
|
2023-02-17 18:23:12 +08:00
|
|
|
*h = hh;
|
|
|
|
*m = mm;
|
|
|
|
*s = ss;
|
|
|
|
*endp = (char *)p;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-09-03 16:13:32 +08:00
|
|
|
/*
|
2011-06-04 01:31:32 +08:00
|
|
|
* parsedate()
|
2009-09-03 16:13:32 +08:00
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
*
|
|
|
|
* PARSEDATE_OK - a fine conversion
|
|
|
|
* PARSEDATE_FAIL - failed to convert
|
|
|
|
* PARSEDATE_LATER - time overflow at the far end of time_t
|
|
|
|
* PARSEDATE_SOONER - time underflow at the low end of time_t
|
|
|
|
*/
|
|
|
|
|
2023-02-17 17:04:31 +08:00
|
|
|
/* Wednesday is the longest name this parser knows about */
|
|
|
|
#define NAME_LEN 12
|
|
|
|
|
2011-06-04 01:31:32 +08:00
|
|
|
static int parsedate(const char *date, time_t *output)
|
2004-09-11 17:24:02 +08:00
|
|
|
{
|
|
|
|
time_t t = 0;
|
2017-09-10 05:09:06 +08:00
|
|
|
int wdaynum = -1; /* day of the week number, 0-6 (mon-sun) */
|
|
|
|
int monnum = -1; /* month of the year number, 0-11 */
|
|
|
|
int mdaynum = -1; /* day of month, 1 - 31 */
|
|
|
|
int hournum = -1;
|
|
|
|
int minnum = -1;
|
|
|
|
int secnum = -1;
|
|
|
|
int yearnum = -1;
|
|
|
|
int tzoff = -1;
|
2004-09-11 17:24:02 +08:00
|
|
|
enum assume dignext = DATE_MDAY;
|
2004-09-13 15:45:19 +08:00
|
|
|
const char *indate = date; /* save the original pointer */
|
2004-09-11 17:24:02 +08:00
|
|
|
int part = 0; /* max 6 parts */
|
|
|
|
|
2004-09-12 03:16:34 +08:00
|
|
|
while(*date && (part < 6)) {
|
2017-09-10 05:09:06 +08:00
|
|
|
bool found = FALSE;
|
2004-09-11 17:24:02 +08:00
|
|
|
|
|
|
|
skip(&date);
|
|
|
|
|
2006-10-18 05:32:56 +08:00
|
|
|
if(ISALPHA(*date)) {
|
2004-09-11 17:24:02 +08:00
|
|
|
/* a name coming up */
|
2023-02-17 17:04:31 +08:00
|
|
|
size_t len = 0;
|
|
|
|
const char *p = date;
|
|
|
|
while(ISALPHA(*p) && (len < NAME_LEN)) {
|
|
|
|
p++;
|
|
|
|
len++;
|
2004-09-11 17:24:02 +08:00
|
|
|
}
|
|
|
|
|
2023-02-17 17:04:31 +08:00
|
|
|
if(len != NAME_LEN) {
|
|
|
|
if(wdaynum == -1) {
|
|
|
|
wdaynum = checkday(date, len);
|
|
|
|
if(wdaynum != -1)
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
if(!found && (monnum == -1)) {
|
|
|
|
monnum = checkmonth(date, len);
|
|
|
|
if(monnum != -1)
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!found && (tzoff == -1)) {
|
|
|
|
/* this just must be a time zone string */
|
|
|
|
tzoff = checktz(date, len);
|
|
|
|
if(tzoff != -1)
|
|
|
|
found = TRUE;
|
|
|
|
}
|
2004-09-11 17:24:02 +08:00
|
|
|
}
|
|
|
|
if(!found)
|
2009-09-03 16:13:32 +08:00
|
|
|
return PARSEDATE_FAIL; /* bad string */
|
2004-09-11 17:24:02 +08:00
|
|
|
|
|
|
|
date += len;
|
|
|
|
}
|
2006-10-18 05:32:56 +08:00
|
|
|
else if(ISDIGIT(*date)) {
|
2004-09-11 17:24:02 +08:00
|
|
|
/* a digit */
|
2004-09-20 21:21:48 +08:00
|
|
|
int val;
|
2004-09-11 17:24:02 +08:00
|
|
|
char *end;
|
|
|
|
if((secnum == -1) &&
|
2023-02-17 18:23:12 +08:00
|
|
|
match_time(date, &hournum, &minnum, &secnum, &end)) {
|
|
|
|
/* time stamp */
|
|
|
|
date = end;
|
2010-09-27 22:54:02 +08:00
|
|
|
}
|
2004-09-11 17:24:02 +08:00
|
|
|
else {
|
2012-03-22 22:53:03 +08:00
|
|
|
long lval;
|
|
|
|
int error;
|
|
|
|
int old_errno;
|
|
|
|
|
2017-06-19 12:52:38 +08:00
|
|
|
old_errno = errno;
|
|
|
|
errno = 0;
|
2012-03-22 22:53:03 +08:00
|
|
|
lval = strtol(date, &end, 10);
|
2017-06-19 12:52:38 +08:00
|
|
|
error = errno;
|
|
|
|
if(errno != old_errno)
|
|
|
|
errno = old_errno;
|
2012-03-22 22:53:03 +08:00
|
|
|
|
|
|
|
if(error)
|
|
|
|
return PARSEDATE_FAIL;
|
|
|
|
|
2014-03-02 16:04:35 +08:00
|
|
|
#if LONG_MAX != INT_MAX
|
2012-03-22 22:53:03 +08:00
|
|
|
if((lval > (long)INT_MAX) || (lval < (long)INT_MIN))
|
|
|
|
return PARSEDATE_FAIL;
|
2014-03-02 16:04:35 +08:00
|
|
|
#endif
|
2012-03-22 22:53:03 +08:00
|
|
|
|
|
|
|
val = curlx_sltosi(lval);
|
2004-09-11 17:24:02 +08:00
|
|
|
|
2004-09-14 04:40:27 +08:00
|
|
|
if((tzoff == -1) &&
|
|
|
|
((end - date) == 4) &&
|
2008-01-06 18:50:57 +08:00
|
|
|
(val <= 1400) &&
|
2004-09-14 04:40:27 +08:00
|
|
|
(indate< date) &&
|
|
|
|
((date[-1] == '+' || date[-1] == '-'))) {
|
2008-01-06 18:50:57 +08:00
|
|
|
/* four digits and a value less than or equal to 1400 (to take into
|
2011-04-19 21:54:13 +08:00
|
|
|
account all sorts of funny time zone diffs) and it is preceded
|
2008-01-06 18:50:57 +08:00
|
|
|
with a plus or minus. This is a time zone indication. 1400 is
|
|
|
|
picked since +1300 is frequently used and +1400 is mentioned as
|
|
|
|
an edge number in the document "ISO C 200X Proposal: Timezone
|
|
|
|
Functions" at http://david.tribble.com/text/c0xtimezone.html If
|
|
|
|
anyone has a more authoritative source for the exact maximum time
|
|
|
|
zone offsets, please speak up! */
|
2004-09-13 15:45:19 +08:00
|
|
|
found = TRUE;
|
|
|
|
tzoff = (val/100 * 60 + val%100)*60;
|
|
|
|
|
|
|
|
/* the + and - prefix indicates the local time compared to GMT,
|
2018-02-24 07:29:01 +08:00
|
|
|
this we need their reversed math to get what we want */
|
2004-09-13 15:45:19 +08:00
|
|
|
tzoff = date[-1]=='+'?-tzoff:tzoff;
|
|
|
|
}
|
|
|
|
|
2004-09-14 04:40:27 +08:00
|
|
|
if(((end - date) == 8) &&
|
|
|
|
(yearnum == -1) &&
|
|
|
|
(monnum == -1) &&
|
|
|
|
(mdaynum == -1)) {
|
|
|
|
/* 8 digits, no year, month or day yet. This is YYYYMMDD */
|
|
|
|
found = TRUE;
|
|
|
|
yearnum = val/10000;
|
|
|
|
monnum = (val%10000)/100-1; /* month is 0 - 11 */
|
|
|
|
mdaynum = val%100;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) {
|
2004-09-11 17:24:02 +08:00
|
|
|
if((val > 0) && (val<32)) {
|
|
|
|
mdaynum = val;
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
dignext = DATE_YEAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
|
|
|
|
yearnum = val;
|
|
|
|
found = TRUE;
|
2018-01-19 03:24:05 +08:00
|
|
|
if(yearnum < 100) {
|
2007-11-07 17:21:35 +08:00
|
|
|
if(yearnum > 70)
|
2004-09-11 17:24:02 +08:00
|
|
|
yearnum += 1900;
|
|
|
|
else
|
|
|
|
yearnum += 2000;
|
|
|
|
}
|
|
|
|
if(mdaynum == -1)
|
|
|
|
dignext = DATE_MDAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!found)
|
2009-09-03 16:13:32 +08:00
|
|
|
return PARSEDATE_FAIL;
|
2004-09-11 17:24:02 +08:00
|
|
|
|
|
|
|
date = end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
part++;
|
|
|
|
}
|
|
|
|
|
2004-09-11 21:07:42 +08:00
|
|
|
if(-1 == secnum)
|
|
|
|
secnum = minnum = hournum = 0; /* no time, make it zero */
|
|
|
|
|
|
|
|
if((-1 == mdaynum) ||
|
|
|
|
(-1 == monnum) ||
|
|
|
|
(-1 == yearnum))
|
|
|
|
/* lacks vital info, fail */
|
2009-09-03 16:13:32 +08:00
|
|
|
return PARSEDATE_FAIL;
|
2004-09-11 21:07:42 +08:00
|
|
|
|
2018-01-19 03:24:05 +08:00
|
|
|
#ifdef HAVE_TIME_T_UNSIGNED
|
|
|
|
if(yearnum < 1970) {
|
|
|
|
/* only positive numbers cannot return earlier */
|
|
|
|
*output = TIME_T_MIN;
|
|
|
|
return PARSEDATE_SOONER;
|
2009-09-03 16:13:32 +08:00
|
|
|
}
|
2004-11-11 17:26:09 +08:00
|
|
|
#endif
|
|
|
|
|
2018-01-19 03:24:05 +08:00
|
|
|
#if (SIZEOF_TIME_T < 5)
|
|
|
|
|
2018-02-01 05:56:00 +08:00
|
|
|
#ifdef HAVE_TIME_T_UNSIGNED
|
2018-01-19 03:24:05 +08:00
|
|
|
/* an unsigned 32 bit time_t can only hold dates to 2106 */
|
|
|
|
if(yearnum > 2105) {
|
|
|
|
*output = TIME_T_MAX;
|
|
|
|
return PARSEDATE_LATER;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* a signed 32 bit time_t can only hold dates to the beginning of 2038 */
|
|
|
|
if(yearnum > 2037) {
|
|
|
|
*output = TIME_T_MAX;
|
|
|
|
return PARSEDATE_LATER;
|
|
|
|
}
|
|
|
|
if(yearnum < 1903) {
|
|
|
|
*output = TIME_T_MIN;
|
2009-09-03 16:13:32 +08:00
|
|
|
return PARSEDATE_SOONER;
|
|
|
|
}
|
2018-01-19 03:24:05 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* The Gregorian calendar was introduced 1582 */
|
|
|
|
if(yearnum < 1583)
|
|
|
|
return PARSEDATE_FAIL;
|
|
|
|
#endif
|
2009-09-03 16:13:32 +08:00
|
|
|
|
2011-06-22 05:18:05 +08:00
|
|
|
if((mdaynum > 31) || (monnum > 11) ||
|
|
|
|
(hournum > 23) || (minnum > 59) || (secnum > 60))
|
|
|
|
return PARSEDATE_FAIL; /* clearly an illegal date */
|
|
|
|
|
2020-09-21 18:55:38 +08:00
|
|
|
/* time2epoch() returns a time_t. time_t is often 32 bits, sometimes even on
|
2018-01-19 03:24:05 +08:00
|
|
|
architectures that feature 64 bit 'long' but ultimately time_t is the
|
|
|
|
correct data type to use.
|
2005-03-09 00:31:56 +08:00
|
|
|
*/
|
2020-09-21 18:55:38 +08:00
|
|
|
t = time2epoch(secnum, minnum, hournum, mdaynum, monnum, yearnum);
|
2004-09-12 03:16:34 +08:00
|
|
|
|
2018-01-19 03:24:05 +08:00
|
|
|
/* Add the time zone diff between local time zone and GMT. */
|
|
|
|
if(tzoff == -1)
|
|
|
|
tzoff = 0;
|
2004-09-11 17:24:02 +08:00
|
|
|
|
2018-01-19 03:24:05 +08:00
|
|
|
if((tzoff > 0) && (t > TIME_T_MAX - tzoff)) {
|
|
|
|
*output = TIME_T_MAX;
|
|
|
|
return PARSEDATE_LATER; /* time_t overflow */
|
2004-09-11 17:24:02 +08:00
|
|
|
}
|
|
|
|
|
2018-01-19 03:24:05 +08:00
|
|
|
t += tzoff;
|
|
|
|
|
2009-09-03 16:13:32 +08:00
|
|
|
*output = t;
|
|
|
|
|
|
|
|
return PARSEDATE_OK;
|
2004-09-11 17:24:02 +08:00
|
|
|
}
|
2019-05-05 23:08:21 +08:00
|
|
|
#else
|
|
|
|
/* disabled */
|
|
|
|
static int parsedate(const char *date, time_t *output)
|
|
|
|
{
|
|
|
|
(void)date;
|
|
|
|
*output = 0;
|
|
|
|
return PARSEDATE_OK; /* a lie */
|
|
|
|
}
|
|
|
|
#endif
|
2004-09-11 17:24:02 +08:00
|
|
|
|
|
|
|
time_t curl_getdate(const char *p, const time_t *now)
|
|
|
|
{
|
2014-12-10 19:38:38 +08:00
|
|
|
time_t parsed = -1;
|
2011-06-04 01:31:32 +08:00
|
|
|
int rc = parsedate(p, &parsed);
|
2009-09-03 16:13:32 +08:00
|
|
|
(void)now; /* legacy argument from the past that we ignore */
|
|
|
|
|
2018-02-02 05:23:20 +08:00
|
|
|
if(rc == PARSEDATE_OK) {
|
2018-01-19 03:24:05 +08:00
|
|
|
if(parsed == -1)
|
|
|
|
/* avoid returning -1 for a working scenario */
|
|
|
|
parsed++;
|
2009-09-03 16:13:32 +08:00
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
/* everything else is fail */
|
|
|
|
return -1;
|
2004-09-11 17:24:02 +08:00
|
|
|
}
|
2011-02-07 22:00:48 +08:00
|
|
|
|
2019-11-28 22:27:58 +08:00
|
|
|
/* Curl_getdate_capped() differs from curl_getdate() in that this will return
|
|
|
|
TIME_T_MAX in case the parsed time value was too big, instead of an
|
|
|
|
error. */
|
|
|
|
|
|
|
|
time_t Curl_getdate_capped(const char *p)
|
|
|
|
{
|
|
|
|
time_t parsed = -1;
|
|
|
|
int rc = parsedate(p, &parsed);
|
|
|
|
|
|
|
|
switch(rc) {
|
|
|
|
case PARSEDATE_OK:
|
|
|
|
if(parsed == -1)
|
|
|
|
/* avoid returning -1 for a working scenario */
|
|
|
|
parsed++;
|
|
|
|
return parsed;
|
|
|
|
case PARSEDATE_LATER:
|
|
|
|
/* this returns the maximum time value */
|
|
|
|
return parsed;
|
|
|
|
default:
|
|
|
|
return -1; /* everything else is fail */
|
|
|
|
}
|
|
|
|
/* UNREACHABLE */
|
|
|
|
}
|
|
|
|
|
2011-02-07 22:09:24 +08:00
|
|
|
/*
|
|
|
|
* Curl_gmtime() is a gmtime() replacement for portability. Do not use the
|
|
|
|
* gmtime_r() or gmtime() functions anywhere else but here.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-02-07 22:00:48 +08:00
|
|
|
CURLcode Curl_gmtime(time_t intime, struct tm *store)
|
|
|
|
{
|
|
|
|
const struct tm *tm;
|
|
|
|
#ifdef HAVE_GMTIME_R
|
|
|
|
/* thread-safe version */
|
2011-02-14 20:42:01 +08:00
|
|
|
tm = (struct tm *)gmtime_r(&intime, store);
|
2011-02-07 22:00:48 +08:00
|
|
|
#else
|
2020-07-27 20:28:37 +08:00
|
|
|
/* !checksrc! disable BANNEDFUNC 1 */
|
2011-02-14 20:42:01 +08:00
|
|
|
tm = gmtime(&intime);
|
2011-02-07 22:00:48 +08:00
|
|
|
if(tm)
|
|
|
|
*store = *tm; /* copy the pointed struct to the local copy */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(!tm)
|
|
|
|
return CURLE_BAD_FUNCTION_ARGUMENT;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|