8000 initialize the current time with midnight before modifying the date · symfony/symfony@d4e9745 · GitHub
[go: up one dir, main page]

Skip to content

Commit d4e9745

Browse files
committed
initialize the current time with midnight before modifying the date
1 parent 00ba3ad commit d4e9745

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Symfony/Component/Clock/DatePoint.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,21 @@ public function __construct(string $datetime = 'now', ?\DateTimeZone $timezone =
3232

3333
if (\PHP_VERSION_ID < 80300) {
3434
try {
35-
$timezone = (new parent($datetime, $timezone ?? $now->getTimezone()))->getTimezone();
35+
$builtInDate = new parent($datetime, $timezone ?? $now->getTimezone());
36+
$timezone = ($builtInDate)->getTimezone();
3637
} catch (\Exception $e) {
3738
throw new \DateMalformedStringException($e->getMessage(), $e->getCode(), $e);
3839
}
3940
} else {
40-
$timezone = (new parent($datetime, $timezone ?? $now->getTimezone()))->getTimezone();
41+
$builtInDate = new parent($datetime, $timezone ?? $now->getTimezone());
42+
$timezone = ($builtInDate)->getTimezone();
4143
}
4244

4345
$now = $now->setTimezone($timezone)->modify($datetime);
46+
47+
if ('00:00:00' === $builtInDate->format('H:i:s')) {
48+
$now = $now->setTime(0, 0);
49+
}
4450
} elseif (null !== $timezone) {
4551
$now = $now->setTimezone($timezone);
4652
}

src/Symfony/Component/Clock/Tests/DatePointTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,11 @@ public function testModify()
5757
$this->expectExceptionMessage('Failed to parse time string (Bad Date)');
5858
$date->modify('Bad Date');
5959
}
60+
61+
public function testTimeDefaultsToMidnight()
62+
{
63+
$date = new DatePoint('2024-04-09');
64+
65+
$this->assertSame('2024-04-09 00:00:00', $date->format('Y-m-d H:i:s'));
66+
}
6067
}

0 commit comments

Comments
 (0)
0