From ae178199116b22c45f4bc65bfa4b281298a57d1e Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Mon, 24 Mar 2025 08:29:45 -0700 Subject: [PATCH] Fixed file modified times not respecting the configured timezone --- app/src/ViewFunctions/ModifiedTime.php | 8 +++++++- tests/ViewFunctions/ModifiedTimeTest.php | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/src/ViewFunctions/ModifiedTime.php b/app/src/ViewFunctions/ModifiedTime.php index 420636e..4df9780 100644 --- a/app/src/ViewFunctions/ModifiedTime.php +++ b/app/src/ViewFunctions/ModifiedTime.php @@ -5,6 +5,8 @@ declare(strict_types=1); namespace App\ViewFunctions; use App\Config; +use DateTimeImmutable; +use DateTimeZone; use RuntimeException; use Symfony\Component\Finder\SplFileInfo; @@ -26,6 +28,10 @@ class ModifiedTime extends ViewFunction $modifiedTime = lstat($file->getPathname())['mtime']; } - return date($this->config->get('date_format'), $modifiedTime); + $date = DateTimeImmutable::createFromTimestamp($modifiedTime)->setTimezone( + new DateTimeZone($this->config->get('timezone')) + ); + + return $date->format($this->config->get('date_format')); } } diff --git a/tests/ViewFunctions/ModifiedTimeTest.php b/tests/ViewFunctions/ModifiedTimeTest.php index dc8cb53..4c9f200 100644 --- a/tests/ViewFunctions/ModifiedTimeTest.php +++ b/tests/ViewFunctions/ModifiedTimeTest.php @@ -16,6 +16,8 @@ class ModifiedTimeTest extends TestCase #[Test] public function it_can_return_the_modified_time_for_a_file(): void { + $this->container->set('timezone', 'UTC'); + $file = $this->createMock(SplFileInfo::class); $file->method('getMTime')->willReturn(516976496);