summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2021-04-28 14:13:07 -0500
committerJordan Cook <jordan.cook@pioneer.com>2021-04-29 09:24:48 -0500
commitb99e95cb7441eb921f9347fc9729257c873b5583 (patch)
tree8fde38f6833ecf7d8603af4e59a5ba1cc818010b
parent96be7f600207fec948889f27038a1661d4f6a151 (diff)
downloadrequests-cache-b99e95cb7441eb921f9347fc9729257c873b5583.tar.gz
Show datetimes in local timezone for str(CachedResponse)
-rwxr-xr-xrequests_cache/response.py11
1 files changed, 8 insertions, 3 deletions
diff --git a/requests_cache/response.py b/requests_cache/response.py
index 1df26a4..9b09474 100755
--- a/requests_cache/response.py
+++ b/requests_cache/response.py
@@ -1,6 +1,6 @@
"""Classes to wrap cached response objects"""
from copy import copy
-from datetime import datetime, timedelta
+from datetime import datetime, timedelta, timezone
from io import BytesIO
from logging import getLogger
from typing import Any, Dict, Optional, Union
@@ -22,7 +22,7 @@ RAW_RESPONSE_ATTRS = [
]
CACHE_ATTRS = ['from_cache', 'created_at', 'expires', 'is_expired']
-DATETIME_SHORT_FORMAT = '%Y-%m-%d %H:%M'
+DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S %Z'
ExpirationTime = Union[None, int, float, datetime, timedelta]
logger = getLogger(__name__)
@@ -180,7 +180,12 @@ AnyResponse = Union[Response, CachedResponse]
def format_datetime(value: Optional[datetime]) -> str:
- return value.strftime(DATETIME_SHORT_FORMAT) if value else "N/A"
+ """Get a formatted datetime string in the local time zone"""
+ if not value:
+ return "N/A"
+ if value.tzinfo is None:
+ value = value.replace(tzinfo=timezone.utc)
+ return value.astimezone().strftime(DATETIME_FORMAT)
def format_file_size(n_bytes: int) -> str: