"""
DateTime Tool - Date and time utilities.
Demonstrates:
- Multiple tools in one module
- Optional parameters with defaults
- Formatted output
"""
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Optional
if TYPE_CHECKING:
from mcp.server.fastmcp import FastMCP
from ..config import ServerConfig
def register(mcp: "FastMCP", config: "ServerConfig") -> None:
"""Register datetime tools with the server."""
@mcp.tool()
def get_current_time(format: str = "%Y-%m-%d %H:%M:%S") -> str:
"""
Get the current date and time in UTC.
Args:
format: strftime format string (default: %Y-%m-%d %H:%M:%S)
Returns:
Formatted current date/time string in UTC
"""
now = datetime.now(timezone.utc)
return now.strftime(format) + " UTC"
@mcp.tool()
def parse_timestamp(timestamp: int, format: str = "%Y-%m-%d %H:%M:%S") -> str:
"""
Convert a Unix timestamp to a human-readable format.
Args:
timestamp: Unix timestamp (seconds since epoch)
format: strftime format string
Returns:
Formatted date/time string in UTC
"""
dt = datetime.fromtimestamp(timestamp, tz=timezone.utc)
return dt.strftime(format) + " UTC"
@mcp.tool()
def get_timestamp() -> int:
"""
Get the current Unix timestamp.
Returns:
Current time as Unix timestamp (seconds since epoch)
"""
return int(datetime.now(timezone.utc).timestamp())
@mcp.tool()
def time_difference(start_timestamp: int, end_timestamp: int) -> str:
"""
Calculate the difference between two timestamps.
Args:
start_timestamp: Start Unix timestamp
end_timestamp: End Unix timestamp
Returns:
Human-readable time difference
"""
diff = abs(end_timestamp - start_timestamp)
days = diff // 86400
hours = (diff % 86400) // 3600
minutes = (diff % 3600) // 60
seconds = diff % 60
parts = []
if days:
parts.append(f"{days} day{'s' if days != 1 else ''}")
if hours:
parts.append(f"{hours} hour{'s' if hours != 1 else ''}")
if minutes:
parts.append(f"{minutes} minute{'s' if minutes != 1 else ''}")
if seconds or not parts:
parts.append(f"{seconds} second{'s' if seconds != 1 else ''}")
return ", ".join(parts)