]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/rfc1123.c
3 * $Id: rfc1123.c,v 1.21 1998/11/12 06:30:15 wessels Exp $
6 * AUTHOR: Harvest Derived
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
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 the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * Duane Wessels and the University of California San Diego. Please
16 * see the COPYRIGHT file for full details. Squid incorporates
17 * software developed and/or copyrighted by other sources. Please see
18 * the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
40 * Adapted from HTSUtils.c in CERN httpd 3.0 (http://info.cern.ch/httpd/)
41 * by Darren Hardy <hardy@cs.colorado.edu>, November 1994.
56 #include <sys/types.h>
68 #define RFC850_STRFTIME "%A, %d-%b-%y %H:%M:%S GMT"
69 #define RFC1123_STRFTIME "%a, %d %b %Y %H:%M:%S GMT"
71 static int make_month(const char *s
);
72 static int make_num(const char *s
);
74 static char *month_names
[12] =
76 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
77 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
82 make_num(const char *s
)
84 if (*s
>= '0' && *s
<= '9')
85 return 10 * (*s
- '0') + *(s
+ 1) - '0';
87 return *(s
+ 1) - '0';
91 make_month(const char *s
)
96 month
[0] = toupper(*s
);
97 month
[1] = tolower(*(s
+ 1));
98 month
[2] = tolower(*(s
+ 2));
100 for (i
= 0; i
< 12; i
++)
101 if (!strncmp(month_names
[i
], month
, 3))
108 parse_rfc1123(const char *str
)
117 memset(&tm
, '\0', sizeof(struct tm
));
118 if ((s
= strchr(str
, ','))) { /* Thursday, 10-Jun-93 01:29:59 GMT */
119 s
++; /* or: Thu, 10 Jan 1993 01:29:59 GMT */
122 if (isdigit(*s
) && !isdigit(*(s
+1))) /* backoff if only one digit */
124 if (strchr(s
, '-')) { /* First format */
125 if ((int) strlen(s
) < 18)
127 tm
.tm_mday
= make_num(s
);
128 tm
.tm_mon
= make_month(s
+ 3);
129 tm
.tm_year
= make_num(s
+ 7);
131 * Y2K: Arjan de Vet <Arjan.deVet@adv.IAEhv.nl>
132 * if tm.tm_year < 70, assume it's after the year 2000.
136 tm
.tm_hour
= make_num(s
+ 10);
137 tm
.tm_min
= make_num(s
+ 13);
138 tm
.tm_sec
= make_num(s
+ 16);
139 } else { /* Second format */
140 if ((int) strlen(s
) < 20)
142 tm
.tm_mday
= make_num(s
);
143 tm
.tm_mon
= make_month(s
+ 3);
144 tm
.tm_year
= (100 * make_num(s
+ 7) - 1900) + make_num(s
+ 9);
145 tm
.tm_hour
= make_num(s
+ 12);
146 tm
.tm_min
= make_num(s
+ 15);
147 tm
.tm_sec
= make_num(s
+ 18);
150 } else { /* Try the other format: */
151 s
= str
; /* Wed Jun 9 01:29:59 1993 GMT */
152 while (*s
&& *s
== ' ')
154 if ((int) strlen(s
) < 24)
156 tm
.tm_mday
= make_num(s
+ 8);
157 tm
.tm_mon
= make_month(s
+ 4);
158 /* Y2K fix, richard.kettlewell@kewill.com */
159 tm
.tm_year
= atoi(s
+ 20) - 1900;
160 tm
.tm_hour
= make_num(s
+ 11);
161 tm
.tm_min
= make_num(s
+ 14);
162 tm
.tm_sec
= make_num(s
+ 17);
164 if (tm
.tm_sec
< 0 || tm
.tm_sec
> 59 ||
165 tm
.tm_min
< 0 || tm
.tm_min
> 59 ||
166 tm
.tm_hour
< 0 || tm
.tm_hour
> 23 ||
167 tm
.tm_mday
< 1 || tm
.tm_mday
> 31 ||
168 tm
.tm_mon
< 0 || tm
.tm_mon
> 11 ||
169 tm
.tm_year
< 70 || tm
.tm_year
> 120) {
179 struct tm
*local
= localtime(&t
);
180 t
+= local
->tm_gmtoff
;
183 /* some systems do not have tm_gmtoff so we fake it */
187 #if defined (_TIMEZONE)
188 #elif defined (_timezone)
189 #elif defined(_SQUID_AIX_)
191 extern time_t timezone
;
194 * The following assumes a fixed DST offset of 1 hour,
195 * which is probably wrong.
200 t
-= (_timezone
+ dst
);
202 t
-= (timezone
+ dst
);
212 static char buf
[128];
214 struct tm
*gmt
= gmtime(&t
);
217 strftime(buf
, 127, RFC1123_STRFTIME
, gmt
);
222 mkhttpdlogtime(const time_t * t
)
224 static char buf
[128];
226 struct tm
*gmt
= gmtime(t
);
229 int gmt_min
, gmt_hour
, gmt_yday
, day_offset
;
234 /* localtime & gmtime may use the same static data */
235 gmt_min
= gmt
->tm_min
;
236 gmt_hour
= gmt
->tm_hour
;
237 gmt_yday
= gmt
->tm_yday
;
240 day_offset
= lt
->tm_yday
- gmt_yday
;
241 min_offset
= day_offset
* 1440 + (lt
->tm_hour
- gmt_hour
) * 60
242 + (lt
->tm_min
- gmt_min
);
244 /* wrap round on end of year */
247 else if (day_offset
< -1)
250 len
= strftime(buf
, 127 - 5, "%d/%b/%Y:%H:%M:%S ", lt
);
251 snprintf(buf
+ len
, 128 - len
, "%+03d%02d",
252 (min_offset
/ 60) % 24,
256 strftime(buf
, 127, "%d/%b/%Y:%H:%M:%S -000", gmt
);
271 printf("HTTP Time: %s\n", x
);
273 pt
= parse_rfc1123(x
);
274 printf("Parsed: %d vs. %d\n", pt
, t
);