hare

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

commit c2032dda507daac991d54b41e55ffce40f8936a9
parent 3b65a1d7ef7e9a1db9ee7f142476926cf745ee76
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Tue,  3 May 2022 09:32:42 -0500

datetime: fix week of the year calculation and add test

January 1st, 2018 was a Monday. The previous method would return
the week as 0, when it should be 1.

Signed-off-by: Nihal Jere <nihal@nihaljere.xyz>

Diffstat:
Mdatetime/date.ha | 29++++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/datetime/date.ha b/datetime/date.ha @@ -180,7 +180,7 @@ fn calc_isoweek(y: int, w: int, wd: int, yd: int) int = { // 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; + return (yd + 7 - wd) / 7; }; // Calculates the weekday, given a epochal day, @@ -502,6 +502,33 @@ fn calc_epochal_from_yd(y: int, yd: int) (chrono::epochal | invalid) = { }; }; +@test fn calc_week() void = { + const cases = [ + ((1, 1), 1), + ((1, 2), 0), + ((1, 3), 0), + ((1, 4), 0), + ((1, 5), 0), + ((1, 6), 0), + ((1, 7), 0), + ((21, 2), 3), + ((61, 3), 9), + ((193, 5), 27), + ((229, 1), 33), + ((286, 4), 41), + ((341, 7), 48), + ((365, 6), 52), + ((366, 1), 53), + ]; + + for (let i = 0z; i < len(cases); i += 1) { + const params = cases[i].0; + const expect = cases[i].1; + const actual = calc_week(params.0, params.1); + assert(expect == actual, "week miscalculation"); + }; +}; + @test fn calc_weekday() void = { const cases = [ (-999999, 4), // -0768-02-05