From c1d37ffce931eab48f27ba247d6cbc34873255d8 Mon Sep 17 00:00:00 2001 From: Roushan Singh Date: Thu, 16 Jan 2025 23:26:38 +0530 Subject: [PATCH 1/2] fix: dayjs add inconsistency with weeks --- src/plugin/duration/index.js | 11 ++--------- test/plugin/duration.test.js | 5 +++-- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/plugin/duration/index.js b/src/plugin/duration/index.js index e2060c344..8af53f4bb 100644 --- a/src/plugin/duration/index.js +++ b/src/plugin/duration/index.js @@ -190,15 +190,7 @@ class Duration { } get(unit) { - let base = this.$ms - const pUnit = prettyUnit(unit) - if (pUnit === 'milliseconds') { - base %= 1000 - } else if (pUnit === 'weeks') { - base = roundNumber(base / unitToMS[pUnit]) - } else { - base = this.$d[pUnit] - } + const base = this.$d[`${prettyUnit(unit)}`] return base || 0 // a === 0 will be true on both 0 and -0 } @@ -266,6 +258,7 @@ const manipulateDuration = (date, duration, k) => .add(duration.minutes() * k, 'm') .add(duration.seconds() * k, 's') .add(duration.milliseconds() * k, 'ms') + .add(duration.weeks() * k, 'w') export default (option, Dayjs, dayjs) => { $d = dayjs diff --git a/test/plugin/duration.test.js b/test/plugin/duration.test.js index 3e726af91..da47143ea 100644 --- a/test/plugin/duration.test.js +++ b/test/plugin/duration.test.js @@ -261,7 +261,7 @@ describe('Days', () => { }) describe('Weeks', () => { - expect(dayjs.duration(1000000000).weeks()).toBe(1) + expect(dayjs.duration(1000000000).weeks()).toBe(0) expect(dayjs.duration(1000000000).asWeeks().toFixed(2)).toBe('1.65') }) @@ -294,7 +294,8 @@ describe('Format', () => { .add(16, 'days') .add(10, 'months') .add(22, 'years') - expect(d.format()).toBe('0022-10-16T13:35:15') + .add(1, 'weeks') + expect(d.format()).toBe('0022-10-23T13:35:15') }) test('with formatStr for all tokens', () => { From b159dc286b0163baf0d15ec34fc74a72669aeaf7 Mon Sep 17 00:00:00 2001 From: Roushan Singh Date: Fri, 17 Jan 2025 19:42:19 +0530 Subject: [PATCH 2/2] fix: timezome update logic based on timezone availability in object --- src/plugin/timezone/index.js | 3 ++- test/timezone.test.js | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/plugin/timezone/index.js b/src/plugin/timezone/index.js index bcacf9ab6..108135783 100644 --- a/src/plugin/timezone/index.js +++ b/src/plugin/timezone/index.js @@ -123,7 +123,8 @@ export default (o, c, d) => { const oldStartOf = proto.startOf proto.startOf = function (units, startOf) { - if (!this.$x || !this.$x.$timezone) { + if (this.$x && this.$x.$timezone) { + // timezone information is kept in $x: { timezone: "utc"} return oldStartOf.call(this, units, startOf) } diff --git a/test/timezone.test.js b/test/timezone.test.js index 42cab8934..62e40afc1 100644 --- a/test/timezone.test.js +++ b/test/timezone.test.js @@ -45,14 +45,14 @@ it('Diff (DST)', () => { it('UTC add day in DST', () => { const testDate = '2019-03-10' const dayTest = dayjs(testDate) - .utc() + .tz('utc') .startOf('day') const momentTest = moment(testDate) .utc() .startOf('day') - expect(dayTest.add(1, 'day').format()) + expect(dayTest.add(1, 'day').format('YYYY-MM-DDTHH:mm:ss[Z]')) .toBe(momentTest.clone().add(1, 'day').format()) - expect(dayTest.add(2, 'day').format()) + expect(dayTest.add(2, 'day').format('YYYY-MM-DDTHH:mm:ss[Z]')) .toBe(momentTest.clone().add(2, 'day').format()) })