more strict delta parsing

This commit is contained in:
Alexander Nigl 2025-04-23 12:48:51 +02:00
parent 4ccabfc661
commit a1f58829a6

View File

@ -24,18 +24,19 @@ DATETIME_FORMAT = [
class DateTime(click.DateTime):
def _try_to_convert_date(self, value: t.Any, format: str) -> t.Optional[datetime]:
try:
if format == DELTASECONDS and value[-1] == "s":
return datetime.datetime.now() + datetime.timedelta(
seconds=int(value[1:-1])
)
if format == DELTAMINUTES and value[-1] == "m":
return datetime.datetime.now() + datetime.timedelta(
minutes=int(value[1:-1])
)
if format == DELTAHOURS and value[-1] == "h":
return datetime.datetime.now() + datetime.timedelta(
hours=int(value[1:-1])
)
if value[0] == "+":
if format == DELTASECONDS and value[-1] == "s":
return datetime.datetime.now() + datetime.timedelta(
seconds=int(value[1:-1])
)
if format == DELTAMINUTES and value[-1] == "m":
return datetime.datetime.now() + datetime.timedelta(
minutes=int(value[1:-1])
)
if format == DELTAHOURS and value[-1] == "h":
return datetime.datetime.now() + datetime.timedelta(
hours=int(value[1:-1])
)
if format == HOURS:
return datetime.datetime.combine(
datetime.datetime.now().date(),