Skip to content

Commit

Permalink
fix(practice): correctly parse dates in the near future (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria authored Nov 1, 2020
1 parent fef7cb0 commit 495d478
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
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

0 comments on commit 495d478

Please sign in to comment.