Fix minute timer

git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@6864 ab8295b8-cf94-4d9e-aec4-7959e3be5d79
This commit is contained in:
Bill Somerville 2016-07-04 02:31:50 +00:00
parent 3f1ea9d10c
commit 18871acc02

View File

@ -144,11 +144,12 @@ namespace
|| (type == 6 && !msg_parts.filter ("73").isEmpty ())); || (type == 6 && !msg_parts.filter ("73").isEmpty ()));
} }
int ms_to_next_minute () int ms_minute_error ()
{ {
auto const& now = QDateTime::currentDateTime (); auto const& now = QDateTime::currentDateTime ();
auto const& time = now.time (); auto const& time = now.time ();
return now.msecsTo (QDateTime {now.date (), QTime {time.hour (), time.minute (), 0}}.addSecs (60)); auto second = time.second ();
return now.msecsTo (now.addSecs (second > 30 ? 60 - second : -second)) - time.msec ();
} }
} }
@ -842,7 +843,7 @@ MainWindow::MainWindow(QDir const& temp_directory, bool multiple,
connect (&minuteTimer, &QTimer::timeout, this, &MainWindow::on_the_minute); connect (&minuteTimer, &QTimer::timeout, this, &MainWindow::on_the_minute);
minuteTimer.setSingleShot (true); minuteTimer.setSingleShot (true);
minuteTimer.start (ms_to_next_minute ()); minuteTimer.start (ms_minute_error () + 60 * 1000);
// this must be the last statement of constructor // this must be the last statement of constructor
if (!m_valid) throw std::runtime_error {"Fatal initialization exception"}; if (!m_valid) throw std::runtime_error {"Fatal initialization exception"};
@ -857,11 +858,11 @@ void MainWindow::on_the_minute ()
} }
else else
{ {
auto const& ms = ms_to_next_minute (); auto const& ms_error = ms_minute_error ();
if (qAbs (60 * 1000 - ms) > 1000) // correct drift if (qAbs (ms_error) > 1000) // keep drift within +-1s
{ {
minuteTimer.setSingleShot (true); minuteTimer.setSingleShot (true);
minuteTimer.start (ms); minuteTimer.start (ms_error + 60 * 1000);
} }
} }