File size: 4,966 Bytes
ba960dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from typing import Any, Optional
from smolagents.tools import Tool
import datetime
import pytz

class GetLocalTimeTool(Tool):
    name = "get_local_time"
    description = "Gets the current local time at a travel destination."
    inputs = {'destination': {'type': 'string', 'description': 'A city or location name (e.g., "Paris", "Tokyo", "New York").'}}
    output_type = "string"

    def __init__(self):
        super().__init__()
        try:
            import pytz
        except ImportError as e:
            raise ImportError(
                "You must install package `pytz` to run this tool: for instance run `pip install pytz`."
            ) from e
        
        # Map of common tourist destinations to their timezones
        self.destination_timezones = {
            "london": "Europe/London",
            "paris": "Europe/Paris",
            "rome": "Europe/Rome",
            "madrid": "Europe/Madrid",
            "berlin": "Europe/Berlin",
            "amsterdam": "Europe/Amsterdam",
            "athens": "Europe/Athens",
            "istanbul": "Europe/Istanbul",
            "dubai": "Asia/Dubai",
            "new delhi": "Asia/Kolkata",
            "mumbai": "Asia/Kolkata",
            "bangkok": "Asia/Bangkok",
            "singapore": "Asia/Singapore",
            "tokyo": "Asia/Tokyo",
            "seoul": "Asia/Seoul",
            "beijing": "Asia/Shanghai",
            "shanghai": "Asia/Shanghai",
            "hong kong": "Asia/Hong_Kong",
            "sydney": "Australia/Sydney",
            "melbourne": "Australia/Melbourne",
            "auckland": "Pacific/Auckland",
            "fiji": "Pacific/Fiji",
            "honolulu": "Pacific/Honolulu",
            "anchorage": "America/Anchorage",
            "los angeles": "America/Los_Angeles",
            "san francisco": "America/Los_Angeles",
            "las vegas": "America/Los_Angeles",
            "denver": "America/Denver",
            "chicago": "America/Chicago",
            "houston": "America/Chicago",
            "new york": "America/New_York",
            "miami": "America/New_York",
            "toronto": "America/Toronto",
            "mexico city": "America/Mexico_City",
            "rio de janeiro": "America/Sao_Paulo",
            "sao paulo": "America/Sao_Paulo",
            "buenos aires": "America/Argentina/Buenos_Aires",
            "cairo": "Africa/Cairo",
            "cape town": "Africa/Johannesburg",
            "johannesburg": "Africa/Johannesburg",
            "nairobi": "Africa/Nairobi"
        }

    def forward(self, destination: str) -> str:
        try:
            # Normalize the destination name
            normalized_dest = destination.lower().strip()
            
            # Find the closest matching timezone
            timezone = None
            for city, tz in self.destination_timezones.items():
                if city in normalized_dest or normalized_dest in city:
                    timezone = tz
                    break
            
            if not timezone:
                # If we don't have a direct match, try to find it through pytz
                try:
                    # Try web search for timezone if available
                    import importlib
                    if importlib.util.find_spec("duckduckgo_search"):
                        from duckduckgo_search import DDGS
                        ddgs = DDGS()
                        results = ddgs.text(f"{destination} timezone")
                        if results:
                            # Simple heuristic to extract timezone from search results
                            for result in results:
                                body = result.get('body', '').lower()
                                if 'utc' in body or 'gmt' in body:
                                    timezone_pos = body.find('utc') if 'utc' in body else body.find('gmt')
                                    timezone_info = body[timezone_pos:timezone_pos+8]
                                    return f"Based on web search, the timezone in {destination} appears to be around {timezone_info.upper()}. Current time information is not available."
                except:
                    pass
                
                return f"I don't have timezone information for {destination}. Please try a major city nearby."
            
            # Get current time in that timezone
            tz = pytz.timezone(timezone)
            local_time = datetime.datetime.now(tz)
            
            # Format the result
            formatted_time = local_time.strftime("%I:%M %p on %A, %B %d, %Y")
            time_diff = local_time.utcoffset().total_seconds() / 3600
            sign = "+" if time_diff >= 0 else ""
            
            return f"The current local time in {destination} is {formatted_time} (UTC{sign}{int(time_diff)})"
        
        except Exception as e:
            return f"Error getting local time for {destination}: {str(e)}"