mirror of
https://github.com/Sea-Haven-Industries/afterhours-shift-manager.git
synced 2026-09-30 07:53:11 +00:00
* Expand /oncall date parser to accept more formats Users entering everyday forms like 7/3/26 hit a generic parse failure because the parser only accepted four-digit years and a bare m/d. Add two-digit-year and month-name (with optional ordinal/year) formats, treating explicit-year inputs as fixed and keeping the year-less roll-forward for bare m/d. Update the help text and per-command parse hints to match. Refs: #133 * Add dash 4-digit year format and pin year boundary Dash inputs like 7-3-2026 previously returned None because only the slash variant had a 4-digit-year format. Add %m-%d-%Y so dash and slash behave alike, and pin the two-digit-year century boundary with a test. Refs: #133 --------- Co-authored-by: amoussa1229 <166072409+amoussa1229@users.noreply.github.com>
This commit is contained in:
parent
b304121cff
commit
eb78a98de0
3 changed files with 101 additions and 10 deletions
|
|
@ -439,7 +439,7 @@ def build_help_blocks(is_admin: bool = False) -> list[dict]:
|
||||||
"`/oncall rate <ext> <amount>` — Set a person's rate\n"
|
"`/oncall rate <ext> <amount>` — Set a person's rate\n"
|
||||||
"`/oncall roster` — Show all registered employees\n"
|
"`/oncall roster` — Show all registered employees\n"
|
||||||
"`/oncall help` — Show this help message\n\n"
|
"`/oncall help` — Show this help message\n\n"
|
||||||
"_Dates can be: today, tomorrow, monday, friday, 4/5, 2026-04-05_"
|
"_Dates can be: today, tomorrow, monday, friday, 4/5, 7/3/26, jul 3, 2026-04-05_"
|
||||||
)
|
)
|
||||||
if is_admin:
|
if is_admin:
|
||||||
text += (
|
text += (
|
||||||
|
|
|
||||||
|
|
@ -57,8 +57,28 @@ DAY_NAMES = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
# Formats carrying an explicit year — parsed as-is, never rolled forward.
|
||||||
|
_DATED_FORMATS = (
|
||||||
|
"%Y-%m-%d",
|
||||||
|
"%m/%d/%Y",
|
||||||
|
"%m/%d/%y",
|
||||||
|
"%m-%d-%Y",
|
||||||
|
"%m-%d-%y",
|
||||||
|
"%b %d %Y",
|
||||||
|
"%B %d %Y",
|
||||||
|
"%b %d %y",
|
||||||
|
"%B %d %y",
|
||||||
|
)
|
||||||
|
# Year-less formats — stamped with the current year, then rolled to next year
|
||||||
|
# if already more than a day in the past.
|
||||||
|
_UNDATED_FORMATS = ("%m/%d", "%m-%d", "%b %d", "%B %d")
|
||||||
|
|
||||||
|
_ORDINAL_RE = re.compile(r"(\d+)(st|nd|rd|th)\b")
|
||||||
|
|
||||||
|
|
||||||
def parse_date(text: str) -> datetime | None:
|
def parse_date(text: str) -> datetime | None:
|
||||||
"""Parse flexible date input: today, tomorrow, day names, m/d, YYYY-MM-DD."""
|
"""Parse flexible date input: today, tomorrow, day names, m/d, m/d/yy,
|
||||||
|
YYYY-MM-DD, and month-name forms (e.g. ``jul 3``, ``july 3rd 2026``)."""
|
||||||
now = datetime.now(EASTERN)
|
now = datetime.now(EASTERN)
|
||||||
text = text.strip().lower()
|
text = text.strip().lower()
|
||||||
|
|
||||||
|
|
@ -75,13 +95,19 @@ def parse_date(text: str) -> datetime | None:
|
||||||
delta = 7
|
delta = 7
|
||||||
return now + timedelta(days=delta)
|
return now + timedelta(days=delta)
|
||||||
|
|
||||||
for fmt in ("%Y-%m-%d", "%m/%d/%Y", "%m/%d", "%m-%d"):
|
candidate = _ORDINAL_RE.sub(r"\1", text)
|
||||||
|
|
||||||
|
for fmt in _DATED_FORMATS:
|
||||||
try:
|
try:
|
||||||
parsed = datetime.strptime(text, fmt)
|
return datetime.strptime(candidate, fmt).replace(tzinfo=EASTERN)
|
||||||
if "%Y" not in fmt:
|
except ValueError:
|
||||||
parsed = parsed.replace(year=now.year)
|
continue
|
||||||
if parsed.replace(tzinfo=EASTERN) < now - timedelta(days=1):
|
|
||||||
parsed = parsed.replace(year=now.year + 1)
|
for fmt in _UNDATED_FORMATS:
|
||||||
|
try:
|
||||||
|
parsed = datetime.strptime(candidate, fmt).replace(year=now.year)
|
||||||
|
if parsed.replace(tzinfo=EASTERN) < now - timedelta(days=1):
|
||||||
|
parsed = parsed.replace(year=now.year + 1)
|
||||||
return parsed.replace(tzinfo=EASTERN)
|
return parsed.replace(tzinfo=EASTERN)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
continue
|
continue
|
||||||
|
|
@ -673,7 +699,7 @@ def _handle_pick(
|
||||||
date = parse_date(date_text)
|
date = parse_date(date_text)
|
||||||
if not date:
|
if not date:
|
||||||
respond(
|
respond(
|
||||||
text=f"Couldn't parse date: `{date_text}`. Try: today, tomorrow, friday, 4/5, 2026-04-05"
|
text=f"Couldn't parse date: `{date_text}`. Try: today, tomorrow, friday, 4/5, 7/3/26, jul 3, 2026-04-05"
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -907,7 +933,7 @@ def _handle_drop(
|
||||||
date = parse_date(parts[1])
|
date = parse_date(parts[1])
|
||||||
if not date:
|
if not date:
|
||||||
respond(
|
respond(
|
||||||
text=f"Couldn't parse date: `{parts[1]}`. Try: today, tomorrow, friday, 4/5, 2026-04-05"
|
text=f"Couldn't parse date: `{parts[1]}`. Try: today, tomorrow, friday, 4/5, 7/3/26, jul 3, 2026-04-05"
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,71 @@ def test_case_insensitive(slackbot_app):
|
||||||
assert slackbot_app.parse_date(" FRIDAY ").strftime("%Y-%m-%d") == "2026-06-05"
|
assert slackbot_app.parse_date(" FRIDAY ").strftime("%Y-%m-%d") == "2026-06-05"
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time(FROZEN)
|
||||||
|
def test_two_digit_year_slash(slackbot_app):
|
||||||
|
assert slackbot_app.parse_date("7/3/26").strftime("%Y-%m-%d") == "2026-07-03"
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time(FROZEN)
|
||||||
|
def test_two_digit_year_padded(slackbot_app):
|
||||||
|
assert slackbot_app.parse_date("07/03/26").strftime("%Y-%m-%d") == "2026-07-03"
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time(FROZEN)
|
||||||
|
def test_two_digit_year_dash(slackbot_app):
|
||||||
|
assert slackbot_app.parse_date("7-3-26").strftime("%Y-%m-%d") == "2026-07-03"
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time(FROZEN)
|
||||||
|
def test_four_digit_year_dash(slackbot_app):
|
||||||
|
assert slackbot_app.parse_date("7-3-2026").strftime("%Y-%m-%d") == "2026-07-03"
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time(FROZEN)
|
||||||
|
def test_two_digit_year_century_boundary(slackbot_app):
|
||||||
|
# Python's %y maps 00–68 → 2000s and 69–99 → 1900s. Pin it so nobody
|
||||||
|
# "fixes" the ambiguity by accident later.
|
||||||
|
assert slackbot_app.parse_date("12/31/99").year == 1999
|
||||||
|
assert slackbot_app.parse_date("1/1/68").year == 2068
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time(FROZEN)
|
||||||
|
def test_two_digit_year_past_not_rolled(slackbot_app):
|
||||||
|
# Explicit two-digit year in the past stays put — no roll-forward.
|
||||||
|
assert slackbot_app.parse_date("1/1/26").strftime("%Y-%m-%d") == "2026-01-01"
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time(FROZEN)
|
||||||
|
def test_month_name_short(slackbot_app):
|
||||||
|
assert slackbot_app.parse_date("jul 3").strftime("%Y-%m-%d") == "2026-07-03"
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time(FROZEN)
|
||||||
|
def test_month_name_full(slackbot_app):
|
||||||
|
assert slackbot_app.parse_date("july 3").strftime("%Y-%m-%d") == "2026-07-03"
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time(FROZEN)
|
||||||
|
def test_month_name_with_year(slackbot_app):
|
||||||
|
assert slackbot_app.parse_date("july 3 2026").strftime("%Y-%m-%d") == "2026-07-03"
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time(FROZEN)
|
||||||
|
def test_month_name_ordinal(slackbot_app):
|
||||||
|
assert slackbot_app.parse_date("july 3rd").strftime("%Y-%m-%d") == "2026-07-03"
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time(FROZEN)
|
||||||
|
def test_month_name_past_wraps_to_next_year(slackbot_app):
|
||||||
|
# Year-less month name in the past rolls forward, like m/d.
|
||||||
|
assert slackbot_app.parse_date("jan 1").year == 2027
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time(FROZEN)
|
||||||
|
def test_eastern_tz_stamped(slackbot_app):
|
||||||
|
assert slackbot_app.parse_date("7/3/26").tzinfo is not None
|
||||||
|
|
||||||
|
|
||||||
@freeze_time(FROZEN)
|
@freeze_time(FROZEN)
|
||||||
def test_invalid_returns_none(slackbot_app):
|
def test_invalid_returns_none(slackbot_app):
|
||||||
assert slackbot_app.parse_date("notaday") is None
|
assert slackbot_app.parse_date("notaday") is None
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue