fix: validate February 29th in date assertion when format lacks a year#355
Open
deeferentleeg wants to merge 2 commits into
Open
fix: validate February 29th in date assertion when format lacks a year#355deeferentleeg wants to merge 2 commits into
deeferentleeg wants to merge 2 commits into
Conversation
The date() assertion uses DateTime::createFromFormat('!'.$format, $value)
where the '!' modifier resets unspecified fields to the Unix epoch
(1970-01-01). Since 1970 is not a leap year, valid dates like '29/02'
with format 'd/m' are incorrectly rejected.
When the initial parse fails and the format does not contain a year
specifier (Y, y, or o), retry by prepending a known leap year (2000)
so that February 29th is validated correctly.
Addresses beberlei#336
Add '29/02' with format 'd/m' to valid date provider (leap day without year) and '30/02' with format 'd/m' to invalid date provider (Feb 30 does not exist in any year). Addresses beberlei#336
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #336.
The
date()assertion usesDateTime::createFromFormat('!'.$format, $value), where the!modifier resets all unspecified fields to the Unix epoch (1970-01-01 00:00:00 UTC). Because 1970 is not a leap year, valid dates such as29/02with formatd/mare incorrectly rejected — there is no February 29th in 1970.The fix
When the initial
!-based parse fails and the format does not contain a year specifier (Y,y, oro), the assertion now retries by prepending a known leap year (2000) to both the value and the format:The round-trip check (
$value !== $dateTime->format($format)) is then performed against the original format, so the prepended year does not appear in the comparison. This makes the fix fully deterministic — it does not depend on the current system year.Behavior preserved
Y-m-d) are unaffected — the retry only triggers when no year token is present.2011-02-29withY-m-dis still correctly rejected (2011 is not a leap year).32/01withd/mare still rejected by the round-trip check.30/02withd/mis still rejected (February 30th does not exist in any year, including 2000).Tests
['29/02', 'd/m']tovalidDateProvider— February 29th without a year should now pass.['30/02', 'd/m']toinvalidDateProvider— February 30th without a year should still fail.AI usage disclosure: An AI assistant was used to help locate the relevant code and draft this change. I have reviewed and understand every line of the diff, and I am the sole author of this contribution.