change utils.iso8601_format to default to no milliseconds, switch to using

utils.iso8601 functions in badges.py
This commit is contained in:
jesopo 2019-04-23 21:27:43 +01:00
parent 537613545d
commit 87340bcbf9
4 changed files with 19 additions and 14 deletions

View file

@ -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"

View file

@ -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):

View file

@ -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):

View file

@ -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