~sircmpwn/hare-dev

hare: time::chrono: use TZDIR environment variable v4 PROPOSED

Antero Mejr: 1
 time::chrono: use TZDIR environment variable

 3 files changed, 21 insertions(+), 9 deletions(-)
#789046 alpine.yml success
#789047 freebsd.yml success
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/hare-dev/patches/33323/mbox | git am -3
Learn more about email & git

[PATCH hare v4] time::chrono: use TZDIR environment variable Export this patch

This patch handles the case where $TZDIR doesn't end in a forward slash,
resulting in TZ_LOCAL.name incorrectly starting with a forward slash due to
the strings::trimprefix call.

TZDIR reference:
tzselect(8)
https://man.archlinux.org/man/tzselect.8.en

tzset(3)
https://man.archlinux.org/man/tzset.3.en

Current Guix System distribution TZDIR:
/gnu/store/7pjzwj9d4fnyzp9x7k8cc4hazypyrk0p-tzdata-2022a/share/zoneinfo

Current Ubuntu 22.04 TZDIR: (unset)

Signed-off-by: Antero Mejr <antero@mailbox.org>
---
 time/chrono/leapsec.ha  | 10 ++++++----
 time/chrono/timezone.ha | 17 +++++++++++++----
 time/chrono/tzdb.ha     |  3 ++-
 3 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/time/chrono/leapsec.ha b/time/chrono/leapsec.ha
index 7706475f..505fb5e7 100644
--- a/time/chrono/leapsec.ha
+++ b/time/chrono/leapsec.ha
@@ -5,6 +5,7 @@ use encoding::utf8;
use fs;
use io;
use os;
use path;
use strconv;
use strings;

@@ -32,16 +33,17 @@ use strings;
// accurate enough to account for small changes in time.
export def SECS_1900_1970: i64 = 2208988800;

// The filepath of the system's leap-seconds.list file.
export def UTC_LEAPSECS_FILE: str = "/usr/share/zoneinfo/leap-seconds.list";

// UTC timestamps and their offsets from TAI, sourced from the system's
// leap-seconds.list file.
let utc_leapsecs: [](i64, i64) = [];

@init fn init_utc_leapsecs() void = {
	os::init_cwd();
	const file = match (os::open(UTC_LEAPSECS_FILE)) {
	const tzdir = os::tryenv("TZDIR", ZONEINFO_PREFIX);
	let leap_seconds_path = path::init();
	path::add(&leap_seconds_path, tzdir, "leap-seconds.list")!;
	const utc_leapsecs_file = path::string(&leap_seconds_path);
	const file = match (os::open(utc_leapsecs_file)) {
	case let file: io::file =>
		yield file;
	case fs::error =>
diff --git a/time/chrono/timezone.ha b/time/chrono/timezone.ha
index 52615b35..cf8d947f 100644
--- a/time/chrono/timezone.ha
+++ b/time/chrono/timezone.ha
@@ -240,10 +240,19 @@ let TZ_LOCAL: timezone = timezone {
		};
		defer io::close(file)!;

		if (strings::hasprefix(filepath, ZONEINFO_PREFIX)) {
			TZ_LOCAL.name = strings::trimprefix(
				filepath, ZONEINFO_PREFIX,
			);
		const tzdir = os::tryenv("TZDIR", ZONEINFO_PREFIX);
		if (strings::hasprefix(filepath, tzdir)) {
			if (!strings::hassuffix(tzdir, "/")) {
				const with_slash = strings::concat(tzdir, "/");
				TZ_LOCAL.name = strings::trimprefix(
					filepath, with_slash,
				);
				free(with_slash);
			} else {
				TZ_LOCAL.name = strings::trimprefix(
					filepath, tzdir,
				);
			};
		};

		static let buf: [os::BUFSIZ]u8 = [0...];
diff --git a/time/chrono/tzdb.ha b/time/chrono/tzdb.ha
index 876d85d9..c9a63bbb 100644
--- a/time/chrono/tzdb.ha
+++ b/time/chrono/tzdb.ha
@@ -37,7 +37,8 @@ export fn strerror(err: error) const str = {
export fn tz(name: str) (timezone | fs::error | io::error | invalidtzif) = {
// TODO: Consolidate errors (use chrono::error?).
	const filepath = path::init();
	path::add(&filepath, ZONEINFO_PREFIX, name)!;
	const tzdir = os::tryenv("TZDIR", ZONEINFO_PREFIX);
	path::add(&filepath, tzdir, name)!;
	const fpath = path::string(&filepath);
	const file = os::open(fpath)?;

-- 
2.36.1
hare/patches: SUCCESS in 1m44s

[time::chrono: use TZDIR environment variable][0] v4 from [Antero Mejr][1]

[0]: https://lists.sr.ht/~sircmpwn/hare-dev/patches/33323
[1]: mailto:antero@mailbox.org

✓ #789046 SUCCESS hare/patches/alpine.yml  https://builds.sr.ht/~sircmpwn/job/789046
✓ #789047 SUCCESS hare/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/789047
Can you use path:: for this?