From 4f603a3e17e47ea3ceb665b1f4f3f6eaecf80fb3 Mon Sep 17 00:00:00 2001 From: Steven Loria Date: Sun, 1 Nov 2020 10:40:28 -0500 Subject: [PATCH] fix(practice): correctly parse dates in the near future --- bot.py | 19 +++++++++++++++++-- tests/test_bot.py | 9 +++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index 1ff82a64..43ee67a0 100644 --- a/bot.py +++ b/bot.py @@ -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 @@ -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()}".' diff --git a/tests/test_bot.py b/tests/test_bot.py index 04f7e6b9..90834da0 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -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", (