reminder将时区设置移入try块,统一为self.timezone
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user