From 9b35b4892a1fc32d30d8ac319ae31e054a33320f Mon Sep 17 00:00:00 2001 From: profitroll Date: Thu, 30 May 2024 17:47:23 +0200 Subject: [PATCH] Attempted a fix for #8 --- modules/reminder.py | 17 ++++++++++------- modules/utils.py | 8 ++++++-- plugins/commands/set_offset.py | 20 ++++++++++---------- plugins/commands/set_time.py | 23 ++++++++++++++--------- plugins/commands/setup.py | 12 ++++++------ plugins/commands/start.py | 18 ++++++++++-------- plugins/commands/toggle.py | 13 ++++++------- 7 files changed, 62 insertions(+), 49 deletions(-) diff --git a/modules/reminder.py b/modules/reminder.py index e2151e0..bd6b9aa 100644 --- a/modules/reminder.py +++ b/modules/reminder.py @@ -1,5 +1,5 @@ import logging -from datetime import datetime, timedelta +from datetime import datetime, timezone from bson import json_util from libbot.pyrogram.classes import PyroClient @@ -9,13 +9,12 @@ from classes.location import Location from classes.pyrouser import PyroUser from modules.database import col_users from modules.database_api import col_entries -from modules.utils import from_utc logger = logging.getLogger(__name__) async def remind(app: PyroClient) -> None: - utcnow = datetime.utcnow() + utcnow = datetime.now(timezone.utc) logger.debug("Performing reminder lookup for %s (UTCNOW)", utcnow) @@ -40,12 +39,16 @@ async def remind(app: PyroClient) -> None: try: location: Location = await app.get_location(user.location.id) # type: ignore except ValueError: + logger.warning("Skipping reminder for %s due to invalid location", user.id) continue - user_date = from_utc( - datetime.utcnow() + timedelta(days=user.offset), - user.location.timezone.zone, - ).replace(hour=0, minute=0, second=0, microsecond=0) + try: + user_date = user.get_reminder_date() + except AttributeError: + logger.warning( + "Skipping reminder for %s due to missing attributes", user.id + ) + continue entries = await col_entries.find( { diff --git a/modules/utils.py b/modules/utils.py index aaece4b..13e1c96 100644 --- a/modules/utils.py +++ b/modules/utils.py @@ -6,7 +6,9 @@ from pytz import timezone as pytz_timezone def to_utc(date: datetime, timezone: Union[str, None] = None) -> datetime: - """Move timezone unaware datetime object to UTC timezone and return it. + """*DEPRECATED AND WILL BE REMOVED IN FUTURE RELEASES* + + Move timezone unaware datetime object to UTC timezone and return it. ### Args: * date (`datetime`): Datetime to be converted. @@ -20,7 +22,9 @@ def to_utc(date: datetime, timezone: Union[str, None] = None) -> datetime: def from_utc(date: datetime, timezone: Union[str, None] = None) -> datetime: - """Move timezone unaware datetime object to the timezone specified and return it. + """*DEPRECATED AND WILL BE REMOVED IN FUTURE RELEASES* + + Move timezone unaware datetime object to the timezone specified and return it. ### Args: * date (`datetime`): Datetime to be converted. diff --git a/plugins/commands/set_offset.py b/plugins/commands/set_offset.py index 30fce12..697c7ea 100644 --- a/plugins/commands/set_offset.py +++ b/plugins/commands/set_offset.py @@ -1,5 +1,5 @@ import logging -from datetime import datetime +from datetime import datetime, timezone from convopyro import listen_message from pyrogram import filters @@ -7,7 +7,6 @@ from pyrogram.types import ForceReply, Message, ReplyKeyboardRemove from classes.pyroclient import PyroClient from modules import custom_filters -from modules.utils import from_utc logger = logging.getLogger(__name__) @@ -65,18 +64,19 @@ async def command_set_offset(app: PyroClient, message: Message): logger.info("User %s has set offset to %s", user.id, offset) - garbage_time = from_utc( - datetime(1970, 1, 1, user.time_hour, user.time_minute), - None if user.location is None else user.location.timezone.zone, - ).strftime(app._("time", "formats")) + garbage_time = ( + datetime.now(timezone.utc) + .replace(hour=user.time_hour, minute=user.time_minute) + .astimezone(user.location.timezone or timezone.utc) + ) await answer.reply_text( app._("set_offset_finished", "messages", locale=user.locale).format( offset=offset, - time=garbage_time, - toggle_notice="" - if user.enabled - else app._("toggle", "messages", locale=user.locale), + time=garbage_time.strftime(app._("time", "formats", locale=user.locale)), + toggle_notice=( + "" if user.enabled else app._("toggle", "messages", locale=user.locale) + ), ), reply_markup=ReplyKeyboardRemove(), ) diff --git a/plugins/commands/set_time.py b/plugins/commands/set_time.py index 166b384..99c5fd7 100644 --- a/plugins/commands/set_time.py +++ b/plugins/commands/set_time.py @@ -1,5 +1,5 @@ import logging -from datetime import datetime +from datetime import datetime, timezone from convopyro import listen_message from pyrogram import filters @@ -7,7 +7,6 @@ from pyrogram.types import ForceReply, Message, ReplyKeyboardRemove from classes.pyroclient import PyroClient from modules import custom_filters -from modules.utils import to_utc logger = logging.getLogger(__name__) @@ -58,28 +57,34 @@ async def command_set_time(app: PyroClient, message: Message): now = datetime.now() parsed_time = datetime.strptime(answer.text, "%H:%M").replace( - year=now.year, month=now.month, day=now.day, second=0, microsecond=0 + year=now.year, + month=now.month, + day=now.day, + second=0, + microsecond=0, + tzinfo=timezone.utc, ) - user_time = to_utc(parsed_time, user.location.timezone.zone) + user_time = parsed_time.astimezone(user.location.timezone or timezone.utc) await user.update_time(hour=user_time.hour, minute=user_time.minute) logger.info( - "User %s has selected notification time of %s", + "User %s has selected notification time of %s (%s UTC)", user.id, + parsed_time.strftime("%H:%M"), user_time.strftime("%H:%M"), ) - garbage_time = parsed_time.strftime(app._("time", "formats")) + garbage_time = parsed_time.strftime(app._("time", "formats", locale=user.locale)) await answer.reply_text( app._("set_time_finished", "messages", locale=user.locale).format( offset=user.offset, time=garbage_time, - toggle_notice="" - if user.enabled - else app._("toggle", "messages", locale=user.locale), + toggle_notice=( + "" if user.enabled else app._("toggle", "messages", locale=user.locale) + ), ), reply_markup=ReplyKeyboardRemove(), ) diff --git a/plugins/commands/setup.py b/plugins/commands/setup.py index 3790831..174ec52 100644 --- a/plugins/commands/setup.py +++ b/plugins/commands/setup.py @@ -1,5 +1,4 @@ import logging -from datetime import datetime from convopyro import listen_message from libbot import i18n @@ -11,7 +10,6 @@ from classes.pyroclient import PyroClient from modules import custom_filters from modules.search_name import search_name from modules.search_nearby import search_nearby -from modules.utils import from_utc logger = logging.getLogger(__name__) @@ -74,10 +72,12 @@ async def command_setup(app: PyroClient, message: Message): await user.update_location(location.id) - user_time = from_utc( - datetime(1970, 1, 1, user.time_hour, user.time_minute), - None if user.location is None else user.location.timezone.zone, - ).strftime(app._("time", "formats", locale=user.locale)) + try: + user_time = user.get_reminder_time().strftime( + app._("time", "formats", locale=user.locale) + ) + except AttributeError: + user_time = "N/A" await message.reply_text( app._("setup_finished", "messages", locale=user.locale).format( diff --git a/plugins/commands/start.py b/plugins/commands/start.py index 12d2c6c..d77325d 100644 --- a/plugins/commands/start.py +++ b/plugins/commands/start.py @@ -1,5 +1,3 @@ -from datetime import datetime - from convopyro import listen_message from pyrogram import filters from pyrogram.types import ( @@ -11,7 +9,6 @@ from pyrogram.types import ( from classes.pyroclient import PyroClient from modules import custom_filters -from modules.utils import from_utc @PyroClient.on_message( @@ -91,13 +88,18 @@ async def command_start(app: PyroClient, message: Message): await user.update_location(location.id) - user_time = from_utc( - datetime(1970, 1, 1, user.time_hour, user.time_minute), - None if user.location is None else user.location.timezone.zone, - ).strftime(app._("time", "formats", locale=user.locale)) + try: + user_time = user.get_reminder_time().strftime( + app._("time", "formats", locale=user.locale) + ) + except AttributeError: + user_time = "N/A" + await answer.reply_text( app._("start_selection_yes", "messages", locale=user.locale).format( - name=location.name, offset=user.offset, time=user_time + name=location.name, + offset=user.offset, + time=user_time, ), reply_markup=ReplyKeyboardRemove(), ) diff --git a/plugins/commands/toggle.py b/plugins/commands/toggle.py index 1b67107..c19e232 100644 --- a/plugins/commands/toggle.py +++ b/plugins/commands/toggle.py @@ -1,11 +1,8 @@ -from datetime import datetime - from pyrogram import filters from pyrogram.types import Message from classes.pyroclient import PyroClient from modules import custom_filters -from modules.utils import from_utc @PyroClient.on_message( @@ -22,10 +19,12 @@ async def command_toggle(app: PyroClient, message: Message): ) return - user_time = from_utc( - datetime(1970, 1, 1, user.time_hour, user.time_minute), - None if user.location is None else user.location.timezone.zone, - ).strftime(app._("time", "formats")) + try: + user_time = user.get_reminder_time().strftime( + app._("time", "formats", locale=user.locale) + ) + except AttributeError: + user_time = "N/A" if user.location is None: await message.reply_text(