Make options accept default kwarg, add new raise_exception. Fix datetime validatorfunc accoring to spec. Resolves #1967.

This commit is contained in:
Griatch 2020-01-20 23:20:05 +01:00
parent 6e561d95a1
commit bfe533441e
5 changed files with 38 additions and 14 deletions

View file

@ -40,24 +40,36 @@ def color(entry, option_key="Color", **kwargs):
def datetime(entry, option_key="Datetime", account=None, from_tz=None, **kwargs):
"""
Process a datetime string in standard forms while accounting for the inputter's timezone.
Process a datetime string in standard forms while accounting for the inputer's timezone. Always
returns a result in UTC.
Args:
entry (str): A date string from a user.
option_key (str): Name to display this datetime as.
account (AccountDB): The Account performing this lookup. Unless from_tz is provided,
account's timezone will be used (if found) for local time and convert the results
to UTC.
from_tz (pytz): An instance of pytz from the user. If not provided, defaults to whatever
the Account uses. If neither one is provided, defaults to UTC.
account (AccountDB): The Account performing this lookup. Unless `from_tz` is provided,
the account's timezone option will be used.
from_tz (pytz.timezone): An instance of a pytz timezone object from the
user. If not provided, tries to use the timezone option of the `account'.
If neither one is provided, defaults to UTC.
Returns:
datetime in utc.
datetime in UTC.
Raises:
ValueError: If encountering a malformed timezone, date string or other format error.
"""
if not entry:
raise ValueError(f"No {option_key} entered!")
if not from_tz:
from_tz = _pytz.UTC
if account:
acct_tz = account.options.get("timezone", "UTC")
try:
from_tz = _pytz.timezone(acct_tz)
except Exception as err:
raise ValueError(f"Timezone string '{acct_tz}' is not a valid timezone ({err})")
else:
from_tz = _pytz.UTC
utc = _pytz.UTC
now = _dt.datetime.utcnow().replace(tzinfo=utc)
cur_year = now.strftime("%Y")