Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify _ParseDateTime #9

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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