hare

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

commit 3c1b9cf1a7851a2d70a622cd1bcf5cf638fa3e8f
parent 09087b54346b22ee1524366180ca9bd2c2c7607a
Author: Byron Torres <b@torresjrjr.com>
Date:   Mon, 15 Nov 2021 17:01:28 +0000

add ymd test, correct spelling

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

Diffstat:
Mdatetime/date+test.ha | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdatetime/date.ha | 22+++++++++++-----------
2 files changed, 63 insertions(+), 11 deletions(-)

diff --git a/datetime/date+test.ha b/datetime/date+test.ha @@ -1,3 +1,55 @@ +use errors; +use time::chrono; + +@test fn calc_epochal_from_ymd() void = { + const cases = [ + ((-0768, 02, 05), -999999, false), + ((-0001, 12, 31), -719529, false), + (( 0000, 01, 01), -719528, false), + (( 0000, 01, 02), -719527, false), + (( 0000, 12, 31), -719163, false), + (( 0001, 01, 01), -719162, false), + (( 0001, 01, 02), -719161, false), + (( 1965, 03, 23), -1745, false), + (( 1969, 12, 31), -1, false), + (( 1970, 01, 01), 0, false), + (( 1970, 01, 02), 1, false), + (( 1999, 12, 31), 10956, false), + (( 2000, 01, 01), 10957, false), + (( 2000, 01, 02), 10958, false), + (( 2038, 01, 18), 24854, false), + (( 2038, 01, 19), 24855, false), + (( 2038, 01, 20), 24856, false), + (( 2243, 10, 17), 100000, false), + (( 4707, 11, 28), 999999, false), + (( 4707, 11, 29), 1000000, false), + ((29349, 01, 25), 9999999, false), + + (( 1970,-99,-99), 0, true), + (( 1970, -9, -9), 0, true), + (( 1970, -1, -1), 0, true), + (( 1970, 00, 00), 0, true), + (( 1970, 00, 01), 0, true), + (( 1970, 01, 99), 0, true), + (( 1970, 99, 99), 0, true), + ]; + for (let i = 0z; i < len(cases); i += 1) { + const params = cases[i].0; + const expect = cases[i].1; + const should_error = cases[i].2; + const actual = calc_epochal_from_ymd( + params.0, params.1, params.2, + ); + + if (should_error) { + assert(actual is errors::invalid, "invalid date accepted"); + } else { + assert(actual is chrono::epochal, "valid date not accepted"); + assert(actual as chrono::epochal == expect, "epochal miscalculation"); + }; + }; +}; + @test fn calc_ymd() void = { const cases = [ (-999999, (-0768, 02, 05)), diff --git a/datetime/date.ha b/datetime/date.ha @@ -128,7 +128,7 @@ fn calc_isoweekyear(y: int, m: int, d: int, wd: int) int = { }; }; -// Calculates the ISO week of a [[chrono::epochal]] +// Calculates a ISO week, given a year, week, Gregorian weekday, and yearday fn calc_isoweek(y: int, w: int, wd: int, yd: int) int = { const jan1wd = (yd - wd + 7) % 7 + 1; @@ -161,7 +161,7 @@ fn calc_isoweek(y: int, w: int, wd: int, yd: int) int = { }; // Calculates the week within a Gregorian year [0..53], -// given a yearday and weekday. +// given a yearday and Gregorian weekday. // All days in a new year before the year's first Monday belong to week 0. fn calc_week(yd: int, wd: int) int = { return (5 + yd - wd) / 7; @@ -188,7 +188,7 @@ fn is_leap_year(y: int) bool = { else true; }; -// Converts a [[chrono::epocal]] to a [[localdate]]. The fields in "date" are +// Converts a [[chrono::epochal]] to a [[localdate]]. The fields in "date" are // populated acording to which fields in "want" are non-void. // // let date = localdate { ... }; @@ -229,8 +229,8 @@ export fn conv_epochal_localdate( }; }; -// Converts a year-month-day date into an [[chrono::epocal]] -fn epocal_from_ymd(y: int, m: int, d: int) (chrono::epochal | errors::invalid) = { +// Converts a year-month-day date into an [[chrono::epochal]] +fn calc_epochal_from_ymd(y: int, m: int, d: int) (chrono::epochal | errors::invalid) = { // Algorithm adapted from: // https://en.wikipedia.org/wiki/Julian_day // @@ -247,19 +247,19 @@ fn epocal_from_ymd(y: int, m: int, d: int) (chrono::epochal | errors::invalid) = }; -// Converts a year-week-weekday date into an [[chrono::epocal]] -fn epocal_from_ywd() (chrono::epochal | errors::invalid) = { +// Converts a year-week-weekday date into an [[chrono::epochal]] +fn calc_epochal_from_ywd() (chrono::epochal | errors::invalid) = { // TODO return 0; }; -// Converts a year-yearday date into an [[chrono::epocal]] -fn epocal_from_yd() (chrono::epochal | errors::invalid) = { +// Converts a year-yearday date into an [[chrono::epochal]] +fn calc_epochal_from_yd() (chrono::epochal | errors::invalid) = { // TODO return 0; }; -// Converts a [[localdate]] to a [[chrono::epocal]]. +// Converts a [[localdate]] to a [[chrono::epochal]]. // Fails if there is insufficient information in the given [[localdate]]. export fn conv_localdate_epochal(ld: localdate) (chrono::epochal | errors::invalid) = { if ( @@ -267,7 +267,7 @@ export fn conv_localdate_epochal(ld: localdate) (chrono::epochal | errors::inval && ld.month is int && ld.day is int ) { - return epocal_from_ymd( + return calc_epochal_from_ymd( ld.year: int, ld.month: int, ld.day: int,