Skip to content

Time format/parse treat week numbers inconsistently #62

Closed

Description

@jheer

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.

d3.timeFormat('%Y %U %w')(new Date(2012, 0, 1)) // "2012 00 0" -> should be "2012 01 0"?
d3.timeParse('%Y %U %w')('2012 00 0').toLocaleString() // "12/25/2011, 12:00:00 AM"
d3.timeParse('%Y %U %w')('2012 01 0').toLocaleString() // "1/1/2012, 12:00:00 AM"

d3.utcFormat('%Y %U %w')(new Date(Date.UTC(2012, 0, 1))) // "2012 00 0"
d3.utcParse('%Y %U %w')('2012 00 0').toUTCString() // "Sun, 25 Dec 2011 00:00:00 GMT"

Using Monday-based week and day-of-week numbers exhibits the same problem:

d3.timeFormat('%Y %W %u')(new Date(2018, 0, 1)) // "2018 00 1" -> should be "2018 01 1"?
d3.timeParse('%Y %W %u')('2018 00 1').toLocaleString() // "12/25/2017, 12:00:00 AM"
d3.timeParse('%Y %W %u')('2018 01 1').toLocaleString() // "1/1/2018, 12:00:00 AM"

d3.utcFormat('%Y %W %u')(new Date(Date.UTC(2018, 0, 1))) // "2018 00 1"
d3.utcParse('%Y %W %u')('2018 00 1').toUTCString() // "Mon, 25 Dec 2017 00:00:00 GMT"

Activity

  1. jheer commented on Oct 21, 2019

    @jheer
    ContributorAuthor

    I believe the error stems from the use of timeSunday and timeMonday:

    For example:

    function formatWeekNumberSunday(d, p) {
      return pad(timeSunday.count(timeYear(d), d), p, 2);
    }

    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.)

  2. mbostock commented on Oct 23, 2019

    @mbostock
    Member

    This certainly looks like a bug! Thanks for the report.

  3. mbostock commented on Oct 23, 2019

    @mbostock
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions