dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

fix: customParseFormat strict mode bug when parsing with timezones (#929)

Open ivansieder opened this issue 4 years ago • 11 comments

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

ivansieder avatar Apr 21 '21 12:04 ivansieder

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.

codecov[bot] avatar Apr 21 '21 12:04 codecov[bot]

Would you please add the relevant test, please?

iamkun avatar May 26 '21 07:05 iamkun

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

ivansieder avatar May 26 '21 07:05 ivansieder

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

iamkun avatar Jun 02 '21 07:06 iamkun

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 avatar Jun 02 '21 07:06 iamkun

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

ivansieder avatar Jun 02 '21 07:06 ivansieder

@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

ivansieder avatar Sep 10 '21 13:09 ivansieder

Any news on this?

BreakBB avatar May 09 '22 06:05 BreakBB

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 avatar May 30 '22 04:05 BePo65

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

BePo65 avatar May 31 '22 09:05 BePo65

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

EriksonBahr avatar Mar 27 '23 15:03 EriksonBahr

Any news on this PR? I see there are conflicts again...

Irian avatar Jan 30 '25 15:01 Irian