File size: 1,815 Bytes
d5b5f42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d3757d
d5b5f42
 
 
 
 
8d3757d
 
d5b5f42
8d3757d
 
d5b5f42
 
 
 
 
 
 
 
 
 
 
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
# tools/get_current_time.py

import gradio as gr
import datetime
import pytz
from utils.tool_manager import tool

def time_ui():
    """
    Builds the Gradio UI components for the Get Current Time tool.
    Returns a tuple: (ui_group, input_components, output_components, button_component)
    """
    with gr.Group(visible=False) as ui_group: # Initially hidden
        timezone_input = gr.Textbox(label="Timezone (e.g., UTC, Asia/Ho_Chi_Minh)", value="UTC", placeholder="Enter IANA timezone name")
        output_text = gr.Textbox(label="Current Time", interactive=False)
        run_button = gr.Button("Get Time")
    # Return the group, input(s), output(s), and the button
    return ui_group, timezone_input, output_text, run_button

@tool(
    name="Get Current Time",
    control_components=time_ui # Pass the UI builder function
)
def get_current_time(timezone: str = "UTC") -> str:
    """
    Gets the current time for the given timezone string.

    This tool takes an IANA timezone name (like "UTC", "America/New_York",
    "Asia/Ho_Chi_Minh") and returns the current datetime in that zone.
    Defaults to "UTC" if no timezone is provided.

    Args:
        timezone (str): The IANA timezone string (e.g., "UTC").

    Returns:
        str: The current time in the specified timezone, or an error message.
    """
    try:
        # pytz requires the timezone string to be correct
        tz = pytz.timezone(timezone)
        now_utc = datetime.datetime.utcnow()
        now_in_tz = pytz.utc.localize(now_utc).astimezone(tz)
        return now_in_tz.strftime('%Y-%m-%d %H:%M:%S %Z%z')
    except pytz.UnknownTimeZoneError:
        return f"Error: Unknown timezone '{timezone}'. Please provide a valid IANA timezone name."
    except Exception as e:
        return f"An unexpected error occurred: {e}"