hare

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

commit 6b464c4043c8215cb409b97e0befc474cf6d6458
parent 1dceb79502fafeaeb4cd94dc584884d23ffe50c9
Author: Byron Torres <b@torresjrjr.com>
Date:   Tue, 24 May 2022 04:00:21 +0100

time::chrono: fix LOCAL init, improve docs

Renamed TZ_local to TZ_LOCAL. Consistent and less ungly, I suppose.

Improved docs for [[TZ_LOCAL]].

TZ_LOCAL.name now defaults to "Local". [[timezone]].name is meant to be
an identifier, rather than descriptive or end-user-friendly, and there's
a convention or unwritten rule to keep them whitespace-less for this
purpose. This convention is (will be) useful when parsing %L. Perhaps we
should add a .description field.

The filepath of the TZif file, if appropriate, is now used as part of
the name for [[TZ_LOCAL]].

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

Diffstat:
Mtime/chrono/timezone.ha | 66++++++++++++++++++++++++++++++++++++++++++------------------------
1 file changed, 42 insertions(+), 24 deletions(-)

diff --git a/time/chrono/timezone.ha b/time/chrono/timezone.ha @@ -4,6 +4,7 @@ use bufio; use io; use os; use path; +use strings; use time; // The locality of a [[moment]]. Contains information about how to present a @@ -167,50 +168,67 @@ export fn fixedzone(ts: *timescale, daylen: time::duration, z: zone) timezone = // The system's [[locality]]; the system's local [[timezone]]. // -// This is set during initialisation. -export const LOCAL: locality = &TZ_local; +// This is set during a program's initialisation, where the TZ environment +// variable is tried, otherwise the /etc/localtime file is tried, otherwise a +// default is used. +// +// The default timezone is equivalent to that of [[UTC]], with "Local" being the +// name of both the timezone and its single zero-offset zone. +export const LOCAL: locality = &TZ_LOCAL; + +let TZ_LOCAL: timezone = timezone { + name = "Local", + timescale = &utc, + daylength = EARTH_DAY, + zones = [ + zone { + zoffset = 0 * time::SECOND, + name = "Local", + abbr = "", + dst = false, + }, + ], + transitions = [], + posix_extend = "", +}; @init fn set_local_timezone() void = { match (os::getenv("TZ")) { case let zone: str => - TZ_local = match (tz(zone)) { + TZ_LOCAL = match (tz(zone)) { case let tz: timezone => yield tz; case => return; }; case void => - const file = match (os::open(LOCALTIME_PATH)) { - case let file: io::file => - yield file; + const filepath = match (os::readlink(LOCALTIME_PATH)) { + case let fp: str => + yield fp; + case => + return; + }; + + const file = match (os::open(filepath)) { + case let f: io::file => + yield f; case => return; }; defer io::close(file)!; + if (strings::hasprefix(filepath, ZONEINFO_PREFIX)) { + TZ_LOCAL.name = strings::trimprefix( + filepath, ZONEINFO_PREFIX, + ); + }; + static let buf: [os::BUFSIZ]u8 = [0...]; const file = bufio::buffered(file, buf, []); - - match (parse_tzif(&file, &TZ_local)) { case => void; }; + match (parse_tzif(&file, &TZ_LOCAL)) { case => void; }; }; }; -let TZ_local: timezone = timezone { - name = "Local time", - timescale = &utc, - daylength = EARTH_DAY, - zones = [ - zone { - zoffset = 0 * time::SECOND, - name = "Local time", - abbr = "", - dst = false, - }, - ], - transitions = [], - posix_extend = "", -}; - // The UTC (Coordinated Universal Time) "Zulu" [[timezone]] as a [[locality]]. export const UTC: locality = &TZ_UTC;