BE/PLG: TZ timestamp work #1251
Some checks failed
docker / docker_dev (push) Has been cancelled

Signed-off-by: jokob-sk <jokob.sk@gmail.com>
This commit is contained in:
jokob-sk
2025-11-04 08:10:50 +11:00
parent db18ca76b4
commit c52e44f90c
4 changed files with 41 additions and 17 deletions

View File

@@ -27,10 +27,12 @@ def get_device_data(mac):
# Open temporary connection for this request
conn = get_temp_db_connection()
cur = conn.cursor()
now = timeNowTZ().astimezone().isoformat()
# Special case for new device
if mac.lower() == "new":
now = timeNowTZ().astimezone().isoformat()
device_data = {
"devMac": "",
"devName": "",

View File

@@ -99,8 +99,15 @@ def parse_datetime(dt_str):
return None
def format_date(date_str: str) -> str:
dt = parse_datetime(date_str)
return dt.strftime('%Y-%m-%d %H:%M') if dt else "invalid"
try:
dt = parse_datetime(date_str)
if dt.tzinfo is None:
# Set timezone if missing — change to timezone.utc if you prefer UTC
now = datetime.datetime.now(conf.tz)
dt = dt.replace(tzinfo=now.astimezone().tzinfo)
return dt.astimezone().isoformat()
except Exception:
return "invalid"
def format_date_diff(date1, date2):
"""

View File

@@ -532,21 +532,33 @@ def update_devices_names(pm):
# Retrieve last time name resolution was checked
last_checked = pm.name_plugins_checked
# Collect valid state update timestamps for name-related plugins
state_times = []
# Collect and normalize valid state update timestamps for name-related plugins
state_times = []
latest_state = None
for p in name_plugins:
state_updated = pm.plugin_states.get(p, {}).get("stateUpdated")
if state_updated: # skip empty or None
state_times.append(state_updated)
if not state_updated:
continue
# Normalize and validate timestamp
if isinstance(state_updated, datetime.datetime):
state_times.append(state_updated)
elif isinstance(state_updated, str):
try:
state_times.append(parser.parse(state_updated))
except Exception as e:
mylog('none', f'[Update Device Name] Failed to parse timestamp for {p}: {state_updated!r} ({e})')
else:
mylog('none', f'[Update Device Name] Unexpected timestamp type for {p}: {type(state_updated)}')
# Determine the latest valid timestamp safely
try:
if state_times:
latest_state = max(state_times)
except Exception as e:
mylog('none', f'[Update Device Name] Failed to determine latest timestamp, using fallback ({e})')
latest_state = state_times[-1] if state_times else None
# Determine the latest valid stateUpdated timestamp
latest_state_str = max(state_times, default=None)
if isinstance(latest_state_str, datetime.datetime):
latest_state = latest_state_str
elif latest_state_str:
latest_state = parser.parse(latest_state_str)
else:
latest_state = None
# Skip if no plugin state changed since last check
if last_checked and latest_state and latest_state <= last_checked:
@@ -650,7 +662,7 @@ def update_devices_names(pm):
# --- Step 3: Log last checked time ---
# After resolving names, update last checked
pm.name_plugins_checked = timeNowTZ()
pm.name_plugins_checked = timeNowTZ().astimezone().isoformat()
#-------------------------------------------------------------------------------
# Updates devPresentLastScan for parent devices based on the presence of their NICs