UTC in the main window should use the NTP time offset

This commit is contained in:
Dan Paulat 2025-08-22 22:29:25 -05:00
parent c76c9b57ed
commit 719142ca12
3 changed files with 45 additions and 9 deletions

View file

@ -1300,8 +1300,8 @@ void MainWindowImpl::ConnectOtherSignals()
this, this,
[this]() [this]()
{ {
timeLabel_->setText(QString::fromStdString( timeLabel_->setText(
util::TimeString(std::chrono::system_clock::now()))); QString::fromStdString(util::TimeString(util::time::now())));
timeLabel_->setVisible(true); timeLabel_->setVisible(true);
}); });
clockTimer_.start(1000); clockTimer_.start(1000);

View file

@ -10,9 +10,7 @@
# include <date/tz.h> # include <date/tz.h>
#endif #endif
namespace scwx namespace scwx::util::time
{
namespace util
{ {
#if (__cpp_lib_chrono >= 201907L) #if (__cpp_lib_chrono >= 201907L)
@ -34,6 +32,9 @@ typedef scwx::util::
ClockFormat GetClockFormat(const std::string& name); ClockFormat GetClockFormat(const std::string& name);
const std::string& GetClockFormatName(ClockFormat clockFormat); const std::string& GetClockFormatName(ClockFormat clockFormat);
template<typename Clock = std::chrono::system_clock>
std::chrono::time_point<Clock> now();
std::chrono::system_clock::time_point TimePoint(uint32_t modifiedJulianDate, std::chrono::system_clock::time_point TimePoint(uint32_t modifiedJulianDate,
uint32_t milliseconds); uint32_t milliseconds);
@ -46,5 +47,17 @@ template<typename T>
std::optional<std::chrono::sys_time<T>> std::optional<std::chrono::sys_time<T>>
TryParseDateTime(const std::string& dateTimeFormat, const std::string& str); TryParseDateTime(const std::string& dateTimeFormat, const std::string& str);
} // namespace util } // namespace scwx::util::time
} // namespace scwx
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

View file

@ -8,6 +8,7 @@
# define __cpp_lib_format 202110L # define __cpp_lib_format 202110L
#endif #endif
#include <scwx/network/ntp_client.hpp>
#include <scwx/util/time.hpp> #include <scwx/util/time.hpp>
#include <scwx/util/enum.hpp> #include <scwx/util/enum.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
@ -21,7 +22,7 @@
# include <date/date.h> # include <date/date.h>
#endif #endif
namespace scwx::util namespace scwx::util::time
{ {
static const std::string logPrefix_ = "scwx::util::time"; static const std::string logPrefix_ = "scwx::util::time";
@ -32,6 +33,8 @@ static const std::unordered_map<ClockFormat, std::string> clockFormatName_ {
{ClockFormat::_24Hour, "24-hour"}, {ClockFormat::_24Hour, "24-hour"},
{ClockFormat::Unknown, "?"}}; {ClockFormat::Unknown, "?"}};
static std::shared_ptr<network::NtpClient> ntpClient_ {nullptr};
SCWX_GET_ENUM(ClockFormat, GetClockFormat, clockFormatName_) SCWX_GET_ENUM(ClockFormat, GetClockFormat, clockFormatName_)
const std::string& GetClockFormatName(ClockFormat clockFormat) const std::string& GetClockFormatName(ClockFormat clockFormat)
@ -39,6 +42,26 @@ const std::string& GetClockFormatName(ClockFormat clockFormat)
return clockFormatName_.at(clockFormat); return clockFormatName_.at(clockFormat);
} }
template<typename Clock>
std::chrono::time_point<Clock> 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<std::chrono::system_clock> now();
std::chrono::system_clock::time_point TimePoint(uint32_t modifiedJulianDate, std::chrono::system_clock::time_point TimePoint(uint32_t modifiedJulianDate,
uint32_t milliseconds) uint32_t milliseconds)
{ {
@ -153,4 +176,4 @@ template std::optional<std::chrono::sys_time<std::chrono::seconds>>
TryParseDateTime<std::chrono::seconds>(const std::string& dateTimeFormat, TryParseDateTime<std::chrono::seconds>(const std::string& dateTimeFormat,
const std::string& str); const std::string& str);
} // namespace scwx::util } // namespace scwx::util::time