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

Conversation

Pareder
Copy link

@Pareder Pareder commented Feb 5, 2023

Update calculation of timezone offset. This change was caused by this issue #2207. The thing is that Hermes in React Native doesn't fully support Intl API (facebook/hermes#23). Let me explain.

This code:

const target = date.toLocaleString('en-US', { timeZone: timezone })

works correct, it returns the date in en-US format, but after that when returned string is passed to Date constructor it returns Date { NaN }:

const target = date.toLocaleString('en-US', { timeZone: timezone })
new Date(target) // Date { NaN }

Date constructor cannot recognise this format, so calculation of difference is broken. And I think that we can use dayjs to do backward formatting.

Note: in my opinion we can improve somehow the calculation of timezone offset not to depend on some APIs support (get rid of Date.toLocaleString).

@codecov
Copy link

codecov bot commented Feb 11, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (2254635) 100.00% compared to head (4567b8d) 100.00%.

❗ Current head 4567b8d differs from pull request most recent head 5c7598a. Consider uploading reports for the commit 5c7598a to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##               dev     #2227    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files          183       183            
  Lines         2249      2112   -137     
  Branches       636       555    -81     
==========================================
- Hits          2249      2112   -137     
Files Changed Coverage Δ
src/plugin/timezone/index.js 100.00% <100.00%> (ø)

... and 5 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@iamkun
Copy link
Owner

iamkun commented Feb 11, 2023

not sure if this is a correct fix, is there a way to add some test cases to prove it right?

@Pareder
Copy link
Author

Pareder commented Feb 12, 2023

@iamkun None of timezone tests is broken, what test do you want me to add? Do you want to simulate Date { NaN } behavior or what?

@@ -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.

@Pareder Pareder requested a review from iamkun February 20, 2023 17:48
@Pareder
Copy link
Author

Pareder commented Sep 20, 2023

@iamkun Could you please review it?

@KingAmo
Copy link

KingAmo commented Apr 7, 2024

any update on this ?

@artemkhalygov
Copy link

@iamkun Please merge this pull request

@jgtorrejon
Copy link

Tried applying a patch but still does not work on iOS 😔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants