File size: 1,547 Bytes
209ffad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from smolagents import tool

import pytz
import datetime

@tool
def get_the_current_time_in_timezone(timezone: str) -> str:
    """
    A tool that fetches the current local time in a specified timezone.

    Args:
        timezone (str): A string representing the timezone in which the local time should be fetched.
                        It should be a valid timezone string recognized by the `pytz` library (e.g., 'America/New_York', 'Europe/London').

    Returns:
        str: A sentence that provides the current time in the specified timezone, formatted as a 12-hour clock (with AM/PM).
             For example: "The current local time in America/New_York is: 02:30 PM".

    Raises:
        ValueError: If the provided timezone is invalid or unrecognized by the `pytz` library.
        Exception: If there is an unexpected error while fetching the time.

    Example:
        >>> get_the_current_time_in_timezone("America/New_York")
        "The current local time in America/New_York is: 02:30 PM"
    """
    try:
        # Create timezone object
        tz = pytz.timezone(timezone)
        # Get current time in that timezone
        local_time = datetime.datetime.now(tz).strftime("%I:%M %p")  # %I for 12-hour clock, %M for minutes, %p for AM/PM
        return f"The current local time in {timezone} is: {local_time}"
    except pytz.UnknownTimeZoneError:
        raise ValueError(f"The timezone '{timezone}' is not recognized.")
    except Exception as e:
        return f"Error fetching time for timezone '{timezone}': {str(e)}"