BE: timestamp work name changes #1251

Signed-off-by: jokob-sk <jokob.sk@gmail.com>
This commit is contained in:
jokob-sk
2025-11-08 22:01:04 +11:00
parent 552d2a8286
commit 5cf8a25bae
2 changed files with 22 additions and 10 deletions

View File

@@ -558,20 +558,20 @@ def check_plugin_data_changed(pm, plugins_to_check):
continue continue
# Normalize and validate last_changed timestamp # Normalize and validate last_changed timestamp
last_changed_ts = normalizeTimeStamp(str(last_data_change)) last_changed_ts = normalizeTimeStamp(last_data_change)
if last_changed_ts == None: if last_changed_ts == None:
mylog('none', f'[check_plugin_data_changed] Unexpected last_data_change timestamp for {plugin_name}: {last_data_change}') mylog('none', f'[check_plugin_data_changed] Unexpected last_data_change timestamp for {plugin_name} (input|output): ({last_data_change}|{last_changed_ts})')
# Normalize and validate last_data_check timestamp # Normalize and validate last_data_check timestamp
last_data_check_ts = normalizeTimeStamp(str(last_data_check)) last_data_check_ts = normalizeTimeStamp(last_data_check)
if last_data_check_ts == None: if last_data_check_ts == None:
mylog('none', f'[check_plugin_data_changed] Unexpected last_data_check timestamp for {plugin_name}: {last_data_check}') mylog('none', f'[check_plugin_data_changed] Unexpected last_data_check timestamp for {plugin_name} (input|output): ({last_data_check}|{last_data_check_ts})')
# Track which plugins have newer state than last_checked # Track which plugins have newer state than last_checked
if last_data_check_ts is None or last_changed_ts is None or last_changed_ts > last_data_check_ts: if last_data_check_ts is None or last_changed_ts is None or last_changed_ts > last_data_check_ts:
mylog('debug', f'[check_plugin_data_changed] plugin_name changed last_changed_ts | last_data_check_ts: {last_changed_ts} | {last_data_check_ts}') mylog('debug', f'[check_plugin_data_changed] {plugin_name} changed (last_changed_ts|last_data_check_ts): ({last_changed_ts}|{last_data_check_ts})')
plugins_changed.append(plugin_name) plugins_changed.append(plugin_name)
# Skip if no plugin state changed since last check # Skip if no plugin state changed since last check

View File

@@ -4,7 +4,9 @@ import os
import pathlib import pathlib
import sys import sys
from datetime import datetime from datetime import datetime
from dateutil import parser
import datetime import datetime
import re
import pytz import pytz
from pytz import timezone from pytz import timezone
from typing import Union from typing import Union
@@ -23,6 +25,10 @@ from const import *
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# DateTime # DateTime
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
DATETIME_PATTERN = "%Y-%m-%d %H:%M:%S"
DATETIME_REGEX = re.compile(r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$')
def timeNowTZ(): def timeNowTZ():
if conf.tz: if conf.tz:
return datetime.datetime.now(conf.tz).replace(microsecond=0) return datetime.datetime.now(conf.tz).replace(microsecond=0)
@@ -56,9 +62,9 @@ def timeNowDB(local=True):
tz = None tz = None
except Exception: except Exception:
tz = None tz = None
return datetime.datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S') return datetime.datetime.now(tz).strftime(DATETIME_PATTERN)
else: else:
return datetime.datetime.now(datetime.UTC).strftime('%Y-%m-%d %H:%M:%S') return datetime.datetime.now(datetime.UTC).strftime(DATETIME_PATTERN)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
@@ -96,8 +102,14 @@ def normalizeTimeStamp(inputTimeStamp):
if not inputTimeStamp: if not inputTimeStamp:
return None return None
try: try:
# Handles SQLite and ISO8601 automatically # match the "2025-11-08 14:32:10" format
return parser.parse(inputTimeStamp) pattern = DATETIME_REGEX
if pattern.match(inputTimeStamp):
return datetime.datetime.strptime(inputTimeStamp, DATETIME_PATTERN)
else:
# Handles SQLite and ISO8601 automatically
return parser.parse(inputTimeStamp)
except Exception: except Exception:
return None return None