diff options
author | Sviatoslav Sydorenko <webknjaz@redhat.com> | 2023-04-02 01:50:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-02 01:50:27 +0200 |
commit | 4febc2c008db9a6b0d137ff3ed8de117a19d5159 (patch) | |
tree | f2bda2c4a2a0e54d6de57af90f2dd01b9985b28b | |
parent | 1f2366fb56663b9e8e480b9cba299ff201cba4da (diff) | |
parent | f119a4a8605ade000a33b83269b21cb3189045b6 (diff) | |
download | cherrypy-git-4febc2c008db9a6b0d137ff3ed8de117a19d5159.tar.gz |
Merge pull request #1992 from chiatchiat/utc-time-in-LazyRfc3339UtcTime
This patch fixes `LazyRfc3339UtcTime` to return correct time using
the UTC timezone. Previously, it mistakenly used the local timezone
value for the RFC 3339 representation.
-rw-r--r-- | cherrypy/_cplogging.py | 6 | ||||
-rw-r--r-- | cherrypy/test/test_logging.py | 28 |
2 files changed, 31 insertions, 3 deletions
diff --git a/cherrypy/_cplogging.py b/cherrypy/_cplogging.py index 151d3b40..bce1c87b 100644 --- a/cherrypy/_cplogging.py +++ b/cherrypy/_cplogging.py @@ -452,6 +452,6 @@ class WSGIErrorHandler(logging.Handler): class LazyRfc3339UtcTime(object): def __str__(self): - """Return now() in RFC3339 UTC Format.""" - now = datetime.datetime.now() - return now.isoformat('T') + 'Z' + """Return utcnow() in RFC3339 UTC Format.""" + iso_formatted_now = datetime.datetime.utcnow().isoformat('T') + return f'{iso_formatted_now!s}Z' diff --git a/cherrypy/test/test_logging.py b/cherrypy/test/test_logging.py index 2d4aa56f..49d41d0a 100644 --- a/cherrypy/test/test_logging.py +++ b/cherrypy/test/test_logging.py @@ -1,5 +1,6 @@ """Basic tests for the CherryPy core: request handling.""" +import datetime import logging from cheroot.test import webtest @@ -197,6 +198,33 @@ def test_custom_log_format(log_tracker, monkeypatch, server): ) +def test_utc_in_timez(monkeypatch): + """Test that ``LazyRfc3339UtcTime`` is rendered as ``str`` using UTC timestamp.""" + utcoffset8_local_time_in_naive_utc = ( + datetime.datetime( + year=2020, + month=1, + day=1, + hour=1, + minute=23, + second=45, + tzinfo=datetime.timezone(datetime.timedelta(hours=8)), + ) + .astimezone(datetime.timezone.utc) + .replace(tzinfo=None) + ) + + class mock_datetime: + @classmethod + def utcnow(cls): + return utcoffset8_local_time_in_naive_utc + + monkeypatch.setattr('datetime.datetime', mock_datetime) + rfc3339_utc_time = str(cherrypy._cplogging.LazyRfc3339UtcTime()) + expected_time = '2019-12-31T17:23:45Z' + assert rfc3339_utc_time == expected_time + + def test_timez_log_format(log_tracker, monkeypatch, server): """Test a customized access_log_format string, which is a feature of _cplogging.LogManager.access().""" |