From a8ae9fc60241e9d11d2b5f8ee5886422666b4de8 Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Mon, 16 Sep 2024 10:42:08 +0200 Subject: [PATCH] Add own Time class (similar QTime) --- library/include/mobilisis/atb_time.h | 58 +++++++++++++++ library/src/atb_time.cpp | 103 +++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 library/include/mobilisis/atb_time.h create mode 100644 library/src/atb_time.cpp diff --git a/library/include/mobilisis/atb_time.h b/library/include/mobilisis/atb_time.h new file mode 100644 index 0000000..c0b03be --- /dev/null +++ b/library/include/mobilisis/atb_time.h @@ -0,0 +1,58 @@ +#ifndef ATB_TIME_H_INCLUDED +#define ATB_TIME_H_INCLUDED + +#include + +class ATBTime { + QDateTime const m_end; + mutable QDateTime m_time; + +public: + explicit ATBTime(); + explicit ATBTime(int h, int m, int s = 0, int ms = 0); + + int hour() const { return m_time.time().hour(); } + int minute() const { return m_time.time().minute(); } + int second() const { return m_time.time().second(); } + int msec() const { return m_time.time().msec(); } + + int secsTo(QTime t) const { return m_time.time().secsTo(t); } + int msecsTo(QTime t) const { return m_time.time().msecsTo(t); } + + bool setHMS(int h, int m, int s, int ms = 0); + + bool isNull() const { return m_time.time().isNull(); } + bool isValid() const { return m_time.time().isValid(); } + + + QTime addMSecs(int ms) const; + QTime addMSecs(int ms); + + QTime addSecs(int s) const; + QTime addSecs(int s); + + int msecsSinceStartOfDay() const; + QString toString(Qt::DateFormat format = Qt::TextDate) const; + + + static bool isValid(int h, int m, int s, int ms = 0); + + static QTime currentTime() { return QDateTime::currentDateTime().time(); } + + static constexpr QTime fromMSecsSinceStartOfDay(int msecs); + + static QTime fromString(QString const &string, Qt::DateFormat format = Qt::TextDate); + static QTime fromString(QString const &string, const QString &format); + + friend bool operator!=(const ATBTime &lhs, const ATBTime &rhs) noexcept; + friend bool operator<(const ATBTime &lhs, const ATBTime &rhs) noexcept; + friend bool operator<=(const ATBTime &lhs, const ATBTime &rhs) noexcept; + friend bool operator<(const ATBTime &lhs, const ATBTime &rhs) noexcept; + friend bool operator>(const ATBTime &lhs, const ATBTime &rhs) noexcept; + friend bool operator==(const ATBTime &lhs, const ATBTime &rhs) noexcept; + friend QDataStream &operator<<(QDataStream &out, ATBTime time); + friend QDataStream &operator>>(QDataStream &in, ATBTime &time); +}; + + +#endif // ATB_TIME_H_INCLUDED diff --git a/library/src/atb_time.cpp b/library/src/atb_time.cpp new file mode 100644 index 0000000..a040d58 --- /dev/null +++ b/library/src/atb_time.cpp @@ -0,0 +1,103 @@ +#include "atb_time.h" + + +ATBTime::ATBTime() + : m_end(QDateTime::fromString("1970-01-02T00:00:00")) + , m_time(QDateTime::fromString("1970-01-01T00:00:00")) { +} + +ATBTime::ATBTime(int h, int m, int s, int ms) + : m_end(QDateTime::fromString("1970-01-02T00:00:00")) + , m_time(QDateTime::fromString("1970-01-01T00:00:00")) { + + QTime t(h, m, s, ms); + m_time.setTime(t); +} + +QTime ATBTime::addMSecs(int ms) const { + return m_time.time().addMSecs(ms); +} + +QTime ATBTime::addMSecs(int ms) { + QTime t = m_time.time(); + t = t.addMSecs(ms); + m_time.setTime(t); + return t; +} + +QTime ATBTime::addSecs(int s) const { + return m_time.time().addSecs(s); +} + +QTime ATBTime::addSecs(int s) { + QTime t = m_time.time(); + t = t.addSecs(s); + m_time.setTime(t); + return t; +} + +constexpr QTime ATBTime::fromMSecsSinceStartOfDay(int msecs) { + return QTime::fromMSecsSinceStartOfDay(msecs); +} + +QTime ATBTime::fromString(QString const &string, Qt::DateFormat format) { + return QTime::fromString(string, format); +} + +QTime ATBTime::fromString(QString const &string, QString const &format) { + return QTime::fromString(string, format); +} + +bool ATBTime::isValid(int h, int m, int s, int ms) { + return QTime(h, m, s, ms).isValid(); +} + +int ATBTime::msecsSinceStartOfDay() const { + return m_time.time().msecsSinceStartOfDay(); +} + +bool ATBTime::setHMS(int h, int m, int s, int ms) { + if (isValid(h, m, s, ms)) { + QTime t(h, m, s, ms); + m_time.setTime(t); + return true; + } + return false; +} + +QString ATBTime::toString(Qt::DateFormat format) const { + return m_time.time().toString(format); +} + +bool operator!=(const ATBTime &lhs, const ATBTime &rhs) noexcept { + return lhs.m_time.time() != rhs.m_time.time(); +} + +bool operator<=(const ATBTime &lhs, const ATBTime &rhs) noexcept { + return lhs.m_time.time() <= rhs.m_time.time(); + +} + +bool operator<(const ATBTime &lhs, const ATBTime &rhs) noexcept { + return lhs.m_time.time() < rhs.m_time.time(); +} + +bool operator>(const ATBTime &lhs, const ATBTime &rhs) noexcept { + return lhs.m_time.time() > rhs.m_time.time(); +} + +bool operator==(const ATBTime &lhs, const ATBTime &rhs) noexcept { + return lhs.m_time.time() == rhs.m_time.time(); +} + +QDataStream &operator<<(QDataStream &out, ATBTime time) { + out << time.m_time.time(); + return out; +} + +QDataStream &operator>>(QDataStream &in, ATBTime &time) { + QTime t; + in >> t; + time.m_time.setTime(t); + return in; +}