Fixed possible bugs and renamed pytz imports

This commit is contained in:
Profitroll 2024-05-30 23:23:07 +02:00 committed by Profitroll
parent 1c76c8d911
commit 65e9e830c1
3 changed files with 21 additions and 26 deletions

View File

@ -1,8 +1,7 @@
from datetime import datetime from datetime import datetime
from typing import Union from typing import Union
from pytz import UTC import pytz
from pytz import timezone as pytz_timezone
def to_utc(date: datetime, timezone: Union[str, None] = None) -> datetime: 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. * `datetime`: Timezone unaware datetime in UTC with timezone's offset applied to it.
""" """
timezone = "UTC" if timezone is None else timezone 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: 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 timezone = "UTC" if timezone is None else timezone
return ( return (
pytz_timezone("UTC") pytz.utc.localize(date).astimezone(pytz.timezone(timezone)).replace(tzinfo=None)
.localize(date)
.astimezone(pytz_timezone(timezone))
.replace(tzinfo=None)
) )

View File

@ -1,6 +1,7 @@
import logging import logging
from datetime import datetime, timezone from datetime import datetime
import pytz
from convopyro import listen_message from convopyro import listen_message
from pyrogram import filters from pyrogram import filters
from pyrogram.types import ForceReply, Message, ReplyKeyboardRemove from pyrogram.types import ForceReply, Message, ReplyKeyboardRemove
@ -54,34 +55,30 @@ async def command_set_time(app: PyroClient, message: Message):
break 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( # Datetime user means in their timezone
year=now.year, user_time = datetime.now(user.location.timezone).replace(
month=now.month, hour=parsed_time.hour, minute=parsed_time.minute, second=0, microsecond=0
day=now.day,
second=0,
microsecond=0,
tzinfo=timezone.utc,
) )
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( logger.info(
"User %s has selected notification time of %s (%s UTC)", "User %s has selected notification time of %s (%s UTC)",
user.id, user.id,
parsed_time.strftime("%H:%M"),
user_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( await answer.reply_text(
app._("set_time_finished", "messages", locale=user.locale).format( app._("set_time_finished", "messages", locale=user.locale).format(
offset=user.offset, offset=user.offset,
time=garbage_time, time=user_time.strftime(app._("time", "formats", locale=user.locale)),
toggle_notice=( toggle_notice=(
"" if user.enabled else app._("toggle", "messages", locale=user.locale) "" if user.enabled else app._("toggle", "messages", locale=user.locale)
), ),

View File

@ -22,13 +22,13 @@ async def command_upcoming(app: PyroClient, message: Message):
) )
return return
date_min = ( date_min = pytz.utc.localize(
datetime.now(user.location.timezone).replace(second=0, microsecond=0) 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) datetime.now(user.location.timezone).replace(second=0, microsecond=0)
+ timedelta(days=30) + timedelta(days=30)
).replace(tzinfo=pytz.utc) )
entries = [ entries = [
await GarbageEntry.from_record(entry) await GarbageEntry.from_record(entry)