Skip to content

Commit

Permalink
Simplify _ParseDateTime (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
helly25 authored Aug 31, 2024
1 parent f2c49a6 commit b510154
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions mbo/app/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,13 @@

import argparse
import collections
import re
import sys
from datetime import datetime, time, timedelta, timezone, tzinfo
from enum import Enum
from typing import Any, Callable, Iterable, Optional, cast

from pytimeparse.timeparse import timeparse


def _Log(message=Any):
print(message, flush=True, file=sys.stderr)


class EnumAction(argparse.Action):
"""Argparse action that handles single Enum values."""

Expand Down Expand Up @@ -178,17 +172,10 @@ def _ParseDateTime(
if value is datetime:
return _MaybeMidnight(cast(datetime, value), midnight=midnight, tz=tz)
v = str(value)
if re.fullmatch("[0-9]{8}", v):
try:
return datetime(
year=int(v[0:4]),
month=int(v[4:6]),
day=int(v[6:8]),
tzinfo=tz,
)
except ValueError as err:
raise ValueError(f"Invalid date string: '{v}', {err}")
return _MaybeMidnight(datetime.fromisoformat(v), midnight=midnight, tz=tz)
try:
return _MaybeMidnight(datetime.fromisoformat(v), midnight=midnight, tz=tz)
except ValueError as err:
raise ValueError(f"Invalid date string: '{v}', {err}")


def ParseDateTimeOrTimeDelta(
Expand Down

0 comments on commit b510154

Please sign in to comment.