Add own Time class (similar QTime)

This commit is contained in:
Gerhard Hoffmann 2024-09-16 10:42:08 +02:00
parent f53e9ceaae
commit a8ae9fc602
2 changed files with 161 additions and 0 deletions

View File

@ -0,0 +1,58 @@
#ifndef ATB_TIME_H_INCLUDED
#define ATB_TIME_H_INCLUDED
#include <QDateTime>
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

103
library/src/atb_time.cpp Normal file
View File

@ -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;
}