Debian Codegraph @

terps/tads/tads2/osstzprs.c

raw symbols SCIP
1 /* Copyright (c) 2012 by Michael J. Roberts. All Rights Reserved. */
2 /*
3 Name
4 osstzprs.c - portable POSIX-style "TZ" variable parser
5 Function
6 This routine parses a POSIX-style "TZ" variable, filling in an
7 os_tzinfo_t structure for os_get_timezone_info().
8 Notes
9
10 Modified
11 02/13/12 MJRoberts - Creation
12 */
13
14 #include <stdlib.h>
15 #include <ctype.h>
16 #include <string.h>
17 #include "os.h"
18 #include "osstzprs.h"
19 #include "lib.h"
20
21
22 /* ------------------------------------------------------------------------ */
23 /*
24 * Internal service routines
25 */
26
27 /*
28 * parse a time zone name; returns the end of the name; optionally copies
29 * the string to an output buffer
30 */
31 static const char *parse_zone_name(char *dst, size_t dstlen,
32 const char *p, size_t *len)
33 {
34 /* if there's a pointer, scan the string */
35 if (p != 0)
36 {
37 /* skip alphabetics */
38 for ( ; *len != 0 && isalpha(*p) ; ++p, --*len)
39 {
40 /*
41 * copy to the output if there's room for this character plus a
42 * null terminator
43 */
44 if (dstlen > 1)
45 *dst++ = *p;
46 }
47 }
48
49 /* null-terminate the output if there's room */
50 if (dstlen > 0)
51 *dst = '\0';
52
53 /* return the final pointer */
54 return p;
55 }
56
57 /* parse digits */
58 static const char *parse_digits(int *val, const char *p, size_t *len)
59 {
60 for (*val = 0 ; *len != 0 && isdigit(*p) ; ++p, --*len)
61 {
62 *val *= 10;
63 *val += *p - '0';
64 }
65 return p;
66 }
67
68 /*
69 * Parse an offset value - [+-]hh[:mm[:ss]]; returns a pointer to the next
70 * character after the parsed offset value, and fills in '*secs' with the
71 * number of seconds represented by the offset value.
72 */
73 static const char *parse_offset(int *secs, const char *p, size_t *len)
74 {
75 int sign = 1, hh = 0, mm = 0, ss = 0;
76
77 /* check for a sign (which must be followed by a digit) */
78 if (*len >= 2 && *p == '+' && isdigit(p[1]))
79 ++p, --*len;
80 else if (*len >= 2 && *p == '-' && isdigit(p[1]))
81 sign = -1, ++p, --*len;
82
83 /* if we don't have a digit, it's not a number */
84 if (*len == 0 || !isdigit(*p))
85 return p;
86
87 /* parse the hours */
88 p = parse_digits(&hh, p, len);
89
90 /* check for minutes */
91 if (*len >= 3 && *p == ':' && isdigit(p[1]) && isdigit(p[2]))
92 {
93 /* parse the minutes */
94 ++p, --*len;
95 p = parse_digits(&mm, p, len);
96
97 /* check for seconds */
98 if (*len >= 3 && *p == ':' && isdigit(p[1]) && isdigit(p[2]))
99 {
100 ++p, --*len;
101 p = parse_digits(&ss, p, len);
102 }
103 }
104
105 /* compute the full time in seconds, and apply the sign */
106 *secs = sign*(hh*60*60 + mm*60 + ss);
107
108 /* return the updated pointer */
109 return p;
110 }
111
112 /*
113 * Parse a DST start/end rule
114 */
115 static const char *parse_dst_rule(struct os_tzrule_t *rule,
116 const char *p, size_t *len)
117 {
118 /* remember the starting point - we'll return this on failure */
119 const char *start = p;
120
121 /* check the date portion */
122 if (*len >= 2 && (*p == 'J' || *p == 'j') && isdigit(p[1]))
123 {
124 /* Julian day, not counting February 29 */
125 ++p, --*len;
126 p = parse_digits(&rule->jday, p, len);
127 }
128 else if (*len >= 1 && isdigit(*p))
129 {
130 /* day of year, counting February 29 */
131 p = parse_digits(&rule->yday, p, len);
132
133 /* TZ uses range 0-365 for this format, but yday uses 1-366 */
134 rule->yday += 1;
135 }
136 else if (*len >= 2 && *p == 'M' && isdigit(p[1]))
137 {
138 /* month.week.day format - start with the month */
139 ++p, --*len;
140 p = parse_digits(&rule->month, p, len);
141 if (rule->month < 1 || rule->month > 12)
142 return start;
143
144 /* check for the required '.' and a following digit */
145 if (*len < 2 || *p != '.' || !isdigit(p[1]))
146 return start;
147
148 /* parse the week */
149 ++p, --*len;
150 p = parse_digits(&rule->week, p, len);
151 if (rule->week < 1 || rule->week > 5)
152 return start;
153
154 /* check for the required '.' and a following digit */
155 if (*len < 2 || *p != '.' || !isdigit(p[1]))
156 return start;
157
158 /* parse the day of the week; adjust from 0-6 to our 1-7 range */
159 ++p, --*len;
160 p = parse_digits(&rule->day, p, len);
161 rule->day += 1;
162 if (rule->day < 1 || rule->day > 7)
163 return start;
164 }
165 else
166 {
167 /* not a valid DST start/end format */
168 return p;
169 }
170
171 /* we got the date; check for a time portion */
172 if (*len >= 2 && *p == '/' && isdigit(p[1]))
173 {
174 /* parse the time; this has the same parsing rules as an offset */
175 ++p, --*len;
176 p = parse_offset(&rule->time, p, len);
177 }
178 else
179 {
180 /* the default is 2:00 AM */
181 rule->time = 2*60*60;
182 }
183
184 /* success - return the updated pointer */
185 return p;
186 }
187
188
189 /* ------------------------------------------------------------------------ */
190 /*
191 * Parse a "TZ" environment variable string
192 */
193 int oss_parse_posix_tz(struct os_tzinfo_t *info, const char *tz, size_t len,
194 int west_is_positive)
195 {
196 const char *p;
197 const char *std_name = 0, *dst_name = 0;
198 size_t std_name_len = 0, dst_name_len = 0;
199 int std_ofs = 0, dst_ofs = 0;
200 struct os_tzrule_t dst_start, dst_end;
201
202 /* clear the daylight savings start/end dates */
203 memset(&dst_start, 0, sizeof(dst_start));
204 memset(&dst_end, 0, sizeof(dst_end));
205
206 /* if there's no TZ string, return failure */
207 if (tz == 0)
208 return FALSE;
209
210 /* skip leading spaces, just in case; if that leaves nothing, fail */
211 for ( ; len != 0 && isspace(*tz) ; ++tz, --len) ;
212 if (len == 0)
213 return FALSE;
214
215 /* parse the standard time name; if that's empty, fail */
216 std_name = tz;
217 tz = parse_zone_name(0, 0, tz, &len);
218 if ((std_name_len = tz - std_name) == 0)
219 return FALSE;
220
221 /* parse the offset value */
222 p = tz;
223 if ((tz = parse_offset(&std_ofs, tz, &len)) == p)
224 return FALSE;
225
226 /* if we're using west-positive notation, convert to os_tzinfo_t specs */
227 if (west_is_positive)
228 std_ofs = -std_ofs;
229
230 /* if we're not done, check for a daylight zone */
231 p = tz;
232 if (len != 0 && (tz = parse_zone_name(0, 0, tz, &len)) != p)
233 {
234 /* found a daylight zone name - note it */
235 dst_name = p;
236 dst_name_len = tz - p;
237
238 /*
239 * check for an optional offset value; if it's not present,
240 * daylight time is implicitly one hour ahead of standard time
241 */
242 p = tz;
243 if ((tz = parse_offset(&dst_ofs, tz, &len)) == p)
244 dst_ofs = std_ofs + 60*60;
245 else if (west_is_positive)
246 dst_ofs = -dst_ofs;
247 }
248 else
249 {
250 /* there's no daylight zone, so just repeat the standard offset */
251 dst_ofs = std_ofs;
252 }
253
254 /* check for the daylight time start/end dates */
255 if (len != 0 && *tz == ',')
256 {
257 /* parse the start date */
258 p = ++tz;
259 --len;
260 if ((tz = parse_dst_rule(&dst_start, tz, &len)) == p)
261 return FALSE;
262
263 /* if there's a start date, there has to be an end date */
264 if (len == 0 || *tz != ',')
265 return FALSE;
266
267 /* parse the end date */
268 p = ++tz;
269 --len;
270 if ((tz = parse_dst_rule(&dst_end, tz, &len)) == p)
271 return FALSE;
272 }
273
274 /* we need to be done now, other than trailing spaces */
275 for ( ; len != 0 && isspace(*tz) ; ++tz) ;
276 if (len != 0)
277 return FALSE;
278
279 /* clear the result structure */
280 memset(info, 0, sizeof(*info));
281
282 /* set the offsets */
283 info->std_ofs = std_ofs;
284 info->dst_ofs = dst_ofs;
285
286 /* copy the abbreviation strings */
287 parse_zone_name(info->std_abbr, sizeof(info->std_abbr),
288 std_name, &std_name_len);
289 parse_zone_name(info->dst_abbr, sizeof(info->dst_abbr),
290 dst_name, &dst_name_len);
291
292 /* copy the daylight start/end rules */
293 memcpy(&info->dst_start, &dst_start, sizeof(info->dst_start));
294 memcpy(&info->dst_end, &dst_end, sizeof(info->dst_end));
295
296 /* success */
297 return TRUE;
298 }
299
300 #ifdef TEST
301
302 static void show_dst(const struct os_tzrule_t *rule)
303 {
304 if (rule->jday != 0)
305 printf(", jday %d at %d", rule->jday, rule->time);
306 else if (rule->yday != 0)
307 printf(", yday %d at %d", rule->yday, rule->time);
308 else if (rule->month != 0)
309 printf(", M%d W%d D%d at %d",
310 rule->month, rule->week, rule->day, rule->time);
311 }
312
313 int main(int argc, char **argv)
314 {
315 static const char *t[] = {
316 "EST5",
317 "EST5,",
318 "EST5,M4.1.0",
319 "EST5,M4.1.0,M10.5.0",
320 "EST5EDT",
321 "EST+5EDT",
322 "EST5EDT4",
323 "PST8",
324 "PST8PDT",
325 "PST8PDT7",
326 "PST+8PDT+7",
327 "PST8PDT6:30",
328 "HST10",
329 "CET-1",
330 "CET-1CEST",
331 "CET-1CEST-2",
332 "GMTO0:1GST-0:59",
333 "GMTO0:0:1GST-1:59:59",
334 "PST8PDT,M3.5.0,M10.1.0",
335 "PST8PDT,M4.1.0/2:30,M10.5.0/2:30",
336 "PST8PDT,J91/3,275/0",
337 "PST8PDT,J91/3,275/0x",
338 0
339 };
340 int i;
341 struct os_tzinfo_t info;
342
343 for (i = 0 ; t[i] != 0 ; ++i)
344 {
345 printf("%s -> ", t[i]);
346 if (oss_parse_posix_tz(&info, t[i], strlen(t[i]), TRUE))
347 {
348 printf("'%s' %d, '%s' %d",
349 info.std_abbr, info.std_ofs, info.dst_abbr, info.dst_ofs);
350 show_dst(&info.dst_start);
351 show_dst(&info.dst_end);
352 printf("\n");
353 }
354 else
355 {
356 printf("invalid\n");
357 }
358 }
359 }
360
361 #endif /* TEST */