/ restai / llms / tools / datetime_tool.py
datetime_tool.py
 1  from typing import Optional
 2  
 3  
 4  def datetime_tool(
 5      action: str = "now",
 6      timezone: Optional[str] = "UTC",
 7      date: Optional[str] = None,
 8      days: Optional[int] = None,
 9  ) -> str:
10      """
11      Get current date/time, convert between timezones, or perform date arithmetic.
12  
13      Args:
14          action (str): Action to perform. One of: "now" (current date/time), "add" (add/subtract days from a date), "weekday" (get day of week for a date), "diff" (days between two dates — put second date in timezone param).
15          timezone (Optional[str]): Timezone name (e.g. "US/Eastern", "Europe/London", "Asia/Tokyo", "UTC"). For "diff" action, this is the second date.
16          date (Optional[str]): Date string in YYYY-MM-DD format. Used with "add", "weekday", and "diff" actions.
17          days (Optional[int]): Number of days to add (positive) or subtract (negative). Used with "add" action.
18      """
19      from datetime import datetime, timedelta, timezone as tz
20      import zoneinfo
21  
22      try:
23          if action == "now":
24              try:
25                  zi = zoneinfo.ZoneInfo(timezone)
26              except Exception:
27                  return f"Error: Unknown timezone '{timezone}'. Examples: UTC, US/Eastern, Europe/London, Asia/Tokyo"
28              now = datetime.now(zi)
29              return f"{now.strftime('%Y-%m-%d %H:%M:%S %Z')} ({now.strftime('%A')})"
30  
31          elif action == "add":
32              if not date:
33                  return "Error: 'date' parameter required for 'add' action (YYYY-MM-DD)"
34              if days is None:
35                  return "Error: 'days' parameter required for 'add' action"
36              dt = datetime.strptime(date, "%Y-%m-%d")
37              result = dt + timedelta(days=days)
38              return f"{result.strftime('%Y-%m-%d')} ({result.strftime('%A')})"
39  
40          elif action == "weekday":
41              if not date:
42                  return "Error: 'date' parameter required for 'weekday' action (YYYY-MM-DD)"
43              dt = datetime.strptime(date, "%Y-%m-%d")
44              return dt.strftime("%A")
45  
46          elif action == "diff":
47              if not date:
48                  return "Error: 'date' parameter required for 'diff' action (YYYY-MM-DD)"
49              if not timezone:
50                  return "Error: put the second date in the 'timezone' parameter for 'diff' action"
51              dt1 = datetime.strptime(date, "%Y-%m-%d")
52              dt2 = datetime.strptime(timezone, "%Y-%m-%d")
53              delta = (dt2 - dt1).days
54              return f"{abs(delta)} days"
55  
56          else:
57              return f"Error: Unknown action '{action}'. Use: now, add, weekday, diff"
58  
59      except ValueError as e:
60          return f"Error: {e}. Use YYYY-MM-DD format for dates."
61      except Exception as e:
62          return f"Error: {e}"