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

[bug] Fix startOf with active timezone/UTC #2481

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
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: 0 additions & 11 deletions src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,6 @@ export default (o, c, d) => {
return result && result.value
}

const oldStartOf = proto.startOf
proto.startOf = function (units, startOf) {
if (!this.$x || !this.$x.$timezone) {
return oldStartOf.call(this, units, startOf)
}

const withoutTz = d(this.format('YYYY-MM-DD HH:mm:ss:SSS'), { locale: this.$L })
const startOfWithoutTz = oldStartOf.call(withoutTz, units, startOf)
return startOfWithoutTz.tz(this.$x.$timezone, true)
}

d.tz = function (input, arg1, arg2) {
const parseFormat = arg2 && arg1
const timezone = arg2 || arg1 || defaultTimezone
Expand Down
41 changes: 41 additions & 0 deletions test/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,44 @@ it('UTC diff in DST', () => {
expect(day1.diff(day2, 'd'))
.toBe(-3)
})

it('startOf("month") returns correct value with active Timezone', () => {
// 2023-08-01T00:00:00Z
const initial = 1692050400000
const month1 = dayjs.utc(initial).startOf('month')
const month2 = dayjs.utc(initial).tz('UTC').startOf('month')
const month3 = dayjs.utc(initial).tz('Africa/Abidjan').startOf('month')
// 2023-08-01T00:00:00+02:00
const month4 = dayjs.utc(initial).tz('Europe/Berlin').startOf('month')
// 2023-08-01T00:00:00-05:00
const month5 = dayjs.utc(initial).tz('America/Cancun').startOf('month')

expect(month1.format()).toEqual('2023-08-01T00:00:00Z')
expect(month2.format()).toEqual('2023-08-01T00:00:00Z')
expect(month3.format()).toEqual('2023-08-01T00:00:00Z')
expect(month4.format()).toEqual('2023-08-01T00:00:00+02:00')
expect(month5.format()).toEqual('2023-08-01T00:00:00-05:00')
})

it('startOf("day") returns correct value with active Timezone', () => {
// initial date = 2023-08-15T12:00:00Z
const initial = 1692100800000
// should return the same day
const day1 = dayjs.utc(initial).tz('UTC').startOf('day')
const day2 = dayjs.utc(initial).tz('Africa/Abidjan').startOf('day')
const day3 = dayjs.utc(initial).tz('Europe/Berlin').startOf('day')
const day4 = dayjs.utc(initial).tz('America/Cancun').startOf('day')

expect(day1.format()).toEqual('2023-08-15T00:00:00Z')
expect(day2.format()).toEqual('2023-08-15T00:00:00Z')
expect(day3.format()).toEqual('2023-08-15T00:00:00+02:00')
expect(day4.format()).toEqual('2023-08-15T00:00:00-05:00')

// switching days when hours are close to the timezone-offset
const day5 = dayjs.utc(initial).hour(23).tz('Europe/Berlin').startOf('day') // 2023-08-15T23:00:00Z
const day6 = dayjs.utc(initial).hour(0).tz('America/Cancun').startOf('day') // 2023-08-15T04:00:00Z


expect(day5.format()).toEqual('2023-08-16T00:00:00+02:00')
expect(day6.format()).toEqual('2023-08-14T00:00:00-05:00')
})