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

Fixes Issue #2810: Inconsistent Behavior with add after startOf call #2812

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 2 additions & 9 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

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 @@ -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')
})

Expand Down Expand Up @@ -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', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

Expand Down