CFDate: return the ISO 8601 week of the year - #128
Open
DTW-Thalion wants to merge 1 commit into
Open
Conversation
CFAbsoluteTimeGetWeekOfYear returned d / 7 % 52, which ignored the day of the week and could never reach week 53. Compute the ISO 8601 week number, where weeks start on Monday and week 1 contains the year first Thursday, so days near a year boundary fall in a week of the adjacent year.
DTW-Thalion
force-pushed
the
fix/cfdate-weekofyear
branch
from
July 24, 2026 15:22
7b02f4b to
36c3358
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CFAbsoluteTimeGetWeekOfYear computed the week as d / 7 % 52. That ignores which day of the week the year starts on and can never return week 53, so almost every date came back with the wrong week (for example 2004-12-31 gave 0 instead of 53).
Compute the ISO 8601 week number instead: weeks start on Monday and week 1 is the week containing the year's first Thursday, so days at the start or end of a year can belong to a week of the adjacent year. A helper returns whether a year has 52 or 53 ISO weeks.
Tests/CFDate/weekofyear.m covers the first weeks of the year, the mid-January boundaries, and the year-end rollovers for both a 52-week and a 53-week year, verified against Apple CoreFoundation.