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
# 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:
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
last_data_check_ts = normalizeTimeStamp(str(last_data_check))
last_data_check_ts = normalizeTimeStamp(last_data_check)
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
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)
# Skip if no plugin state changed since last check

View File

@@ -4,7 +4,9 @@ import os
import pathlib
import sys
from datetime import datetime
from dateutil import parser
import datetime
import re
import pytz
from pytz import timezone
from typing import Union
@@ -23,6 +25,10 @@ from const import *
#-------------------------------------------------------------------------------
# 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():
if conf.tz:
return datetime.datetime.now(conf.tz).replace(microsecond=0)
@@ -56,9 +62,9 @@ def timeNowDB(local=True):
tz = None
except Exception:
tz = None
return datetime.datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S')
return datetime.datetime.now(tz).strftime(DATETIME_PATTERN)
else:
return datetime.datetime.now(datetime.UTC).strftime('%Y-%m-%d %H:%M:%S')
return datetime.datetime.now(datetime.UTC).strftime(DATETIME_PATTERN)
#-------------------------------------------------------------------------------
@@ -85,7 +91,7 @@ def normalizeTimeStamp(inputTimeStamp):
# Epoch timestamp (integer or float)
if isinstance(inputTimeStamp, (int, float)):
try:
try:
return datetime.datetime.fromtimestamp(inputTimeStamp)
except (OSError, OverflowError, ValueError):
return None
@@ -96,8 +102,14 @@ def normalizeTimeStamp(inputTimeStamp):
if not inputTimeStamp:
return None
try:
# Handles SQLite and ISO8601 automatically
return parser.parse(inputTimeStamp)
# match the "2025-11-08 14:32:10" format
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:
return None