Added the parse_date() utility to the Mia core application.

This commit is contained in:
依瑪貓 2020-09-03 23:27:44 +08:00
parent bb0478a597
commit 0adec7557b

View File

@ -18,6 +18,7 @@
"""The utilities of the Mia core application. """The utilities of the Mia core application.
""" """
import datetime
import random import random
import urllib.parse import urllib.parse
from typing import Dict, List, Any, Type from typing import Dict, List, Any, Type
@ -58,6 +59,27 @@ def strip_post(post: Dict[str, str]) -> None:
del post[key] del post[key]
def parse_date(s: str):
"""Parses a string for a date. The date can be either YYYY-MM-DD,
Y/M/D, or M/D/Y.
Args:
s: The string.
Returns:
The date.
Raises:
ValueError: When the string is not in a valid format.
"""
for f in ["%Y-%m-%d", "%m/%d/%Y", "%Y/%m/%d"]:
try:
return datetime.datetime.strptime(s, f)
except ValueError:
pass
raise ValueError(F"not a recognized date {s}")
class Language: class Language:
"""A language. """A language.