d3-time-format icon indicating copy to clipboard operation
d3-time-format copied to clipboard

formatUTCWeekNumberISO and formatWeekNumberISO return incorrect results at edge of the year

Open 28raining opened this issue 2 years ago • 4 comments

These functions work by counting how many Thursdays have occurred up to this date

function formatUTCWeekNumberISO(d, p) { d = UTCdISO(d); return pad(utcThursday.count(utcYear(d), d) + (utcYear(d).getUTCDay() === 4), p, 2); }

Unfortunately the argument d is re-assigned and can cause the year to roll over, so that utcYear(d) returns a different year because UTC is in a different timezone

Codepen: https://codepen.io/28raining/pen/VwqYaKL?editors=1111

Using a separate variable avoids this

function formatUTCWeekNumberISO(d, p) { var dUTC = UTCdISO(d); return pad(utcThursday.count(utcYear(d), dUTC ) + (utcYear(d).getUTCDay() === 4), p, 2); }

28raining avatar Aug 20 '23 23:08 28raining

The issue may also exist in formatUTCFullYearISO and formatFullYearISO

28raining avatar Aug 20 '23 23:08 28raining

Created PR: https://github.com/d3/d3-time-format/pull/121

28raining avatar Aug 20 '23 23:08 28raining

ISO weeks do rollover, and the results shown by this codepen seem correct to me. The two last days of 2019 (Dec 30 and 31) belong to Week 01, see for example http://www.whatweekisit.org/calendar-2019.html

Fil avatar Aug 21 '23 06:08 Fil

That's really interesting! Glad you know your dates. Let me look again at the original issue and confirm it's unrelated to this.

28raining avatar Aug 22 '23 04:08 28raining