fix: customParseFormat strict mode bug when parsing with timezones (#929)
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 Report
All modified and coverable lines are covered by tests :white_check_mark:
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
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
Would you please add the relevant test, please?
@iamkun I've added the missing tests. Totally forgot about that.
Thanks for the PR, need some time to review this.
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.
@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?
@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
Any news on this?
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.
@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.
Hello @iamkun, are you willing to accept this PR? If not, what's missing?
Any news on this PR? I see there are conflicts again...