之前用AI安排进度和行程,无法知道当前时间,官网的工具没有自己需要的。就之前在别人的代码上更改了{AI帮忙修改}。
先上测试结果
open webui官网获取
代码如下
from datetime import datetime, timezone, timedelta
class Tools:
"""
CW_NTP Tool to retrieve and display the current date and time
in Shanghai, Asia (UTC+8).
Note: This implementation does not use an NTP client due to limitations
within the Open WebUI environment. Time accuracy relies on the system clock.
"""
def get_shanghai_time(self) -> str:
"""
Calculates the current time for Shanghai, Asia (UTC+8) based on the system clock.
"""
try:
current_utc_time = datetime.now(
timezone.utc
) # More efficient than replacing timezone info
shanghai_time = current_utc_time + timedelta(hours=8)
return shanghai_time.strftime("%A, %B %d, %Y %H:%M:%S (Shanghai Time)")
except Exception as e:
# More specific error handling would be ideal, but limited by Open WebUI
return f"Error calculating Shanghai time: {e}"
def preface_with_time(self, text: str) -> str:
"""
Prepends the calculated Shanghai timestamp to the given text.
"""
current_time = self.get_shanghai_time()
return f"{current_time}\n\n{text}"
如有错误请给我留言