reminder将时区设置移入try块,统一为self.timezone

This commit is contained in:
Raven95676
2025-04-05 21:08:52 +08:00
parent c20f4f5adf
commit 43b6297b5d
+10 -6
View File
@@ -18,11 +18,15 @@ class Main(star.Star):
def __init__(self, context: star.Context) -> None:
self.context = context
self.timezone = self.context.get_config().get("timezone", "Asia/Shanghai")
self.timezone = self.context.get_config().get("timezone")
if not self.timezone:
self.timezone = None
try:
self.timezone = zoneinfo.ZoneInfo(self.timezone) if self.timezone else None
except Exception as e:
logger.error(f"时区设置错误: {e}, 使用本地时区")
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"):
@@ -72,8 +76,8 @@ class Main(star.Star):
if "datetime" in reminder:
reminder_time = datetime.datetime.strptime(
reminder["datetime"], "%Y-%m-%d %H:%M"
).replace(tzinfo=self.tzinfo)
return reminder_time < datetime.datetime.now(self.tzinfo)
).replace(tzinfo=self.timezone)
return reminder_time < datetime.datetime.now(self.timezone)
return False
async def _save_data(self):
@@ -176,14 +180,14 @@ class Main(star.Star):
reminders = self.reminder_data.get(unified_msg_origin, [])
if not reminders:
return []
now = datetime.datetime.now(self.tzinfo)
now = datetime.datetime.now(self.timezone)
upcoming_reminders = [
reminder
for reminder in reminders
if "datetime" not in reminder
or datetime.datetime.strptime(
reminder["datetime"], "%Y-%m-%d %H:%M"
).replace(tzinfo=self.tzinfo)
).replace(tzinfo=self.timezone)
>= now
]
return upcoming_reminders