Skip to content
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
10 changes: 7 additions & 3 deletions src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ export default (o, c, d) => {
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)
let ins
if (this.$x.$timezone.toUpperCase() === 'UTC') {
ins = d(this.format('YYYY-MM-DD HH:mm:ss:SSS'), {locale: this.$L, utc: true })
} else {
ins = d(this.format('YYYY-MM-DD HH:mm:ss:SSS'), { locale: this.$L })
}
const startOfWithoutTz = oldStartOf.call(ins, units, startOf)
return startOfWithoutTz.tz(this.$x.$timezone, true)
}

Expand Down
29 changes: 29 additions & 0 deletions test/plugin/transitionDSTtoUTC.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import dayjs from '../../src'
import utc from '../../src/plugin/utc'
import timezone from '../../src/plugin/timezone'

dayjs.extend(utc)
dayjs.extend(timezone)

describe('timezone', () => {
it('should preserve UTC semantics for startOf(day) across host DST transitions', () => {
// 2024-11-03 is the DST fall-back date in America/Los_Angeles.
// When the host timezone is set to America/Los_Angeles,
// dayjs.tz(ms, 'UTC') should still behave exactly like dayjs.utc(ms).

const ms = Date.parse('2024-11-03T23:00:00.000Z')

const tzResult = dayjs
.tz(ms, 'UTC')
.startOf('day')
.valueOf()

const utcResult = dayjs
.utc(ms)
.startOf('day')
.valueOf()

expect(tzResult).toBe(utcResult)
expect(tzResult).toBe(Date.parse('2024-11-03T00:00:00.000Z'))
})
})