Yet another timezones fix

This commit is contained in:
Profitroll 2023-10-15 22:21:10 +02:00
parent 192602471b
commit 842218859c
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2
2 changed files with 25 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import logging import logging
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta
from bson import json_util
from libbot.pyrogram.classes import PyroClient from libbot.pyrogram.classes import PyroClient
from classes.enums import GarbageType from classes.enums import GarbageType
@ -15,11 +16,21 @@ logger = logging.getLogger(__name__)
async def remind(app: PyroClient) -> None: async def remind(app: PyroClient) -> None:
utcnow = datetime.utcnow() utcnow = datetime.utcnow()
logger.debug("Performing reminder lookup for %s (UTCNOW)", utcnow)
users = await col_users.find( users = await col_users.find(
{"time_hour": utcnow.hour, "time_minute": utcnow.minute} {"time_hour": utcnow.hour, "time_minute": utcnow.minute}
).to_list() ).to_list()
logger.debug(
"Found following reminders for %s (UTC NOW): %s",
utcnow,
json_util.dumps(users, indent=None),
)
for user_db in users: for user_db in users:
logger.debug("Processing user %s...", json_util.dumps(user_db, indent=None))
user = await PyroUser.from_dict(**user_db) user = await PyroUser.from_dict(**user_db)
if not user.enabled or user.location is None: if not user.enabled or user.location is None:
@ -31,20 +42,26 @@ async def remind(app: PyroClient) -> None:
continue continue
user_date = from_utc( user_date = from_utc(
datetime(1970, 1, 1, user.time_hour, user.time_minute), datetime.utcnow() + timedelta(days=user.offset),
user.location.timezone.zone, user.location.timezone.zone,
) ).replace(hour=0, minute=0, second=0, microsecond=0)
entries = await col_entries.find( entries = await col_entries.find(
{ {
"locations": location.id, "locations": location.id,
"date": user_date.replace(hour=0, minute=0), "date": user_date,
} }
).to_list() ).to_list()
logger.info("Entries of %s for %s: %s", user.id, user_date, entries) logger.info("Entries of %s for %s: %s", user.id, user_date, entries)
for entry in entries: for entry in entries:
logger.debug(
"Sending %s notification about %s",
user.id,
json_util.dumps(entry, indent=None),
)
try: try:
garbage_type = app._( garbage_type = app._(
str(GarbageType(entry["garbage_type"]).value), str(GarbageType(entry["garbage_type"]).value),

View File

@ -55,9 +55,12 @@ async def command_set_time(app: PyroClient, message: Message):
break break
now = datetime.now()
parsed_time = datetime.strptime(answer.text, "%H:%M").replace( parsed_time = datetime.strptime(answer.text, "%H:%M").replace(
year=1970, month=1, day=1, second=0, microsecond=0 year=now.year, month=now.month, day=now.day, second=0, microsecond=0
) )
user_time = to_utc(parsed_time, user.location.timezone.zone) user_time = to_utc(parsed_time, user.location.timezone.zone)
await user.update_time(hour=user_time.hour, minute=user_time.minute) await user.update_time(hour=user_time.hour, minute=user_time.minute)