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

fix(practice): correctly parse dates in the near future #47

Merged
merged 1 commit into from
Nov 1, 2020
Merged
Show file tree
Hide file tree
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
19 changes: 17 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,22 @@ async def schedule_command(ctx: Context, *, when: Optional[str]):
)


def parse_practice_time(human_readable_datetime: str) -> Optional[dt.datetime]:
# First try current_period to capture dates in the near future
dtime = parse_human_readable_datetime(
human_readable_datetime, settings={"PREFER_DATES_FROM": "current_period"}
)
# Can't parse into datetime, return early
if dtime is None:
return dtime
# If date is in the past, prefer future dates
if dtime < utcnow():
dtime = parse_human_readable_datetime(
human_readable_datetime, settings={"PREFER_DATES_FROM": "future"}
)
return dtime


def practice_impl(*, guild_id: int, host: str, start_time: str):
if start_time.lower() in {
# Common mistakes: don't try to parse these into a datetime
Expand All @@ -519,8 +535,7 @@ def practice_impl(*, guild_id: int, host: str, start_time: str):
raise commands.errors.BadArgument(PRACTICE_ERROR)
logger.info(f"attempting to schedule new practice session: {start_time}")
human_readable_datetime, quoted = get_and_strip_quoted_text(start_time)
settings = dict(PREFER_DATES_FROM="future")
dtime = parse_human_readable_datetime(human_readable_datetime, settings=settings)
dtime = parse_practice_time(human_readable_datetime)
if not dtime:
raise commands.errors.BadArgument(
f'⚠️Could not parse "{start_time}" into a date or time. Make sure to include "am" or "pm" as well as a timezone, e.g. "{PACIFIC_CURRENT_NAME.lower()}".'
Expand Down
9 changes: 9 additions & 0 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ def test_practice(snapshot, monkeypatch, mock_worksheet, start_time):
assert mock_worksheet.append_row.call_args[0][0] == snapshot


def test_practice_nearby_date(snapshot, mock_worksheet):
with freeze_time("2020-09-25 14:00:00"):
bot.practice_impl(guild_id=1234, host="Steve", start_time="10:30am edt")

mock_worksheet.append_row.assert_called_once()
appended_row = mock_worksheet.append_row.call_args[0][0]
assert appended_row == ("Friday, September 25 07:30 AM PDT 2020", "Steve", "")


@pytest.mark.parametrize(
"start_time",
(
Expand Down