From 65e9e830c18e60a046aac492a267cb1ac190643e Mon Sep 17 00:00:00 2001 From: profitroll Date: Thu, 30 May 2024 23:23:07 +0200 Subject: [PATCH] Fixed possible bugs and renamed pytz imports --- modules/utils.py | 12 +++++------- plugins/commands/set_time.py | 27 ++++++++++++--------------- plugins/commands/upcoming.py | 8 ++++---- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/modules/utils.py b/modules/utils.py index 13e1c96..19b0586 100644 --- a/modules/utils.py +++ b/modules/utils.py @@ -1,8 +1,7 @@ from datetime import datetime from typing import Union -from pytz import UTC -from pytz import timezone as pytz_timezone +import pytz def to_utc(date: datetime, timezone: Union[str, None] = None) -> datetime: @@ -18,7 +17,9 @@ def to_utc(date: datetime, timezone: Union[str, None] = None) -> datetime: * `datetime`: Timezone unaware datetime in UTC with timezone's offset applied to it. """ timezone = "UTC" if timezone is None else timezone - return pytz_timezone(timezone).localize(date).astimezone(UTC).replace(tzinfo=None) + return ( + pytz.timezone(timezone).localize(date).astimezone(pytz.utc).replace(tzinfo=None) + ) def from_utc(date: datetime, timezone: Union[str, None] = None) -> datetime: @@ -35,8 +36,5 @@ def from_utc(date: datetime, timezone: Union[str, None] = None) -> datetime: """ timezone = "UTC" if timezone is None else timezone return ( - pytz_timezone("UTC") - .localize(date) - .astimezone(pytz_timezone(timezone)) - .replace(tzinfo=None) + pytz.utc.localize(date).astimezone(pytz.timezone(timezone)).replace(tzinfo=None) ) diff --git a/plugins/commands/set_time.py b/plugins/commands/set_time.py index 99c5fd7..3d190e6 100644 --- a/plugins/commands/set_time.py +++ b/plugins/commands/set_time.py @@ -1,6 +1,7 @@ import logging -from datetime import datetime, timezone +from datetime import datetime +import pytz from convopyro import listen_message from pyrogram import filters from pyrogram.types import ForceReply, Message, ReplyKeyboardRemove @@ -54,34 +55,30 @@ async def command_set_time(app: PyroClient, message: Message): break - now = datetime.now() + # Time we got from the user + parsed_time = datetime.strptime(answer.text, "%H:%M") - parsed_time = datetime.strptime(answer.text, "%H:%M").replace( - year=now.year, - month=now.month, - day=now.day, - second=0, - microsecond=0, - tzinfo=timezone.utc, + # Datetime user means in their timezone + user_time = datetime.now(user.location.timezone).replace( + hour=parsed_time.hour, minute=parsed_time.minute, second=0, microsecond=0 ) - user_time = parsed_time.astimezone(user.location.timezone or timezone.utc) + # Datetime in user's timezone moved to UTC timezone + utc_time = user_time.astimezone(pytz.utc) - await user.update_time(hour=user_time.hour, minute=user_time.minute) + await user.update_time(hour=utc_time.hour, minute=utc_time.minute) logger.info( "User %s has selected notification time of %s (%s UTC)", user.id, - parsed_time.strftime("%H:%M"), user_time.strftime("%H:%M"), + utc_time.strftime("%H:%M"), ) - 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, + time=user_time.strftime(app._("time", "formats", locale=user.locale)), toggle_notice=( "" if user.enabled else app._("toggle", "messages", locale=user.locale) ), diff --git a/plugins/commands/upcoming.py b/plugins/commands/upcoming.py index e34e79c..9ede51a 100644 --- a/plugins/commands/upcoming.py +++ b/plugins/commands/upcoming.py @@ -22,13 +22,13 @@ async def command_upcoming(app: PyroClient, message: Message): ) return - date_min = ( + date_min = pytz.utc.localize( datetime.now(user.location.timezone).replace(second=0, microsecond=0) - ).replace(tzinfo=pytz.utc) - date_max = ( + ) + date_max = pytz.utc.localize( datetime.now(user.location.timezone).replace(second=0, microsecond=0) + timedelta(days=30) - ).replace(tzinfo=pytz.utc) + ) entries = [ await GarbageEntry.from_record(entry)