From 719142ca12c25febead8edc5a23f5aa5fd3cfe0f Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Fri, 22 Aug 2025 22:29:25 -0500 Subject: [PATCH] UTC in the main window should use the NTP time offset --- scwx-qt/source/scwx/qt/main/main_window.cpp | 4 +-- wxdata/include/scwx/util/time.hpp | 23 ++++++++++++++---- wxdata/source/scwx/util/time.cpp | 27 +++++++++++++++++++-- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/scwx-qt/source/scwx/qt/main/main_window.cpp b/scwx-qt/source/scwx/qt/main/main_window.cpp index 4ab5d56b..4fa2f28a 100644 --- a/scwx-qt/source/scwx/qt/main/main_window.cpp +++ b/scwx-qt/source/scwx/qt/main/main_window.cpp @@ -1300,8 +1300,8 @@ void MainWindowImpl::ConnectOtherSignals() this, [this]() { - timeLabel_->setText(QString::fromStdString( - util::TimeString(std::chrono::system_clock::now()))); + timeLabel_->setText( + QString::fromStdString(util::TimeString(util::time::now()))); timeLabel_->setVisible(true); }); clockTimer_.start(1000); diff --git a/wxdata/include/scwx/util/time.hpp b/wxdata/include/scwx/util/time.hpp index e31e8ca9..fe052756 100644 --- a/wxdata/include/scwx/util/time.hpp +++ b/wxdata/include/scwx/util/time.hpp @@ -10,9 +10,7 @@ # include #endif -namespace scwx -{ -namespace util +namespace scwx::util::time { #if (__cpp_lib_chrono >= 201907L) @@ -34,6 +32,9 @@ typedef scwx::util:: ClockFormat GetClockFormat(const std::string& name); const std::string& GetClockFormatName(ClockFormat clockFormat); +template +std::chrono::time_point now(); + std::chrono::system_clock::time_point TimePoint(uint32_t modifiedJulianDate, uint32_t milliseconds); @@ -46,5 +47,17 @@ template std::optional> TryParseDateTime(const std::string& dateTimeFormat, const std::string& str); -} // namespace util -} // namespace scwx +} // namespace scwx::util::time + +namespace scwx::util +{ +// Add types and functions to scwx::util for compatibility +using time::ClockFormat; +using time::ClockFormatIterator; +using time::GetClockFormat; +using time::GetClockFormatName; +using time::time_zone; +using time::TimePoint; +using time::TimeString; +using time::TryParseDateTime; +} // namespace scwx::util diff --git a/wxdata/source/scwx/util/time.cpp b/wxdata/source/scwx/util/time.cpp index 563aea1b..1a687d32 100644 --- a/wxdata/source/scwx/util/time.cpp +++ b/wxdata/source/scwx/util/time.cpp @@ -8,6 +8,7 @@ # define __cpp_lib_format 202110L #endif +#include #include #include #include @@ -21,7 +22,7 @@ # include #endif -namespace scwx::util +namespace scwx::util::time { static const std::string logPrefix_ = "scwx::util::time"; @@ -32,6 +33,8 @@ static const std::unordered_map clockFormatName_ { {ClockFormat::_24Hour, "24-hour"}, {ClockFormat::Unknown, "?"}}; +static std::shared_ptr ntpClient_ {nullptr}; + SCWX_GET_ENUM(ClockFormat, GetClockFormat, clockFormatName_) const std::string& GetClockFormatName(ClockFormat clockFormat) @@ -39,6 +42,26 @@ const std::string& GetClockFormatName(ClockFormat clockFormat) return clockFormatName_.at(clockFormat); } +template +std::chrono::time_point now() +{ + if (ntpClient_ == nullptr) + { + ntpClient_ = network::NtpClient::Instance(); + } + + if (ntpClient_ != nullptr) + { + return Clock::now() + ntpClient_->time_offset(); + } + else + { + return Clock::now(); + } +} + +template std::chrono::time_point now(); + std::chrono::system_clock::time_point TimePoint(uint32_t modifiedJulianDate, uint32_t milliseconds) { @@ -153,4 +176,4 @@ template std::optional> TryParseDateTime(const std::string& dateTimeFormat, const std::string& str); -} // namespace scwx::util +} // namespace scwx::util::time