You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Time format/parse treat week numbers inconsistently #62
Using Sunday-based week and day-of-week numbers, the format and parse methods treat week zero inconsistently. This can cause round-tripping of a date to fail. Based on the documentation, my understanding is that week one commences with the first Sunday of the year, and days prior to that lie in week zero. If so, it looks like the format method is buggy.
In most cases the flooring performed inside timeSunday.count shifts the starting date value to the prior Sunday (the week zero boundary). However, if the first day of the year is itself a Sunday, the starting date value gets floored to the week one boundary instead, resulting in an off-by-one-week error. One solution might be to add logic that suitably prepares the first argument to count so that it maps to the week zero boundary.
(Similar reasoning then applies for Monday-based weeks.)
I agree that d3.timeFormat looks wrong here; 2012-01-01, a Sunday, should be considered week 1. d3.timeParse appears to be parsing the week number correctly.
Strangely, though, I think d3.utcParse is showing a separate error. I would expect d3.utcParse("%Y %U %w")("2012 01 0") to return Sunday 2012-01-01, but it returns Monday 2012-01-02.
Activity
jheer commentedon Oct 21, 2019
I believe the error stems from the use of
timeSundayandtimeMonday:For example:
In most cases the flooring performed inside
timeSunday.countshifts the starting date value to the prior Sunday (the week zero boundary). However, if the first day of the year is itself a Sunday, the starting date value gets floored to the week one boundary instead, resulting in an off-by-one-week error. One solution might be to add logic that suitably prepares the first argument tocountso that it maps to the week zero boundary.(Similar reasoning then applies for Monday-based weeks.)
mbostock commentedon Oct 23, 2019
This certainly looks like a bug! Thanks for the report.
mbostock commentedon Oct 23, 2019
Test cases as a notebook: https://observablehq.com/d/d1cc30312fd3d256
I agree that d3.timeFormat looks wrong here; 2012-01-01, a Sunday, should be considered week 1. d3.timeParse appears to be parsing the week number correctly.
Strangely, though, I think d3.utcParse is showing a separate error. I would expect
d3.utcParse("%Y %U %w")("2012 01 0")to return Sunday 2012-01-01, but it returns Monday 2012-01-02.