Skip to content

Commit 8a21b1d

Browse files
Jozef PistejRastusik
authored andcommitted
fix(access-log): set default timezone from php since $timezone parameter and the current timezone are ignored when the $datetime parameter is a UNIX timestamp (starts with @)
1 parent 94d9314 commit 8a21b1d

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/Bridge/Log/SymfonyAccessLogDataMap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SwooleBundle\SwooleBundle\Bridge\Log;
66

77
use DateTimeImmutable;
8+
use DateTimeZone;
89
use IntlDateFormatter;
910
use Symfony\Component\HttpFoundation\Request;
1011
use Symfony\Component\HttpFoundation\Response;
@@ -301,6 +302,7 @@ public function getRequestTime(string $format): string
301302
default:
302303
// Cast to int first, as it may be a float
303304
$requestTime = new DateTimeImmutable('@' . (int) $time);
305+
$requestTime = $requestTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
304306

305307
return IntlDateFormatter::formatObject(
306308
$requestTime,

tests/Unit/Bridge/Log/AccessLogFormatterTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public function testFormatterDelegatesToDataMapToReplacePlaceholdersInFormat():
3131
->method('getRequestDuration')
3232
->willReturnCallback(
3333
static fn(string $format) => match ([$format]) { // @phpstan-ignore-line
34-
['ms'] => '4321', // %D
35-
['s'] => '22', // %T
36-
['us'] => '22', // %{us}T
34+
['ms'] => '4321', // %{ms}T
35+
['s'] => '4', // %T
36+
['us'] => '4321210', // %{us}T, %D
3737
}
3838
);
3939
$dataMap->method('getFilename')->willReturn(__FILE__); // %f
@@ -84,7 +84,7 @@ public function testFormatterDelegatesToDataMapToReplacePlaceholdersInFormat():
8484
'127.0.0.1',
8585
'1234',
8686
'1234',
87-
'4321',
87+
'4321210',
8888
__FILE__,
8989
$hostname,
9090
'HTTP/1.1',
@@ -94,7 +94,7 @@ public function testFormatterDelegatesToDataMapToReplacePlaceholdersInFormat():
9494
'POST /path?foo=bar HTTP/1.1',
9595
'202',
9696
'[1234567890]',
97-
'22',
97+
'4',
9898
'swoole',
9999
'/path',
100100
'swoole.local',
@@ -108,7 +108,7 @@ public function testFormatterDelegatesToDataMapToReplacePlaceholdersInFormat():
108108
'response',
109109
'9999',
110110
'[1234567890]',
111-
'22',
111+
'4321210',
112112
];
113113
$expected = implode(' ', $expected);
114114

tests/Unit/Bridge/Log/SymfonyAccessLogDataMapTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,18 @@ public function testGetHttpFoundationRequestTimeAsRequestedByFormatterReturnsFor
9898

9999
$this->assertSame('[02/Dec/2021:02:21:12 ' . $date->format('O') . ']', $requestTime);
100100
}
101+
102+
public function testGetHttpFoundationRequestTimeInDifferentTimeZoneAsUTC(): void
103+
{
104+
$oldTZ = date_default_timezone_get();
105+
date_default_timezone_set('Europe/Bratislava');
106+
$tz = new DateTimeZone(date_default_timezone_get());
107+
$date = new DateTimeImmutable('2021-12-02T02:21:12.4242', $tz);
108+
$this->request->server = new ServerBag(['REQUEST_TIME_FLOAT' => (float) $date->getTimestamp()]);
109+
$map = new SymfonyAccessLogDataMap($this->request, $this->response, false);
110+
$requestTime = $map->getRequestTime('begin:%d/%b/%Y:%H:%M:%S %z');
111+
112+
$this->assertSame('[02/Dec/2021:02:21:12 ' . $date->format('O') . ']', $requestTime);
113+
date_default_timezone_set($oldTZ);
114+
}
101115
}

0 commit comments

Comments
 (0)