diff options
author | chiatchiat <none@none> | 2023-03-29 23:52:32 +0800 |
---|---|---|
committer | chiatchiat <none@none> | 2023-03-29 23:52:32 +0800 |
commit | a112f2adaf075d5a2e594cd7ba3a45a6333ae3b1 (patch) | |
tree | a867f6ce05be8022699c3e21e3bd88211b2a148c | |
parent | 6e3de3744600007dd4ac9f3dde25d0e1962ad37c (diff) | |
download | cherrypy-git-a112f2adaf075d5a2e594cd7ba3a45a6333ae3b1.tar.gz |
Fix: LazyRfc3339UtcTime return correct utc time
-rw-r--r-- | cherrypy/_cplogging.py | 5 | ||||
-rw-r--r-- | 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().""" |