Skip to content

Commit 70f06f2

Browse files
committed
get timezone aware time
1 parent 194b886 commit 70f06f2

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ AI agents with custom tools for calculations, time queries, and web search.
110110
uv run python scripts/agents_langgraph.py interactive
111111

112112
# Single query
113-
uv run python scripts/agents_langgraph.py chat "What time is it?"
113+
uv run python scripts/agents_langgraph.py chat "京都の今の時間から12時間後は何時?"
114114

115115
# Available tools
116116
uv run python scripts/agents_langgraph.py tools

template_fastapi/internals/langgraph/nodes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
from langchain_core.messages import ToolMessage
44

5+
from template_fastapi.settings.logging import get_logger
6+
57
from .llms import LLMFactory
68
from .states import AgentState
79
from .tools import get_tools
810

11+
logger = get_logger(__name__)
12+
913

1014
def agent_node(state: AgentState) -> dict:
1115
"""
@@ -68,6 +72,7 @@ def tool_node(state: AgentState) -> dict:
6872
tool = tools_by_name[tool_name]
6973
try:
7074
result = tool.invoke(tool_args)
75+
logger.info(f"Executed {tool_name} with args {tool_args}: {result}")
7176
tool_messages.append(
7277
ToolMessage(
7378
content=str(result),

template_fastapi/internals/langgraph/tools.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tools for LangGraph agent."""
22

3-
import time
3+
from datetime import datetime
4+
from zoneinfo import ZoneInfo
45

56
from langchain_core.tools import BaseTool
67
from pydantic import BaseModel, Field
@@ -33,7 +34,9 @@ class CurrentTimeTool(BaseTool):
3334

3435
def _run(self, timezone: str = "UTC") -> str:
3536
"""Get current time."""
36-
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
37+
tz = ZoneInfo(timezone)
38+
now_in_tz = datetime.now(tz)
39+
current_time = now_in_tz.strftime("%Y-%m-%d %H:%M:%S %Z%z")
3740
return f"Current time ({timezone}): {current_time}"
3841

3942

tests/test_langgraph.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def test_current_time_tool():
6464
assert "Current time (UTC):" in result
6565

6666
# Test with timezone parameter
67-
result = time_tool._run("JST")
68-
assert "Current time (JST):" in result
67+
result = time_tool._run("Asia/Tokyo")
68+
assert "Current time (Asia/Tokyo):" in result
6969

7070

7171
def test_search_tool():

0 commit comments

Comments
 (0)