Spaces:
Sleeping
Sleeping
Use IANA format for timezone
Browse files
app.py
CHANGED
|
@@ -7,15 +7,65 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
def get_current_local_time_and_timezone()-> str:
|
| 13 |
"""A tool that gets the time and the timezone of the user
|
| 14 |
Args:
|
| 15 |
"""
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
@tool
|
| 21 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
import os
|
| 12 |
+
|
| 13 |
+
def get_iana_zone_from_etc():
|
| 14 |
+
"""
|
| 15 |
+
Attempt to read /etc/localtime as a symlink and extract the IANA timezone key.
|
| 16 |
+
This works on many Unix-like systems where /etc/localtime is a symlink to a file
|
| 17 |
+
inside a directory like /usr/share/zoneinfo/.
|
| 18 |
+
"""
|
| 19 |
+
etc_localtime = "/etc/localtime"
|
| 20 |
+
if os.path.exists(etc_localtime) and os.path.islink(etc_localtime):
|
| 21 |
+
try:
|
| 22 |
+
link = os.readlink(etc_localtime)
|
| 23 |
+
# If the link contains 'zoneinfo', we assume the part after it is the zone name.
|
| 24 |
+
if "zoneinfo" in link:
|
| 25 |
+
# Normalize path separators
|
| 26 |
+
parts = link.split(os.sep)
|
| 27 |
+
if "zoneinfo" in parts:
|
| 28 |
+
idx = parts.index("zoneinfo")
|
| 29 |
+
return os.sep.join(parts[idx+1:])
|
| 30 |
+
except OSError:
|
| 31 |
+
pass
|
| 32 |
+
return None
|
| 33 |
+
|
| 34 |
+
def get_local_timezone_name():
|
| 35 |
+
"""
|
| 36 |
+
Attempt to obtain a human-readable IANA timezone name.
|
| 37 |
+
First, check if the tzinfo object from datetime.now().astimezone() has a 'key' or 'zone' attribute.
|
| 38 |
+
If not, try to extract it from /etc/localtime (Unix only) or from the TZ environment variable.
|
| 39 |
+
"""
|
| 40 |
+
local_dt = datetime.datetime.now().astimezone()
|
| 41 |
+
tz = local_dt.tzinfo
|
| 42 |
+
# Try to get the IANA name from the tzinfo object
|
| 43 |
+
tz_name = getattr(tz, 'key', None) or getattr(tz, 'zone', None)
|
| 44 |
+
# If the name is an offset (e.g. "+01"), we want to try a fallback.
|
| 45 |
+
if tz_name is None or tz_name.startswith(('+', '-')):
|
| 46 |
+
# First try reading /etc/localtime (works on many Unix systems)
|
| 47 |
+
zone_from_etc = get_iana_zone_from_etc()
|
| 48 |
+
if zone_from_etc:
|
| 49 |
+
return zone_from_etc
|
| 50 |
+
# Next, check if TZ environment variable is set to an IANA name.
|
| 51 |
+
tz_env = os.environ.get("TZ")
|
| 52 |
+
if tz_env:
|
| 53 |
+
return tz_env
|
| 54 |
+
# Otherwise, fallback to the string representation of tzinfo
|
| 55 |
+
return str(tz)
|
| 56 |
+
return tz_name
|
| 57 |
+
|
| 58 |
+
|
| 59 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 60 |
@tool
|
| 61 |
def get_current_local_time_and_timezone()-> str:
|
| 62 |
"""A tool that gets the time and the timezone of the user
|
| 63 |
Args:
|
| 64 |
"""
|
| 65 |
+
local_dt = datetime.datetime.now().astimezone()
|
| 66 |
+
current_time = local_dt.strftime('%Y-%m-%d %H:%M:%S')
|
| 67 |
+
timezone_name = get_local_timezone_name()
|
| 68 |
+
return f"The current local time in {timezone_name} is: {current_time}"
|
| 69 |
|
| 70 |
@tool
|
| 71 |
def get_current_time_in_timezone(timezone: str) -> str:
|