~sircmpwn/hare-dev

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

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

 9 files changed, 56 insertions(+), 21 deletions(-)
#788647 alpine.yml success
#788648 freebsd.yml success
Sebastian <sebastian@sebsite.pw> writes:
Next
Thanks for contributing.

The TZDIR seems to be a de facto standard not really described anywhere.
I think it's a good idea to support it. If you could compile some
sources which describe the usage of TZDIR in certain OSs, and put that
in the commentary, that would help onlookers.

Note, it is common practice to create a new email thread for each patch
version, not reply to the old version.

On Mon Jun 27, 2022 at 2:24 AM BST, Antero Mejr wrote:
Next
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/33317/mbox | git am -3
Learn more about email & git

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

This is useful for functional distros (Guix/Nix) and setups where the tz data
is in a different place.

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

diff --git a/time/chrono/leapsec.ha b/time/chrono/leapsec.ha
index 7706475f..39215924 100644
--- a/time/chrono/leapsec.ha
+++ b/time/chrono/leapsec.ha
@@ -1,10 +1,12 @@
// License: MPL-2.0
// (c) 2021-2022 Byron Torres <b@torresjrjr.com>
// (c) 2022 Antero Mejr <antero@mailbox.org>
use bufio;
use encoding::utf8;
use fs;
use io;
use os;
use path;
use strconv;
use strings;

@@ -32,16 +34,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 = get_tzdir();
	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 =>
@@ -79,3 +82,13 @@ fn read_utc_leapsecs_file(
		append(utc_leapsecs, pair);
	};
};

export fn get_tzdir() str = {
	const tzdir = match (os::getenv("TZDIR")) {
	case let tzdir: str =>
		yield tzdir;
	case void =>
		yield ZONEINFO_PREFIX;
	};
	return tzdir;
};
diff --git a/time/chrono/timezone.ha b/time/chrono/timezone.ha
index 52615b35..5549fe78 100644
--- a/time/chrono/timezone.ha
+++ b/time/chrono/timezone.ha
@@ -240,9 +240,10 @@ let TZ_LOCAL: timezone = timezone {
		};
		defer io::close(file)!;

		if (strings::hasprefix(filepath, ZONEINFO_PREFIX)) {
		const tzdir = get_tzdir();
		if (strings::hasprefix(filepath, tzdir)) {
			TZ_LOCAL.name = strings::trimprefix(
				filepath, ZONEINFO_PREFIX,
				filepath, tzdir,
			);
		};

diff --git a/time/chrono/tzdb.ha b/time/chrono/tzdb.ha
index 876d85d9..010f74e9 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 = get_tzdir();
	path::add(&filepath, tzdir, name)!;
	const fpath = path::string(&filepath);
	const file = os::open(fpath)?;

-- 
2.36.1
hare/patches: SUCCESS in 1m58s

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

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

✓ #788647 SUCCESS hare/patches/alpine.yml  https://builds.sr.ht/~sircmpwn/job/788647
✓ #788648 SUCCESS hare/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/788648

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

This patch has documentation and moved code around to make more sense.
Please ignore the last one, sorry about that.

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, 23 insertions(+), 7 deletions(-)

diff --git a/time/chrono/leapsec.ha b/time/chrono/leapsec.ha
index 7706475f..7160d392 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 = get_tzdir();
	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..6c82b159 100644
--- a/time/chrono/timezone.ha
+++ b/time/chrono/timezone.ha
@@ -1,5 +1,6 @@
// License: MPL-2.0
// (c) 2021-2022 Byron Torres <b@torresjrjr.com>
// (c) 2022 Antero Mejr <antero@mailbox.org>
use bufio;
use io;
use os;
@@ -240,9 +241,10 @@ let TZ_LOCAL: timezone = timezone {
		};
		defer io::close(file)!;

		if (strings::hasprefix(filepath, ZONEINFO_PREFIX)) {
		const tzdir = get_tzdir();
		if (strings::hasprefix(filepath, tzdir)) {
			TZ_LOCAL.name = strings::trimprefix(
				filepath, ZONEINFO_PREFIX,
				filepath, tzdir,
			);
		};

@@ -346,3 +348,14 @@ const TZ_MTC: timezone = timezone {
	transitions = [],
	posix_extend = "",
};

// Get the tzdata directory, from $TZDIR or the fallback /usr/share/zoneinfo.
export fn get_tzdir() str = {
	const tzdir = match (os::getenv("TZDIR")) {
	case let tzdir: str =>
		yield tzdir;
	case void =>
		yield ZONEINFO_PREFIX;
	};
	return tzdir;
};
diff --git a/time/chrono/tzdb.ha b/time/chrono/tzdb.ha
index 876d85d9..010f74e9 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 = get_tzdir();
	path::add(&filepath, tzdir, name)!;
	const fpath = path::string(&filepath);
	const file = os::open(fpath)?;

-- 
2.36.1

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

Good point, forgot about tryenv.

Thanks,
Antero

Signed-off-by: Antero Mejr <antero@mailbox.org>
---
 time/chrono/leapsec.ha  | 10 ++++++----
 time/chrono/timezone.ha |  5 +++--
 time/chrono/tzdb.ha     |  3 ++-
 3 files changed, 11 insertions(+), 7 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..59fece66 100644
--- a/time/chrono/timezone.ha
+++ b/time/chrono/timezone.ha
@@ -240,9 +240,10 @@ let TZ_LOCAL: timezone = timezone {
		};
		defer io::close(file)!;

		if (strings::hasprefix(filepath, ZONEINFO_PREFIX)) {
		const tzdir = os::tryenv("TZDIR", ZONEINFO_PREFIX);
		if (strings::hasprefix(filepath, tzdir)) {
			TZ_LOCAL.name = strings::trimprefix(
				filepath, ZONEINFO_PREFIX,
				filepath, tzdir,
			);
		};

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
Thanks for contributing.

The TZDIR seems to be a de facto standard not really described anywhere.
I think it's a good idea to support it. If you could compile some
sources which describe the usage of TZDIR in certain OSs, and put that
in the commentary, that would help onlookers.

Note, it is common practice to create a new email thread for each patch
version, not reply to the old version.

On Mon Jun 27, 2022 at 2:24 AM BST, Antero Mejr wrote: