agdar/test_timezone.py
2025-11-02 14:35:35 +03:00

73 lines
2.2 KiB
Python

#!/usr/bin/env python
"""
Test script to verify timezone configuration.
Run this with: python test_timezone.py
"""
import os
import django
# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'AgdarCentre.settings')
django.setup()
from django.utils import timezone
from django.conf import settings
import datetime
print("=" * 60)
print("TIMEZONE CONFIGURATION TEST")
print("=" * 60)
# Check settings
print(f"\n1. Django Settings:")
print(f" TIME_ZONE: {settings.TIME_ZONE}")
print(f" USE_TZ: {settings.USE_TZ}")
print(f" CELERY_TIMEZONE: {settings.CELERY_TIMEZONE}")
# Get current time
print(f"\n2. Current Time:")
now = timezone.now()
print(f" timezone.now(): {now}")
print(f" Timezone: {now.tzinfo}")
print(f" ISO Format: {now.isoformat()}")
# Compare with naive datetime
naive_now = datetime.datetime.now()
print(f"\n3. Comparison:")
print(f" datetime.now() (naive): {naive_now}")
print(f" timezone.now() (aware): {now}")
print(f" Difference: {(now.replace(tzinfo=None) - naive_now).total_seconds()} seconds")
# Test timezone conversion
print(f"\n4. Timezone Info:")
print(f" Current timezone name: {timezone.get_current_timezone()}")
print(f" Current timezone offset: {now.strftime('%z')}")
# Test with HR attendance (if exists)
try:
from hr.models import Attendance
from core.models import User
# Try to get a recent attendance record
recent_attendance = Attendance.objects.order_by('-check_in').first()
if recent_attendance:
print(f"\n5. Recent Attendance Record:")
print(f" Check-in time: {recent_attendance.check_in}")
print(f" Check-in timezone: {recent_attendance.check_in.tzinfo}")
print(f" Is timezone aware: {timezone.is_aware(recent_attendance.check_in)}")
else:
print(f"\n5. No attendance records found to test")
except Exception as e:
print(f"\n5. Could not test attendance: {e}")
print("\n" + "=" * 60)
print("TEST COMPLETE")
print("=" * 60)
print("\nExpected Results:")
print("- TIME_ZONE should be: Asia/Riyadh")
print("- USE_TZ should be: True")
print("- timezone.now() should show UTC+3 offset")
print("- All datetime objects should be timezone-aware")
print("=" * 60)