"""
时间工具
"""
from datetime import datetime
from typing import Optional


def get_current_time(timezone: Optional[str] = None) -> dict:
    """
    获取当前时间

    Args:
        timezone: 时区名称，如 "UTC", "Asia/Shanghai"

    Returns:
        当前时间信息
    """
    now = datetime.now()

    return {
        "success": True,
        "datetime": now.isoformat(),
        "timestamp": now.timestamp(),
        "date": now.strftime("%Y-%m-%d"),
        "time": now.strftime("%H:%M:%S"),
        "weekday": now.strftime("%A"),
        "timezone": timezone or "Local Time"
    }


def format_time(timestamp: float, format_str: str = "%Y-%m-%d %H:%M:%S") -> dict:
    """
    格式化时间戳

    Args:
        timestamp: Unix 时间戳
        format_str: 格式字符串

    Returns:
        格式化后的时间
    """
    try:
        dt = datetime.fromtimestamp(timestamp)
        return {
            "success": True,
            "formatted": dt.strftime(format_str),
            "datetime": dt.isoformat()
        }
    except Exception as e:
        return {
            "success": False,
            "error": str(e)
        }


# 工具定义
TOOL_DEFINITION = {
    "name": "get_current_time",
    "description": "Get the current date and time. Useful for timestamps or scheduling.",
    "parameters": {
        "type": "object",
        "properties": {
            "timezone": {
                "type": "string",
                "description": "Optional timezone (e.g., 'UTC', 'Asia/Shanghai')",
                "default": "Local"
            }
        }
    }
}
