Skip to content

Commit 8036305

Browse files
committed
Move date formatting logic to utils_date.py
Refactored the format_dates_in_messages function from app_kernel.py to common/utils/utils_date.py for better modularity and reuse. Updated imports to reflect the new location.
1 parent 1b72057 commit 8036305

File tree

2 files changed

+54
-53
lines changed

2 files changed

+54
-53
lines changed

src/backend/app_kernel.py

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from common.config.app_config import config
1313
from auth.auth_utils import get_authenticated_user_details
1414
from azure.monitor.opentelemetry import configure_azure_monitor
15-
from dateutil import parser
1615
from common.utils.event_utils import track_event_if_configured
1716

1817
# FastAPI imports
@@ -39,6 +38,7 @@
3938
from common.utils.utils_kernel import rai_success
4039

4140
from common.database.database_factory import DatabaseFactory
41+
from common.utils.utils_date import format_dates_in_messages
4242
from v3.api.router import app_v3
4343

4444
# Check if the Application Insights Instrumentation Key is set in the environment variables
@@ -92,58 +92,6 @@
9292
logging.info("Added health check middleware")
9393

9494

95-
def format_dates_in_messages(messages, target_locale="en-US"):
96-
"""
97-
Format dates in agent messages according to the specified locale.
98-
99-
Args:
100-
messages: List of message objects or string content
101-
target_locale: Target locale for date formatting (default: en-US)
102-
103-
Returns:
104-
Formatted messages with dates converted to target locale format
105-
"""
106-
# Define target format patterns per locale
107-
locale_date_formats = {
108-
"en-IN": "%d %b %Y", # 30 Jul 2025
109-
"en-US": "%b %d, %Y", # Jul 30, 2025
110-
}
111-
112-
output_format = locale_date_formats.get(target_locale, "%d %b %Y")
113-
# Match both "Jul 30, 2025, 12:00:00 AM" and "30 Jul 2025"
114-
date_pattern = r"(\d{1,2} [A-Za-z]{3,9} \d{4}|[A-Za-z]{3,9} \d{1,2}, \d{4}(, \d{1,2}:\d{2}:\d{2} ?[APap][Mm])?)"
115-
116-
def convert_date(match):
117-
date_str = match.group(0)
118-
try:
119-
dt = parser.parse(date_str)
120-
return dt.strftime(output_format)
121-
except Exception:
122-
return date_str # Leave it unchanged if parsing fails
123-
124-
# Process messages
125-
if isinstance(messages, list):
126-
formatted_messages = []
127-
for message in messages:
128-
if hasattr(message, "content") and message.content:
129-
# Create a copy of the message with formatted content
130-
formatted_message = (
131-
message.model_copy() if hasattr(message, "model_copy") else message
132-
)
133-
if hasattr(formatted_message, "content"):
134-
formatted_message.content = re.sub(
135-
date_pattern, convert_date, formatted_message.content
136-
)
137-
formatted_messages.append(formatted_message)
138-
else:
139-
formatted_messages.append(message)
140-
return formatted_messages
141-
elif isinstance(messages, str):
142-
return re.sub(date_pattern, convert_date, messages)
143-
else:
144-
return messages
145-
146-
14795
@app.post("/api/user_browser_language")
14896
async def user_browser_language_endpoint(user_language: UserLanguage, request: Request):
14997
"""

src/backend/common/utils/utils_date.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from datetime import datetime
44
import logging
55
from typing import Optional
6+
from dateutil import parser
67

78

89
def format_date_for_user(date_str: str, user_locale: Optional[str] = None) -> str:
@@ -32,3 +33,55 @@ def default(self, obj):
3233
if isinstance(obj, datetime):
3334
return obj.isoformat()
3435
return super().default(obj)
36+
37+
38+
def format_dates_in_messages(messages, target_locale="en-US"):
39+
"""
40+
Format dates in agent messages according to the specified locale.
41+
42+
Args:
43+
messages: List of message objects or string content
44+
target_locale: Target locale for date formatting (default: en-US)
45+
46+
Returns:
47+
Formatted messages with dates converted to target locale format
48+
"""
49+
# Define target format patterns per locale
50+
locale_date_formats = {
51+
"en-IN": "%d %b %Y", # 30 Jul 2025
52+
"en-US": "%b %d, %Y", # Jul 30, 2025
53+
}
54+
55+
output_format = locale_date_formats.get(target_locale, "%d %b %Y")
56+
# Match both "Jul 30, 2025, 12:00:00 AM" and "30 Jul 2025"
57+
date_pattern = r"(\d{1,2} [A-Za-z]{3,9} \d{4}|[A-Za-z]{3,9} \d{1,2}, \d{4}(, \d{1,2}:\d{2}:\d{2} ?[APap][Mm])?)"
58+
59+
def convert_date(match):
60+
date_str = match.group(0)
61+
try:
62+
dt = parser.parse(date_str)
63+
return dt.strftime(output_format)
64+
except Exception:
65+
return date_str # Leave it unchanged if parsing fails
66+
67+
# Process messages
68+
if isinstance(messages, list):
69+
formatted_messages = []
70+
for message in messages:
71+
if hasattr(message, "content") and message.content:
72+
# Create a copy of the message with formatted content
73+
formatted_message = (
74+
message.model_copy() if hasattr(message, "model_copy") else message
75+
)
76+
if hasattr(formatted_message, "content"):
77+
formatted_message.content = re.sub(
78+
date_pattern, convert_date, formatted_message.content
79+
)
80+
formatted_messages.append(formatted_message)
81+
else:
82+
formatted_messages.append(message)
83+
return formatted_messages
84+
elif isinstance(messages, str):
85+
return re.sub(date_pattern, convert_date, messages)
86+
else:
87+
return messages

0 commit comments

Comments
 (0)