iamkun / iamkun/dayjs

negative ISO8601 duration strings are not handled properly by the duration function

Open
#1,788 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
negative ISO8601 duration strings are not handled properly.
```
const durationString = dayjs.duration(-1000).toISOString(); //Results in -PT1S
expect(durationString).toEqual('-PT1S');
const actual = dayjs.duration(durationString);
expect(actual.asSeconds()).toEqual(-1); // this fails !!! results in +1
```

**Expected behavior**
Solution 1
toISOString should apply negative sign to the numeric portions of the string ( for the above eg it should return PT-1S)

Solution 2
duration should handle leading negative sign as a negative duration. ( for the above example asSeconds should return -1)

**Relates to**
https://github.com/iamkun/dayjs/issues/1700

**Information**
- Day.js Version [e.g. v1.10.7]
- OS: node/ubuntu

Contributor guide

Open the contributing guide

Research direction

No source file or test is named. Start by locating the duration parsing and toISOString entry points, then reproduce the provided -PT1S example; done means negative ISO8601 durations round-trip with asSeconds() returning -1 and regression coverage records the chosen behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.