Skip to content
Open
Changes from 4 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
25 changes: 24 additions & 1 deletion Backend/agent/handlers/appointment.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,42 @@ def parse_appointment_command(query: str):
}

def parse_date(date_str):

"""Parse date string to ISO format."""
if not date_str:
return None

today = datetime.now()
date_str_lower = date_str.lower()
allowed_days = {
"mon": 0, "monday": 0,
"tue": 1, "tuesday": 1,
"wed": 2, "wednesday": 2,
"thu": 3, "thursday": 3,
"fri": 4, "friday": 4,
"sat": 5, "saturday": 5,
"sun": 6, "sunday": 6,
}

if date_str_lower == 'today':
return today.strftime('%Y-%m-%d')
elif date_str_lower == 'tomorrow':
return (today + timedelta(days=1)).strftime('%Y-%m-%d')
elif date_str_lower == 'next week':
return (today + timedelta(days=7)).strftime('%Y-%m-%d')
elif date_str_lower == 'next month':
month = today.month + 1 if today.month < 12 else 1
year = today.year if today.month < 12 else today.year + 1
day = 1 # Normalize to first day of next month
return f"{year}-{month:02d}-{day:02d}"
elif date_str_lower in allowed_days:
# Handles next occurrence of the specified day
target_day = allowed_days[date_str_lower]
days_ahead = target_day - today.weekday()
if days_ahead <= 0:
days_ahead += 7
target_date = today + timedelta(days=days_ahead)
return target_date.strftime('%Y-%m-%d')

# Try to parse as MM/DD or MM/DD/YYYY
try:
Expand Down Expand Up @@ -231,4 +254,4 @@ def handle(query: str, user_context=None):
for r in rows
)

return "\n".join(response_parts)
return "\n".join(response_parts)