fix: 使用 zoneinfo 替代 tzinfo; 默认不设置时区(使用系统默认时区)
This commit is contained in:
@@ -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",
|
||||
|
||||
+10
-13
@@ -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)")
|
||||
)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user