fix: timezone-aware datetime for elapsed timer in screen API

This commit is contained in:
Forge
2026-03-19 01:17:45 +08:00
parent fe839cb6ae
commit 1be2ba729e
2 changed files with 9 additions and 5 deletions

View File

@@ -8,7 +8,7 @@ from app.models.match import Match, MatchPlayer, MatchStatus
from app.models.player import Player from app.models.player import Player
from app.models.tournament import Tournament, TournamentStatus from app.models.tournament import Tournament, TournamentStatus
from app.services.tournament import get_bracket_data from app.services.tournament import get_bracket_data
from datetime import datetime from datetime import datetime, timezone
router = APIRouter(prefix="/screen", tags=["screen"]) router = APIRouter(prefix="/screen", tags=["screen"])
@@ -47,7 +47,11 @@ def get_court_display(court_id: int, db: Session = Depends(get_db)):
elapsed = None elapsed = None
if active_match.started_at: if active_match.started_at:
elapsed = int((datetime.utcnow() - active_match.started_at).total_seconds()) now = datetime.now(timezone.utc)
started = active_match.started_at
if started.tzinfo is None:
started = started.replace(tzinfo=timezone.utc)
elapsed = int((now - started).total_seconds())
match_data = { match_data = {
"id": active_match.id, "id": active_match.id,
@@ -89,7 +93,7 @@ def get_court_display(court_id: int, db: Session = Depends(get_db)):
}, },
"active_match": match_data, "active_match": match_data,
"recent_matches": recent, "recent_matches": recent,
"timestamp": datetime.utcnow().isoformat(), "timestamp": datetime.now(timezone.utc).isoformat(),
} }
@@ -152,5 +156,5 @@ def get_overview(db: Session = Depends(get_db)):
} }
for i, p in enumerate(top_players) for i, p in enumerate(top_players)
], ],
"timestamp": datetime.utcnow().isoformat(), "timestamp": datetime.now(timezone.utc).isoformat(),
} }

View File

@@ -15,7 +15,7 @@ except Exception as e:
done done
echo "Running seed..." echo "Running seed..."
cd /app && python seed.py 2>&1 && echo "Seed complete" || echo "Seed failed or already ran (continuing)" cd /app && python seed.py && echo "Seed complete" || echo "Seed failed (continuing)"
echo "Starting server..." echo "Starting server..."
exec uvicorn app.main:app --host 0.0.0.0 --port 8000 exec uvicorn app.main:app --host 0.0.0.0 --port 8000