Spaces:
Sleeping
Sleeping
Updated the timezone function in app.py script with the robust and recommended pytz implementation
Browse files
app.py
CHANGED
|
@@ -22,17 +22,30 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
| 24 |
Args:
|
| 25 |
-
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
|
|
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
#
|
| 29 |
tz = pytz.timezone(timezone)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
|
|
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
| 24 |
Args:
|
| 25 |
+
timezone: A string representing a valid IANA timezone (e.g., 'America/New_York').
|
| 26 |
+
IANA Time Zones are standardized by the Internet Assigned Numbers Authority.
|
| 27 |
"""
|
| 28 |
try:
|
| 29 |
+
# 1. Get the timezone object
|
| 30 |
tz = pytz.timezone(timezone)
|
| 31 |
+
|
| 32 |
+
# 2. Get the current UTC time and make it timezone-aware
|
| 33 |
+
# The preferred way: Use pytz.utc.localize(datetime.utcnow()) and then astimezone()
|
| 34 |
+
utc_dt = pytz.utc.localize(datetime.datetime.utcnow())
|
| 35 |
+
|
| 36 |
+
# 3. Convert the UTC time to the target timezone
|
| 37 |
+
local_time = utc_dt.astimezone(tz)
|
| 38 |
+
|
| 39 |
+
# 4. Format the time
|
| 40 |
+
formatted_time = local_time.strftime("%A, %B %d, %Y at %I:%M:%S %p %Z")
|
| 41 |
+
|
| 42 |
+
return f"The current local time in {timezone} is: {formatted_time}"
|
| 43 |
+
|
| 44 |
+
except pytz.exceptions.UnknownTimeZoneError:
|
| 45 |
+
return f"Error: The timezone '{timezone}' is not recognized."
|
| 46 |
except Exception as e:
|
| 47 |
+
# Catching any other execution error and returning a message instead of crashing
|
| 48 |
+
return f"An unexpected error occurred while getting the time: {e}"
|
| 49 |
|
| 50 |
final_answer = FinalAnswerTool()
|
| 51 |
|