More flexibility for QDateTime round/truncate routine

This commit is contained in:
Bill Somerville 2020-08-07 21:28:52 +01:00
parent 09dc647c12
commit 0ba5d7e1bf
No known key found for this signature in database
GPG Key ID: D864B06D1E81618F
7 changed files with 175 additions and 11 deletions

1
.gitignore vendored
View File

@ -9,6 +9,7 @@ jnq*
*.mod
*.pro.user
*.txt
!**/CMakeLists.txt
cmake-build-debug
cmake-build-release
CMakeFiles

View File

@ -926,7 +926,9 @@ endif ()
if (WSJT_GENERATE_DOCS)
add_subdirectory (doc)
endif (WSJT_GENERATE_DOCS)
if (EXISTS ${CMAKE_SOURCE_DIR}/tests AND IS_DIRECTORY ${CMAKE_SOURCE_DIR}/tests)
add_subdirectory (tests)
endif ()
#
# Library building setup

View File

@ -37,14 +37,14 @@ void update_dynamic_property (QWidget * widget, char const * property, QVariant
widget->update ();
}
QDateTime qt_round_date_time_to (QDateTime dt, int seconds)
QDateTime qt_round_date_time_to (QDateTime dt, int milliseconds)
{
dt.setSecsSinceEpoch (dt.addSecs (seconds - 1).toSecsSinceEpoch () / seconds * seconds);
dt.setMSecsSinceEpoch (dt.addMSecs (milliseconds / 2).toMSecsSinceEpoch () / milliseconds * milliseconds);
return dt;
}
QDateTime qt_truncate_date_time_to (QDateTime dt, int seconds)
QDateTime qt_truncate_date_time_to (QDateTime dt, int milliseconds)
{
dt.setSecsSinceEpoch (dt.toSecsSinceEpoch () / seconds * seconds);
dt.setMSecsSinceEpoch (dt.toMSecsSinceEpoch () / milliseconds * milliseconds);
return dt;
}

View File

@ -69,11 +69,11 @@ QString font_as_stylesheet (QFont const&);
// conditional style sheet updates
void update_dynamic_property (QWidget *, char const * property, QVariant const& value);
// round a QDateTime instance to an interval
QDateTime qt_round_date_time_to (QDateTime dt, int seconds);
// round a QDateTime instance to an integral interval of milliseconds
QDateTime qt_round_date_time_to (QDateTime dt, int milliseconds);
// truncate a QDateTime to an interval
QDateTime qt_truncate_date_time_to (QDateTime dt, int seconds);
// truncate a QDateTime to an integral interval of milliseconds
QDateTime qt_truncate_date_time_to (QDateTime dt, int milliseconds);
template <class T>
class VPtr

23
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,23 @@
find_package (Qt5Test 5 REQUIRED)
#
# Compiler options
#
set (CMAKE_CXX_STANDARD 11)
add_compile_options ("$<$<COMPILE_LANGUAGE:Fortran>:-Wall;-Wno-conversion;-fno-second-underscore;-fno-f2c>")
add_compile_options ("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Debug>>:-fbounds-check>")
add_compile_options ("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<NOT:$<CONFIG:Debug>>>:-funroll-all-loops>")
add_compile_options ("$<$<AND:$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>,$<OR:$<C_COMPILER_ID:GNU>,$<C_COMPILER_ID:Clang>,$<C_COMPILER_ID:AppleClang>>>:-Wall;-Wextra>")
add_compile_options ("$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:GNU>>:-Wno-pragmas>")
add_compile_options ("$<$<AND:$<OR:$<C_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:GNU>>,$<NOT:$<CONFIG:Debug>>>:-fdata-sections;-ffunction-sections>")
if (${OPENMP_FOUND} OR APPLE)
add_compile_options ("$<$<AND:$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>,$<C_COMPILER_ID:GNU>>:${OpenMP_C_FLAGS}>")
endif ()
# Tell CMake to run moc when necessary
set (CMAKE_AUTOMOC ON)
include_directories (${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR})
add_executable (test_qt_helpers test_qt_helpers.cpp)
target_link_libraries (test_qt_helpers wsjt_qt Qt5::Test)
add_test (test_qt_helpers test_qt_helpers)

138
tests/test_qt_helpers.cpp Normal file
View File

@ -0,0 +1,138 @@
#include <QtTest>
#include <QDateTime>
#include <QDebug>
#include "qt_helpers.hpp"
class TestQtHelpers
: public QObject
{
Q_OBJECT
public:
private:
Q_SLOT void round_15s_date_time_up ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 500}};
QCOMPARE (qt_round_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 30)));
}
Q_SLOT void truncate_15s_date_time_up ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 500}};
QCOMPARE (qt_truncate_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15)));
}
Q_SLOT void round_15s_date_time_down ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 499}};
QCOMPARE (qt_round_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15)));
}
Q_SLOT void truncate_15s_date_time_down ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 499}};
QCOMPARE (qt_truncate_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15)));
}
Q_SLOT void round_15s_date_time_on ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 15}};
QCOMPARE (qt_round_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15)));
}
Q_SLOT void truncate_15s_date_time_on ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 15}};
QCOMPARE (qt_truncate_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15)));
}
Q_SLOT void round_15s_date_time_under ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 14, 999}};
QCOMPARE (qt_round_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15)));
}
Q_SLOT void truncate_15s_date_time_under ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 14, 999}};
QCOMPARE (qt_truncate_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15)));
}
Q_SLOT void round_15s_date_time_over ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 15, 1}};
QCOMPARE (qt_round_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15)));
}
Q_SLOT void truncate_15s_date_time_over ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 15, 1}};
QCOMPARE (qt_truncate_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15)));
}
Q_SLOT void round_7p5s_date_time_up ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 26, 250}};
QCOMPARE (qt_round_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 30)));
}
Q_SLOT void truncate_7p5s_date_time_up ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 26, 250}};
QCOMPARE (qt_truncate_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500)));
}
Q_SLOT void round_7p5s_date_time_down ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 26, 249}};
QCOMPARE (qt_round_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500)));
}
Q_SLOT void truncate_7p5s_date_time_down ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 26, 249}};
QCOMPARE (qt_truncate_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500)));
}
Q_SLOT void round_7p5s_date_time_on ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 500}};
QCOMPARE (qt_round_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500)));
}
Q_SLOT void truncate_7p5s_date_time_on ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 500}};
QCOMPARE (qt_truncate_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500)));
}
Q_SLOT void round_7p5s_date_time_under ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 499}};
QCOMPARE (qt_round_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500)));
}
Q_SLOT void truncate_7p5s_date_time_under ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 499}};
QCOMPARE (qt_truncate_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15)));
}
Q_SLOT void round_7p5s_date_time_over ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 501}};
QCOMPARE (qt_round_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500)));
}
Q_SLOT void truncate_7p5s_date_time_over ()
{
QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 501}};
QCOMPARE (qt_truncate_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500)));
}
};
QTEST_MAIN (TestQtHelpers);
#include "test_qt_helpers.moc"

View File

@ -8017,8 +8017,8 @@ void MainWindow::p1ReadFromStdout() //p1readFromStdout
QString MainWindow::beacon_start_time (int n)
{
auto bt = qt_truncate_date_time_to (QDateTime::currentDateTimeUtc ().addSecs (n), m_TRperiod);
if (m_TRperiod < 60)
auto bt = qt_truncate_date_time_to (QDateTime::currentDateTimeUtc ().addSecs (n), m_TRperiod * 1.e3);
if (m_TRperiod < 60.)
{
return bt.toString ("HHmmss");
}