Add some basic QDateTime rounding/truncation functions

This commit is contained in:
Bill Somerville 2020-07-27 19:50:34 +01:00
parent 6cb72a5f3e
commit 08d20e4366
No known key found for this signature in database
GPG Key ID: D864B06D1E81618F
2 changed files with 19 additions and 0 deletions

View File

@ -5,6 +5,7 @@
#include <QWidget>
#include <QStyle>
#include <QVariant>
#include <QDateTime>
QString font_as_stylesheet (QFont const& font)
{
@ -35,3 +36,15 @@ void update_dynamic_property (QWidget * widget, char const * property, QVariant
widget->style ()->polish (widget);
widget->update ();
}
QDateTime qt_round_date_time_to (QDateTime dt, int seconds)
{
dt.setSecsSinceEpoch (dt.addSecs (seconds - 1).toSecsSinceEpoch () / seconds * seconds);
return dt;
}
QDateTime qt_truncate_date_time_to (QDateTime dt, int seconds)
{
dt.setSecsSinceEpoch (dt.toSecsSinceEpoch () / seconds * seconds);
return dt;
}

View File

@ -69,6 +69,12 @@ 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);
// truncate a QDateTime to an interval
QDateTime qt_truncate_date_time_to (QDateTime dt, int seconds);
template <class T>
class VPtr
{