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

fix: offset calculation of timezone plugin #2227

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ export default (o, c, d) => {
const oldOffset = this.utcOffset()
const date = this.toDate()
const target = date.toLocaleString('en-US', { timeZone: timezone })
const diff = Math.round((date - new Date(target)) / 1000 / 60)
const targetDate = d(target, 'M/D/YYYY H:mm:ss A')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use d(target) directlly?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we can't use it, because parseDate method returns new Date (https://github.com/iamkun/dayjs/blob/dev/src/index.js#L79), which is the reason of this PR and in case of using customParseFormat plugin without passing format argument it calls original parseDate method under the hood https://github.com/iamkun/dayjs/blob/dev/src/plugin/customParseFormat/index.js#L257. So my fix will work only when using customParseFormat plugin, what do you think about it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, that's my concern. If we implement it in this way, the timezone plugin must rely on customParseFormat plugin to work that our users may get confused

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got your point. Maybe I can find a better solution to calculate timezone offset not to depend on some APIs support.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamkun Could you please check the new solution? I am using formatToParts method of Intl.DateTimeFormat instance and getting year, month, day, etc. from its result. What do you think? I tried this in my React Native environment and it works perfectly. All tests are passed correctly as well.

const diff = Math.round(this.diff(targetDate, 'ms') / 1000 / 60)
Pareder marked this conversation as resolved.
Show resolved Hide resolved
let ins = d(target).$set(MS, this.$ms)
.utcOffset((-Math.round(date.getTimezoneOffset() / 15) * 15) - diff, true)
if (keepLocalTime) {
Expand Down