hare

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

commit 05eb7293847b1be053a932e5d7c47384ac903703
parent 7396d5b7fcd0fdb68841bd66456e57b14ff96374
Author: Byron Torres <b@torresjrjr.com>
Date:   Sat, 13 Nov 2021 21:25:47 +0000

add init functions

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

Diffstat:
Mdatetime/calendar.ha | 1+
Mdatetime/date.ha | 17+++++++++++++++--
Mdatetime/datetime.ha | 29+++++++++++++++--------------
Mdatetime/time.ha | 7+++++++
4 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/datetime/calendar.ha b/datetime/calendar.ha @@ -30,6 +30,7 @@ export fn conv_moment_datetime(m: chrono::moment, dt: *datetime) void = { year = 0, month = 0, day = 0, + ... }); const lt = conv_time_localtime(m.time); diff --git a/datetime/date.ha b/datetime/date.ha @@ -14,6 +14,17 @@ export type localdate = struct { yearday: (void | int), }; +export fn init_date() localdate = localdate { + era = void, + year = void, + month = void, + day = void, + weekyear = void, + week = void, + weekday = void, + yearday = void, +}; + // TODO: The following function can be split up for efficiency. // calc_y, calc_m, calc_d, etc. @@ -88,13 +99,15 @@ export fn conv_epochal_localdate( || want.day is int ) { calc_ymd(e, date); - } else if ( + }; + if ( want.year is int || want.week is int || want.weekday is int ) { calc_ywd(e, date); - } else if ( + }; + if ( want.year is int || want.yearday is int ) { diff --git a/datetime/datetime.ha b/datetime/datetime.ha @@ -21,6 +21,13 @@ export type datetime = struct { loc: locality, }; +// Returns a [[datetime]], with all subfields initialised with void +export fn init_datetime() datetime = datetime { + date = init_date(), + time = init_time(), + loc = chrono::local, +}; + // Creates a new moment // // // 1995 July 18th 09:16:00.000 @@ -37,20 +44,7 @@ export fn new_moment( nsec: int, loc: locality, ) (chrono::moment | errors::invalid) = { - const dt = datetime { - date = localdate { - year = year, - month = month, - day = day, - }, - time = localtime { - hour = hour, - min = min, - sec = sec, - nsec = nsec, - }, - loc = loc, - }; + const dt = new_datetime(year, month, day, hour, min, sec, nsec, loc)?; const m = conv_datetime_moment(dt)?; return m; }; @@ -73,9 +67,15 @@ export fn new_datetime( ) (datetime | errors::invalid) = { const dt = datetime { date = localdate { + era = if (year >= EPOCH_COMMONERA) 1 else 0, year = year, month = month, day = day, + weekyear = void, + week = void, + weekday = void, + yearday = void, + }, time = localtime { hour = hour, @@ -118,6 +118,7 @@ export fn now_datetime() datetime = { year = 0, month = 0, day = 0, + ... }); const dt = datetime { date = ld, diff --git a/datetime/time.ha b/datetime/time.ha @@ -9,6 +9,13 @@ export type localtime = struct { nsec: (void | int), }; +export fn init_time() localtime = localtime { + hour = void, + min = void, + sec = void, + nsec = void, +}; + // Converts a [[time::duration]] to a [[localtime]] export fn conv_time_localtime(t: time::duration) localtime = { const lt = localtime {