From dc1f222cd2ee643f6034de7e415ed50f560568b6 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sat, 5 Apr 2025 17:27:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BD=BF=E7=94=A8=20zoneinfo=20?= =?UTF-8?q?=E6=9B=BF=E4=BB=A3=20tzinfo;=20=E9=BB=98=E8=AE=A4=E4=B8=8D?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E6=97=B6=E5=8C=BA(=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E9=BB=98=E8=AE=A4=E6=97=B6=E5=8C=BA)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/config/default.py | 14 +++++++------- packages/astrbot/main.py | 23 ++++++++++------------- packages/reminder/main.py | 22 +++++++++++++++------- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index b62f839ae..7ad1ecc07 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -47,7 +47,6 @@ DEFAULT_CONFIG = { "web_search_link": False, "identifier": False, "datetime_system_prompt": True, - "timezone": "Asia/Shanghai", "default_personality": "default", "prompt_prefix": "", "max_context_length": -1, @@ -99,6 +98,7 @@ DEFAULT_CONFIG = { "plugin_repo_mirror": "", "knowledge_db": {}, "persona": [], + "timezone": "", } @@ -978,12 +978,6 @@ CONFIG_METADATA_2 = { "obvious_hint": True, "hint": "启用后,会在系统提示词中加上当前机器的日期时间。", }, - "timezone": { - "description": "时区设置", - "type": "string", - "obvious_hint": True, - "hint": "时区设置。请填写时区名称,如 Asia/Shanghai。", - }, "default_personality": { "description": "默认采用的人格情景的名称", "type": "string", @@ -1179,6 +1173,12 @@ CONFIG_METADATA_2 = { "type": "string", "hint": "启用后,会以添加环境变量的方式设置代理。格式为 `http://ip:port`", }, + "timezone": { + "description": "时区", + "type": "string", + "obvious_hint": True, + "hint": "时区设置。请填写 IANA 时区名称, 如 Asia/Shanghai, 为空时使用系统默认时区。所有时区请查看: https://data.iana.org/time-zones/tzdb-2021a/zone1970.tab", + }, "log_level": { "description": "控制台日志级别", "type": "string", diff --git a/packages/astrbot/main.py b/packages/astrbot/main.py index 207067a9d..8d421ed6d 100644 --- a/packages/astrbot/main.py +++ b/packages/astrbot/main.py @@ -3,6 +3,7 @@ import datetime import builtins import traceback import re +import zoneinfo import astrbot.api.star as star import astrbot.api.event.filter as filter from astrbot.api.event import AstrMessageEvent, MessageEventResult @@ -23,7 +24,6 @@ from .long_term_memory import LongTermMemory from astrbot.core import logger from astrbot.api.message_components import Plain, Image, Reply from typing import Union -import pytz @star.register( @@ -39,8 +39,11 @@ class Main(star.Star): self.prompt_prefix = cfg["provider_settings"]["prompt_prefix"] self.identifier = cfg["provider_settings"]["identifier"] self.enable_datetime = cfg["provider_settings"]["datetime_system_prompt"] - self.timezone = cfg["provider_settings"]["timezone"] - + self.timezone = cfg.get("timezone", "Asia/Shanghai") + if not self.timezone: + # 系统默认时区 + self.timezone = None + logger.info(f"Timezone set to: {self.timezone}") self.ltm = None if ( self.context.get_config()["provider_ltm_settings"]["group_icl_enable"] @@ -1195,21 +1198,15 @@ UID: {user_id} 此 ID 可用于设置管理员。 # 启用附加时间戳 if self.enable_datetime: - # 启用时区 + current_time = None if self.timezone: + # 启用时区 try: - tz = pytz.timezone(self.timezone) - now = datetime.datetime.now(tz) + now = datetime.datetime.now(zoneinfo.ZoneInfo(self.timezone)) current_time = now.strftime("%Y-%m-%d %H:%M (%Z)") except Exception as e: logger.error(f"时区设置错误: {e}, 使用本地时区") - current_time = ( - datetime.datetime.now() - .astimezone() - .strftime("%Y-%m-%d %H:%M (%Z)") - ) - # 未启用时区 - else: + if not current_time: current_time = ( datetime.datetime.now().astimezone().strftime("%Y-%m-%d %H:%M (%Z)") ) diff --git a/packages/reminder/main.py b/packages/reminder/main.py index e3f6c0a97..5b9b32588 100644 --- a/packages/reminder/main.py +++ b/packages/reminder/main.py @@ -2,6 +2,7 @@ import os import json import datetime import uuid +import zoneinfo import astrbot.api.star as star from astrbot.api.event import filter from apscheduler.schedulers.asyncio import AsyncIOScheduler @@ -17,7 +18,11 @@ class Main(star.Star): def __init__(self, context: star.Context) -> None: self.context = context - self.scheduler = AsyncIOScheduler(timezone="Asia/Shanghai") + self.timezone = self.context.get_config().get("timezone", "Asia/Shanghai") + if not self.timezone: + self.timezone = None + self.scheduler = AsyncIOScheduler(timezone=self.timezone) + self.tzinfo = zoneinfo.ZoneInfo(self.timezone) if self.timezone else None # set and load config if not os.path.exists("data/astrbot-reminder.json"): @@ -65,10 +70,10 @@ class Main(star.Star): def check_is_outdated(self, reminder: dict): """Check if the reminder is outdated.""" if "datetime" in reminder: - return ( - datetime.datetime.strptime(reminder["datetime"], "%Y-%m-%d %H:%M") - < datetime.datetime.now() - ) + reminder_time = datetime.datetime.strptime( + reminder["datetime"], "%Y-%m-%d %H:%M" + ).replace(tzinfo=self.tzinfo) + return reminder_time < datetime.datetime.now(self.tzinfo) return False async def _save_data(self): @@ -171,12 +176,15 @@ class Main(star.Star): reminders = self.reminder_data.get(unified_msg_origin, []) if not reminders: return [] - now = datetime.datetime.now() + now = datetime.datetime.now(self.tzinfo) upcoming_reminders = [ reminder for reminder in reminders if "datetime" not in reminder - or datetime.datetime.strptime(reminder["datetime"], "%Y-%m-%d %H:%M") >= now + or datetime.datetime.strptime( + reminder["datetime"], "%Y-%m-%d %H:%M" + ).replace(tzinfo=self.tzinfo) + >= now ] return upcoming_reminders