import requests
from datetime import datetime
import pytz
class Tools:
"""
CW_NTP Tool to retrieve and display the current date and time in Detroit, USA.
"""
def get_detroit_time(self) -> str:
"""
Fetches the current time from an internet time server, formatted for Detroit, USA.
"""
try:
response = requests.get("http://worldtimeapi.org/api/timezone/America/Detroit")
response.raise_for_status()
data = response.json()
detroit_time = datetime.fromisoformat(data["datetime"]).astimezone(pytz.timezone("America/Detroit"))
return detroit_time.strftime("%A, %B %d, %Y %I:%M:%S %p (Detroit Local Time)")
except requests.RequestException:
return "Unable to fetch current time."
def preface_with_time(self, text: str) -> str:
"""
Adds the Detroit timestamp to the start of a response.
"""
current_time = self.get_detroit_time()
return f"{current_time}\n\n{text}"