fix: BUG-06 — convert naive Pi timestamps to TZ-aware before upload so Postgres TIMESTAMPTZ stores correct instant
This commit is contained in:
+20
-1
@@ -38,6 +38,7 @@ import hashlib
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
from datetime import datetime
|
||||
from threading import Thread
|
||||
|
||||
import requests
|
||||
@@ -139,6 +140,24 @@ def _hash_dashcam_file(abs_path, rel_path):
|
||||
return sha256
|
||||
|
||||
|
||||
def _to_tz_aware(dt_str):
|
||||
"""Convert a naive datetime string (from SQLite) to a timezone-aware ISO
|
||||
string the server's TIMESTAMPTZ column will interpret correctly.
|
||||
|
||||
SQLite stores naive local-time strings like '2026-08-10 19:27:30.123456'.
|
||||
Without a timezone, Postgres interprets these as UTC, shifting displayed
|
||||
times by the local offset. This helper attaches the Pi's local IANA
|
||||
offset so the server stores and displays the correct local time.
|
||||
"""
|
||||
try:
|
||||
dt = datetime.fromisoformat(str(dt_str))
|
||||
except (TypeError, ValueError):
|
||||
return dt_str # unparseable — send as-is and let the server reject it
|
||||
if dt.tzinfo is not None:
|
||||
return dt_str # already TZ-aware — nothing to do
|
||||
return dt.astimezone().isoformat()
|
||||
|
||||
|
||||
def _upload_tracking_batch():
|
||||
"""Send up to TRACKING_BATCH_SIZE not-yet-uploaded rows. Returns count sent.
|
||||
|
||||
@@ -159,7 +178,7 @@ def _upload_tracking_batch():
|
||||
|
||||
points = [
|
||||
{
|
||||
"recorded_at": row["datetime"],
|
||||
"recorded_at": _to_tz_aware(row["datetime"]),
|
||||
"gps_lat": row["gpsx"],
|
||||
"gps_lon": row["gpsy"],
|
||||
"speed": row["speed"],
|
||||
|
||||
Reference in New Issue
Block a user