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