Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: duration plugin - MILLISECONDS_A_MONTH const calculation #2362

Merged
merged 3 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../../constant'

const MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365
const MILLISECONDS_A_MONTH = MILLISECONDS_A_DAY * 30
const MILLISECONDS_A_MONTH = MILLISECONDS_A_YEAR / 12

const durationRegex = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/

Expand Down
5 changes: 3 additions & 2 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ describe('Parse ISO string', () => {
it('ISO string with week', () => {
const d = dayjs.duration('P2M3W4D')
expect(d.toISOString()).toBe('P2M25D')
expect(d.asDays()).toBe(85) // moment 85, count 2M as 61 days
expect(d.asWeeks()).toBe(12.142857142857142) // moment 12.285714285714286
expect(d.asDays()).toBe(85.83333333333333) // moment 86, count 2M as 61 days
expect(d.asWeeks()).toBe(12.261904761904763) // moment 12.285714285714286
expect(d.asMonths()).toBe(2.8219178082191783) // moment 2.8213721020965523
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be better to not have different results from moment.js.

This will cause a huge problem during migration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already a huge difference with moment.js right now, after my changes it's very close to moment.js result, you can check.

e.g.
existing code
days --> 85 --> moment.js 86
weeks --> 12.142857142857142 --> moment.js 12.285714285714286
months --> 2.8333333333333335 --> moment.js 2.8213721020965523

with my changes (improved and close to moment.js now)
days --> 85.83333333333333
weeks --> 12.261904761904763
months --> 2.8219178082191783

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems you are correct

})
it('Invalid ISO string', () => {
expect(dayjs.duration('Invalid').toISOString()).toBe('P0D')
Expand Down