diff --git a/app/__init__.py b/app/__init__.py index 9c2aa28..358544e 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,7 +1,10 @@ +from datetime import datetime, timezone, timedelta from flask import Flask from app.config import Config from app.db import init_db, close_db +SYDNEY_TZ = timezone(timedelta(hours=11)) + def create_app(): app = Flask(__name__) @@ -11,6 +14,15 @@ def create_app(): init_db(app) app.teardown_appcontext(close_db) + # Jinja2 filter: convert UTC to Sydney time + @app.template_filter('sydney') + def sydney_time_filter(dt, fmt='%d %b %Y, %H:%M'): + if dt is None: + return '' + if dt.tzinfo is None: + dt = dt.replace(tzinfo=timezone.utc) + return dt.astimezone(SYDNEY_TZ).strftime(fmt) + # Register blueprints from app.routes.auth import bp as auth_bp from app.routes.dashboard import bp as dashboard_bp diff --git a/app/routes/api.py b/app/routes/api.py index a4436e5..d415d4a 100644 --- a/app/routes/api.py +++ b/app/routes/api.py @@ -1,6 +1,8 @@ from flask import Blueprint, jsonify +from app import SYDNEY_TZ from app.auth import login_required from app.db import query +from datetime import timezone bp = Blueprint("api", __name__, url_prefix="/api") @@ -16,7 +18,7 @@ def chart_data(user_id): (user_id,), ) - labels = [c["checked_in_at"].strftime("%d %b") for c in checkins] + labels = [c["checked_in_at"].replace(tzinfo=timezone.utc).astimezone(SYDNEY_TZ).strftime("%d %b") for c in checkins] weights = [float(c["weight_kg"]) for c in checkins] bmis = [float(c["bmi"]) if c["bmi"] else None for c in checkins] @@ -88,7 +90,7 @@ def weekly_change(user_id): prev_w = float(checkins[i - 1]["weight_kg"]) curr_w = float(checkins[i]["weight_kg"]) change = round(curr_w - prev_w, 1) - label = checkins[i]["checked_in_at"].strftime("%d %b") + label = checkins[i]["checked_in_at"].replace(tzinfo=timezone.utc).astimezone(SYDNEY_TZ).strftime("%d %b") labels.append(label) changes.append(change) diff --git a/app/templates/dashboard.html b/app/templates/dashboard.html index 9316909..0db53f6 100644 --- a/app/templates/dashboard.html +++ b/app/templates/dashboard.html @@ -75,7 +75,7 @@