diff options
author | Linus Groh <mail@linusgroh.de> | 2021-11-15 21:21:43 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-11-15 21:33:26 +0000 |
commit | a757f3f421df252937295ba8f453551682de0bed (patch) | |
tree | 9519392cb7a13ef5b0c6b6e65eb3b912e6bd37a5 /Userland/Libraries/LibJS | |
parent | b7af536f9bc925bf29de9b2d032b9c95c9bb55bc (diff) | |
download | serenity-a757f3f421df252937295ba8f453551682de0bed.zip |
LibJS: Fix leap year check in to_iso_week_of_year() for week < 1
When the resulting week is in the previous year, we need to check if the
previous year is a leap year and can potentially have 53 weeks, instead
of the given year.
Also added a comment to briefly explain what's going on, as it took me a
while to figure out.
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index 1e40dffff7..e01a683834 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -700,8 +700,12 @@ u8 to_iso_week_of_year(i32 year, u8 month, u8 day) auto week = (day_of_year - day_of_week + 10) / 7; if (week < 1) { + // NOTE: The resulting week is actually part of the previous year. If that year ends with a + // Thursday (i.e. the first day of the given year is a Friday, or day 5), or the previous + // year is a leap year and ends with a Friday (i.e. the first day of the given year is a + // Saturday, or day 6), it has 53 weeks, and 52 weeks otherwise. auto day_of_jump = to_iso_day_of_week(year, 1, 1); - if (day_of_jump == 5 || (is_iso_leap_year(year) && day_of_jump == 6)) + if (day_of_jump == 5 || (is_iso_leap_year(year - 1) && day_of_jump == 6)) return 53; else return 52; |