Skip to content
Snippets Groups Projects
Verified Commit aade334f authored by Ernesto Rico Schmidt's avatar Ernesto Rico Schmidt
Browse files

feat: handle no date/time/datetime string given

parent c90c83fe
No related branches found
No related tags found
No related merge requests found
...@@ -32,10 +32,16 @@ if typing.TYPE_CHECKING: ...@@ -32,10 +32,16 @@ if typing.TYPE_CHECKING:
from program.models import Host, Note, Show from program.models import Host, Note, Show
def parse_datetime(date_string: str) -> datetime: def parse_datetime(date_string: str | None) -> datetime | None:
""" """
parse a datetime string and return a timezone aware datetime object parse a datetime string and return a timezone aware datetime object.
Returns `None` if no datetime string is given.
""" """
if date_string is None:
return None
try: try:
parsed_datetime = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S") parsed_datetime = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
except ValueError: except ValueError:
...@@ -48,18 +54,29 @@ def parse_datetime(date_string: str) -> datetime: ...@@ -48,18 +54,29 @@ def parse_datetime(date_string: str) -> datetime:
) )
def parse_date(date_string: str) -> date: def parse_date(date_string: str | None) -> date | None:
""" """
parse a date string and return a date object parse a date string and return a date object.
Returns `None` if no date string is given.
""" """
if date_string is None:
return None
return datetime.strptime(date_string, "%Y-%m-%d").date() return datetime.strptime(date_string, "%Y-%m-%d").date()
def parse_time(date_string: str) -> time: def parse_time(date_string: str | None) -> time | None:
""" """
parse a time string and return a time object parse a time string and return a time object.
Returns `None` if no time string is given.
""" """
if date_string is None:
return None
if len(date_string) == 5: if len(date_string) == 5:
return datetime.strptime(date_string, "%H:%M").time() return datetime.strptime(date_string, "%H:%M").time()
else: else:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment