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: customParseFormat strict mode bug when parsing with timezones (#929) #1467

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from

Conversation

ivansieder
Copy link

@ivansieder ivansieder commented Apr 21, 2021

The parseFormattedInput function returns a UTC object for dates with timezones. When using dayjs().format() it usually formats the date with the current timezone. This fix manually removes the current timezone's offset and manually adds the originally parsed offset. Then the timezone part (+/-xx:xx or +-xxxx) will be removed from both strings and the timestamp without the timezone can be compared correctly.

Resolves #929

@codecov
Copy link

codecov bot commented Apr 21, 2021

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (b5a1391) to head (b2075b3).
Report is 179 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff            @@
##               dev     #1467   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          179       179           
  Lines         1996      2008   +12     
  Branches       507       513    +6     
=========================================
+ Hits          1996      2008   +12     

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

const currentOffset = this.utcOffset()
if (isStrict) {
if (
(parsedOffsetMilliseconds !== undefined && date.replace(/[+-]\d\d:?\d\d$/, '') !== this.subtract(currentOffset, 'minute').subtract(parsedOffsetMilliseconds, 'millisecond').format(format.replace(/(ZZ|ZZZ)$/, '')))

Choose a reason for hiding this comment

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

Why is ZZZ (triple zed) here? I don't see in the docs: https://day.js.org/docs/en/parse/string-format

@iamkun
Copy link
Owner

iamkun commented May 26, 2021

Would you please add the relevant test, please?

@ivansieder
Copy link
Author

@iamkun I've added the missing tests. Totally forgot about that.

@iamkun
Copy link
Owner

iamkun commented Jun 2, 2021

Thanks for the PR, need some time to review this.

@iamkun
Copy link
Owner

iamkun commented Jun 2, 2021

Just a small concern, can we re-order the logic a little bit?

Cause to me, it's a little complicated to understand the code of this PR.

@ivansieder
Copy link
Author

@iamkun I've tried to keep the order of the logic similar to how it was before this PR in order to make merging easier and keep the logic mostly in the same place as it was before. What kind of refactoring did you think about?

src/plugin/customParseFormat/index.js Outdated Show resolved Hide resolved
@ivansieder
Copy link
Author

@imwh0im I've updated the code now with the dev branch and included your suggested changes (changed the variable naming a little bit). Now waiting for further feedback from @iamkun

@BreakBB
Copy link

BreakBB commented May 9, 2022

Any news on this?

@BePo65
Copy link
Contributor

BePo65 commented May 30, 2022

Perhaps it would be a good idea to allow for all time zone formats that dayjs supports in line 253?
In customParseFormat the definition for zone strings is const matchOffset = /[+-]\d\d:?(\d\d)?|Z/ // +00:00 -00:00 +0000 or -0000 +00 or Z.

@BePo65
Copy link
Contributor

BePo65 commented May 31, 2022

@iamkun : the check for strictness (even in the original implementation) makes use of the 'format' function of dayjs. But the 'format' definition for the token 'Do' (Day of Month with ordinal) requires the AdvancedFormat plugin.

Probably it would be a good idea to add a hint to the 'String + Format' documentation saying the using the 'Do' format requires the AdvancedFormat plugin to work.

@EriksonBahr
Copy link

Hello @iamkun, are you willing to accept this PR? If not, what's missing?

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.

customParseFormat strict mode bug
7 participants