hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 57b90d374aa139600d76ddb34fedc8847b9e26d3
parent 14f7c62220a0e8e9b3a50a334bb5501671bab28a
Author: Byron Torres <b@torresjrjr.com>
Date:   Wed,  2 Feb 2022 22:22:03 +0000

WIP leap-seconds.list reader

Signed-off-by: Byron Torres <b@torresjrjr.com>

Diffstat:
Mscripts/gen-stdlib | 2+-
Mstdlib.mk | 4++--
Mtime/chrono/leapsec.ha | 78+++++++++++++++++++++++++++++++++++++++++++-----------------------------------
3 files changed, 46 insertions(+), 38 deletions(-)

diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -1182,7 +1182,7 @@ time_chrono() { leapsec.ha \ timescale.ha \ timezone.ha - gen_ssa time::chrono time + gen_ssa time::chrono bufio fmt io os strconv strings time } time_tzdb() { diff --git a/stdlib.mk b/stdlib.mk @@ -1802,7 +1802,7 @@ stdlib_time_chrono_any_srcs= \ $(STDLIB)/time/chrono/timescale.ha \ $(STDLIB)/time/chrono/timezone.ha -$(HARECACHE)/time/chrono/time_chrono-any.ssa: $(stdlib_time_chrono_any_srcs) $(stdlib_rt) $(stdlib_time_$(PLATFORM)) +$(HARECACHE)/time/chrono/time_chrono-any.ssa: $(stdlib_time_chrono_any_srcs) $(stdlib_rt) $(stdlib_bufio_$(PLATFORM)) $(stdlib_fmt_$(PLATFORM)) $(stdlib_io_$(PLATFORM)) $(stdlib_os_$(PLATFORM)) $(stdlib_strconv_$(PLATFORM)) $(stdlib_strings_$(PLATFORM)) $(stdlib_time_$(PLATFORM)) @printf 'HAREC \t$@\n' @mkdir -p $(HARECACHE)/time/chrono @HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Ntime::chrono \ @@ -3812,7 +3812,7 @@ testlib_time_chrono_any_srcs= \ $(STDLIB)/time/chrono/timescale.ha \ $(STDLIB)/time/chrono/timezone.ha -$(TESTCACHE)/time/chrono/time_chrono-any.ssa: $(testlib_time_chrono_any_srcs) $(testlib_rt) $(testlib_time_$(PLATFORM)) +$(TESTCACHE)/time/chrono/time_chrono-any.ssa: $(testlib_time_chrono_any_srcs) $(testlib_rt) $(testlib_bufio_$(PLATFORM)) $(testlib_fmt_$(PLATFORM)) $(testlib_io_$(PLATFORM)) $(testlib_os_$(PLATFORM)) $(testlib_strconv_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_time_$(PLATFORM)) @printf 'HAREC \t$@\n' @mkdir -p $(TESTCACHE)/time/chrono @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Ntime::chrono \ diff --git a/time/chrono/leapsec.ha b/time/chrono/leapsec.ha @@ -1,4 +1,9 @@ -use time; +use bufio; +use fmt; +use io; +use os; +use strconv; +use strings; // Hare uses raw leap second informtion when dealing with the UTC and TAI // timescales. This information is source from a standard file installed at @@ -18,38 +23,41 @@ use time; // design also inhibits our ambitions for dealing with multiple, dynamic // timescales. Therefore, we have decided to take an alternative approach. +// The filepath of the leap-seconds.list file +export def UTC_LEAPSECS_FILE: str = "/usr/share/zoneinfo/leap-seconds.list"; + // UTC timestamps and their TAI offsets, sourced from leap-seconds.list -// updated: 8 July 2016 -// expires: 28 June 2022 -// -// TODO: Initialise this data from /usr/share/zoneinfo/leap-seconds.list -const leaps_utc: [](i64, i64) = [ - (2272060800, 10), - (2287785600, 11), - (2303683200, 12), - (2335219200, 13), - (2366755200, 14), - (2398291200, 15), - (2429913600, 16), - (2461449600, 17), - (2492985600, 18), - (2524521600, 19), - (2571782400, 20), - (2603318400, 21), - (2634854400, 22), - (2698012800, 23), - (2776982400, 24), - (2840140800, 25), - (2871676800, 26), - (2918937600, 27), - (2950473600, 28), - (2982009600, 29), - (3029443200, 30), - (3076704000, 31), - (3124137600, 32), - (3345062400, 33), - (3439756800, 34), - (3550089600, 35), - (3644697600, 36), - (3692217600, 37), -]; +let utc_leapsecs: [](i64, i64) = []; + +// TODO: BUG: Cannot compile harec2 with this line. +//@init fn init_utc_leapsecs() void = init_utc_leapsecs(); + +fn init_utc_leapsecs() void = { + const file = os::open(UTC_LEAPSECS_FILE)!; + read_leapsecfile(file, &utc_leapsecs)!; +}; + +fn read_leapsecfile(h: io::handle, leapsecs: *[](i64, i64)) (void | io::error) = { + for (true) { + const line = match (bufio::scanline(h)) { + case let err: io::error => + return err; + case io::EOF => + return; + case let line: []u8 => + yield line; + }; + const line = fmt::bsprint(line); + if (strings::hasprefix(line, '#')) { + continue; + }; + const pair = strings::splitN(line, "\t", 3); + if (len(pair) < 2) { + continue; + }; + const a = strconv::stoi64(pair[0])!; + const b = strconv::stoi64(pair[1])!; + const pair = (a: i64, b: i64); + append(utc_leapsecs, pair); + }; +};