Spaces:
Sleeping
Sleeping
Add tools to have the countries that are in the same time zone
Browse files
app.py
CHANGED
|
@@ -9,14 +9,50 @@ 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
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
+
def countries_with_same_current_offset(timezone : str, return_country_names : bool = True) -> str:
|
| 13 |
+
"""
|
| 14 |
+
Return a list of all countries that have the same current UTC offset
|
| 15 |
+
as the given timezone_name.
|
| 16 |
+
Args :
|
| 17 |
+
timezone: A string, e.g. 'America/New_York'
|
| 18 |
+
return_country_names: If True, return a list of country names;
|
| 19 |
+
If False, return ISO country codes.
|
| 20 |
+
:return: A list of matching country names or ISO country codes.
|
| 21 |
"""
|
| 22 |
+
# Current UTC time
|
| 23 |
+
now_utc = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
|
| 24 |
+
|
| 25 |
+
# Get the current offset of the specified timezone
|
| 26 |
+
try:
|
| 27 |
+
ref_tz = pytz.timezone(timezone)
|
| 28 |
+
except pytz.UnknownTimeZoneError:
|
| 29 |
+
raise ValueError(f"Unknown timezone: {timezone}")
|
| 30 |
+
|
| 31 |
+
ref_offset = now_utc.astimezone(ref_tz).utcoffset()
|
| 32 |
+
|
| 33 |
+
matching_countries = []
|
| 34 |
+
|
| 35 |
+
# Traverse all known countries in pytz
|
| 36 |
+
for country_code, timezones in pytz.country_timezones.items():
|
| 37 |
+
for tz_name in timezones:
|
| 38 |
+
tz = pytz.timezone(tz_name)
|
| 39 |
+
current_offset = now_utc.astimezone(tz).utcoffset()
|
| 40 |
+
|
| 41 |
+
if current_offset == ref_offset:
|
| 42 |
+
# We found a matching offset; record this country
|
| 43 |
+
if return_country_names:
|
| 44 |
+
# Get the human-readable name from pytz.country_names
|
| 45 |
+
country_name = pytz.country_names.get(country_code, country_code)
|
| 46 |
+
matching_countries.append(country_name)
|
| 47 |
+
else:
|
| 48 |
+
matching_countries.append(country_code)
|
| 49 |
+
# No need to check other timezones for this country
|
| 50 |
+
break
|
| 51 |
+
|
| 52 |
+
return sorted(matching_countries)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
|
| 57 |
@tool
|
| 58 |
def get_current_time_in_timezone(timezone: str) -> str:
|