From 4c0ce8d2a24eb502680d90290607dfc05d5ca6a5 Mon Sep 17 00:00:00 2001 From: serversdwn Date: Thu, 29 Jan 2026 21:27:19 +0000 Subject: [PATCH] fix: time now sent in UTC to account for terra-view's TZ user settings. --- series4_emitter.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/series4_emitter.py b/series4_emitter.py index 0979ee4..6480673 100644 --- a/series4_emitter.py +++ b/series4_emitter.py @@ -19,8 +19,9 @@ import re import time import json import sys -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from typing import Dict, Any, Optional, Tuple +from zoneinfo import ZoneInfo try: # urllib is in stdlib; used instead of requests for portability @@ -299,16 +300,19 @@ def build_sfm_payload(unit_map: Dict[str, Dict[str, Any]]) -> Dict[str, Any]: """ Build a JSON-serializable payload for SFM backend. + All timestamps are converted to UTC for transmission (standard practice). + Terra-View stores UTC and converts to local time for display. + Structure (example): { "source": "series4_emitter", - "generated_at": "2025-12-04T20:01:00", + "generated_at": "2025-12-04T20:01:00Z", "units": [ { "unit_id": "UM11719", "type": "micromate", "project_hint": "Clearwater - ECMS 57940", - "last_call": "2025-12-04T19:30:42", + "last_call": "2025-12-05T00:30:42Z", "status": "OK", "age_days": 0.04, "age_hours": 0.9, @@ -318,7 +322,9 @@ def build_sfm_payload(unit_map: Dict[str, Dict[str, Any]]) -> Dict[str, Any]: ] } """ - now = datetime.now() + now_local = datetime.now() + now_utc = datetime.now(timezone.utc) + local_tz = ZoneInfo("America/New_York") payload_units = [] for unit_id, entry in unit_map.items(): @@ -326,15 +332,19 @@ def build_sfm_payload(unit_map: Dict[str, Dict[str, Any]]) -> Dict[str, Any]: project = entry["project"] mlg_path = entry["mlg_path"] - status, age_days = determine_status(last_call, now) + # Use local time for status calculation (age comparison) + status, age_days = determine_status(last_call, now_local) age_hours = age_days * 24.0 + # Convert last_call from local time to UTC for transmission + last_call_utc = last_call.replace(tzinfo=local_tz).astimezone(timezone.utc) + payload_units.append( { "unit_id": unit_id, "type": "micromate", "project_hint": project, - "last_call": last_call.isoformat(), + "last_call": last_call_utc.strftime("%Y-%m-%dT%H:%M:%SZ"), "status": status, "age_days": age_days, "age_hours": age_hours, @@ -344,7 +354,7 @@ def build_sfm_payload(unit_map: Dict[str, Dict[str, Any]]) -> Dict[str, Any]: payload = { "source": "series4_emitter", - "generated_at": now.isoformat(), + "generated_at": now_utc.strftime("%Y-%m-%dT%H:%M:%SZ"), "units": payload_units, } return payload