Skip to content

Commit be745b8

Browse files
authored
[10.x] Ensure captured time is in configured timezone (#47567)
* Ensure captured time is in configured timezone * Update Kernel.php
1 parent 3558da8 commit be745b8

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

src/Illuminate/Foundation/Console/Kernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ public function terminate($input, $status)
218218
{
219219
$this->app->terminate();
220220

221+
$this->commandStartedAt->setTimezone($this->app['config']->get('app.timezone') ?? 'UTC');
222+
221223
foreach ($this->commandLifecycleDurationHandlers as ['threshold' => $threshold, 'handler' => $handler]) {
222224
$end ??= Carbon::now();
223225

src/Illuminate/Foundation/Http/Kernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ public function terminate($request, $response)
214214

215215
$this->app->terminate();
216216

217+
$this->requestStartedAt->setTimezone($this->app['config']->get('app.timezone') ?? 'UTC');
218+
217219
foreach ($this->requestLifecycleDurationHandlers as ['threshold' => $threshold, 'handler' => $handler]) {
218220
$end ??= Carbon::now();
219221

tests/Integration/Console/CommandDurationThresholdTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Carbon\CarbonInterval;
66
use Illuminate\Contracts\Console\Kernel;
77
use Illuminate\Support\Carbon;
8+
use Illuminate\Support\Facades\Config;
89
use Orchestra\Testbench\TestCase;
910
use Symfony\Component\Console\Input\StringInput;
1011
use Symfony\Component\Console\Output\ConsoleOutput;
@@ -177,4 +178,24 @@ public function testItClearsStartTimeAfterHandlingCommand()
177178
$kernel->terminate($input, 21);
178179
$this->assertNull($kernel->commandStartedAt());
179180
}
181+
182+
public function testUsesTheConfiguredDateTimezone()
183+
{
184+
Config::set('app.timezone', 'UTC');
185+
$startedAt = null;
186+
$kernel = $this->app[Kernel::class];
187+
$kernel->command('foo', fn () => null);
188+
$kernel->whenCommandLifecycleIsLongerThan(0, function ($started) use (&$startedAt) {
189+
$startedAt = $started;
190+
});
191+
192+
Config::set('app.timezone', 'Australia/Melbourne');
193+
Carbon::setTestNow(Carbon::now());
194+
$kernel->handle($input = new StringInput('foo'), new ConsoleOutput);
195+
196+
Carbon::setTestNow(now()->addMinute());
197+
$kernel->terminate($input, 21);
198+
199+
$this->assertSame('Australia/Melbourne', $startedAt->timezone->getName());
200+
}
180201
}

tests/Integration/Http/RequestDurationThresholdTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Http\Request;
88
use Illuminate\Http\Response;
99
use Illuminate\Support\Carbon;
10+
use Illuminate\Support\Facades\Config;
1011
use Illuminate\Support\Facades\Route;
1112
use Orchestra\Testbench\TestCase;
1213

@@ -72,6 +73,25 @@ public function testItProvidesRequestToHandler()
7273
$this->assertSame('http://localhost/test-route', $url);
7374
}
7475

76+
public function testUsesTheConfiguredDateTimezone()
77+
{
78+
Config::set('app.timezone', 'UTC');
79+
Route::get('test-route', fn () => 'ok');
80+
$kernel = $this->app[Kernel::class];
81+
$startedAt = null;
82+
$kernel->whenRequestLifecycleIsLongerThan(CarbonInterval::seconds(1), function ($started) use (&$startedAt) {
83+
$startedAt = $started;
84+
});
85+
86+
Config::set('app.timezone', 'Australia/Melbourne');
87+
Carbon::setTestNow(now()->startOfDay());
88+
$kernel->handle($request = Request::create('http://localhost/test-route'));
89+
Carbon::setTestNow(now()->addMinute());
90+
$kernel->terminate($request, new Response);
91+
92+
$this->assertSame('Australia/Melbourne', $startedAt->timezone->getName());
93+
}
94+
7595
public function testItCanExceedThresholdWhenSpecifyingDurationAsMilliseconds()
7696
{
7797
Route::get('test-route', fn () => 'ok');

0 commit comments

Comments
 (0)