Put a helper function in utils to do iso8601 formatting. change IRCServer's

last-read setting to use it.
This commit is contained in:
jesopo 2019-02-17 14:15:40 +00:00
parent d940729d12
commit cb0314da67
5 changed files with 13 additions and 18 deletions

View file

@ -380,7 +380,7 @@ class Module(ModuleManager.BaseModule):
return url return url
def _iso8601(self, s): def _iso8601(self, s):
return datetime.datetime.strptime(s, utils.ISO8601_FORMAT) return datetime.datetime.strptime(s, utils.ISO8601_PARSE)
def ping(self, data): def ping(self, data):
return ["Received new webhook"] return ["Received new webhook"]

View file

@ -234,8 +234,7 @@ class Server(IRCObject.Object):
self.ping_sent = False self.ping_sent = False
now = datetime.datetime.utcnow() now = datetime.datetime.utcnow()
self.set_setting("last-read", datetime.datetime.strftime(now, self.set_setting("last-read", utils.iso8601_format(now))
utils.ISO8601_FORMAT))
return lines return lines
def send(self, line: str): def send(self, line: str):

View file

@ -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 = { LEVELS = {
"trace": logging.DEBUG-1, "trace": logging.DEBUG-1,
@ -10,18 +11,9 @@ LEVELS = {
} }
class BitBotFormatter(logging.Formatter): class BitBotFormatter(logging.Formatter):
converter = time.gmtime
def formatTime(self, record, datefmt=None): def formatTime(self, record, datefmt=None):
ct = self.converter(record.created) datetime_obj = datetime.datetime.fromtimestamp(record.created)
if datefmt: return utils.iso8601_format(datetime_obj)
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
class Log(object): class Log(object):
def __init__(self, level: str, location: str): def __init__(self, level: str, location: str):

View file

@ -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 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_SECOND = 1
TIME_MINUTE = TIME_SECOND*60 TIME_MINUTE = TIME_SECOND*60

View file

@ -4,7 +4,7 @@ def bool_input(s: str):
result = input("%s (Y/n): " % s) result = input("%s (Y/n): " % s)
return not result or result[0].lower() in ["", "y"] return not result or result[0].lower() in ["", "y"]
def add_server(database: Database.Database): def add_server(database: "Database.Database"):
alias = input("alias: ") alias = input("alias: ")
hostname = input("hostname: ") hostname = input("hostname: ")
port = int(input("port: ")) port = int(input("port: "))