Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 581 lines (497 sloc) 16.329 kb
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
e8a3243 @yangtse parsedate.c: fix a numeric overflow
yangtse authored
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
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 ***************************************************************************/
22 /*
ade8e47 @bagder more docs and fixed the delta compared to GMT that prevented test case 1...
authored
23 A brief summary of the date string formats this parser groks:
24
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
25 RFC 2616 3.3.1
26
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
30
31 we support dates without week day name:
32
33 06 Nov 1994 08:49:37 GMT
34 06-Nov-94 08:49:37 GMT
35 Nov 6 08:49:37 1994
36
b85a036 @bagder added more examples/docs in the top comment
authored
37 without the time zone:
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
38
39 06 Nov 1994 08:49:37
40 06-Nov-94 08:49:37
41
ade8e47 @bagder more docs and fixed the delta compared to GMT that prevented test case 1...
authored
42 weird order:
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
43
2e7dcc1 @bagder support for YYYYMMDD added, which allows us to keep using the lib/ftp.c ...
authored
44 1994 Nov 6 08:49:37 (GNU date fails)
ade8e47 @bagder more docs and fixed the delta compared to GMT that prevented test case 1...
authored
45 GMT 08:49:37 06-Nov-94 Sunday
2e7dcc1 @bagder support for YYYYMMDD added, which allows us to keep using the lib/ftp.c ...
authored
46 94 6 Nov 08:49:37 (GNU date fails)
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
47
ade8e47 @bagder more docs and fixed the delta compared to GMT that prevented test case 1...
authored
48 time left out:
49
50 1994 Nov 6
51 06-Nov-94
52 Sun Nov 6 94
53
b85a036 @bagder added more examples/docs in the top comment
authored
54 unusual separators:
ade8e47 @bagder more docs and fixed the delta compared to GMT that prevented test case 1...
authored
55
56 1994.Nov.6
57 Sun/Nov/6/94/GMT
58
b85a036 @bagder added more examples/docs in the top comment
authored
59 commonly used time zone names:
60
61 Sun, 06 Nov 1994 08:49:37 CET
62 06 Nov 1994 08:49:37 EST
63
64 time zones specified using RFC822 style:
65
66 Sun, 12 Sep 2004 15:05:58 -0700
67 Sat, 11 Sep 2004 21:32:11 +0200
68
f71b3f4 @bagder Replaced the former date parser with a rewrite. No more yacc/bison neede...
authored
69 compact numerical date strings:
2e7dcc1 @bagder support for YYYYMMDD added, which allows us to keep using the lib/ftp.c ...
authored
70
71 20040912 15:05:58 -0700
72 20040911 +0200
73
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
74 */
75
f1586cb @yangtse stdio.h, stdlib.h, string.h, stdarg.h and ctype.h inclusion done in setu...
yangtse authored
76 #include "setup.h"
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
77
e8a3243 @yangtse parsedate.c: fix a numeric overflow
yangtse authored
78 #ifdef HAVE_LIMITS_H
79 #include <limits.h>
80 #endif
81
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
82 #include <curl/curl.h>
b701ea3 @bagder moved the Curl_raw_ functions into the new lib/rawstr.c file for easier ...
authored
83 #include "rawstr.h"
7e3f0bf @yangtse fix compiler warning: conversion may lose significant bits
yangtse authored
84 #include "warnless.h"
f7ef60c @yangtse fix compiler warning: external definition with no prior declaration
yangtse authored
85 #include "parsedate.h"
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
86
e7cefd6 @bagder Removed all uses of strftime() since it uses the localised version of th...
authored
87 const char * const Curl_wkday[] =
88 {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
80d3012 @dfandrich Make some more arrays of pointers const.
dfandrich authored
89 static const char * const weekday[] =
e7cefd6 @bagder Removed all uses of strftime() since it uses the localised version of th...
authored
90 { "Monday", "Tuesday", "Wednesday", "Thursday",
91 "Friday", "Saturday", "Sunday" };
92 const char * const Curl_month[]=
93 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
94 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
95
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
96 struct tzinfo {
7fc4e8a @dfandrich Changed some arrays of char* to arrays of char[] to reduce data size and
dfandrich authored
97 char name[5];
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
98 int offset; /* +/- in minutes */
99 };
100
a6f14e1 @bagder parsedate: turn private and static
authored
101 /*
102 * parsedate()
103 *
104 * Returns:
105 *
106 * PARSEDATE_OK - a fine conversion
107 * PARSEDATE_FAIL - failed to convert
108 * PARSEDATE_LATER - time overflow at the far end of time_t
109 * PARSEDATE_SOONER - time underflow at the low end of time_t
110 */
111
112 static int parsedate(const char *date, time_t *output);
113
114 #define PARSEDATE_OK 0
115 #define PARSEDATE_FAIL -1
116 #define PARSEDATE_LATER 1
117 #define PARSEDATE_SOONER 2
118
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
119 /* Here's a bunch of frequently used time zone names. These were supported
120 by the old getdate parser. */
6c157a4 @bagder Christopher R. Palmer fixed the offsets used for date parsings when the ...
authored
121 #define tDAYZONE -60 /* offset for daylight savings time */
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
122 static const struct tzinfo tz[]= {
6c157a4 @bagder Christopher R. Palmer fixed the offsets used for date parsings when the ...
authored
123 {"GMT", 0}, /* Greenwich Mean */
124 {"UTC", 0}, /* Universal (Coordinated) */
125 {"WET", 0}, /* Western European */
126 {"BST", 0 tDAYZONE}, /* British Summer */
127 {"WAT", 60}, /* West Africa */
128 {"AST", 240}, /* Atlantic Standard */
129 {"ADT", 240 tDAYZONE}, /* Atlantic Daylight */
130 {"EST", 300}, /* Eastern Standard */
131 {"EDT", 300 tDAYZONE}, /* Eastern Daylight */
132 {"CST", 360}, /* Central Standard */
133 {"CDT", 360 tDAYZONE}, /* Central Daylight */
134 {"MST", 420}, /* Mountain Standard */
135 {"MDT", 420 tDAYZONE}, /* Mountain Daylight */
136 {"PST", 480}, /* Pacific Standard */
137 {"PDT", 480 tDAYZONE}, /* Pacific Daylight */
138 {"YST", 540}, /* Yukon Standard */
139 {"YDT", 540 tDAYZONE}, /* Yukon Daylight */
140 {"HST", 600}, /* Hawaii Standard */
141 {"HDT", 600 tDAYZONE}, /* Hawaii Daylight */
142 {"CAT", 600}, /* Central Alaska */
143 {"AHST", 600}, /* Alaska-Hawaii Standard */
144 {"NT", 660}, /* Nome */
145 {"IDLW", 720}, /* International Date Line West */
146 {"CET", -60}, /* Central European */
147 {"MET", -60}, /* Middle European */
148 {"MEWT", -60}, /* Middle European Winter */
db83a0e @bagder Michael Wallner reported that the date parser had wrong offset stored fo...
authored
149 {"MEST", -60 tDAYZONE}, /* Middle European Summer */
150 {"CEST", -60 tDAYZONE}, /* Central European Summer */
6c157a4 @bagder Christopher R. Palmer fixed the offsets used for date parsings when the ...
authored
151 {"MESZ", -60 tDAYZONE}, /* Middle European Summer */
152 {"FWT", -60}, /* French Winter */
153 {"FST", -60 tDAYZONE}, /* French Summer */
154 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
155 {"WAST", -420}, /* West Australian Standard */
156 {"WADT", -420 tDAYZONE}, /* West Australian Daylight */
157 {"CCT", -480}, /* China Coast, USSR Zone 7 */
158 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
159 {"EAST", -600}, /* Eastern Australian Standard */
160 {"EADT", -600 tDAYZONE}, /* Eastern Australian Daylight */
161 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
162 {"NZT", -720}, /* New Zealand */
163 {"NZST", -720}, /* New Zealand Standard */
164 {"NZDT", -720 tDAYZONE}, /* New Zealand Daylight */
165 {"IDLE", -720}, /* International Date Line East */
6f4a5a4 @bagder - Ray Dassen provided a patch in Debian's bug tracker (bug number #55146...
authored
166 /* Next up: Military timezone names. RFC822 allowed these, but (as noted in
167 RFC 1123) had their signs wrong. Here we use the correct signs to match
168 actual military usage.
169 */
170 {"A", +1 * 60}, /* Alpha */
171 {"B", +2 * 60}, /* Bravo */
172 {"C", +3 * 60}, /* Charlie */
173 {"D", +4 * 60}, /* Delta */
174 {"E", +5 * 60}, /* Echo */
175 {"F", +6 * 60}, /* Foxtrot */
176 {"G", +7 * 60}, /* Golf */
177 {"H", +8 * 60}, /* Hotel */
178 {"I", +9 * 60}, /* India */
b903186 @bagder source cleanup: unify look, style and indent levels
authored
179 /* "J", Juliet is not used as a timezone, to indicate the observer's local
180 time */
6f4a5a4 @bagder - Ray Dassen provided a patch in Debian's bug tracker (bug number #55146...
authored
181 {"K", +10 * 60}, /* Kilo */
182 {"L", +11 * 60}, /* Lima */
183 {"M", +12 * 60}, /* Mike */
184 {"N", -1 * 60}, /* November */
185 {"O", -2 * 60}, /* Oscar */
186 {"P", -3 * 60}, /* Papa */
187 {"Q", -4 * 60}, /* Quebec */
188 {"R", -5 * 60}, /* Romeo */
189 {"S", -6 * 60}, /* Sierra */
190 {"T", -7 * 60}, /* Tango */
191 {"U", -8 * 60}, /* Uniform */
192 {"V", -9 * 60}, /* Victor */
193 {"W", -10 * 60}, /* Whiskey */
194 {"X", -11 * 60}, /* X-ray */
195 {"Y", -12 * 60}, /* Yankee */
196 {"Z", 0}, /* Zulu, zero meridian, a.k.a. UTC */
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
197 };
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
198
199 /* returns:
200 -1 no day
201 0 monday - 6 sunday
202 */
203
caf880b Constified from arguments.
Gisle Vanem authored
204 static int checkday(const char *check, size_t len)
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
205 {
206 int i;
80d3012 @dfandrich Make some more arrays of pointers const.
dfandrich authored
207 const char * const *what;
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
208 bool found= FALSE;
209 if(len > 3)
210 what = &weekday[0];
211 else
e7cefd6 @bagder Removed all uses of strftime() since it uses the localised version of th...
authored
212 what = &Curl_wkday[0];
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
213 for(i=0; i<7; i++) {
9d16b40 @bagder Renamed Curl_ascii_equal to Curl_raw_equal and bugfixed the my_toupper f...
authored
214 if(Curl_raw_equal(check, what[0])) {
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
215 found=TRUE;
216 break;
217 }
218 what++;
219 }
220 return found?i:-1;
221 }
222
caf880b Constified from arguments.
Gisle Vanem authored
223 static int checkmonth(const char *check)
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
224 {
225 int i;
80d3012 @dfandrich Make some more arrays of pointers const.
dfandrich authored
226 const char * const *what;
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
227 bool found= FALSE;
228
e7cefd6 @bagder Removed all uses of strftime() since it uses the localised version of th...
authored
229 what = &Curl_month[0];
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
230 for(i=0; i<12; i++) {
9d16b40 @bagder Renamed Curl_ascii_equal to Curl_raw_equal and bugfixed the my_toupper f...
authored
231 if(Curl_raw_equal(check, what[0])) {
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
232 found=TRUE;
233 break;
234 }
235 what++;
236 }
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
237 return found?i:-1; /* return the offset or -1, no real offset is -1 */
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
238 }
239
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
240 /* return the time zone offset between GMT and the input one, in number
241 of seconds or -1 if the timezone wasn't found/legal */
242
caf880b Constified from arguments.
Gisle Vanem authored
243 static int checktz(const char *check)
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
244 {
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
245 unsigned int i;
246 const struct tzinfo *what;
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
247 bool found= FALSE;
248
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
249 what = tz;
250 for(i=0; i< sizeof(tz)/sizeof(tz[0]); i++) {
9d16b40 @bagder Renamed Curl_ascii_equal to Curl_raw_equal and bugfixed the my_toupper f...
authored
251 if(Curl_raw_equal(check, what->name)) {
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
252 found=TRUE;
253 break;
254 }
255 what++;
256 }
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
257 return found?what->offset*60:-1;
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
258 }
259
260 static void skip(const char **date)
261 {
262 /* skip everything that aren't letters or digits */
44d84ac @bagder Avoid typecasting a signed char to an int when using is*() functions, as...
authored
263 while(**date && !ISALNUM(**date))
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
264 (*date)++;
265 }
266
267 enum assume {
268 DATE_MDAY,
269 DATE_YEAR,
270 DATE_TIME
271 };
272
ba9963b @bagder I replaced the use of 'struct tm' with a private clone of that struct si...
authored
273 /* this is a clone of 'struct tm' but with all fields we don't need or use
274 cut out */
275 struct my_tm {
276 int tm_sec;
277 int tm_min;
278 int tm_hour;
279 int tm_mday;
280 int tm_mon;
281 int tm_year;
282 };
283
6d24719 @dfandrich Made the month days table static const
dfandrich authored
284 /* struct tm to time since epoch in GMT time zone.
285 * This is similar to the standard mktime function but for GMT only, and
286 * doesn't suffer from the various bugs and portability problems that
287 * some systems' implementations have.
288 */
ba9963b @bagder I replaced the use of 'struct tm' with a private clone of that struct si...
authored
289 static time_t my_timegm(struct my_tm *tm)
d369a2b @bagder - Introducing Jamie Lokier's function for date to epoch conversion used ...
authored
290 {
6d24719 @dfandrich Made the month days table static const
dfandrich authored
291 static const int month_days_cumulative [12] =
d369a2b @bagder - Introducing Jamie Lokier's function for date to epoch conversion used ...
authored
292 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
293 int month, year, leap_days;
294
295 if(tm->tm_year < 70)
296 /* we don't support years before 1970 as they will cause this function
297 to return a negative value */
298 return -1;
299
300 year = tm->tm_year + 1900;
301 month = tm->tm_mon;
b903186 @bagder source cleanup: unify look, style and indent levels
authored
302 if(month < 0) {
d369a2b @bagder - Introducing Jamie Lokier's function for date to epoch conversion used ...
authored
303 year += (11 - month) / 12;
304 month = 11 - (11 - month) % 12;
305 }
b903186 @bagder source cleanup: unify look, style and indent levels
authored
306 else if(month >= 12) {
d369a2b @bagder - Introducing Jamie Lokier's function for date to epoch conversion used ...
authored
307 year -= month / 12;
308 month = month % 12;
309 }
310
311 leap_days = year - (tm->tm_mon <= 1);
312 leap_days = ((leap_days / 4) - (leap_days / 100) + (leap_days / 400)
313 - (1969 / 4) + (1969 / 100) - (1969 / 400));
314
315 return ((((time_t) (year - 1970) * 365
316 + leap_days + month_days_cumulative [month] + tm->tm_mday - 1) * 24
317 + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec;
318 }
319
777168c @bagder provide and export Curl_parsedate() as a library-wide internal function
authored
320 /*
a6f14e1 @bagder parsedate: turn private and static
authored
321 * parsedate()
777168c @bagder provide and export Curl_parsedate() as a library-wide internal function
authored
322 *
323 * Returns:
324 *
325 * PARSEDATE_OK - a fine conversion
326 * PARSEDATE_FAIL - failed to convert
327 * PARSEDATE_LATER - time overflow at the far end of time_t
328 * PARSEDATE_SOONER - time underflow at the low end of time_t
329 */
330
a6f14e1 @bagder parsedate: turn private and static
authored
331 static int parsedate(const char *date, time_t *output)
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
332 {
333 time_t t = 0;
334 int wdaynum=-1; /* day of the week number, 0-6 (mon-sun) */
335 int monnum=-1; /* month of the year number, 0-11 */
2de62cb @bagder less long => int implicit conversion warnings
authored
336 int mdaynum=-1; /* day of month, 1 - 31 */
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
337 int hournum=-1;
338 int minnum=-1;
339 int secnum=-1;
2de62cb @bagder less long => int implicit conversion warnings
authored
340 int yearnum=-1;
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
341 int tzoff=-1;
ba9963b @bagder I replaced the use of 'struct tm' with a private clone of that struct si...
authored
342 struct my_tm tm;
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
343 enum assume dignext = DATE_MDAY;
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
344 const char *indate = date; /* save the original pointer */
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
345 int part = 0; /* max 6 parts */
346
ade8e47 @bagder more docs and fixed the delta compared to GMT that prevented test case 1...
authored
347 while(*date && (part < 6)) {
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
348 bool found=FALSE;
349
350 skip(&date);
351
44d84ac @bagder Avoid typecasting a signed char to an int when using is*() functions, as...
authored
352 if(ISALPHA(*date)) {
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
353 /* a name coming up */
354 char buf[32]="";
355 size_t len;
d369a2b @bagder - Introducing Jamie Lokier's function for date to epoch conversion used ...
authored
356 sscanf(date, "%31[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]",
357 buf);
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
358 len = strlen(buf);
359
360 if(wdaynum == -1) {
361 wdaynum = checkday(buf, len);
362 if(wdaynum != -1)
363 found = TRUE;
364 }
365 if(!found && (monnum == -1)) {
366 monnum = checkmonth(buf);
367 if(monnum != -1)
368 found = TRUE;
369 }
370
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
371 if(!found && (tzoff == -1)) {
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
372 /* this just must be a time zone string */
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
373 tzoff = checktz(buf);
374 if(tzoff != -1)
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
375 found = TRUE;
376 }
377
378 if(!found)
777168c @bagder provide and export Curl_parsedate() as a library-wide internal function
authored
379 return PARSEDATE_FAIL; /* bad string */
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
380
381 date += len;
382 }
44d84ac @bagder Avoid typecasting a signed char to an int when using is*() functions, as...
authored
383 else if(ISDIGIT(*date)) {
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
384 /* a digit */
2de62cb @bagder less long => int implicit conversion warnings
authored
385 int val;
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
386 char *end;
387 if((secnum == -1) &&
388 (3 == sscanf(date, "%02d:%02d:%02d", &hournum, &minnum, &secnum))) {
389 /* time stamp! */
390 date += 8;
391 }
4d58f97 @bagder parsedate: allow time specified without seconds
authored
392 else if((secnum == -1) &&
393 (2 == sscanf(date, "%02d:%02d", &hournum, &minnum))) {
394 /* time stamp without seconds */
395 date += 5;
396 secnum = 0;
397 }
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
398 else {
e8a3243 @yangtse parsedate.c: fix a numeric overflow
yangtse authored
399 long lval;
400 int error;
401 int old_errno;
402
403 old_errno = ERRNO;
404 SET_ERRNO(0);
405 lval = strtol(date, &end, 10);
406 error = ERRNO;
407 if(error != old_errno)
408 SET_ERRNO(old_errno);
409
410 if(error)
411 return PARSEDATE_FAIL;
412
413 if((lval > (long)INT_MAX) || (lval < (long)INT_MIN))
414 return PARSEDATE_FAIL;
415
416 val = curlx_sltosi(lval);
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
417
2e7dcc1 @bagder support for YYYYMMDD added, which allows us to keep using the lib/ftp.c ...
authored
418 if((tzoff == -1) &&
419 ((end - date) == 4) &&
4233095 @bagder Jeff Johnson filed bug report #1863171
authored
420 (val <= 1400) &&
2e7dcc1 @bagder support for YYYYMMDD added, which allows us to keep using the lib/ftp.c ...
authored
421 (indate< date) &&
422 ((date[-1] == '+' || date[-1] == '-'))) {
4233095 @bagder Jeff Johnson filed bug report #1863171
authored
423 /* four digits and a value less than or equal to 1400 (to take into
1702a2c Fix a couple of spelling errors in lib/
Fabian Keil authored
424 account all sorts of funny time zone diffs) and it is preceded
4233095 @bagder Jeff Johnson filed bug report #1863171
authored
425 with a plus or minus. This is a time zone indication. 1400 is
426 picked since +1300 is frequently used and +1400 is mentioned as
427 an edge number in the document "ISO C 200X Proposal: Timezone
428 Functions" at http://david.tribble.com/text/c0xtimezone.html If
429 anyone has a more authoritative source for the exact maximum time
430 zone offsets, please speak up! */
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
431 found = TRUE;
432 tzoff = (val/100 * 60 + val%100)*60;
433
434 /* the + and - prefix indicates the local time compared to GMT,
435 this we need ther reversed math to get what we want */
436 tzoff = date[-1]=='+'?-tzoff:tzoff;
437 }
438
2e7dcc1 @bagder support for YYYYMMDD added, which allows us to keep using the lib/ftp.c ...
authored
439 if(((end - date) == 8) &&
440 (yearnum == -1) &&
441 (monnum == -1) &&
442 (mdaynum == -1)) {
443 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
444 found = TRUE;
445 yearnum = val/10000;
446 monnum = (val%10000)/100-1; /* month is 0 - 11 */
447 mdaynum = val%100;
448 }
449
450 if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) {
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
451 if((val > 0) && (val<32)) {
452 mdaynum = val;
453 found = TRUE;
454 }
455 dignext = DATE_YEAR;
456 }
457
458 if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
459 yearnum = val;
460 found = TRUE;
461 if(yearnum < 1900) {
cbd1a77 @bagder if () => if()
authored
462 if(yearnum > 70)
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
463 yearnum += 1900;
464 else
465 yearnum += 2000;
466 }
467 if(mdaynum == -1)
468 dignext = DATE_MDAY;
469 }
470
471 if(!found)
777168c @bagder provide and export Curl_parsedate() as a library-wide internal function
authored
472 return PARSEDATE_FAIL;
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
473
474 date = end;
475 }
476 }
477
478 part++;
479 }
480
17acdb5 @bagder slightly better but still lacks
authored
481 if(-1 == secnum)
482 secnum = minnum = hournum = 0; /* no time, make it zero */
483
484 if((-1 == mdaynum) ||
485 (-1 == monnum) ||
486 (-1 == yearnum))
487 /* lacks vital info, fail */
777168c @bagder provide and export Curl_parsedate() as a library-wide internal function
authored
488 return PARSEDATE_FAIL;
17acdb5 @bagder slightly better but still lacks
authored
489
cd73a73 @bagder dates from 2038 or later now return 0x7fffffff when 32 bit time_t is use...
authored
490 #if SIZEOF_TIME_T < 5
491 /* 32 bit time_t can only hold dates to the beginning of 2038 */
777168c @bagder provide and export Curl_parsedate() as a library-wide internal function
authored
492 if(yearnum > 2037) {
493 *output = 0x7fffffff;
494 return PARSEDATE_LATER;
495 }
cd73a73 @bagder dates from 2038 or later now return 0x7fffffff when 32 bit time_t is use...
authored
496 #endif
497
777168c @bagder provide and export Curl_parsedate() as a library-wide internal function
authored
498 if(yearnum < 1970) {
499 *output = 0;
500 return PARSEDATE_SOONER;
501 }
502
57d51be @bagder parsedate: detect more invalid dates better
authored
503 if((mdaynum > 31) || (monnum > 11) ||
504 (hournum > 23) || (minnum > 59) || (secnum > 60))
505 return PARSEDATE_FAIL; /* clearly an illegal date */
506
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
507 tm.tm_sec = secnum;
508 tm.tm_min = minnum;
509 tm.tm_hour = hournum;
510 tm.tm_mday = mdaynum;
511 tm.tm_mon = monnum;
512 tm.tm_year = yearnum - 1900;
513
d369a2b @bagder - Introducing Jamie Lokier's function for date to epoch conversion used ...
authored
514 /* my_timegm() returns a time_t. time_t is often 32 bits, even on many
8a96aec @bagder mktime() returns a time_t. time_t is often 32 bits, even on many archite...
authored
515 architectures that feature 64 bit 'long'.
516
517 Some systems have 64 bit time_t and deal with years beyond 2038. However,
d369a2b @bagder - Introducing Jamie Lokier's function for date to epoch conversion used ...
authored
518 even on some of the systems with 64 bit time_t mktime() returns -1 for
519 dates beyond 03:14:07 UTC, January 19, 2038. (Such as AIX 5100-06)
8a96aec @bagder mktime() returns a time_t. time_t is often 32 bits, even on many archite...
authored
520 */
d369a2b @bagder - Introducing Jamie Lokier's function for date to epoch conversion used ...
authored
521 t = my_timegm(&tm);
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
522
0040a60 fix questionable compare
Marty Kuhrt authored
523 /* time zone adjust (cast t to int to compare to negative one) */
b11dec5 putting back into dist
Marty Kuhrt authored
524 if(-1 != (int)t) {
ade8e47 @bagder more docs and fixed the delta compared to GMT that prevented test case 1...
authored
525
d369a2b @bagder - Introducing Jamie Lokier's function for date to epoch conversion used ...
authored
526 /* Add the time zone diff between local time zone and GMT. */
527 long delta = (long)(tzoff!=-1?tzoff:0);
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
528
7885264 @bagder Since many users probably already use local time strings as input, I now
authored
529 if((delta>0) && (t + delta < t))
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
530 return -1; /* time_t overflow */
531
532 t += delta;
533 }
534
777168c @bagder provide and export Curl_parsedate() as a library-wide internal function
authored
535 *output = t;
536
537 return PARSEDATE_OK;
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
538 }
539
540 time_t curl_getdate(const char *p, const time_t *now)
541 {
777168c @bagder provide and export Curl_parsedate() as a library-wide internal function
authored
542 time_t parsed;
a6f14e1 @bagder parsedate: turn private and static
authored
543 int rc = parsedate(p, &parsed);
777168c @bagder provide and export Curl_parsedate() as a library-wide internal function
authored
544 (void)now; /* legacy argument from the past that we ignore */
545
546 switch(rc) {
547 case PARSEDATE_OK:
548 case PARSEDATE_LATER:
549 case PARSEDATE_SOONER:
550 return parsed;
551 }
552 /* everything else is fail */
553 return -1;
f643321 @bagder getdate replacement code. smaller, slicker, faster.
authored
554 }
54d9f06 @bagder Curl_gmtime: added a portable gmtime
authored
555
0696260 @bagder Curl_gmtime: avoid future mistakes
authored
556 /*
557 * Curl_gmtime() is a gmtime() replacement for portability. Do not use the
558 * gmtime_r() or gmtime() functions anywhere else but here.
559 *
560 * To make sure no such function calls slip in, we define them to cause build
561 * errors, which is why we use the name within parentheses in this function.
562 *
563 */
564
54d9f06 @bagder Curl_gmtime: added a portable gmtime
authored
565 CURLcode Curl_gmtime(time_t intime, struct tm *store)
566 {
567 const struct tm *tm;
568 #ifdef HAVE_GMTIME_R
569 /* thread-safe version */
f19ace8 @bagder gmtime: remove define
authored
570 tm = (struct tm *)gmtime_r(&intime, store);
54d9f06 @bagder Curl_gmtime: added a portable gmtime
authored
571 #else
f19ace8 @bagder gmtime: remove define
authored
572 tm = gmtime(&intime);
54d9f06 @bagder Curl_gmtime: added a portable gmtime
authored
573 if(tm)
574 *store = *tm; /* copy the pointed struct to the local copy */
575 #endif
576
577 if(!tm)
578 return CURLE_BAD_FUNCTION_ARGUMENT;
579 return CURLE_OK;
580 }
Something went wrong with that request. Please try again.