From ee4fc596a2ac90775f5150955facedba1e56d767 Mon Sep 17 00:00:00 2001 From: Soubhik Kumar Gon Date: Sat, 1 Jun 2024 20:13:10 +0530 Subject: [PATCH] feat: improves ist time checks --- time_check.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/time_check.py b/time_check.py index 318dc98..173ad54 100644 --- a/time_check.py +++ b/time_check.py @@ -1,35 +1,34 @@ from datetime import datetime, time, timedelta import pytz from db import connect_to_database -initial = 15 -final= 18 +initial = 16 +final= 12 def is_in_time_bracket(msg_sending_time): - ist = pytz.timezone('Asia/Kolkata') msg_sending_time_ist = msg_sending_time.astimezone(ist) + msg_time = msg_sending_time_ist.time() - - start_time = time(initial, 0) - end_time = time(final, 0) - - if msg_sending_time_ist.time() >= start_time or msg_sending_time_ist.time() < end_time: + start_time = time(initial, 0) + end_time = time(final, 0) + + if start_time <= msg_time or msg_time <= end_time: return True return False + def is_unique_in_time_bracket(discord_user_id, msg_sending_time): conn = connect_to_database() cur = conn.cursor() - ist = pytz.timezone('Asia/Kolkata') + msg_sending_time_ist = msg_sending_time.astimezone(ist) + msg_sending_date_ist = msg_sending_time_ist.date() + + bracket_start = ist.localize(datetime.combine(msg_sending_date_ist, time(initial, 0))) + bracket_end = ist.localize(datetime.combine(msg_sending_date_ist + timedelta(days=1), time(final, 0))) - if msg_sending_time_ist.time() >= time(initial, 0): - bracket_start = msg_sending_time_ist.replace(hour=initial, minute=0, second=0, microsecond=0) - bracket_end = (bracket_start + timedelta(hours=14)).replace(hour=final, minute=0, second=0, microsecond=0) - else: - bracket_start = (msg_sending_time_ist.replace(hour=initial, minute=0, second=0, microsecond=0) - timedelta(days=1)) - bracket_end = msg_sending_time_ist.replace(hour=final, minute=0, second=0, microsecond=0) + print(f"Bracket start: {bracket_start}, Bracket end: {bracket_end}") cur.execute(""" SELECT COUNT(*) @@ -41,10 +40,13 @@ def is_unique_in_time_bracket(discord_user_id, msg_sending_time): result = cur.fetchone() cur.close() + conn.close() + + print(f"Query result: {result}") if result[0] > 0: return False - + return True def can_send_message(discord_user_id, msg_sending_time):