forked from langgenius/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add timestamp conversion and timestamp retrieval for time tool (l…
- Loading branch information
Showing
4 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
api/core/tools/provider/builtin/time/tools/localtime_to_timestamp.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from datetime import datetime | ||
from typing import Any, Union | ||
|
||
import pytz | ||
|
||
from core.tools.entities.tool_entities import ToolInvokeMessage | ||
from core.tools.errors import ToolInvokeError | ||
from core.tools.tool.builtin_tool import BuiltinTool | ||
|
||
|
||
class LocaltimeToTimestampTool(BuiltinTool): | ||
def _invoke( | ||
self, | ||
user_id: str, | ||
tool_parameters: dict[str, Any], | ||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]: | ||
""" | ||
Convert localtime to timestamp | ||
""" | ||
localtime = tool_parameters.get("localtime") | ||
timezone = tool_parameters.get("timezone", "Asia/Shanghai") | ||
if not timezone: | ||
timezone = None | ||
time_format = "%Y-%m-%d %H:%M:%S" | ||
|
||
timestamp = self.localtime_to_timestamp(localtime, time_format, timezone) | ||
if not timestamp: | ||
return self.create_text_message(f"Invalid localtime: {localtime}") | ||
|
||
return self.create_text_message(f"{timestamp}") | ||
|
||
@staticmethod | ||
def localtime_to_timestamp(localtime: str, time_format: str, local_tz=None) -> int | None: | ||
try: | ||
if local_tz is None: | ||
local_tz = datetime.now().astimezone().tzinfo | ||
if isinstance(local_tz, str): | ||
local_tz = pytz.timezone(local_tz) | ||
local_time = datetime.strptime(localtime, time_format) | ||
localtime = local_tz.localize(local_time) | ||
timestamp = int(localtime.timestamp()) | ||
return timestamp | ||
except Exception as e: | ||
raise ToolInvokeError(str(e)) |
33 changes: 33 additions & 0 deletions
33
api/core/tools/provider/builtin/time/tools/localtime_to_timestamp.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
identity: | ||
name: localtime_to_timestamp | ||
author: zhuhao | ||
label: | ||
en_US: localtime to timestamp | ||
zh_Hans: 获取时间戳 | ||
description: | ||
human: | ||
en_US: A tool for localtime convert to timestamp | ||
zh_Hans: 获取时间戳 | ||
llm: A tool for localtime convert to timestamp | ||
parameters: | ||
- name: localtime | ||
type: string | ||
required: true | ||
form: llm | ||
label: | ||
en_US: localtime | ||
zh_Hans: 本地时间 | ||
human_description: | ||
en_US: localtime, such as 2024-1-1 0:0:0 | ||
zh_Hans: 本地时间, 比如2024-1-1 0:0:0 | ||
- name: timezone | ||
type: string | ||
required: false | ||
form: llm | ||
label: | ||
en_US: Timezone | ||
zh_Hans: 时区 | ||
human_description: | ||
en_US: Timezone, such as Asia/Shanghai | ||
zh_Hans: 时区, 比如Asia/Shanghai | ||
default: Asia/Shanghai |
44 changes: 44 additions & 0 deletions
44
api/core/tools/provider/builtin/time/tools/timestamp_to_localtime.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from datetime import datetime | ||
from typing import Any, Union | ||
|
||
import pytz | ||
|
||
from core.tools.entities.tool_entities import ToolInvokeMessage | ||
from core.tools.errors import ToolInvokeError | ||
from core.tools.tool.builtin_tool import BuiltinTool | ||
|
||
|
||
class TimestampToLocaltimeTool(BuiltinTool): | ||
def _invoke( | ||
self, | ||
user_id: str, | ||
tool_parameters: dict[str, Any], | ||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]: | ||
""" | ||
Convert timestamp to localtime | ||
""" | ||
timestamp = tool_parameters.get("timestamp") | ||
timezone = tool_parameters.get("timezone", "Asia/Shanghai") | ||
if not timezone: | ||
timezone = None | ||
time_format = "%Y-%m-%d %H:%M:%S" | ||
|
||
locatime = self.timestamp_to_localtime(timestamp, timezone) | ||
if not locatime: | ||
return self.create_text_message(f"Invalid timestamp: {timestamp}") | ||
|
||
localtime_format = locatime.strftime(time_format) | ||
|
||
return self.create_text_message(f"{localtime_format}") | ||
|
||
@staticmethod | ||
def timestamp_to_localtime(timestamp: int, local_tz=None) -> datetime | None: | ||
try: | ||
if local_tz is None: | ||
local_tz = datetime.now().astimezone().tzinfo | ||
if isinstance(local_tz, str): | ||
local_tz = pytz.timezone(local_tz) | ||
local_time = datetime.fromtimestamp(timestamp, local_tz) | ||
return local_time | ||
except Exception as e: | ||
raise ToolInvokeError(str(e)) |
33 changes: 33 additions & 0 deletions
33
api/core/tools/provider/builtin/time/tools/timestamp_to_localtime.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
identity: | ||
name: timestamp_to_localtime | ||
author: zhuhao | ||
label: | ||
en_US: Timestamp to localtime | ||
zh_Hans: 时间戳转换 | ||
description: | ||
human: | ||
en_US: A tool for timestamp convert to localtime | ||
zh_Hans: 时间戳转换 | ||
llm: A tool for timestamp convert to localtime | ||
parameters: | ||
- name: timestamp | ||
type: number | ||
required: true | ||
form: llm | ||
label: | ||
en_US: Timestamp | ||
zh_Hans: 时间戳 | ||
human_description: | ||
en_US: Timestamp | ||
zh_Hans: 时间戳 | ||
- name: timezone | ||
type: string | ||
required: false | ||
form: llm | ||
label: | ||
en_US: Timezone | ||
zh_Hans: 时区 | ||
human_description: | ||
en_US: Timezone, such as Asia/Shanghai | ||
zh_Hans: 时区, 比如Asia/Shanghai | ||
default: Asia/Shanghai |