From a997f86a9faa9652ece8d171d90e8dfcfee8979f Mon Sep 17 00:00:00 2001 From: agessaman Date: Sat, 6 Jun 2026 21:16:28 -0700 Subject: [PATCH] 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. --- modules/service_plugins/packet_capture_service.py | 11 ++++++++--- tests/unit/test_packet_capture_log_level.py | 7 +++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/modules/service_plugins/packet_capture_service.py b/modules/service_plugins/packet_capture_service.py index 73785f2..165b25f 100644 --- a/modules/service_plugins/packet_capture_service.py +++ b/modules/service_plugins/packet_capture_service.py @@ -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"), diff --git a/tests/unit/test_packet_capture_log_level.py b/tests/unit/test_packet_capture_log_level.py index ec3d965..99de961 100644 --- a/tests/unit/test_packet_capture_log_level.py +++ b/tests/unit/test_packet_capture_log_level.py @@ -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)