Skip to content

Commit a2e17e5

Browse files
committed
Implement API method to get formatted date/time
1 parent de92e83 commit a2e17e5

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ class _CurrentTimeRequest(BaseModel):
8181
class _CurrentTimeResponse(BaseModel):
8282
current_timestamp: float
8383

84+
class _FormattedTimeResponse(BaseModel):
85+
formatted_time: str
86+
formatted_date: str
87+
current_weekday: str
8488

8589
def speakable_timezone(tz: str) -> str:
8690
"""Convert timezone to a better speakable version
@@ -240,6 +244,24 @@ def get_current_time(self) -> \
240244
"""
241245
return _CurrentTimeResponse(current_timestamp=time())
242246

247+
@skill_api_method
248+
def get_formatted_time(self, request: _CurrentTimeRequest) -> \
249+
_FormattedTimeResponse:
250+
"""
251+
Get the current time formatted as time, date, and weekday strings
252+
"""
253+
location = request.location or self.location['city']['name']
254+
dt = self.get_local_datetime(location)
255+
if not dt:
256+
raise ValueError(f"Invalid location: {location}")
257+
formatted_time = dt.strftime("%H:%M")
258+
formatted_date = dt.strftime("%Y-%m-%d")
259+
current_weekday = dt.strftime("%A")
260+
return _FormattedTimeResponse(formatted_time=formatted_time,
261+
formatted_date=formatted_date,
262+
current_weekday=current_weekday)
263+
264+
243265
@skill_api_method
244266
def get_display_date(self, day: Optional[datetime] = None,
245267
location: Optional[str] = None,

0 commit comments

Comments
 (0)