hare

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

commit 5edd24a061c20235cbb3c3e26b5c354b0f229db6
parent 08adeb6abe774eebb8e0e49d5fa76eaacc1d56be
Author: Byron Torres <b@torresjrjr.com>
Date:   Sun, 19 Dec 2021 23:25:47 +0000

have datetime::datetime inherit chrono::moment

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

Diffstat:
Mdatetime/calendar.ha | 32+++++++++++---------------------
Mdatetime/datetime.ha | 32+++++++++++++++++---------------
Mtime/chrono/chronology.ha | 1+
Mtime/chrono/timezone.ha | 1-
4 files changed, 29 insertions(+), 37 deletions(-)

diff --git a/datetime/calendar.ha b/datetime/calendar.ha @@ -20,15 +20,17 @@ export def EPOCH_COMMONERA: i64 = -719164; // Evaluates a [[datetime]]'s number of days since the calendar epoch 0000-01-01 export fn epochal(dt: *datetime) chrono::epochal = { - match (dt.date) { - case void => - // How to resolve? - // Use calc_epochal_from_*? How to avoid recursion? - // When to rely on input validation? - abort("TODO"); - case let e: chrono::epochal => - return e; - }; + return dt.date; + +// match (dt.date) { +// case void => +// // How to resolve? +// // Use calc_epochal_from_*? How to avoid recursion? +// // When to rely on input validation? +// abort("TODO"); +// case let e: chrono::epochal => +// return e; +// }; }; // Evaluates a [[datetime]]'s era @@ -49,9 +51,6 @@ export fn era(dt: *datetime) int = { export fn year(dt: *datetime) int = { match (dt.year) { case void => - if (dt.date is void) { - epochal(dt); - }; const ymd = calc_ymd(dt.date: chrono::epochal); dt.year = ymd.0; dt.month = ymd.1; @@ -66,9 +65,6 @@ export fn year(dt: *datetime) int = { export fn month(dt: *datetime) int = { match (dt.month) { case void => - if (dt.date is void) { - epochal(dt); - }; const ymd = calc_ymd(dt.date: chrono::epochal); dt.year = ymd.0; dt.month = ymd.1; @@ -83,9 +79,6 @@ export fn month(dt: *datetime) int = { export fn day(dt: *datetime) int = { match (dt.day) { case void => - if (dt.date is void) { - epochal(dt); - }; const ymd = calc_ymd(dt.date: chrono::epochal); dt.year = ymd.0; dt.month = ymd.1; @@ -100,9 +93,6 @@ export fn day(dt: *datetime) int = { export fn weekday(dt: *datetime) int = { match (dt.weekday) { case void => - if (dt.date is void) { - epochal(dt); - }; dt.weekday = calc_weekday(dt.date: chrono::epochal); return dt.weekday: int; case let y: int => diff --git a/datetime/datetime.ha b/datetime/datetime.ha @@ -5,9 +5,7 @@ use time::chrono; // Represents a datetime; a single, reasonably unique moment in time, specified // by a calendar date and a wallclock time, contextualised within a locality. export type datetime = struct { - date: (void | chrono::epochal), - time: (void | time::duration), - loc: chrono::locality, + chrono::moment, era: (void | int), year: (void | int), @@ -28,9 +26,10 @@ export type datetime = struct { fn init() datetime = datetime { - date = void, - time = void, - loc = chrono::locality{ timezone = chrono::local, ... }, + date = 0, + time = 0, + loc = chrono::locality{ ... }, + tz = chrono::local, era = void, year = void, @@ -61,12 +60,14 @@ export fn new( min: int, sec: int, nsec: int, - loc: chrono::locality, + zoffset: chrono::zoffset, + tz: (*chrono::timezone | void), ) (datetime | errors::invalid) = { const dt = datetime { date = calc_epochal_from_ymd(year, month, day)?, time = calc_time_from_hmsn(hour, min, sec, nsec)?, - loc = loc, + loc = chrono::locality{ zoffset = zoffset, ... }, + tz = if (tz is void) chrono::local else tz: *chrono::timezone, era = void, year = year, @@ -90,21 +91,22 @@ export fn new( export fn now() datetime = { const i = time::now(time::clock::REALTIME); const unix = time::unix(i); - const daynum = (unix / 86400); - const date = calc_ymd(daynum); + const date = (unix / 86400); + const caldate = calc_ymd(date); const dt = datetime { - date = daynum, + date = date, time = ((i.sec / 86400) * time::NANOSECOND + i.nsec), // TODO: What to do here? How to get the timezone from // /etc/localtime or $TZ? How to determine the system's // timescale? Assuming UTC may be sufficient. - loc = chrono::locality{ timezone = chrono::local, ... }, + loc = chrono::locality{ ... }, + tz = chrono::local, era = void, - year = date.0, - month = date.1, - day = date.2, + year = caldate.0, + month = caldate.1, + day = caldate.2, isoweekyear = void, isoweek = void, week = void, diff --git a/time/chrono/chronology.ha b/time/chrono/chronology.ha @@ -5,6 +5,7 @@ export type moment = struct { date: epochal, time: time::duration, loc: locality, + tz: (*timezone | local), }; // An ordinal day on earth since the calendar epoch (zeroth day) 1970-01-01 diff --git a/time/chrono/timezone.ha b/time/chrono/timezone.ha @@ -4,7 +4,6 @@ use time; export type locality = struct { zrepr: str, // %Z zoffset: zoffset, // %z - timezone: (*timezone | local), }; // A destructured dual std/dst POSIX timezone. See tzset(3).