From a8f3907f34945eb4e55d58cc6c3af01803a2e489 Mon Sep 17 00:00:00 2001 From: AIIrondev Date: Tue, 15 Sep 2026 10:51:00 +0200 Subject: [PATCH] Fix of some timezone problems --- Web/app.py | 27 +++++++++++++++++++-------- Web/modules/database/ausleihung.py | 10 +++++----- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/Web/app.py b/Web/app.py index 24a7ba7..ff0894f 100755 --- a/Web/app.py +++ b/Web/app.py @@ -7990,8 +7990,12 @@ def check_availability(): items_col = db['items'] # Collect potential conflicts (planned and active) for this day - same_day_start = datetime.datetime.combine(booking_date.date(), datetime.time.min) - same_day_end = datetime.datetime.combine(booking_date.date(), datetime.time.max) + same_day_start = datetime.datetime.combine( + booking_date.date(), datetime.time.min, tzinfo=ZoneInfo("Europe/Berlin") + ) + same_day_end = datetime.datetime.combine( + booking_date.date(), datetime.time.max, tzinfo=ZoneInfo("Europe/Berlin") + ) candidates = list(ausleihungen.find({ 'Item': item_id, 'Status': {'$in': ['planned', 'active']}, @@ -8009,6 +8013,8 @@ def check_availability(): if r_start is None: r_start = same_day_start # Overlap check: req_start < r_end and req_end > r_start + r_start = au.ensure_timezone_aware(r_start) + r_end = au.ensure_timezone_aware(r_end) if req_start < r_end and req_end > r_start: conflicts.append({ 'id': str(r.get('_id')), @@ -8223,16 +8229,19 @@ def add_booking(): period = request.form.get('period') notes = request.form.get('notes', '') - # Parse dates as naive datetime objects + # Form timestamps represent local school time. try: - # Simple datetime parsing without timezone if start_date_str: - start_date = datetime.datetime.strptime(start_date_str, '%Y-%m-%d %H:%M:%S') + start_date = datetime.datetime.strptime( + start_date_str, '%Y-%m-%d %H:%M:%S' + ).replace(tzinfo=ZoneInfo("Europe/Berlin")) else: return jsonify({'success': False, 'error': 'Missing start date'}) if end_date_str: - end_date = datetime.datetime.strptime(end_date_str, '%Y-%m-%d %H:%M:%S') + end_date = datetime.datetime.strptime( + end_date_str, '%Y-%m-%d %H:%M:%S' + ).replace(tzinfo=ZoneInfo("Europe/Berlin")) else: end_date = None @@ -11274,12 +11283,14 @@ def get_period_times(booking_date, period_num): # Create datetime objects for start and end times start_datetime = datetime.datetime.combine( booking_date.date(), - datetime.time(start_hour, start_min) + datetime.time(start_hour, start_min), + tzinfo=ZoneInfo("Europe/Berlin") ) end_datetime = datetime.datetime.combine( booking_date.date(), - datetime.time(end_hour, end_min) + datetime.time(end_hour, end_min), + tzinfo=ZoneInfo("Europe/Berlin") ) return { diff --git a/Web/modules/database/ausleihung.py b/Web/modules/database/ausleihung.py index df60065..f6e8a97 100755 --- a/Web/modules/database/ausleihung.py +++ b/Web/modules/database/ausleihung.py @@ -42,12 +42,12 @@ def _get_client(): return MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT) # Add this helper function after imports def ensure_timezone_aware(dt): - """Ensures a datetime is timezone-aware, using UTC if naive""" + """Return a timezone-aware datetime, treating naive DB values as UTC.""" if dt is None: return None if dt.tzinfo is None: - # Treat naive datetimes as UTC - return dt.replace(tzinfo=None) + # PyMongo returns BSON datetimes as naive UTC unless tz_aware is enabled. + return dt.replace(tzinfo=datetime.timezone.utc) return dt def get_current_status(ausleihung, log_changes=False, user=None): @@ -82,8 +82,8 @@ def get_current_status(ausleihung, log_changes=False, user=None): return 'completed' current_time = datetime.datetime.now(ZoneInfo("Europe/Berlin")) - start_time = ausleihung.get('Start') - end_time = ausleihung.get('End') + start_time = ensure_timezone_aware(ausleihung.get('Start')) + end_time = ensure_timezone_aware(ausleihung.get('End')) # Wenn kein Startdatum vorhanden ist, Status auf 'planned' setzen if not start_time: