hare

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

commit 6f3e97bf2192929cfbc33df215e0aa3ce18c0d28
parent 978ff10ecf6e7f4f9ae790121d1653c953a1b943
Author: Byron Torres <b@torresjrjr.com>
Date:   Sun,  9 Jan 2022 21:39:43 +0000

new datetime::lookupzone()

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

Diffstat:
Mdatetime/timezone.ha | 8++++++++
Mtime/chrono/timezone.ha | 24++++++++++++++++++++++--
2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/datetime/timezone.ha b/datetime/timezone.ha @@ -14,3 +14,11 @@ export fn in(loc: chrono::locality, dt: datetime) datetime = { export fn localize(dt: datetime) datetime = { return from_moment(chrono::localize(to_moment(dt))); }; + +// Finds a [[datetime]]'s currently observed zone +export fn lookupzone(dt: *datetime) chrono::zone = { + const m = to_moment(*dt); + const z = chrono::lookupzone(&m); + dt.zone = z; + return z; +}; diff --git a/time/chrono/timezone.ha b/time/chrono/timezone.ha @@ -66,15 +66,35 @@ export fn in(loc: locality, m: moment) moment = { // Returns a fictitious moment which assumes it's own locality is the normal // locality. Its .date & .time fields are adjusted by its current zone's offset. export fn localize(m: moment) moment = { - const zone = lookupzone(m); + const zone = lookupzone(&m); const newtime = m.time + zone.zoffset; m.time = newtime % m.loc.daylength; m.date += (newtime / m.loc.daylength); return m; }; -export fn lookupzone(m: moment) zone = { +// Finds a [[moment]]'s currently observed zone +export fn lookupzone(m: *moment) zone = { + if (m.zone is zone) { + return m.zone as zone; + }; + + if (len(m.loc.zones) == 0) { + // TODO: redesign to avoid this? + abort("timezones should have at least one zone"); + }; + if (len(m.loc.zones) == 1) { + const z = m.loc.zones[0]; + m.zone = z; + return z; + }; + // TODO: search through m.loc.trans using m.date and m.time + if (len(m.loc.zones) > 1) { + const z = m.loc.zones[0]; + m.zone = z; + return z; + }; return zone{ ... }; };