Skip to content

Commit

Permalink
Fixes DateComponents.dayOfWeek fails with arithmetic overflow on …
Browse files Browse the repository at this point in the history
…Saturdays #4
  • Loading branch information
Gekctek committed Nov 20, 2023
1 parent 8a51a5c commit 8386b5d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/Components.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,11 @@ module Module {
let yearWithoutCentury = year % 100;
let century = year / 100;
let h = day + 13 * (month + 1) / 5 + yearWithoutCentury + yearWithoutCentury / 4 + century / 4 + 5 * century;
Int.abs(h) % 7 - 1;
let position = Int.abs(h) % 7;
switch (position) {
case (0) 6; // Wrap around -1 (Saturday)
case (i) i - 1;
};
};

public func daysInYear(year : Int) : Nat {
Expand Down
16 changes: 16 additions & 0 deletions test/Components.test.mo
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,22 @@ test(
dayOfYear = 114;
};
},
{
components = {
year = 2_023;
month = 11;
day = 18;
hour = 0;
minute = 0;
nanosecond = 0;
};
expected = {
weekOfYear = 47;
weekYear = 2023;
dayOfWeek = #saturday;
dayOfYear = 322;
};
},
];
for (testCase in Iter.fromArray(testCases)) {
let actual : Components.DayOfWeek = Components.dayOfWeek(testCase.components);
Expand Down

0 comments on commit 8386b5d

Please sign in to comment.