Weekday.daysUntil
Closed this issue · 1 comments
dfrei commented
Nice library !!
I think there is a little bug. The test fails for wed.daysUntil(.mon)
and wed.daysUntil(.sun)
.
test "daysUntil" {
const wed: Weekday = .wed;
try std.testing.expectEqual(0, wed.daysUntil(.wed));
try std.testing.expectEqual(6, wed.daysUntil(.tue));
try std.testing.expectEqual(5, wed.daysUntil(.mon));
try std.testing.expectEqual(4, wed.daysUntil(.sun));
}
Probably it has something to do with -%
or +%
because u3
has 8 values, not 7.
rockorager commented
Ooh good find.
This is because the algorithm assumes a larger integer than a u3. It's relying
on underflow to work:
https://howardhinnant.github.io/date_algorithms.html#weekday_difference
For Wednesday -> Monday, the math is:
Monday (1) - Wednesday (3) = -2
…-2 as a u3 = 6, but we are checking if the difference is <= 6 so we return 6
instead of adding 7.
If we do this as a u8, -2 is 254. Add 7 with overflow, and we get 5.