]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/rfc1123.c
windows NT defines _timezone apparently
[thirdparty/squid.git] / lib / rfc1123.c
1
2 /*
3 * $Id: rfc1123.c,v 1.17 1998/04/24 05:20:23 wessels Exp $
4 *
5 * DEBUG:
6 * AUTHOR: Harvest Derived
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * --------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by
14 * the National Science Foundation.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 */
31
32 /*
33 * Copyright (c) 1994, 1995. All rights reserved.
34 *
35 * The Harvest software was developed by the Internet Research Task
36 * Force Research Group on Resource Discovery (IRTF-RD):
37 *
38 * Mic Bowman of Transarc Corporation.
39 * Peter Danzig of the University of Southern California.
40 * Darren R. Hardy of the University of Colorado at Boulder.
41 * Udi Manber of the University of Arizona.
42 * Michael F. Schwartz of the University of Colorado at Boulder.
43 * Duane Wessels of the University of Colorado at Boulder.
44 *
45 * This copyright notice applies to software in the Harvest
46 * ``src/'' directory only. Users should consult the individual
47 * copyright notices in the ``components/'' subdirectories for
48 * copyright information about other software bundled with the
49 * Harvest source code distribution.
50 *
51 * TERMS OF USE
52 *
53 * The Harvest software may be used and re-distributed without
54 * charge, provided that the software origin and research team are
55 * cited in any use of the system. Most commonly this is
56 * accomplished by including a link to the Harvest Home Page
57 * (http://harvest.cs.colorado.edu/) from the query page of any
58 * Broker you deploy, as well as in the query result pages. These
59 * links are generated automatically by the standard Broker
60 * software distribution.
61 *
62 * The Harvest software is provided ``as is'', without express or
63 * implied warranty, and with no support nor obligation to assist
64 * in its use, correction, modification or enhancement. We assume
65 * no liability with respect to the infringement of copyrights,
66 * trade secrets, or any patents, and are not responsible for
67 * consequential damages. Proper use of the Harvest software is
68 * entirely the responsibility of the user.
69 *
70 * DERIVATIVE WORKS
71 *
72 * Users may make derivative works from the Harvest software, subject
73 * to the following constraints:
74 *
75 * - You must include the above copyright notice and these
76 * accompanying paragraphs in all forms of derivative works,
77 * and any documentation and other materials related to such
78 * distribution and use acknowledge that the software was
79 * developed at the above institutions.
80 *
81 * - You must notify IRTF-RD regarding your distribution of
82 * the derivative work.
83 *
84 * - You must clearly notify users that your are distributing
85 * a modified version and not the original Harvest software.
86 *
87 * - Any derivative product is also subject to these copyright
88 * and use restrictions.
89 *
90 * Note that the Harvest software is NOT in the public domain. We
91 * retain copyright, as specified above.
92 *
93 * HISTORY OF FREE SOFTWARE STATUS
94 *
95 * Originally we required sites to license the software in cases
96 * where they were going to build commercial products/services
97 * around Harvest. In June 1995 we changed this policy. We now
98 * allow people to use the core Harvest software (the code found in
99 * the Harvest ``src/'' directory) for free. We made this change
100 * in the interest of encouraging the widest possible deployment of
101 * the technology. The Harvest software is really a reference
102 * implementation of a set of protocols and formats, some of which
103 * we intend to standardize. We encourage commercial
104 * re-implementations of code complying to this set of standards.
105 */
106
107 #include "config.h"
108
109
110 /*
111 * Adapted from HTSUtils.c in CERN httpd 3.0 (http://info.cern.ch/httpd/)
112 * by Darren Hardy <hardy@cs.colorado.edu>, November 1994.
113 */
114 #if HAVE_STDIO_H
115 #include <stdio.h>
116 #endif
117 #if HAVE_STRING_H
118 #include <string.h>
119 #endif
120 #if HAVE_CTYPE_H
121 #include <ctype.h>
122 #endif
123 #if HAVE_SYS_TYPES_H
124 #include <sys/types.h>
125 #endif
126 #if HAVE_TIME_H
127 #include <time.h>
128 #endif
129 #if HAVE_SYS_TIME_H
130 #include <sys/time.h>
131 #endif
132
133 #include "util.h"
134 #include "snprintf.h"
135
136 #define RFC850_STRFTIME "%A, %d-%b-%y %H:%M:%S GMT"
137 #define RFC1123_STRFTIME "%a, %d %b %Y %H:%M:%S GMT"
138
139 static int make_month(const char *s);
140 static int make_num(const char *s);
141
142 static char *month_names[12] =
143 {
144 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
145 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
146 };
147
148
149 static int
150 make_num(const char *s)
151 {
152 if (*s >= '0' && *s <= '9')
153 return 10 * (*s - '0') + *(s + 1) - '0';
154 else
155 return *(s + 1) - '0';
156 }
157
158 static int
159 make_month(const char *s)
160 {
161 int i;
162 char month[3];
163
164 month[0] = toupper(*s);
165 month[1] = tolower(*(s + 1));
166 month[2] = tolower(*(s + 2));
167
168 for (i = 0; i < 12; i++)
169 if (!strncmp(month_names[i], month, 3))
170 return i;
171 return 0;
172 }
173
174
175 time_t
176 parse_rfc1123(const char *str)
177 {
178 const char *s;
179 struct tm tm;
180 time_t t;
181
182 if (!str)
183 return -1;
184
185 memset(&tm, '\0', sizeof(struct tm));
186 if ((s = strchr(str, ','))) { /* Thursday, 10-Jun-93 01:29:59 GMT */
187 s++; /* or: Thu, 10 Jan 1993 01:29:59 GMT */
188 while (*s == ' ')
189 s++;
190 if (isdigit(*s) && !isdigit(*(s+1))) /* backoff if only one digit */
191 s--;
192 if (strchr(s, '-')) { /* First format */
193 if ((int) strlen(s) < 18)
194 return -1;
195 tm.tm_mday = make_num(s);
196 tm.tm_mon = make_month(s + 3);
197 tm.tm_year = make_num(s + 7);
198 /*
199 * Y2K: Arjan de Vet <Arjan.deVet@adv.IAEhv.nl>
200 * if tm.tm_year < 70, assume it's after the year 2000.
201 */
202 if (tm.tm_year < 70)
203 tm.tm_year += 100;
204 tm.tm_hour = make_num(s + 10);
205 tm.tm_min = make_num(s + 13);
206 tm.tm_sec = make_num(s + 16);
207 } else { /* Second format */
208 if ((int) strlen(s) < 20)
209 return -1;
210 tm.tm_mday = make_num(s);
211 tm.tm_mon = make_month(s + 3);
212 tm.tm_year = (100 * make_num(s + 7) - 1900) + make_num(s + 9);
213 tm.tm_hour = make_num(s + 12);
214 tm.tm_min = make_num(s + 15);
215 tm.tm_sec = make_num(s + 18);
216
217 }
218 } else { /* Try the other format: */
219 s = str; /* Wed Jun 9 01:29:59 1993 GMT */
220 while (*s && *s == ' ')
221 s++;
222 if ((int) strlen(s) < 24)
223 return -1;
224 tm.tm_mday = make_num(s + 8);
225 tm.tm_mon = make_month(s + 4);
226 tm.tm_year = make_num(s + 22);
227 tm.tm_hour = make_num(s + 11);
228 tm.tm_min = make_num(s + 14);
229 tm.tm_sec = make_num(s + 17);
230 }
231 if (tm.tm_sec < 0 || tm.tm_sec > 59 ||
232 tm.tm_min < 0 || tm.tm_min > 59 ||
233 tm.tm_hour < 0 || tm.tm_hour > 23 ||
234 tm.tm_mday < 1 || tm.tm_mday > 31 ||
235 tm.tm_mon < 0 || tm.tm_mon > 11 ||
236 tm.tm_year < 70 || tm.tm_year > 120) {
237 return -1;
238 }
239 tm.tm_isdst = -1;
240
241 #ifdef HAVE_TIMEGM
242 t = timegm(&tm);
243 #elif HAVE_TM_GMTOFF
244 t = mktime(&tm);
245 {
246 struct tm *local = localtime(&t);
247 t += local->tm_gmtoff;
248 }
249 #else
250 /* some systems do not have tm_gmtoff so we fake it */
251 t = mktime(&tm);
252 {
253 time_t dst = 0;
254 #if !defined _TIMEZONE && !defined _timezone
255 extern time_t timezone;
256 #endif
257 /*
258 * The following assumes a fixed DST offset of 1 hour,
259 * which is probably wrong.
260 */
261 if (tm.tm_isdst > 0)
262 dst = -3600;
263 #ifdef _timezone
264 t -= (_timezone + dst);
265 #else
266 t -= (timezone + dst);
267 #endif
268 }
269 #endif
270 return t;
271 }
272
273 const char *
274 mkrfc1123(time_t t)
275 {
276 static char buf[128];
277
278 struct tm *gmt = gmtime(&t);
279
280 buf[0] = '\0';
281 strftime(buf, 127, RFC1123_STRFTIME, gmt);
282 return buf;
283 }
284
285 const char *
286 mkhttpdlogtime(const time_t * t)
287 {
288 static char buf[128];
289
290 struct tm *gmt = gmtime(t);
291
292 #ifndef USE_GMT
293 int gmt_min, gmt_hour, gmt_yday, day_offset;
294 size_t len;
295 struct tm *lt;
296 int min_offset;
297
298 /* localtime & gmtime may use the same static data */
299 gmt_min = gmt->tm_min;
300 gmt_hour = gmt->tm_hour;
301 gmt_yday = gmt->tm_yday;
302
303 lt = localtime(t);
304 day_offset = lt->tm_yday - gmt_yday;
305 min_offset = day_offset * 1440 + (lt->tm_hour - gmt_hour) * 60
306 + (lt->tm_min - gmt_min);
307
308 /* wrap round on end of year */
309 if (day_offset > 1)
310 day_offset = -1;
311 else if (day_offset < -1)
312 day_offset = 1;
313
314 len = strftime(buf, 127 - 5, "%d/%b/%Y:%H:%M:%S ", lt);
315 snprintf(buf + len, 128 - len, "%+03d%02d",
316 (min_offset / 60) % 24,
317 min_offset % 60);
318 #else /* USE_GMT */
319 buf[0] = '\0';
320 strftime(buf, 127, "%d/%b/%Y:%H:%M:%S -000", gmt);
321 #endif /* USE_GMT */
322
323 return buf;
324 }
325
326 #if 0
327 int
328 main()
329 {
330 char *x;
331 time_t t, pt;
332
333 t = time(NULL);
334 x = mkrfc1123(t);
335 printf("HTTP Time: %s\n", x);
336
337 pt = parse_rfc1123(x);
338 printf("Parsed: %d vs. %d\n", pt, t);
339 }
340
341 #endif