Debian Codegraph @

time/date/posix.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use ascii;
5 use strings;
6 use time;
7
8 // Invalid POSIX extending TZ string.
9 export type invalidpetzstr = !void;
10
11 // A POSIX "extending timezone" of the "TZ" environment variable format.
12 // Used for extending calculations beyond the last known transition.
13 //
14 // Example PETZ in string form: "CET-1CEST,M3.5.0,M10.5.0/3".
15 export type petz = struct {
16 std_abbr: str,
17 std_offset: time::duration,
18 dst_abbr: str, // empty string means no DST
19 dst_offset: time::duration,
20 dst_startdate: petz_ruledate,
21 dst_starttime: time::duration,
22 dst_enddate: petz_ruledate,
23 dst_endtime: time::duration,
24 };
25
26 export type petz_ruledate = (
27 petz_ruledate_julian
28 | petz_ruledate_ordinal
29 | petz_ruledate_weekdate
30 );
31
32 export type petz_ruledate_julian = i16; // "Jn"
33
34 export type petz_ruledate_ordinal = i16; // "n"
35
36 export type petz_ruledate_weekdate = (u8, u8, u8); // "Mm.w.d"
37
38 def PETZ_EMPTY = petz {
39 std_abbr = "",
40 std_offset = 0 * time::HOUR,
41 dst_abbr = "",
42 dst_offset = 0 * time::HOUR,
43 dst_startdate = (0, 0, 0),
44 dst_starttime = 2 * time::HOUR,
45 dst_enddate = (0, 0, 0),
46 dst_endtime = 2 * time::HOUR,
47 };
48
49 fn petz_finish(p: *petz) void = {
50 free(p.std_abbr);
51 free(p.dst_abbr);
52 };
53
54 fn parse_petz(p: *petz, petzstring: str) (void | invalidpetzstr) = {
55 match (parse_petz_string(p, petzstring)) {
56 case parsed_err =>
57 return invalidpetzstr;
58 case parsed_done =>
59 return void;
60 };
61 };
62
63 fn parse_petz_string(p: *petz, petzstring: str) (parsed | nomem) = {
64 p.dst_starttime = 2 * time::HOUR;
65 p.dst_endtime = 2 * time::HOUR;
66
67 let t = strings::iter(petzstring);
68
69 p.std_abbr = strings::dup(scan_petz_abbr(&t)?)?;
70 p.std_offset = -scan_petz_time(&t, 25, parsed_err)?;
71
72 scan_rune(&t, void, parsed_done)?; strings::prev(&t);
73
74 p.dst_abbr = strings::dup(scan_petz_abbr(&t)?)?;
75 p.dst_offset = -scan_petz_time(&t, 25, -(p.std_offset + 1 * time::HOUR))?;
76
77 // enforce PETZ strings with DST to specify a rule
78 scan_rune(&t, ',', parsed_err)?;
79
80 p.dst_startdate = scan_petz_ruledate(&t, parsed_err)?;
81 if (scan_rune(&t, void, parsed_err)? == '/') {
82 p.dst_starttime = scan_petz_time(&t, 168, 2 * time::HOUR)?;
83 } else {
84 strings::prev(&t);
85 };
86
87 scan_rune(&t, ',', parsed_err)?;
88
89 p.dst_enddate = scan_petz_ruledate(&t, parsed_err)?;
90 scan_rune(&t, '/', parsed_done)?;
91 p.dst_endtime = scan_petz_time(&t, 168, 2 * time::HOUR)?;
92
93 return parsed_done;
94 };
95
96 fn scan_petz_time(
97 t: *strings::iterator,
98 hour_range: int,
99 default: (time::duration | parsed),
100 ) (time::duration | parsed) = {
101 let sign: int = 1;
102 let hour: int = 2;
103 let mins: int = 0;
104 let secs: int = 0;
105
106 switch (scan_rune(t, void, parsed_done)?) {
107 case =>
108 strings::prev(t);
109 return default;
110 case '+' =>
111 sign = 1;
112 case '-' =>
113 sign = -1;
114 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' =>
115 strings::prev(t);
116 };
117
118 hour = scan_num(t, 1, 3, parsed_err)?: int;
119 if (hour >= hour_range) {
120 return parsed_err;
121 };
122
123 match (strings::next(t)) {
124 case done =>
125 return calc_petz_daytime(sign, hour, mins, secs);
126 case let r: rune =>
127 if (r != ':') {
128 strings::prev(t);
129 return calc_petz_daytime(sign, hour, mins, secs);
130 };
131 };
132
133 mins = scan_num(t, 1, 2, 0)?: int;
134 if (mins >= 60) {
135 return parsed_err;
136 };
137
138 match (strings::next(t)) {
139 case done =>
140 return calc_petz_daytime(sign, hour, mins, secs);
141 case let r: rune =>
142 if (r != ':') {
143 strings::prev(t);
144 return calc_petz_daytime(sign, hour, mins, secs);
145 };
146 };
147
148 secs = scan_num(t, 1, 2, 0)?: int;
149 if (mins >= 60) {
150 return parsed_err;
151 };
152
153 return calc_petz_daytime(sign, hour, mins, secs);
154 };
155
156 fn calc_petz_daytime(sign: int, hour: int, mins: int, secs: int) time::duration = {
157 return sign * (
158 hour * time::HOUR
159 + mins * time::MINUTE
160 + secs * time::SECOND
161 );
162 };
163
164 fn scan_petz_abbr(t: *strings::iterator) (str | parsed) = {
165 let start = *t;
166 let quoted = scan_rune(t, void, parsed_done)? == '<';
167
168 if (!quoted) {
169 strings::prev(t);
170 };
171
172 for (let r => strings::next(t)) {
173 if (quoted) {
174 if (r == '<') return parsed_err;
175 if (r == '>') break;
176 if (!(ascii::isalnum(r) || r == '+' || r == '-')) {
177 strings::prev(t);
178 break;
179 };
180 } else {
181 if (!ascii::isalpha(r)) {
182 strings::prev(t);
183 break;
184 };
185 };
186 };
187
188 let end = *t;
189 if (quoted) {
190 strings::next(&start);
191 strings::prev(&end);
192 };
193
194 let abbr = strings::slice(&start, &end);
195 if (len(abbr) < 3) {
196 return parsed_err;
197 };
198
199 return abbr;
200 };
201
202 fn scan_petz_ruledate(
203 t: *strings::iterator,
204 default: parsed,
205 ) (petz_ruledate | parsed) = {
206 switch (scan_rune(t, void, default)?) {
207 case 'M' =>
208 // Mm.w.d
209 let m = scan_num(t, 1, 2, parsed_err)?: u8;
210 scan_rune(t, '.', parsed_err)?;
211 let w = scan_num(t, 1, 1, parsed_err)?: u8;
212 scan_rune(t, '.', parsed_err)?;
213 let d = scan_num(t, 1, 1, parsed_err)?: u8;
214 if (m < 1 || 12 < m) return parsed_err;
215 if (w < 1 || 5 < w) return parsed_err;
216 if (d < 0 || 6 < d) return parsed_err;
217 return (m, w, d): petz_ruledate_weekdate;
218 case 'J' =>
219 // Jn
220 let n = scan_num(t, 1, 3, parsed_err)?: petz_ruledate_julian;
221 if (n < 1 || 365 < n) return parsed_err;
222 return n;
223 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' =>
224 // n
225 strings::prev(t);
226 let n = scan_num(t, 1, 3, parsed_err)?: petz_ruledate_ordinal;
227 if (n < 1 || 365 < n) return parsed_err;
228 return n;
229 case =>
230 return parsed_err;
231 };
232 };
233
234 fn petz_is_twophased(p: petz) bool = {
235 return p.dst_abbr != "";
236 };
237
238 // Determines the correct zonephase/zone-offset for a date. Used for dates
239 // outside the range of zonephase transitions a date's timezone has.
240 fn lookupzone_posix(loc: locality, d: *date) *zonephase = {
241 let p = loc.petz;
242
243 if (!petz_is_twophased(p)) {
244 let pz = zonephase{
245 zoff = p.std_offset,
246 abbr = p.std_abbr,
247 ...
248 };
249 return search_zonephase(pz, loc.phases...);
250 };
251
252 let stdphase = zonephase{
253 zoff = p.std_offset,
254 abbr = p.std_abbr,
255 ...
256 };
257 let dstphase = zonephase{
258 zoff = p.dst_offset,
259 abbr = p.dst_abbr,
260 ...
261 };
262
263 // virtual dates for STD and DST.
264 // one of these is the correct date to be observed.
265 //
266 // we use vstd to help observe STD time with the STD offset to see where
267 // it falls in relation to the two most "recent" STD->DST
268 // zonetransition. vice versa for vdst.
269 let vstd = *d; vstd.zonephase = &stdphase;
270 let vdst = *d; vdst.zonephase = &dstphase;
271
272 // the daydates of the two most recent zonephase transitions for STD and
273 // DST each.
274 //
275 // we record two because of edge cases around the year boundary, where
276 // vstd and vdst can observe different years, causing the calculated
277 // transitions' daydates to be in different years. we account for this
278 // below.
279 let std_daydate1 = 0i64; let std_daydate2 = 0i64;
280 let dst_daydate1 = 0i64; let dst_daydate2 = 0i64;
281
282 match (p.dst_startdate) {
283 case let r: petz_ruledate_julian =>
284 abort("POSIX Timezone detail unimplemented (julian)"); // TODO
285
286 case let r: petz_ruledate_ordinal =>
287 abort("POSIX Timezone detail unimplemented (ordinal)"); // TODO
288
289 case let r: petz_ruledate_weekdate =>
290 let (month, week, weekday) = r;
291 let weekday = (weekday + 6) % 7; // Sunday=0 -> Monday=0
292
293 let weekday_monthfirst = calc_weekday(
294 calc_daydate__ymd(_year(&vstd), month: int, 1)!
295 );
296 let monthday = (
297 1
298 + (weekday: int - weekday_monthfirst)
299 + (week: int - 1) * 7
300 );
301 if (monthday > calc_days_in_month(_year(&vstd), month: int)) {
302 monthday -= 7;
303 };
304
305 dst_daydate1 = calc_daydate__ymd(
306 _year(&vstd) - 1, month: int, monthday,
307 )!;
308 dst_daydate2 = calc_daydate__ymd(
309 _year(&vstd), month: int, monthday,
310 )!;
311 };
312
313 match (p.dst_enddate) {
314 case let r: petz_ruledate_julian =>
315 abort("POSIX Timezone detail unimplemented (julian)"); // TODO
316
317 case let r: petz_ruledate_ordinal =>
318 abort("POSIX Timezone detail unimplemented (ordinal)"); // TODO
319
320 case let r: petz_ruledate_weekdate =>
321 let (month, week, weekday) = r;
322 let weekday = (weekday + 6) % 7; // Sunday=0 -> Monday=0
323
324 let weekday_monthfirst = calc_weekday(
325 calc_daydate__ymd(_year(&vdst), month: int, 1)!
326 );
327 let monthday = (
328 1
329 + (weekday: int - weekday_monthfirst)
330 + (week: int - 1) * 7
331 );
332 if (monthday > calc_days_in_month(_year(&vdst), month: int)) {
333 monthday -= 7;
334 };
335
336 std_daydate1 = calc_daydate__ymd(
337 _year(&vdst) - 1, month: int, monthday,
338 )!;
339 std_daydate2 = calc_daydate__ymd(
340 _year(&vdst), month: int, monthday,
341 )!;
342 };
343
344 // handle edge cases near the year boundaries
345 //
346 // (A) DST: | 1..... | 2..... | '..... |
347 // STD: |.. 1..|.. 2..|.. '..|
348 // ^
349 //
350 // (B) DST: | 1..... | 2..... | '..... |
351 // STD: |.. '..|.. 1..|.. 2..|
352 // ^
353 //
354 // (C) DST: |.. 1..|.. 2..|.. '..|
355 // STD: | 1..... | 2..... | '..... |
356 // ^
357 //
358 // (D) DST: |.. '..|.. 1..|.. 2..|
359 // STD: | 1..... | 2..... | '..... |
360 // ^
361 //
362 let (dst_daydate, std_daydate) = :dd {
363 if (dst_daydate2 < std_daydate1) { // (B)
364 yield :dd, (dst_daydate2, std_daydate1);
365 };
366
367 if (std_daydate2 < dst_daydate1) { // (D)
368 yield :dd, (dst_daydate1, std_daydate2);
369 };
370
371 yield (dst_daydate2, std_daydate2);
372 };
373
374 let after_dst = {
375 if (daydate(&vstd) < dst_daydate) yield false;
376 if (daydate(&vstd) > dst_daydate) yield true;
377 if (daytime(&vstd) < p.dst_starttime) yield false;
378 if (daytime(&vstd) >= p.dst_starttime) yield true;
379 abort("Unreachable");
380 };
381
382 let after_std = {
383 if (daydate(&vdst) < std_daydate) yield false;
384 if (daydate(&vdst) > std_daydate) yield true;
385 if (daytime(&vdst) < p.dst_endtime) yield false;
386 if (daytime(&vdst) >= p.dst_endtime) yield true;
387 abort("Unreachable");
388 };
389
390 let phase = if (dst_daydate < std_daydate) {
391 yield if (!after_dst && !after_std) {
392 yield stdphase;
393 } else if (after_dst && !after_std) {
394 yield dstphase;
395 } else if (after_dst && after_std) {
396 yield stdphase;
397 } else if (!after_dst && after_std) {
398 yield dstphase;
399 } else {
400 abort("POSIX Timezone error");
401 };
402 } else if (std_daydate < dst_daydate) {
403 yield if (!after_std && !after_dst) {
404 yield dstphase;
405 } else if (after_std && !after_dst) {
406 yield stdphase;
407 } else if (after_std && after_dst) {
408 yield dstphase;
409 } else if (!after_std && after_dst) {
410 yield stdphase;
411 } else {
412 abort("POSIX Timezone error");
413 };
414 } else {
415 abort("POSIX Timezone error");
416 };
417
418 return search_zonephase(phase, loc.phases...);
419 };
420
421 fn search_zonephase(z: zonephase, haystack: zonephase...) *zonephase = {
422 for (let hz &.. haystack) {
423 // TODO: full comparison with .dst doesnt work. maybe it should.
424 if (z.zoff == hz.zoff && z.abbr == hz.abbr) {
425 return hz;
426 };
427 };
428
429 abort("POSIX Timezone missing zonephase");
430 };