commit f9c6d44f3f1192e767cc51c71c72a43226df7351
parent 06069f2e68f993a3f45b8f5fea780a6186b7e918
Author: Byron Torres <b@torresjrjr.com>
Date: Sun, 9 Jan 2022 01:50:34 +0000
chrono: add moment.zone, new(), from_instant()
Signed-off-by: Byron Torres <b@torresjrjr.com>
Diffstat:
3 files changed, 48 insertions(+), 27 deletions(-)
diff --git a/datetime/datetime.ha b/datetime/datetime.ha
@@ -31,6 +31,7 @@ fn init() datetime = datetime {
date = 0,
time = 0,
loc = chrono::local,
+ zone = void,
era = void,
year = void,
@@ -48,10 +49,10 @@ fn init() datetime = datetime {
nsec = void,
};
-// Creates a new datetime
+// Creates a new datetime. When loc=void, defaults to chrono::local.
//
-// // 2038 January 19th 03:14:07.618 +0000
-// datetime::new(2038, 01, 19, 03, 14, 07, 618, 0, chrono::local)
+// // 2038 January 19th 03:14:07.000000618 +0000 Local
+// datetime::new(2038, 01, 19, 03, 14, 07, 618, 0000, void)
//
export fn new(
year: int,
@@ -68,6 +69,7 @@ export fn new(
date = calc_epochal_from_ymd(year, month, day)?,
time = calc_time_from_hmsn(hour, min, sec, nsec)?,
loc = if (loc is void) chrono::local else loc: *chrono::timezone,
+ zone = void,
era = void,
year = year,
@@ -87,40 +89,22 @@ export fn new(
return dt;
};
-// Returns the current datetime
+// Returns a [[datetime]] of the immediate system time
export fn now() datetime = {
+ // TODO: should the clock and timezone be choosable?
const i = time::now(time::clock::REALTIME);
- const unix = time::unix(i);
- const date = (unix / 86400);
- const dt = datetime {
- date = date,
- time = ((i.sec / 86400) * time::NANOSECOND + i.nsec),
- loc = chrono::local,
-
- era = void,
- year = void,
- month = void,
- day = void,
- isoweekyear = void,
- isoweek = void,
- week = void,
- weekday = void,
- yearday = void,
-
- hour = (i.sec / 3600): int % 24,
- min = (i.sec / 60): int % 60,
- sec = i.sec: int % 60,
- nsec = i.nsec: int,
- };
+ const m = chrono::from_instant(i, chrono::local);
+ const dt = from_moment(m);
return dt;
};
-// Creates a copy of a datetime
+// Creates a copy of a [[datetime]]
export fn clone(dt: datetime) datetime = {
return datetime {
date = dt.date,
time = dt.time,
loc = dt.loc,
+ zone = dt.zone,
era = dt.era,
year = dt.year,
@@ -145,6 +129,7 @@ export fn from_moment(m: chrono::moment) datetime = {
dt.date = m.date;
dt.time = m.time;
dt.loc = m.loc;
+ dt.zone = m.zone;
return dt;
};
@@ -154,6 +139,7 @@ export fn to_moment(dt: datetime) chrono::moment = {
date = dt.date,
time = dt.time,
loc = dt.loc,
+ zone = dt.zone,
};
};
diff --git a/time/chrono/chronology.ha b/time/chrono/chronology.ha
@@ -2,15 +2,49 @@ use time;
// A date & time, within a locality, intepreted via a chronology
export type moment = struct {
+ // The ordinal day (on Earth or otherwise)
+ // since the Hare epoch (zeroth day) 1970-01-01
date: epochal,
+
+ // The time since the start of the day
time: time::duration,
+
+ // The timezone used for interpreting a moment's date and time
loc: locality,
+
+ // The cached temporal zone this moment observes
+ zone: (zone | void),
};
// An ordinal day (on Earth or otherwise) since the Hare epoch (zeroth day)
// 1970-01-01
export type epochal = i64;
+// Creates a new [[moment]]
+export fn new(date: epochal, time: time::duration, loc: locality) moment = {
+ return moment {
+ date = date,
+ time = time,
+ loc = loc,
+ zone = void,
+ };
+};
+
+// Creates a new [[moment]] from a [[time::instant]] in a [[locality]]
+export fn from_instant(i: time::instant, loc: locality) moment = {
+ const daysec = (loc.daylength / time::SECOND);
+ const m = moment {
+ date = i.sec / daysec,
+ time = (
+ (i.sec % daysec) * time::SECOND
+ + i.nsec * time::NANOSECOND
+ ),
+ loc = loc,
+ zone = void,
+ };
+ return m;
+};
+
// The temporal length of a day on Earth.
// Interpreted with an appropriate timescale like UTC, TAI, GPS.
export def EARTH_DAY: time::duration = 86400 * time::SECOND;
diff --git a/time/chrono/timezone.ha b/time/chrono/timezone.ha
@@ -1,5 +1,6 @@
use time;
+// The virtual region a moment is interpreted in
export type locality = *timezone;
// A timezone; a political region with a ruleset regarding offsets for