feat(packet_capture): add UTC ISO 8601 timestamp method and update timestamp usage

- Introduced a new static method `_utc_iso_timestamp` to generate UTC ISO 8601 timestamps with a 'Z' suffix for compatibility.
- Updated timestamp generation in `log_packet` and status message to utilize the new method, ensuring consistent timestamp formatting across the service.
- Added a unit test to verify the correct format of the new timestamp method.
This commit is contained in:
agessaman
2026-06-06 21:16:28 -07:00
parent fca42c50a7
commit a997f86a9f
2 changed files with 15 additions and 3 deletions
@@ -11,7 +11,7 @@ import json
import logging
import os
import time
from datetime import datetime
from datetime import datetime, timezone
from typing import Any, Optional
# Import meshcore
@@ -393,6 +393,11 @@ class PacketCaptureService(BaseServicePlugin):
ttl = 86400
return iat, iat + ttl
@staticmethod
def _utc_iso_timestamp() -> str:
"""UTC ISO 8601 timestamp with Z suffix for broad consumer compatibility."""
return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
@staticmethod
def _jwt_ttl_log_phrase(ttl_seconds: int) -> str:
"""Short TTL description for log lines."""
@@ -694,7 +699,7 @@ class PacketCaptureService(BaseServicePlugin):
dict[str, Any]: Formatted packet dictionary.
"""
current_time = datetime.now()
timestamp = current_time.isoformat()
timestamp = self._utc_iso_timestamp()
# Remove 0x prefix if present
clean_raw_hex = raw_hex.replace("0x", "").upper()
@@ -1839,7 +1844,7 @@ class PacketCaptureService(BaseServicePlugin):
status_msg = {
"status": status,
"timestamp": datetime.now().isoformat(),
"timestamp": self._utc_iso_timestamp(),
"origin": device_name,
"origin_id": device_public_key,
"model": firmware_info.get("model", "unknown"),
@@ -4,6 +4,7 @@ from __future__ import annotations
import configparser
import logging
import re
from unittest.mock import MagicMock
from modules.service_plugins.packet_capture_service import PacketCaptureService
@@ -73,3 +74,9 @@ def test_log_packet_summary_only_when_verbose_or_debug():
svc._log_packet_summary("packet line")
assert len(records) == 1
assert records[0].levelno == logging.INFO
def test_utc_iso_timestamp_is_z_suffixed():
ts = PacketCaptureService._utc_iso_timestamp()
assert ts.endswith("Z")
assert re.match(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$", ts)