From 2e2e9576acfa267324fe9bed837fb91c0dc81973 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 10 Feb 2026 14:52:37 -0500 Subject: [PATCH] refactor: move format_absolute_time_iso8601 from zm_monitor_onvif to zm_time Move the ISO 8601 time formatting function to zm_time.cpp/h so it is reusable and not duplicated. Remove the local copies from zm_monitor_onvif.cpp (was static) and tests/zm_onvif_renewal.cpp (was a copy for testing). Both now use the shared declaration from zm_time.h. Co-Authored-By: Claude Opus 4.6 --- src/zm_monitor_onvif.cpp | 14 +------------- src/zm_time.cpp | 11 +++++++++++ src/zm_time.h | 1 + tests/zm_onvif_renewal.cpp | 14 -------------- 4 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/zm_monitor_onvif.cpp b/src/zm_monitor_onvif.cpp index d76c3622e..20438ae06 100644 --- a/src/zm_monitor_onvif.cpp +++ b/src/zm_monitor_onvif.cpp @@ -20,6 +20,7 @@ #include "zm_monitor_onvif.h" #include "zm_monitor.h" #include "zm_signal.h" +#include "zm_time.h" #include "zm_utils.h" #include @@ -1029,19 +1030,6 @@ void ONVIF::log_subscription_timing(const char* context) { } -// Format an absolute time as ISO 8601 string for ONVIF RenewRequest -// Returns a string like "2026-01-13T15:30:45.000Z" -std::string format_absolute_time_iso8601(time_t time) { - struct tm *tm_utc = gmtime(&time); - if (!tm_utc) { - return ""; - } - - char buffer[32]; - strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S.000Z", tm_utc); - return std::string(buffer); -} - // Perform ONVIF subscription renewal // Returns true if renewal succeeded or is not supported, false on error bool ONVIF::Renew() { diff --git a/src/zm_time.cpp b/src/zm_time.cpp index b801c128a..17ce6f047 100644 --- a/src/zm_time.cpp +++ b/src/zm_time.cpp @@ -67,6 +67,17 @@ std::string TimePointToString(TimePoint tp) { return timeString; } +std::string format_absolute_time_iso8601(time_t time) { + struct tm *tm_utc = gmtime(&time); + if (!tm_utc) { + return ""; + } + + char buffer[32]; + strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S.000Z", tm_utc); + return std::string(buffer); +} + SystemTimePoint StringToSystemTimePoint(const std::string ×tamp) { std::tm t{}; strptime(timestamp.c_str(), "%Y-%m-%d %H:%M:%S", &t); diff --git a/src/zm_time.h b/src/zm_time.h index 8d5465b02..9bef9f5e5 100644 --- a/src/zm_time.h +++ b/src/zm_time.h @@ -121,6 +121,7 @@ class TimeSegmentAdder { bool finished_; }; +std::string format_absolute_time_iso8601(time_t time); std::string SystemTimePointToString(SystemTimePoint tp); std::string SystemTimePointToMysqlString(SystemTimePoint tp); std::string TimePointToString(TimePoint tp); diff --git a/tests/zm_onvif_renewal.cpp b/tests/zm_onvif_renewal.cpp index b7e99e212..d958ab8f8 100644 --- a/tests/zm_onvif_renewal.cpp +++ b/tests/zm_onvif_renewal.cpp @@ -150,20 +150,6 @@ TEST_CASE("ONVIF Subscription Cleanup Logic") { } } -// Helper function from zm_monitor_onvif.cpp for testing -// Format an absolute time as ISO 8601 string for ONVIF RenewRequest -// Returns a string like "2026-01-13T15:30:45.000Z" -std::string format_absolute_time_iso8601(time_t time) { - struct tm *tm_utc = gmtime(&time); - if (!tm_utc) { - return ""; - } - - char buffer[32]; - strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S.000Z", tm_utc); - return std::string(buffer); -} - // Test the ISO 8601 absolute time formatting for ONVIF renewal requests TEST_CASE("ONVIF Absolute Time Formatting") { SECTION("Format known timestamp as ISO 8601") {