Sort configuration variables.
[polipo.git] / parse_time.c
blobe8af0d8945cafe9771172dadce6324832b05f879
1 /*
2 Copyright (c) 2003-2006 by Juliusz Chroboczek
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
23 #define _USE_BSD
24 #include <time.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include "polipo.h"
29 const time_t time_t_max = ((time_t)~(1U << 31));
31 static inline int
32 d2i(char c)
34 if(c >= '0' && c <= '9')
35 return c - '0';
36 else
37 return -1;
40 static int
41 parse_int(const char *buf, int i, int len, int *val_return)
43 int val, d;
45 if(i >= len)
46 return -1;
48 val = d2i(buf[i]);
49 if(val < 0)
50 return -1;
51 else
52 i++;
54 while(i < len) {
55 d = d2i(buf[i]);
56 if(d < 0)
57 break;
58 val = val * 10 + d;
59 i++;
61 *val_return = val;
62 return i;
65 static const char month_names[12][3] = {
66 "jan", "feb", "mar", "apr", "may", "jun",
67 "jul", "aug", "sep", "oct", "nov", "dec",
70 static int
71 skip_word(const char *buf, int i, int len)
73 if(i >= len)
74 return -1;
76 if(!letter(buf[i]))
77 return -1;
79 while(i < len) {
80 if(!letter(buf[i]))
81 break;
82 i++;
85 return i;
88 static int
89 parse_month(const char *buf, int i, int len, int *val_return)
91 int j, k, l;
92 j = skip_word(buf, i, len);
93 if(j != i + 3)
94 return -1;
95 for(k = 0; k < 12; k++) {
96 for(l = 0; l < 3; l++) {
97 if(lwr(buf[i + l]) != month_names[k][l])
98 break;
100 if(l == 3)
101 break;
103 if(k >= 12)
104 return -1;
105 *val_return = k;
106 return j;
109 static int
110 issep(char c)
112 return c == ' ' || c == '\t' || c == ',' || c == ':' || c == '-';
116 skip_separator(const char *buf, int i, int len)
118 if(i >= len)
119 return -1;
121 if(issep(buf[i]))
122 i++;
123 else
124 return -1;
126 while(i < len) {
127 if(issep(buf[i]))
128 i++;
129 else
130 break;
132 return i;
136 parse_time(const char *buf, int offset, int len, time_t *time_return)
138 struct tm tm;
139 time_t t;
140 int i = offset;
142 i = skip_word(buf, i, len); if(i < 0) return -1;
143 i = skip_separator(buf, i, len); if(i < 0) return -1;
145 if(i >= len)
146 return -1;
148 if(d2i(buf[i]) >= 0) {
149 i = parse_int(buf, i, len, &tm.tm_mday); if(i < 0) return -1;
150 i = skip_separator(buf, i, len); if(i < 0) return -1;
151 i = parse_month(buf, i, len, &tm.tm_mon); if(i < 0) return -1;
152 i = skip_separator(buf, i, len); if(i < 0) return -1;
153 i = parse_int(buf, i, len, &tm.tm_year); if(i < 0) return -1;
154 if(tm.tm_year < 100)
155 tm.tm_year += 1900;
156 if(tm.tm_year < 1937)
157 tm.tm_year += 100;
158 if(tm.tm_year < 1937)
159 return -1;
161 i = skip_separator(buf, i, len); if(i < 0) return -1;
162 i = parse_int(buf, i, len, &tm.tm_hour); if(i < 0) return -1;
163 i = skip_separator(buf, i, len); if(i < 0) return -1;
164 i = parse_int(buf, i, len, &tm.tm_min); if(i < 0) return -1;
165 i = skip_separator(buf, i, len); if(i < 0) return -1;
166 i = parse_int(buf, i, len, &tm.tm_sec); if(i < 0) return -1;
167 i = skip_separator(buf, i, len); if(i < 0) return -1;
168 i = skip_word(buf, i, len); if(i < 0) return -1;
169 } else { /* funny American format */
170 i = parse_month(buf, i, len, &tm.tm_mon); if(i < 0) return -1;
171 i = skip_separator(buf, i, len); if(i < 0) return -1;
172 i = parse_int(buf, i, len, &tm.tm_mday); if(i < 0) return -1;
173 i = skip_separator(buf, i, len); if(i < 0) return -1;
174 i = parse_int(buf, i, len, &tm.tm_hour); if(i < 0) return -1;
175 i = skip_separator(buf, i, len); if(i < 0) return -1;
176 i = parse_int(buf, i, len, &tm.tm_min); if(i < 0) return -1;
177 i = skip_separator(buf, i, len); if(i < 0) return -1;
178 i = parse_int(buf, i, len, &tm.tm_sec); if(i < 0) return -1;
179 i = skip_separator(buf, i, len); if(i < 0) return -1;
180 i = parse_int(buf, i, len, &tm.tm_year); if(i < 0) return -1;
181 if(tm.tm_year < 100)
182 tm.tm_year += 1900;
183 if(tm.tm_year < 1937)
184 tm.tm_year += 100;
185 if(tm.tm_year < 1937 || tm.tm_year > 2040)
186 return -1;
189 if(tm.tm_year < 2038) {
190 tm.tm_year -= 1900;
191 tm.tm_isdst = -1;
192 t = mktime_gmt(&tm);
193 if(t == -1)
194 return -1;
195 } else {
196 t = time_t_max;
199 *time_return = t;
200 return i;
204 format_time(char *buf, int i, int len, time_t t)
206 struct tm *tm;
207 int rc;
209 if(i < 0 || i > len)
210 return -1;
212 tm = gmtime(&t);
213 if(tm == NULL)
214 return -1;
215 rc = strftime(buf + i, len - i, "%a, %d %b %Y %H:%M:%S GMT", tm);
216 if(rc <= 0) /* yes, that's <= */
217 return -1;
218 return i + rc;