diff --git a/modules/github/__init__.py b/modules/github/__init__.py index abb561e4..8046b50e 100644 --- a/modules/github/__init__.py +++ b/modules/github/__init__.py @@ -380,7 +380,7 @@ class Module(ModuleManager.BaseModule): return url def _iso8601(self, s): - return datetime.datetime.strptime(s, utils.ISO8601_FORMAT) + return datetime.datetime.strptime(s, utils.ISO8601_PARSE) def ping(self, data): return ["Received new webhook"] diff --git a/src/IRCServer.py b/src/IRCServer.py index e146fc6a..e6d93d0b 100644 --- a/src/IRCServer.py +++ b/src/IRCServer.py @@ -234,8 +234,7 @@ class Server(IRCObject.Object): self.ping_sent = False now = datetime.datetime.utcnow() - self.set_setting("last-read", datetime.datetime.strftime(now, - utils.ISO8601_FORMAT)) + self.set_setting("last-read", utils.iso8601_format(now)) return lines def send(self, line: str): diff --git a/src/Logging.py b/src/Logging.py index 879b8330..f82770fc 100644 --- a/src/Logging.py +++ b/src/Logging.py @@ -1,4 +1,5 @@ -import logging, logging.handlers, os, queue, sys, time, typing +import datetime, logging, logging.handlers, os, queue, sys, time, typing +from src import utils LEVELS = { "trace": logging.DEBUG-1, @@ -10,18 +11,9 @@ LEVELS = { } class BitBotFormatter(logging.Formatter): - converter = time.gmtime def formatTime(self, record, datefmt=None): - ct = self.converter(record.created) - if datefmt: - if "%f" in datefmt: - msec = "%03d" % record.msecs - datefmt = datefmt.replace("%f", msec) - s = time.strftime(datefmt, ct) - else: - t = time.strftime("%Y-%m-%d %H:%M:%S", ct) - s = "%s.%03d" % (t, record.msecs) - return s + datetime_obj = datetime.datetime.fromtimestamp(record.created) + return utils.iso8601_format(datetime_obj) class Log(object): def __init__(self, level: str, location: str): diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 692d305a..9cb99e93 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -1,7 +1,11 @@ -import decimal, io, ipaddress, re, typing +import datetime, decimal, io, ipaddress, re, typing from src.utils import cli, consts, irc, http, parse, security -ISO8601_FORMAT = "%Y-%m-%dT%H:%M:%S%z" +ISO8601_PARSE = "%Y-%m-%dT%H:%M:%S%z" + +def iso8601_format(dt: datetime.datetime) -> str: + formatted = dt.isoformat(timespec="milliseconds") + return "%sZ" % formatted TIME_SECOND = 1 TIME_MINUTE = TIME_SECOND*60 diff --git a/src/utils/cli.py b/src/utils/cli.py index 598bbc2a..638ff749 100644 --- a/src/utils/cli.py +++ b/src/utils/cli.py @@ -4,7 +4,7 @@ def bool_input(s: str): result = input("%s (Y/n): " % s) return not result or result[0].lower() in ["", "y"] -def add_server(database: Database.Database): +def add_server(database: "Database.Database"): alias = input("alias: ") hostname = input("hostname: ") port = int(input("port: "))