From 87340bcbf91bba1aafd6beb77d6c64a595e6e5b0 Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 23 Apr 2019 21:27:43 +0100 Subject: [PATCH] change utils.iso8601_format to default to no milliseconds, switch to using utils.iso8601 functions in badges.py --- modules/badges.py | 16 ++++++---------- src/IRCServer.py | 3 ++- src/Logging.py | 2 +- src/utils/__init__.py | 12 ++++++++++-- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/modules/badges.py b/modules/badges.py index d601f062..24b32395 100644 --- a/modules/badges.py +++ b/modules/badges.py @@ -9,10 +9,6 @@ DATE_FORMAT = "%Y-%m-%d" class Module(ModuleManager.BaseModule): def _now(self): return datetime.datetime.utcnow() - def _format_datetime(self, dt: datetime.datetime): - return datetime.datetime.strftime(dt, DATETIME_FORMAT) - def _parse_datetime(self, dt: str): - return datetime.datetime.strptime(dt, DATETIME_FORMAT) def _parse_date(self, dt: str): if dt.lower() == "today": @@ -64,7 +60,7 @@ class Module(ModuleManager.BaseModule): found_badge = self._find_badge(badges, badge) if found_badge: - dt = self._parse_datetime(badges[found_badge]) + dt = utils.iso8601_parse(badges[found_badge]) days_since = self._days_since(now, dt) event["stdout"].write("(%s) %s on day %s (%s)" % ( event["user"].nickname, found_badge, days_since, @@ -84,7 +80,7 @@ class Module(ModuleManager.BaseModule): now = self._round_up_day(self._now()) badges = [] for badge, date in self._get_badges(user).items(): - days_since = self._days_since(now, self._parse_datetime(date)) + days_since = self._days_since(now, utils.iso8601_parse(date)) badges.append("%s on day %s" % ( badge, days_since)) @@ -105,7 +101,7 @@ class Module(ModuleManager.BaseModule): if badge_name.lower() == badge_lower: raise utils.EventError("You already have a '%s' badge" % badge) - badges[badge] = self._format_datetime(self._now()) + badges[badge] = utils.iso8601_format_now() self._set_badges(event["user"], badges) event["stdout"].write("Added '%s' badge" % badge) @@ -141,7 +137,7 @@ class Module(ModuleManager.BaseModule): found_badge = self._find_badge(badges, badge) if found_badge: - badges[found_badge] = self._format_datetime(self._now()) + badges[found_badge] = utils.iso8601_format_now() self._set_badges(event["user"], badges) event["stdout"].write("Reset badge '%s'" % found_badge) else: @@ -164,7 +160,7 @@ class Module(ModuleManager.BaseModule): dt = self._parse_date(event["args_split"][-1]) - badges[found_badge] = self._format_datetime(dt) + badges[found_badge] = utils.iso8601_format(dt) self._set_badges(event["user"], badges) event["stdout"].write("Updated '%s' badge to %s" % ( found_badge, self._date_str(dt))) @@ -182,7 +178,7 @@ class Module(ModuleManager.BaseModule): found_badge = self._find_badge(badges, badge) dt = self._parse_date(event["args_split"][-1]) - badges[found_badge or badge] = self._format_datetime(dt) + badges[found_badge or badge] = utils.iso8601_format(dt) self._set_badges(event["user"], badges) add_or_update = "Added" if not found_badge else "Updated" diff --git a/src/IRCServer.py b/src/IRCServer.py index 3989374f..217dfe4c 100644 --- a/src/IRCServer.py +++ b/src/IRCServer.py @@ -236,7 +236,8 @@ class Server(IRCObject.Object): self.ping_sent = False now = datetime.datetime.utcnow() - self.set_setting("last-read", utils.iso8601_format(now)) + self.set_setting("last-read", + utils.iso8601_format(now, milliseconds=True)) return lines def send(self, line_parsed: IRCLine.ParsedLine): diff --git a/src/Logging.py b/src/Logging.py index fe7f3f91..49267086 100644 --- a/src/Logging.py +++ b/src/Logging.py @@ -13,7 +13,7 @@ LEVELS = { class BitBotFormatter(logging.Formatter): def formatTime(self, record, datefmt=None): datetime_obj = datetime.datetime.fromtimestamp(record.created) - return utils.iso8601_format(datetime_obj) + return utils.iso8601_format(datetime_obj, milliseconds=True) class Log(object): def __init__(self, to_file: bool, level: str, location: str): diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 22cff3b1..0a51493f 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -7,9 +7,17 @@ class Direction(enum.Enum): ISO8601_PARSE = "%Y-%m-%dT%H:%M:%S%z" -def iso8601_format(dt: datetime.datetime) -> str: - formatted = dt.isoformat(timespec="milliseconds") +def iso8601_format(dt: datetime.datetime, milliseconds: bool=False) -> str: + timespec = "seconds" + if milliseconds: + timespec = "milliseconds" + + formatted = dt.isoformat(timespec=timespec) return "%sZ" % formatted +def iso8601_format_now() -> str: + return iso8601_format(datetime.datetime.utcnow()) +def iso8601_parse(s: str) -> datetime.datetime: + return datetime.datetime.strptime(s, ISO8601_PARSE) TIME_SECOND = 1 TIME_MINUTE = TIME_SECOND*60