add 'date' command spec type

This commit is contained in:
jesopo 2020-01-30 21:21:12 +00:00
parent d0d5cc4d08
commit 5d3e1ea492
2 changed files with 16 additions and 0 deletions

View file

@ -6,6 +6,12 @@ from .common import *
def iso8601(s: str) -> _datetime.datetime:
return dateutil.parser.parse(s)
def date_human(s: str) -> typing.Optional[_datetime.datetime]:
try:
return _datetime.datetime.strptime(s, DATE_HUMAN)
except ValueError:
return None
REGEX_PRETTYTIME = re.compile(
r"(?:(\d+)w)?(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?", re.I)

View file

@ -1,6 +1,7 @@
import enum, typing
from .time import duration
from .types import try_int
from src.utils.datetime.parse import date_human
class SpecArgumentContext(enum.IntFlag):
CHANNEL = 1
@ -67,6 +68,14 @@ class SpecArgumentTypeDuration(SpecArgumentType):
def error(self) -> typing.Optional[str]:
return "Invalid timeframe"
class SpecArgumentTypeDate(SpecArgumentType):
def name(self):
return SpecArgumentType.name(self) or "yyyy-mm-dd"
def simple(self, args):
if args:
return date_human(args[0], 1)
return None, 1
class SpecArgumentPrivateType(SpecArgumentType):
context = SpecArgumentContext.PRIVATE
@ -77,6 +86,7 @@ SPEC_ARGUMENT_TYPES = {
"string": SpecArgumentTypeString,
"tstring": SpecArgumentTypeTrimString,
"int": SpecArgumentTypeInt,
"date": SpecArgumentTypeDate,
"duration": SpecArgumentTypeDuration
}