diff options
author | 2019-09-26 20:45:19 +0200 | |
---|---|---|
committer | 2019-09-26 20:45:19 +0200 | |
commit | 8164717102b1fc25d9b5b9107a0b6fa163b90d94 (patch) | |
tree | 1ddf6de366ce32a944579102a837fdca645d0f8c | |
parent | Add utility function to consistently format infraction timestamps (diff) |
Use format_infaction datetime util in talentpool
The watchchannel ABC defined its own private utility function to
format ISO datetime strings to something more human-readable. I have
removed this private utility function and replaced the calls to it
with calls to the new `format_infraction` utility function defined in
bot.utils.time.
In addition, I've changed the utility function to use `dateutil` to
parse the datetime string, since `dateutil.parser.isoparse` supports
the strings our API generates out of the box. With the built-in
`datetime.datetime.fromisoformat`, we needed to prepare the string by
slicing of the `Z` timezone indicator.
-rw-r--r-- | bot/cogs/watchchannels/talentpool.py | 5 | ||||
-rw-r--r-- | bot/cogs/watchchannels/watchchannel.py | 8 | ||||
-rw-r--r-- | bot/utils/time.py | 3 |
3 files changed, 5 insertions, 11 deletions
diff --git a/bot/cogs/watchchannels/talentpool.py b/bot/cogs/watchchannels/talentpool.py index ffe7693a9..4a23902d5 100644 --- a/bot/cogs/watchchannels/talentpool.py +++ b/bot/cogs/watchchannels/talentpool.py @@ -10,6 +10,7 @@ from bot.api import ResponseCodeError from bot.constants import Channels, Guild, Roles, Webhooks from bot.decorators import with_role from bot.pagination import LinePaginator +from bot.utils import time from .watchchannel import WatchChannel, proxy_user log = logging.getLogger(__name__) @@ -198,7 +199,7 @@ class TalentPool(WatchChannel, Cog, name="Talentpool"): log.debug(active) log.debug(type(nomination_object["inserted_at"])) - start_date = self._get_human_readable(nomination_object["inserted_at"]) + start_date = time.format_infraction(nomination_object["inserted_at"]) if active: lines = textwrap.dedent( f""" @@ -212,7 +213,7 @@ class TalentPool(WatchChannel, Cog, name="Talentpool"): """ ) else: - end_date = self._get_human_readable(nomination_object["ended_at"]) + end_date = time.format_infraction(nomination_object["ended_at"]) lines = textwrap.dedent( f""" =============== diff --git a/bot/cogs/watchchannels/watchchannel.py b/bot/cogs/watchchannels/watchchannel.py index e78282900..f706ff634 100644 --- a/bot/cogs/watchchannels/watchchannel.py +++ b/bot/cogs/watchchannels/watchchannel.py @@ -329,14 +329,6 @@ class WatchChannel(metaclass=CogABCMeta): return time_delta - @staticmethod - def _get_human_readable(time_string: str, output_format: str = "%Y-%m-%d %H:%M:%S") -> str: - date_time = datetime.datetime.strptime( - time_string, - "%Y-%m-%dT%H:%M:%S.%fZ" - ).replace(tzinfo=None) - return date_time.strftime(output_format) - def _remove_user(self, user_id: int) -> None: """Removes a user from a watch channel.""" self.watched_users.pop(user_id, None) diff --git a/bot/utils/time.py b/bot/utils/time.py index 0d4192921..da28f2c76 100644 --- a/bot/utils/time.py +++ b/bot/utils/time.py @@ -1,6 +1,7 @@ import asyncio import datetime +import dateutil.parser from dateutil.relativedelta import relativedelta RFC1123_FORMAT = "%a, %d %b %Y %H:%M:%S GMT" @@ -100,4 +101,4 @@ async def wait_until(time: datetime.datetime) -> None: def format_infraction(timestamp: str) -> str: """Format an infraction timestamp to a more readable ISO 8601 format.""" - return datetime.datetime.fromisoformat(timestamp[:-1]).strftime(INFRACTION_FORMAT) + return dateutil.parser.isoparse(timestamp).strftime(INFRACTION_FORMAT) |