Skip to content

Commit c693fb5

Browse files
committed
Merge remote-tracking branch 'origin/3.next' into 4.x
2 parents fba6a96 + 7a3db17 commit c693fb5

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/Chronos.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,9 +729,11 @@ protected static function applyTestNowToMissingComponents(
729729
$hasMicro = (bool)array_intersect($formatChars, ['u', 'v']);
730730

731731
// If the format includes '!' or '|', PHP resets unspecified components to Unix epoch or zero
732-
// In that case, we should not override with testNow
732+
// If 'U' is present, all components are set from the Unix timestamp
733+
// In these cases, we should not override with testNow
733734
$hasReset = in_array('!', $formatChars, true) || in_array('|', $formatChars, true);
734-
if ($hasReset) {
735+
$hasUnixTimestamp = in_array('U', $formatChars, true);
736+
if ($hasReset || $hasUnixTimestamp) {
735737
return $dateTime;
736738
}
737739

tests/TestCase/DateTime/CreateFromFormatTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ public function testCreateFromFormatWithTestNowMicroseconds()
111111
$this->assertSame(123456, $d->micro);
112112
}
113113

114+
public function testCreateFromFormatWithTestNowUnixTimestamp()
115+
{
116+
// Unix timestamp ('U' format) sets all components, should not use testNow
117+
Chronos::setTestNow(new Chronos('2020-12-01 14:30:45'));
118+
$d = Chronos::createFromFormat('U', '0');
119+
$this->assertDateTime($d, 1970, 1, 1, 0, 0, 0);
120+
}
121+
122+
public function testCreateFromFormatWithTestNowNegativeUnixTimestamp()
123+
{
124+
// Negative Unix timestamp should also not use testNow
125+
Chronos::setTestNow(new Chronos('2020-12-01 14:30:45'));
126+
$d = Chronos::createFromFormat('U', '-1000');
127+
$this->assertDateTime($d, 1969, 12, 31, 23, 43, 20);
128+
}
129+
114130
public function testCreateFromFormatWithTimezoneString()
115131
{
116132
$d = Chronos::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11', 'Europe/London');

0 commit comments

Comments
 (0)