Skip to content
Snippets Groups Projects
Unverified Commit 5bace921 authored by Dominik George's avatar Dominik George
Browse files

Move handling of default timezone to instance and db code

parent 5fc92aa6
No related branches found
No related tags found
No related merge requests found
...@@ -51,6 +51,11 @@ class ReminderBot(Plugin): ...@@ -51,6 +51,11 @@ class ReminderBot(Plugin):
bc = self.config["base_command"] bc = self.config["base_command"]
self.base_command = bc[0] if isinstance(bc, list) else bc self.base_command = bc[0] if isinstance(bc, list) else bc
self.base_aliases = tuple(bc) if isinstance(bc, list) else (bc,) self.base_aliases = tuple(bc) if isinstance(bc, list) else (bc,)
try:
self.default_timezone = pytz.timezone(self.config["timezone"])
except pytz.UnknownTimeZoneError:
self.log.warning(f"Unknown default timezone {self.config['timezone']}")
self.default_timezone = pytz.UTC
async def stop(self) -> None: async def stop(self) -> None:
self.reminder_loop_task.cancel() self.reminder_loop_task.cancel()
......
...@@ -70,7 +70,7 @@ class ReminderDatabase: ...@@ -70,7 +70,7 @@ class ReminderDatabase:
tx.execute(self.timezone.insert().values(user_id=user_id, timezone=tz.zone)) tx.execute(self.timezone.insert().values(user_id=user_id, timezone=tz.zone))
self.tz_cache[user_id] = tz self.tz_cache[user_id] = tz
def get_timezone(self, user_id: UserID) -> Optional[pytz.timezone]: def get_timezone(self, user_id: UserID, default_tz: Optional[pytz.timezone]) -> Optional[pytz.timezone]:
try: try:
return self.tz_cache[user_id] return self.tz_cache[user_id]
except KeyError: except KeyError:
...@@ -79,7 +79,7 @@ class ReminderDatabase: ...@@ -79,7 +79,7 @@ class ReminderDatabase:
try: try:
self.tz_cache[user_id] = pytz.timezone(next(rows)[0]) self.tz_cache[user_id] = pytz.timezone(next(rows)[0])
except (pytz.UnknownTimeZoneError, StopIteration, IndexError): except (pytz.UnknownTimeZoneError, StopIteration, IndexError):
self.tz_cache[user_id] = pytz.UTC self.tz_cache[user_id] = default_tz or pytz.UTC
return self.tz_cache[user_id] return self.tz_cache[user_id]
def set_locales(self, user_id: UserID, locales: List[str]) -> None: def set_locales(self, user_id: UserID, locales: List[str]) -> None:
......
...@@ -44,13 +44,10 @@ class DateArgument(Argument): ...@@ -44,13 +44,10 @@ class DateArgument(Argument):
def match(self, val: str, evt: MessageEvent = None, instance: 'ReminderBot' = None def match(self, val: str, evt: MessageEvent = None, instance: 'ReminderBot' = None
) -> Tuple[str, Optional[datetime]]: ) -> Tuple[str, Optional[datetime]]:
try: tz = pytz.UTC
tz = pytz.timezone(instance.config["timezone"])
except (pytz.UnknownTimeZoneError, StopIteration, IndexError):
tz = pytz.UTC
use_locales = [locales["en_iso"]] use_locales = [locales["en_iso"]]
if instance: if instance:
tz = instance.db.get_timezone(evt.sender) tz = instance.db.get_timezone(evt.sender, instance.default_timezone)
locale_ids = instance.db.get_locales(evt.sender) locale_ids = instance.db.get_locales(evt.sender)
use_locales = [locales[id] for id in locale_ids if id in locales] use_locales = [locales[id] for id in locale_ids if id in locales]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment