Debian Codegraph @

time/chrono/utc.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use bufio;
5 use encoding::utf8;
6 use fs;
7 use io;
8 use os;
9 use strconv;
10 use strings;
11
12 // Hare uses raw leap second information when dealing with the UTC and TAI
13 // timescales. This information is source from a standard file installed at
14 // /usr/share/zoneinfo/leap-seconds.list, which itself is fetched from and
15 // periodically maintained at various observatories.
16 //
17 // https://data.iana.org/time-zones/code/leap-seconds.list
18 // https://www.ietf.org/timezones/data/leap-seconds.list
19 // ftp://ftp.nist.gov/pub/time/leap-seconds.list
20 // ftp://ftp.boulder.nist.gov/pub/time/leap-seconds.list
21 //
22 // This is in contrast to previous systems which rely on TZif files, which are
23 // installed typically at /usr/share/zoneinfo, as part of the "Olson" IANA
24 // Timezone database. These files couple timezone and leap second data.
25 //
26 // Depending on a system's installation, leap second information may be
27 // deliberately left out of the TZif files, or duplicated throughout. This
28 // design also inhibits our ambitions for dealing with multiple, dynamic
29 // timescales. Therefore, we have decided to take an alternative approach.
30
31 // Error initializing the [[utc]] [[timescale]].
32 export type utciniterror = !(fs::error | io::error | utf8::invalid);
33
34 // The number of seconds between the years 1900 and 1970.
35 //
36 // This number is notinal since timekeeping before atomic clocks was not
37 // accurate enough to account for small changes in time.
38 def SECONDS_1900_1970: i64 = 2208988800;
39
40 // UTC/TAI leap second data; UTC timestamps and their offsets from TAI.
41 // Sourced from [[UTC_LEAPSECS_PATH]].
42 let utc_leapsecs: [](i64, i64) = [];
43
44 export let utc_status: (bool | utciniterror) = false;
45
46 @fini fn free_utc() void = {
47 free(utc_leapsecs);
48 };
49
50 fn utc_init_leapsecs() (void | utciniterror | nomem) = {
51 const file = os::open(UTC_LEAPSECS_PATH)?;
52 defer io::close(file)!;
53
54 match (utc_parse_leapsecs(file)) {
55 case void => void;
56 // passing on errors manually since the result is not assignable to
57 // function result type.
58 case utf8::invalid =>
59 return utf8::invalid;
60 case let e: io::error =>
61 return e;
62 };
63 };
64
65 // Parse UTC/TAI leapsecond data from [[UTC_LEAPSECS_PATH]].
66 // See that file for format details.
67 fn utc_parse_leapsecs(h: io::handle) (void | utf8::invalid | io::error | nomem) = {
68 const scan = bufio::newscanner(h);
69 defer bufio::finish(&scan);
70
71 for (let line => bufio::scan_line(&scan)?) {
72 if (strings::hasprefix(line, '#')) {
73 continue;
74 };
75
76 const iter = strings::iter(line);
77 const secs = scan_number(&iter); scan_whitespace(&iter);
78 const diff = scan_number(&iter);
79 if (secs is void || diff is void) {
80 continue;
81 };
82
83 let secs = secs as i64 - SECONDS_1900_1970;
84 let diff = diff as i64;
85 append(utc_leapsecs, (secs, diff))?;
86 };
87 };
88
89 fn scan_number(iter: *strings::iterator) (i64 | void) = {
90 let begin = *iter;
91
92 for (let rn => strings::next(iter)) {
93 switch (rn) {
94 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' =>
95 continue;
96 case =>
97 strings::prev(iter);
98 break;
99 };
100 };
101
102 return strconv::stoi64(strings::slice(&begin, iter))!;
103 };
104
105 fn scan_whitespace(iter: *strings::iterator) void = {
106 for (let rn => strings::next(iter)) {
107 switch (rn) {
108 case ' ', '\t' =>
109 continue;
110 case =>
111 strings::prev(iter);
112 return;
113 };
114 };
115 };