coderuday21 commited on
Commit
e3becbb
·
1 Parent(s): b084ff6

Convert timestamps to IST server-side so display is correct regardless of browser cache

Browse files
Files changed (1) hide show
  1. app/main.py +10 -9
app/main.py CHANGED
@@ -3,7 +3,7 @@ import io
3
  import json
4
  import os
5
  import uuid
6
- from datetime import timezone
7
  from pathlib import Path
8
  from typing import Optional
9
 
@@ -32,15 +32,16 @@ import logging
32
  logger = logging.getLogger(__name__)
33
 
34
 
35
- def _isoformat_utc(dt):
36
- """Return an ISO-8601 string with explicit +00:00 so the browser can
37
- convert to local time. SQLite strips tzinfo on read, so naive datetimes
38
- that we know are UTC need the suffix added back."""
 
39
  if dt is None:
40
  return None
41
  if dt.tzinfo is None:
42
  dt = dt.replace(tzinfo=timezone.utc)
43
- return dt.isoformat()
44
 
45
  # Create tables and run migrations without crashing the app (HF Spaces can restart if startup fails)
46
  try:
@@ -354,7 +355,7 @@ async def detect(
354
  "beforeThumbUrl": f"/api/overlay/{relative_before_thumb}",
355
  "afterThumbUrl": f"/api/overlay/{relative_after_thumb}",
356
  "notificationSent": notification_sent,
357
- "createdAt": _isoformat_utc(run.created_at),
358
  }
359
 
360
 
@@ -395,7 +396,7 @@ def history(
395
  "overlayUrl": f"/api/overlay/{r.overlay_path}" if r.overlay_path else None,
396
  "beforeThumbUrl": f"/api/overlay/{r.before_thumb_path}" if (getattr(r, "before_thumb_path", None) or "").strip() else None,
397
  "afterThumbUrl": f"/api/overlay/{r.after_thumb_path}" if (getattr(r, "after_thumb_path", None) or "").strip() else None,
398
- "createdAt": _isoformat_utc(r.created_at),
399
  }
400
  for r in runs
401
  ]
@@ -434,7 +435,7 @@ def get_run(
434
  "beforeFullUrl": f"/api/overlay/{run.before_full_path}" if (getattr(run, "before_full_path", None) or "").strip() else None,
435
  "beforeThumbUrl": f"/api/overlay/{run.before_thumb_path}" if (getattr(run, "before_thumb_path", None) or "").strip() else None,
436
  "afterThumbUrl": f"/api/overlay/{run.after_thumb_path}" if (getattr(run, "after_thumb_path", None) or "").strip() else None,
437
- "createdAt": _isoformat_utc(run.created_at),
438
  }
439
 
440
 
 
3
  import json
4
  import os
5
  import uuid
6
+ from datetime import timedelta, timezone
7
  from pathlib import Path
8
  from typing import Optional
9
 
 
32
  logger = logging.getLogger(__name__)
33
 
34
 
35
+ _IST = timezone(timedelta(hours=5, minutes=30))
36
+
37
+
38
+ def _isoformat_ist(dt):
39
+ """Convert a UTC datetime to IST (GMT+5:30) and return ISO-8601 string."""
40
  if dt is None:
41
  return None
42
  if dt.tzinfo is None:
43
  dt = dt.replace(tzinfo=timezone.utc)
44
+ return dt.astimezone(_IST).isoformat()
45
 
46
  # Create tables and run migrations without crashing the app (HF Spaces can restart if startup fails)
47
  try:
 
355
  "beforeThumbUrl": f"/api/overlay/{relative_before_thumb}",
356
  "afterThumbUrl": f"/api/overlay/{relative_after_thumb}",
357
  "notificationSent": notification_sent,
358
+ "createdAt": _isoformat_ist(run.created_at),
359
  }
360
 
361
 
 
396
  "overlayUrl": f"/api/overlay/{r.overlay_path}" if r.overlay_path else None,
397
  "beforeThumbUrl": f"/api/overlay/{r.before_thumb_path}" if (getattr(r, "before_thumb_path", None) or "").strip() else None,
398
  "afterThumbUrl": f"/api/overlay/{r.after_thumb_path}" if (getattr(r, "after_thumb_path", None) or "").strip() else None,
399
+ "createdAt": _isoformat_ist(r.created_at),
400
  }
401
  for r in runs
402
  ]
 
435
  "beforeFullUrl": f"/api/overlay/{run.before_full_path}" if (getattr(run, "before_full_path", None) or "").strip() else None,
436
  "beforeThumbUrl": f"/api/overlay/{run.before_thumb_path}" if (getattr(run, "before_thumb_path", None) or "").strip() else None,
437
  "afterThumbUrl": f"/api/overlay/{run.after_thumb_path}" if (getattr(run, "after_thumb_path", None) or "").strip() else None,
438
+ "createdAt": _isoformat_ist(run.created_at),
439
  }
440
 
441