Sets TZ_LOCAL.name correctly when TZDIR does not end in a slash.
Reference:
tzset(3)
https://man.archlinux.org/man/tzset.3.en
tzselect(8)
https://man.archlinux.org/man/tzselect.8.en
Signed-off-by: Antero Mejr <antero@mailbox.org>
---
time/chrono/leapsec.ha | 10 ++++++----
time/chrono/timezone.ha | 9 +++++++--
time/chrono/tzdb.ha | 3 ++-
3 files changed, 15 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..c459b136 100644
--- a/time/chrono/timezone.ha
+++ b/time/chrono/timezone.ha
@@ -240,9 +240,14 @@ let TZ_LOCAL: timezone = timezone {
};
defer io::close(file)!;
- if (strings::hasprefix(filepath, ZONEINFO_PREFIX)) {
+ const tzdir = os::tryenv("TZDIR", ZONEINFO_PREFIX);
+ let tzdir_path = path::init();
+ path::add(&tzdir_path, tzdir, "/")!;
+ const tzdir_str = path::string(&tzdir_path);
+
+ if (strings::hasprefix(filepath, tzdir_str)) {
TZ_LOCAL.name = strings::trimprefix(
- filepath, ZONEINFO_PREFIX,
+ filepath, tzdir_str,
);
};
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
On Thu Jul 7, 2022 at 7:05 PM CEST, Antero Mejr wrote:
> @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")!;
Should use path::set here. We can also just call the leap_seconds_path
variable "path" or "buf" or something, no need to get verbose.