From a112f2adaf075d5a2e594cd7ba3a45a6333ae3b1 Mon Sep 17 00:00:00 2001 From: chiatchiat Date: Wed, 29 Mar 2023 23:52:32 +0800 Subject: Fix: LazyRfc3339UtcTime return correct utc time --- cherrypy/_cplogging.py | 5 ++--- cherrypy/test/test_logging.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cherrypy/_cplogging.py b/cherrypy/_cplogging.py index 151d3b40..0e57c6ac 100644 --- a/cherrypy/_cplogging.py +++ b/cherrypy/_cplogging.py @@ -452,6 +452,5 @@ 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.""" + return f"{datetime.datetime.utcnow().isoformat('T')}Z" diff --git a/cherrypy/test/test_logging.py b/cherrypy/test/test_logging.py index 2d4aa56f..7968220f 100644 --- a/cherrypy/test/test_logging.py +++ b/cherrypy/test/test_logging.py @@ -197,6 +197,36 @@ def test_custom_log_format(log_tracker, monkeypatch, server): ) +def test_utc_in_timez(monkeypatch): + """Test utc timestamp is used in + cherrypy._cplogging.LazyRfc3339UtcTime""" + import datetime + + 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().""" -- cgit v1.2.1 From 6142fa8c0dea7ec1ff30195d681868b3b14adcec Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 30 Mar 2023 13:52:42 +0000 Subject: [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- cherrypy/test/test_logging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cherrypy/test/test_logging.py b/cherrypy/test/test_logging.py index 7968220f..bf070e23 100644 --- a/cherrypy/test/test_logging.py +++ b/cherrypy/test/test_logging.py @@ -198,7 +198,7 @@ def test_custom_log_format(log_tracker, monkeypatch, server): def test_utc_in_timez(monkeypatch): - """Test utc timestamp is used in + """Test utc timestamp is used in cherrypy._cplogging.LazyRfc3339UtcTime""" import datetime -- cgit v1.2.1 From 556cd32114f84a378488b381a65ed3ae1beda8a4 Mon Sep 17 00:00:00 2001 From: chiatchiat Date: Sat, 1 Apr 2023 21:01:30 +0800 Subject: Update cherrypy/_cplogging.py Co-authored-by: Sviatoslav Sydorenko --- cherrypy/_cplogging.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cherrypy/_cplogging.py b/cherrypy/_cplogging.py index 0e57c6ac..bce1c87b 100644 --- a/cherrypy/_cplogging.py +++ b/cherrypy/_cplogging.py @@ -453,4 +453,5 @@ class WSGIErrorHandler(logging.Handler): class LazyRfc3339UtcTime(object): def __str__(self): """Return utcnow() in RFC3339 UTC Format.""" - return f"{datetime.datetime.utcnow().isoformat('T')}Z" + iso_formatted_now = datetime.datetime.utcnow().isoformat('T') + return f'{iso_formatted_now!s}Z' -- cgit v1.2.1 From 0e1e0743971a6e641fb8f0bd7233534cef8da24d Mon Sep 17 00:00:00 2001 From: chiatchiat Date: Sat, 1 Apr 2023 21:04:33 +0800 Subject: Update cherrypy/test/test_logging.py Co-authored-by: Sviatoslav Sydorenko --- cherrypy/test/test_logging.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cherrypy/test/test_logging.py b/cherrypy/test/test_logging.py index bf070e23..bc7b2bb0 100644 --- a/cherrypy/test/test_logging.py +++ b/cherrypy/test/test_logging.py @@ -198,8 +198,7 @@ def test_custom_log_format(log_tracker, monkeypatch, server): def test_utc_in_timez(monkeypatch): - """Test utc timestamp is used in - cherrypy._cplogging.LazyRfc3339UtcTime""" + """Test that ``LazyRfc3339UtcTime`` is rendered as ``str`` using UTC timestamp.""" import datetime utcoffset8_local_time_in_naive_utc = ( -- cgit v1.2.1 From f119a4a8605ade000a33b83269b21cb3189045b6 Mon Sep 17 00:00:00 2001 From: chiatchiat Date: Sat, 1 Apr 2023 21:20:48 +0800 Subject: Move import statement to top of the file --- cherrypy/test/test_logging.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cherrypy/test/test_logging.py b/cherrypy/test/test_logging.py index bc7b2bb0..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 @@ -199,8 +200,6 @@ 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.""" - import datetime - utcoffset8_local_time_in_naive_utc = ( datetime.datetime( year=2020, -- cgit v1.2.1