From 36bdbd0259d4d054aba08a22dbf71b06d4092098 Mon Sep 17 00:00:00 2001 From: Alexander Ulaev Date: Thu, 15 Jun 2023 16:03:16 +0400 Subject: [PATCH 1/3] fix: Duration add/subtract #2336 --- src/plugin/duration/index.js | 30 ++++++++++++++++++++++++++++-- test/plugin/duration.test.js | 12 ++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/plugin/duration/index.js b/src/plugin/duration/index.js index 5d4cc6aa8..f9eceeb92 100644 --- a/src/plugin/duration/index.js +++ b/src/plugin/duration/index.js @@ -257,6 +257,24 @@ class Duration { asYears() { return this.as('years') } } +const addDuration = (date, duration) => + date.add(duration.years(), 'year') + .add(duration.months(), 'month') + .add(duration.days(), 'day') + .add(duration.hours(), 'hour') + .add(duration.minutes(), 'minute') + .add(duration.seconds(), 'second') + .add(duration.milliseconds(), 'millisecond') + +const subtractDuration = (date, duration) => + date.subtract(duration.years(), 'year') + .subtract(duration.months(), 'month') + .subtract(duration.days(), 'day') + .subtract(duration.hours(), 'hour') + .subtract(duration.minutes(), 'minute') + .subtract(duration.seconds(), 'second') + .subtract(duration.milliseconds(), 'millisecond') + export default (option, Dayjs, dayjs) => { $d = dayjs $u = dayjs().$utils() @@ -268,12 +286,20 @@ export default (option, Dayjs, dayjs) => { const oldAdd = Dayjs.prototype.add const oldSubtract = Dayjs.prototype.subtract + Dayjs.prototype.add = function (value, unit) { - if (isDuration(value)) value = value.asMilliseconds() + if (isDuration(value)) { + return addDuration(this, value) + } + return oldAdd.bind(this)(value, unit) } + Dayjs.prototype.subtract = function (value, unit) { - if (isDuration(value)) value = value.asMilliseconds() + if (isDuration(value)) { + return subtractDuration(this, value) + } + return oldSubtract.bind(this)(value, unit) } } diff --git a/test/plugin/duration.test.js b/test/plugin/duration.test.js index 8a7e0e6cd..1d9616fb8 100644 --- a/test/plugin/duration.test.js +++ b/test/plugin/duration.test.js @@ -184,6 +184,12 @@ test('Add duration', () => { expect(a.add(days).format('YYYY-MM-DD')).toBe('2020-10-03') }) +test('Add duration month', () => { + const a = dayjs('2023-02-01') + const days = dayjs.duration(1, 'month') + expect(a.add(days).format('YYYY-MM-DD')).toBe('2023-03-01') +}) + describe('Subtract', () => { const a = dayjs.duration(3, 'days') const b = dayjs.duration(2, 'days') @@ -196,6 +202,12 @@ test('Subtract duration', () => { expect(a.subtract(days).format('YYYY-MM-DD')).toBe('2020-10-18') }) +test('Subtract duration month', () => { + const a = dayjs('2023-03-01') + const days = dayjs.duration(1, 'month') + expect(a.subtract(days).format('YYYY-MM-DD')).toBe('2023-02-01') +}) + describe('Seconds', () => { expect(dayjs.duration(500).seconds()).toBe(0) expect(dayjs.duration(1500).seconds()).toBe(1) From b6a5a448a50aaab949204d33afbf5be4cd69affb Mon Sep 17 00:00:00 2001 From: Alexander Ulaev Date: Fri, 23 Jun 2023 09:22:22 +0400 Subject: [PATCH 2/3] fix: #2336 combine add and subtract --- src/plugin/duration/index.js | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/plugin/duration/index.js b/src/plugin/duration/index.js index f9eceeb92..77e4d1349 100644 --- a/src/plugin/duration/index.js +++ b/src/plugin/duration/index.js @@ -257,23 +257,14 @@ class Duration { asYears() { return this.as('years') } } -const addDuration = (date, duration) => - date.add(duration.years(), 'year') - .add(duration.months(), 'month') - .add(duration.days(), 'day') - .add(duration.hours(), 'hour') - .add(duration.minutes(), 'minute') - .add(duration.seconds(), 'second') - .add(duration.milliseconds(), 'millisecond') - -const subtractDuration = (date, duration) => - date.subtract(duration.years(), 'year') - .subtract(duration.months(), 'month') - .subtract(duration.days(), 'day') - .subtract(duration.hours(), 'hour') - .subtract(duration.minutes(), 'minute') - .subtract(duration.seconds(), 'second') - .subtract(duration.milliseconds(), 'millisecond') +const manipulateDuration = (date, duration, k) => + date.add(duration.years() * k, 'year') + .add(duration.months() * k, 'month') + .add(duration.days() * k, 'day') + .add(duration.hours() * k, 'hour') + .add(duration.minutes() * k, 'minute') + .add(duration.seconds() * k, 'second') + .add(duration.milliseconds() * k, 'millisecond') export default (option, Dayjs, dayjs) => { $d = dayjs @@ -289,7 +280,7 @@ export default (option, Dayjs, dayjs) => { Dayjs.prototype.add = function (value, unit) { if (isDuration(value)) { - return addDuration(this, value) + return manipulateDuration(this, value, 1) } return oldAdd.bind(this)(value, unit) @@ -297,7 +288,7 @@ export default (option, Dayjs, dayjs) => { Dayjs.prototype.subtract = function (value, unit) { if (isDuration(value)) { - return subtractDuration(this, value) + return manipulateDuration(this, value, -1) } return oldSubtract.bind(this)(value, unit) From 705d9b368531ca9317b6ac75f47ce5471b30feab Mon Sep 17 00:00:00 2001 From: Alexander Ulaev Date: Sat, 24 Jun 2023 11:03:03 +0400 Subject: [PATCH 3/3] fix: Duration add/subtract #2336 --- src/plugin/duration/index.js | 14 +++++++------- test/plugin/duration.test.js | 16 ++++++---------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/plugin/duration/index.js b/src/plugin/duration/index.js index 77e4d1349..26bc75e34 100644 --- a/src/plugin/duration/index.js +++ b/src/plugin/duration/index.js @@ -258,13 +258,13 @@ class Duration { } const manipulateDuration = (date, duration, k) => - date.add(duration.years() * k, 'year') - .add(duration.months() * k, 'month') - .add(duration.days() * k, 'day') - .add(duration.hours() * k, 'hour') - .add(duration.minutes() * k, 'minute') - .add(duration.seconds() * k, 'second') - .add(duration.milliseconds() * k, 'millisecond') + date.add(duration.years() * k, 'y') + .add(duration.months() * k, 'M') + .add(duration.days() * k, 'd') + .add(duration.hours() * k, 'h') + .add(duration.minutes() * k, 'm') + .add(duration.seconds() * k, 's') + .add(duration.milliseconds() * k, 'ms') export default (option, Dayjs, dayjs) => { $d = dayjs diff --git a/test/plugin/duration.test.js b/test/plugin/duration.test.js index 1d9616fb8..4a4753931 100644 --- a/test/plugin/duration.test.js +++ b/test/plugin/duration.test.js @@ -182,12 +182,10 @@ test('Add duration', () => { const a = dayjs('2020-10-01') const days = dayjs.duration(2, 'days') expect(a.add(days).format('YYYY-MM-DD')).toBe('2020-10-03') -}) -test('Add duration month', () => { - const a = dayjs('2023-02-01') - const days = dayjs.duration(1, 'month') - expect(a.add(days).format('YYYY-MM-DD')).toBe('2023-03-01') + const b = dayjs('2023-02-01 00:00:00') + const p = dayjs.duration('P1Y1M1DT1H1M1S') + expect(b.add(p).format('YYYY-MM-DD HH:mm:ss')).toBe('2024-03-02 01:01:01') }) describe('Subtract', () => { @@ -200,12 +198,10 @@ test('Subtract duration', () => { const a = dayjs('2020-10-20') const days = dayjs.duration(2, 'days') expect(a.subtract(days).format('YYYY-MM-DD')).toBe('2020-10-18') -}) -test('Subtract duration month', () => { - const a = dayjs('2023-03-01') - const days = dayjs.duration(1, 'month') - expect(a.subtract(days).format('YYYY-MM-DD')).toBe('2023-02-01') + const b = dayjs('2023-03-02 02:02:02') + const p = dayjs.duration('P1Y1M1DT1H1M1S') + expect(b.subtract(p).format('YYYY-MM-DD HH:mm:ss')).toBe('2022-02-01 01:01:01') }) describe('Seconds', () => {