use a TimeSpec system to request milliseconds in utils.datetime
This commit is contained in:
parent
fbe4c93c58
commit
cb73507c5e
2 changed files with 20 additions and 10 deletions
|
@ -13,7 +13,8 @@ LEVELS = {
|
||||||
class BitBotFormatter(logging.Formatter):
|
class BitBotFormatter(logging.Formatter):
|
||||||
def formatTime(self, record, datefmt=None):
|
def formatTime(self, record, datefmt=None):
|
||||||
datetime_obj = datetime.datetime.fromtimestamp(record.created)
|
datetime_obj = datetime.datetime.fromtimestamp(record.created)
|
||||||
return utils.datetime.iso8601_format(datetime_obj, milliseconds=True)
|
return utils.datetime.iso8601_format(datetime_obj,
|
||||||
|
timespec=utils.datetime.TimeSpec.MILLISECOND)
|
||||||
|
|
||||||
class HookedHandler(logging.StreamHandler):
|
class HookedHandler(logging.StreamHandler):
|
||||||
def __init__(self, func: typing.Callable[[int, str], None]):
|
def __init__(self, func: typing.Callable[[int, str], None]):
|
||||||
|
|
|
@ -1,37 +1,46 @@
|
||||||
import re, typing
|
import enum, re, typing
|
||||||
import datetime as _datetime
|
import datetime as _datetime
|
||||||
import dateutil.parser
|
import dateutil.parser
|
||||||
|
|
||||||
ISO8601_FORMAT_DT = "%Y-%m-%dT%H:%M:%S"
|
ISO8601_FORMAT_DT = "%Y-%m-%dT%H:%M:%S"
|
||||||
ISO8601_FORMAT_TZ = "%z"
|
ISO8601_FORMAT_TZ = "%z"
|
||||||
|
|
||||||
DATETIME_HUMAN = "%Y/%m/%d %H:%M:%S"
|
TIME_HUMAN = "%H:%M:%S"
|
||||||
DATE_HUMAN = "%Y-%m-%d"
|
DATE_HUMAN = "%Y-%m-%d"
|
||||||
|
|
||||||
|
class TimeSpec(enum.Enum):
|
||||||
|
NORMAL = 1
|
||||||
|
MILLISECOND = 2
|
||||||
|
|
||||||
def utcnow() -> _datetime.datetime:
|
def utcnow() -> _datetime.datetime:
|
||||||
return _datetime.datetime.utcnow().replace(tzinfo=_datetime.timezone.utc)
|
return _datetime.datetime.utcnow().replace(tzinfo=_datetime.timezone.utc)
|
||||||
def datetime_timestamp(seconds: float) -> _datetime.datetime:
|
def datetime_timestamp(seconds: float) -> _datetime.datetime:
|
||||||
return _datetime.datetime.fromtimestamp(seconds).replace(
|
return _datetime.datetime.fromtimestamp(seconds).replace(
|
||||||
tzinfo=_datetime.timezone.utc)
|
tzinfo=_datetime.timezone.utc)
|
||||||
|
|
||||||
def iso8601_format(dt: _datetime.datetime, milliseconds: bool=False) -> str:
|
def iso8601_format(dt: _datetime.datetime, timespec: TimeSpec=TimeSpec.NORMAL
|
||||||
|
) -> str:
|
||||||
dt_format = dt.strftime(ISO8601_FORMAT_DT)
|
dt_format = dt.strftime(ISO8601_FORMAT_DT)
|
||||||
tz_format = dt.strftime(ISO8601_FORMAT_TZ)
|
tz_format = dt.strftime(ISO8601_FORMAT_TZ)
|
||||||
|
|
||||||
ms_format = ""
|
ms_format = ""
|
||||||
if milliseconds:
|
if timespec == TimeSpec.MILLISECOND:
|
||||||
ms_format = ".%s" % str(int(dt.microsecond/1000)).zfill(3)
|
ms_format = ".%s" % str(int(dt.microsecond/1000)).zfill(3)
|
||||||
|
|
||||||
return "%s%s%s" % (dt_format, ms_format, tz_format)
|
return "%s%s%s" % (dt_format, ms_format, tz_format)
|
||||||
def iso8601_format_now(milliseconds: bool=False) -> str:
|
def iso8601_format_now(timespec: TimeSpec=TimeSpec.NORMAL) -> str:
|
||||||
return iso8601_format(utcnow(), milliseconds=milliseconds)
|
return iso8601_format(utcnow(), timespec)
|
||||||
|
|
||||||
def iso8601_parse(s: str) -> _datetime.datetime:
|
def iso8601_parse(s: str) -> _datetime.datetime:
|
||||||
return dateutil.parser.parse(s)
|
return dateutil.parser.parse(s)
|
||||||
|
|
||||||
def datetime_human(dt: _datetime.datetime):
|
def datetime_human(dt: _datetime.datetime, timespec: TimeSpec=TimeSpec.NORMAL):
|
||||||
return _datetime.datetime.strftime(dt, DATETIME_HUMAN)
|
date = _datetime.datetime.strftime(dt, DATE_HUMAN)
|
||||||
def date_human(dt: _datetime.datetime):
|
time = _datetime.datetime.strftime(dt, TIME_HUMAN)
|
||||||
|
if timespec == TimeSpec.MILLISECOND:
|
||||||
|
time += ".%s" % int(dt.microsecond/1000)
|
||||||
|
return "%s %s" % (date, time)
|
||||||
|
def date_human(dt: _datetime.datetime, timespec: TimeSpec=TimeSpec.NORMAL):
|
||||||
return _datetime.datetime.strftime(dt, DATE_HUMAN)
|
return _datetime.datetime.strftime(dt, DATE_HUMAN)
|
||||||
|
|
||||||
TIME_SECOND = 1
|
TIME_SECOND = 1
|
||||||
|
|
Loading…
Reference in a new issue