Skip to content

ISO 8601 parsing is not correct and erroneously uses modulo logic #4815

@ipavlic

Description

@ipavlic

Description of the Issue and Steps to Reproduce:

Given a duration >=31 days, moment.duration will incorrectly parse it with modulo logic, assuming that a month is always 31 days long.

For example, moment.duration("P32D") is converted into a 1 month 1 day moment.duration. If a duration designator is parsed in that way is added to different time instants, the absolute duration will differ, which is valid under ISO 8601. For example, adding 1 month and 1 day to February 1 is different than adding it to May 1:

  • 28+1 or 29+1 days for February
  • 31+1 days for May.

More generally, all duration elements greater than their assumed modulo are not parsed correctly. For hours, "PT36H" should not be parsed as 1 day and 12 hours, but as 36 hours, because daylight saving changes can occur.

Steps to reproduce:

var dur1 = moment.duration("P32D");
// dur.months() is 1, but should be 0
// dur.days() is 1, but should be 32

var dur2 = moment.duration("PT36H");
// dur.days() is 1, but should be 0
// dur.hours() is 12, but should be 36

Environment:

Chrome 69.0.3497.100 OSX

Mon Oct 15 2018 07:47:34 GMT-0400 (Eastern Daylight Time)
10/15/2018, 7:47:34 AM
240
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
2.22.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions