mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-29 18:40:06 +00:00
UTC in the main window should use the NTP time offset
This commit is contained in:
parent
c76c9b57ed
commit
719142ca12
3 changed files with 45 additions and 9 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -10,9 +10,7 @@
|
|||
# include <date/tz.h>
|
||||
#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<typename Clock = std::chrono::system_clock>
|
||||
std::chrono::time_point<Clock> now();
|
||||
|
||||
std::chrono::system_clock::time_point TimePoint(uint32_t modifiedJulianDate,
|
||||
uint32_t milliseconds);
|
||||
|
||||
|
|
@ -46,5 +47,17 @@ template<typename T>
|
|||
std::optional<std::chrono::sys_time<T>>
|
||||
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
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
# define __cpp_lib_format 202110L
|
||||
#endif
|
||||
|
||||
#include <scwx/network/ntp_client.hpp>
|
||||
#include <scwx/util/time.hpp>
|
||||
#include <scwx/util/enum.hpp>
|
||||
#include <scwx/util/logger.hpp>
|
||||
|
|
@ -21,7 +22,7 @@
|
|||
# include <date/date.h>
|
||||
#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<ClockFormat, std::string> clockFormatName_ {
|
|||
{ClockFormat::_24Hour, "24-hour"},
|
||||
{ClockFormat::Unknown, "?"}};
|
||||
|
||||
static std::shared_ptr<network::NtpClient> 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<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,
|
||||
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,
|
||||
const std::string& str);
|
||||
|
||||
} // namespace scwx::util
|
||||
} // namespace scwx::util::time
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue