4.x: Auto-append pipe modifier in createFromFormat#504
Open
dereuromark wants to merge 1 commit into4.xfrom
Open
4.x: Auto-append pipe modifier in createFromFormat#504dereuromark wants to merge 1 commit into4.xfrom
dereuromark wants to merge 1 commit into4.xfrom
Conversation
Unlike PHP's native DateTimeImmutable::createFromFormat(), this method
now automatically appends the | modifier if no reset modifier (| or \!)
is present. This ensures that unparsed components are reset to zero
instead of being filled from the current time.
This provides more predictable and deterministic behavior, especially
useful for testing where flaky tests can occur due to missing components
being filled with the current system time.
Examples:
- createFromFormat('Y-m-d', '2024-03-14') -> time is 00:00:00 (not current)
- createFromFormat('Y-m-d H:i', '2024-03-14 12:30') -> seconds is 00
This is a behavior change from PHP's native method, but arguably the
more intuitive default.
93101ba to
a7011fc
Compare
Member
Author
Approach ComparisonBehavior Matrix
Feature Comparison
Pros and Cons#494 (testNow approach)Pros:
Cons:
#504 (auto-append pipe)Pros:
Cons:
Implementation Complexity#494 (testNow) - Format Parsing Required// Must detect which components are present in format
// Handle escaped characters (\Y, \\, etc.)
// Special cases: U, U.u, timezone formats
// ~50+ lines of parsing logic#504 (auto-pipe) - Simple String Checkif (!str_contains($format, '|') && !str_contains($format, '!')) {
$format .= '|';
}
// 3 lines, delegates complexity to PHPReal-World ImpactWho is affected by #504 (auto-pipe)?Code that intentionally does this: // "Give me this date with current system time"
$d = Chronos::createFromFormat('Y-m-d', $dateString);
// Expected: 2024-03-14 14:32:45 (current time)
// With #504: 2024-03-14 00:00:00This is a rare/unusual pattern. More common is users being surprised that they got random seconds. Who is affected by #494 (testNow)?Code that uses
Code that uses
|
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.
Attempt of #494 redo of "deterministic behavior"
Summary
Auto-append
|modifier increateFromFormat()for deterministic behavior.The Change
Unlike PHP's native
DateTimeImmutable::createFromFormat(), this method now automatically appends the|modifier if no reset modifier (|or!) is present.This ensures that unparsed components are reset to zero instead of being filled from the current time.
The 4.x philosophy: Always deterministic, everywhere
Examples
This is what most users intuitively expect. When you parse just a date, you get midnight - not "midnight plus whatever seconds happen to be on the clock."
The only "BC break" scenario is someone who intentionally wanted:
That's a rare/unusual pattern. If someone actually needs current time for missing components, they'd explicitly do:
Benefits
|to format strings manuallyMigration
Test Code that relied on missing components being filled from current time will need adjustment.
But this "test" BC break is more theoretical than practical: