Skip to content
2 changes: 2 additions & 0 deletions CHANGES/11283.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Made `AccessLogger` use `tm_gmtoff` for timestamps to be aware of DST.
-- by :user:`sgaist`
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ Roman Postnov
Rong Zhang
Samir Akarioh
Samuel Colvin
Samuel Gaist
Sean Hunt
Sebastian Acuna
Sebastian Hanula
Expand Down
4 changes: 3 additions & 1 deletion aiohttp/web_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ def _format_a(request: BaseRequest, response: StreamResponse, time: float) -> st

@staticmethod
def _format_t(request: BaseRequest, response: StreamResponse, time: float) -> str:
tz = datetime.timezone(datetime.timedelta(seconds=-time_mod.timezone))
tz = datetime.timezone(
datetime.timedelta(seconds=time_mod.localtime().tm_gmtoff)
)
now = datetime.datetime.now(tz)
start_time = now - datetime.timedelta(seconds=time)
return start_time.strftime("[%d/%b/%Y:%H:%M:%S %z]")
Expand Down
6 changes: 5 additions & 1 deletion tests/test_web_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import platform
import sys
import time
from contextvars import ContextVar
from typing import Dict, NoReturn, Optional
from unittest import mock
Expand Down Expand Up @@ -96,8 +97,11 @@ class PatchedDatetime(datetime.datetime):
def now(cls, tz: Optional[datetime.tzinfo] = None) -> Self:
return cls(1843, 1, 1, 0, 30, tzinfo=tz)

def patched_localtime(sec: Optional[int] = None) -> time.struct_time:
return time.struct_time((0, 0, 0, 0, 0, 0, 0, 0, 0, "test-tz", 28800))

monkeypatch.setattr("datetime.datetime", PatchedDatetime)
monkeypatch.setattr("time.timezone", -28800)
monkeypatch.setattr("time.localtime", patched_localtime)
monkeypatch.setattr("os.getpid", lambda: 42)
mock_logger = mock.Mock()
access_logger = AccessLogger(mock_logger, log_format)
Expand Down
Loading