Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(daysUntil): use larger integer type for proper underflow #9

Merged
merged 1 commit into from
Sep 10, 2024

Conversation

rockorager
Copy link
Owner

Use a larger integer type for proper underflow in Weekday.daysUntil.

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.

Note that this exposed a bug in the DST calculation for fixed posix
timezones as well. The days var suffered from an off-by-one bug. It is
supposed to be the day of the month (1-31), NOT a zero-indexed day.

Fixes: #8

Use a larger integer type for proper underflow in Weekday.daysUntil.

    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.

Note that this exposed a bug in the DST calculation for fixed posix
timezones as well. The `days` var suffered from an off-by-one bug. It is
supposed to be the day of the month (1-31), NOT a zero-indexed day.

Fixes: #8
@rockorager rockorager merged commit 4a41cf3 into main Sep 10, 2024
4 checks passed
@rockorager rockorager deleted the fix-days-until branch October 31, 2024 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Weekday.daysUntil
1 participant