anssio commited on
Commit
de86dce
·
verified ·
1 Parent(s): 8c5c24b

Update app.py

Browse files

Added time tool with timezones.

Files changed (1) hide show
  1. app.py +59 -10
app.py CHANGED
@@ -18,20 +18,69 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
18
  """
19
  return "What magic will you build ?"
20
 
 
21
  @tool
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
- # Create timezone object
29
- tz = pytz.timezone(timezone)
30
- # Get current time in that timezone
31
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
- return f"The current local time in {timezone} is: {local_time}"
33
- except Exception as e:
34
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
 
 
 
35
 
36
 
37
  final_answer = FinalAnswerTool()
 
18
  """
19
  return "What magic will you build ?"
20
 
21
+
22
  @tool
23
+ def get_current_time_in_timezone(timezone_or_location: str) -> str:
24
+ """Get the current local time for a timezone identifier or a common city name.
25
+
26
  Args:
27
+ timezone_or_location: A tz database name (e.g. 'Europe/Helsinki') or a common city name
28
+ (e.g. 'Helsinki', 'New York', 'London', 'Tokyo'). The function recognizes several
29
+ common city names and maps them to the corresponding tz database name.
30
+
31
+ Returns:
32
+ A string describing the current local time in the requested timezone, or an error message
33
+ explaining the problem and how to provide a valid timezone.
34
  """
35
+ # Added time zones -AO
36
+ city_map = {
37
+ "helsinki": "Europe/Helsinki",
38
+ "oslo": "Europe/Oslo",
39
+ "stockholm": "Europe/Stockholm",
40
+ "london": "Europe/London",
41
+ "paris": "Europe/Paris",
42
+ "berlin": "Europe/Berlin",
43
+ "madrid": "Europe/Madrid",
44
+ "rome": "Europe/Rome",
45
+ "new york": "America/New_York",
46
+ "nyc": "America/New_York",
47
+ "los angeles": "America/Los_Angeles",
48
+ "san francisco": "America/Los_Angeles",
49
+ "chicago": "America/Chicago",
50
+ "toronto": "America/Toronto",
51
+ "sao paulo": "America/Sao_Paulo",
52
+ "buenos aires": "America/Argentina/Buenos_Aires",
53
+ "mexico city": "America/Mexico_City",
54
+ "tokyo": "Asia/Tokyo",
55
+ "seoul": "Asia/Seoul",
56
+ "beijing": "Asia/Shanghai",
57
+ "shanghai": "Asia/Shanghai",
58
+ "hong kong": "Asia/Hong_Kong",
59
+ "singapore": "Asia/Singapore",
60
+ "sydney": "Australia/Sydney",
61
+ "melbourne": "Australia/Melbourne",
62
+ }
63
+
64
+ query = (timezone_or_location or "").strip()
65
+ if not query:
66
+ return "Please provide a timezone or city name (e.g. 'Europe/Helsinki' or 'Helsinki')."
67
+
68
+ tz_name = query
69
+ lower = query.lower()
70
+ if lower in city_map:
71
+ tz_name = city_map[lower]
72
+
73
  try:
74
+ tz = pytz.timezone(tz_name)
75
+ except Exception:
76
+ return (
77
+ f"Unrecognized timezone or city: '{timezone_or_location}'. "
78
+ "Provide a valid tz database name (e.g. 'Europe/Helsinki') or one of the supported city names: "
79
+ + ", ".join(sorted(set(city_map.keys())))
80
+ )
81
+
82
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
83
+ return f"The current local time in {tz_name} is: {local_time}"
84
 
85
 
86
  final_answer = FinalAnswerTool()