fix: time now sent in UTC to account for terra-view's TZ user settings.
This commit is contained in:
@@ -19,8 +19,9 @@ import re
|
|||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta, timezone
|
||||||
from typing import Dict, Any, Optional, Tuple
|
from typing import Dict, Any, Optional, Tuple
|
||||||
|
from zoneinfo import ZoneInfo
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# urllib is in stdlib; used instead of requests for portability
|
# 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.
|
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):
|
Structure (example):
|
||||||
{
|
{
|
||||||
"source": "series4_emitter",
|
"source": "series4_emitter",
|
||||||
"generated_at": "2025-12-04T20:01:00",
|
"generated_at": "2025-12-04T20:01:00Z",
|
||||||
"units": [
|
"units": [
|
||||||
{
|
{
|
||||||
"unit_id": "UM11719",
|
"unit_id": "UM11719",
|
||||||
"type": "micromate",
|
"type": "micromate",
|
||||||
"project_hint": "Clearwater - ECMS 57940",
|
"project_hint": "Clearwater - ECMS 57940",
|
||||||
"last_call": "2025-12-04T19:30:42",
|
"last_call": "2025-12-05T00:30:42Z",
|
||||||
"status": "OK",
|
"status": "OK",
|
||||||
"age_days": 0.04,
|
"age_days": 0.04,
|
||||||
"age_hours": 0.9,
|
"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 = []
|
payload_units = []
|
||||||
|
|
||||||
for unit_id, entry in unit_map.items():
|
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"]
|
project = entry["project"]
|
||||||
mlg_path = entry["mlg_path"]
|
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
|
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(
|
payload_units.append(
|
||||||
{
|
{
|
||||||
"unit_id": unit_id,
|
"unit_id": unit_id,
|
||||||
"type": "micromate",
|
"type": "micromate",
|
||||||
"project_hint": project,
|
"project_hint": project,
|
||||||
"last_call": last_call.isoformat(),
|
"last_call": last_call_utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||||
"status": status,
|
"status": status,
|
||||||
"age_days": age_days,
|
"age_days": age_days,
|
||||||
"age_hours": age_hours,
|
"age_hours": age_hours,
|
||||||
@@ -344,7 +354,7 @@ def build_sfm_payload(unit_map: Dict[str, Dict[str, Any]]) -> Dict[str, Any]:
|
|||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
"source": "series4_emitter",
|
"source": "series4_emitter",
|
||||||
"generated_at": now.isoformat(),
|
"generated_at": now_utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||||
"units": payload_units,
|
"units": payload_units,
|
||||||
}
|
}
|
||||||
return payload
|
return payload
|
||||||
|
|||||||
Reference in New Issue
Block a user