Skip to content

Commit 4205293

Browse files
committed
core/clock: expose date as a QDateTime
1 parent 325be88 commit 4205293

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

src/core/clock.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#include <qtmetamacros.h>
77
#include <qtypes.h>
88

9-
#include "util.hpp"
10-
119
SystemClock::SystemClock(QObject* parent): QObject(parent) {
1210
QObject::connect(&this->timer, &QTimer::timeout, this, &SystemClock::onTimeout);
1311
this->update();
@@ -48,19 +46,16 @@ void SystemClock::update() {
4846
void SystemClock::setTime(const QDateTime& targetTime) {
4947
auto currentTime = QDateTime::currentDateTime();
5048
auto offset = currentTime.msecsTo(targetTime);
51-
auto dtime = offset > -500 && offset < 500 ? targetTime : currentTime;
52-
auto time = dtime.time();
53-
54-
auto secondPrecision = this->mPrecision >= SystemClock::Seconds;
55-
auto secondChanged = this->setSeconds(secondPrecision ? time.second() : 0);
49+
this->currentTime = offset > -500 && offset < 500 ? targetTime : currentTime;
5650

57-
auto minutePrecision = this->mPrecision >= SystemClock::Minutes;
58-
auto minuteChanged = this->setMinutes(minutePrecision ? time.minute() : 0);
51+
auto time = this->currentTime.time();
52+
this->currentTime.setTime(QTime(
53+
this->mPrecision >= SystemClock::Hours ? time.hour() : 0,
54+
this->mPrecision >= SystemClock::Minutes ? time.minute() : 0,
55+
this->mPrecision >= SystemClock::Seconds ? time.second() : 0
56+
));
5957

60-
auto hourPrecision = this->mPrecision >= SystemClock::Hours;
61-
auto hourChanged = this->setHours(hourPrecision ? time.hour() : 0);
62-
63-
DropEmitter::call(secondChanged, minuteChanged, hourChanged);
58+
emit this->dateChanged();
6459
}
6560

6661
void SystemClock::schedule(const QDateTime& targetTime) {
@@ -76,11 +71,11 @@ void SystemClock::schedule(const QDateTime& targetTime) {
7671
auto nextTime = offset > 0 && offset < 500 ? targetTime : currentTime;
7772

7873
auto baseTimeT = nextTime.time();
79-
nextTime.setTime(
80-
{hourPrecision ? baseTimeT.hour() : 0,
81-
minutePrecision ? baseTimeT.minute() : 0,
82-
secondPrecision ? baseTimeT.second() : 0}
83-
);
74+
nextTime.setTime(QTime(
75+
hourPrecision ? baseTimeT.hour() : 0,
76+
minutePrecision ? baseTimeT.minute() : 0,
77+
secondPrecision ? baseTimeT.second() : 0
78+
));
8479

8580
if (secondPrecision) nextTime = nextTime.addSecs(1);
8681
else if (minutePrecision) nextTime = nextTime.addSecs(60);
@@ -91,7 +86,3 @@ void SystemClock::schedule(const QDateTime& targetTime) {
9186
this->timer.start(static_cast<qint32>(delay));
9287
this->targetTime = nextTime;
9388
}
94-
95-
DEFINE_MEMBER_GETSET(SystemClock, hours, setHours);
96-
DEFINE_MEMBER_GETSET(SystemClock, minutes, setMinutes);
97-
DEFINE_MEMBER_GETSET(SystemClock, seconds, setSeconds);

src/core/clock.hpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,26 @@
77
#include <qtmetamacros.h>
88
#include <qtypes.h>
99

10-
#include "util.hpp"
11-
1210
///! System clock accessor.
11+
/// SystemClock is a view into the system's clock.
12+
/// It updates at hour, minute, or second intervals depending on @@precision.
13+
///
14+
/// # Examples
15+
/// ```qml
16+
/// SystemClock {
17+
/// id: clock
18+
/// precision: SystemClock.Seconds
19+
/// }
20+
///
21+
/// @@QtQuick.Text {
22+
/// text: Qt.formatDateTime(clock.date, "hh:mm:ss - yyyy-MM-dd")
23+
/// }
24+
/// ```
25+
///
26+
/// > [!WARNING] Clock updates will trigger within 50ms of the system clock changing,
27+
/// > however this can be either before or after the clock changes (+-50ms). If you
28+
/// > need a date object, use @@date instead of constructing a new one, or the time
29+
/// > of the constructed object could be off by up to a second.
1330
class SystemClock: public QObject {
1431
Q_OBJECT;
1532
/// If the clock should update. Defaults to true.
@@ -18,12 +35,17 @@ class SystemClock: public QObject {
1835
Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged);
1936
/// The precision the clock should measure at. Defaults to `SystemClock.Seconds`.
2037
Q_PROPERTY(SystemClock::Enum precision READ precision WRITE setPrecision NOTIFY precisionChanged);
38+
/// The current date and time.
39+
///
40+
/// > [!TIP] You can use @@QtQml.Qt.formatDateTime() to get the time as a string in
41+
/// > your format of choice.
42+
Q_PROPERTY(QDateTime date READ date NOTIFY dateChanged);
2143
/// The current hour.
22-
Q_PROPERTY(quint32 hours READ hours NOTIFY hoursChanged);
44+
Q_PROPERTY(quint32 hours READ hours NOTIFY dateChanged);
2345
/// The current minute, or 0 if @@precision is `SystemClock.Hours`.
24-
Q_PROPERTY(quint32 minutes READ minutes NOTIFY minutesChanged);
46+
Q_PROPERTY(quint32 minutes READ minutes NOTIFY dateChanged);
2547
/// The current second, or 0 if @@precision is `SystemClock.Hours` or `SystemClock.Minutes`.
26-
Q_PROPERTY(quint32 seconds READ seconds NOTIFY secondsChanged);
48+
Q_PROPERTY(quint32 seconds READ seconds NOTIFY dateChanged);
2749
QML_ELEMENT;
2850

2951
public:
@@ -43,30 +65,27 @@ class SystemClock: public QObject {
4365
[[nodiscard]] SystemClock::Enum precision() const;
4466
void setPrecision(SystemClock::Enum precision);
4567

68+
[[nodiscard]] QDateTime date() const { return this->currentTime; }
69+
[[nodiscard]] quint32 hours() const { return this->currentTime.time().hour(); }
70+
[[nodiscard]] quint32 minutes() const { return this->currentTime.time().minute(); }
71+
[[nodiscard]] quint32 seconds() const { return this->currentTime.time().second(); }
72+
4673
signals:
4774
void enabledChanged();
4875
void precisionChanged();
49-
void hoursChanged();
50-
void minutesChanged();
51-
void secondsChanged();
76+
void dateChanged();
5277

5378
private slots:
5479
void onTimeout();
5580

5681
private:
5782
bool mEnabled = true;
5883
SystemClock::Enum mPrecision = SystemClock::Seconds;
59-
quint32 mHours = 0;
60-
quint32 mMinutes = 0;
61-
quint32 mSeconds = 0;
6284
QTimer timer;
85+
QDateTime currentTime;
6386
QDateTime targetTime;
6487

6588
void update();
6689
void setTime(const QDateTime& targetTime);
6790
void schedule(const QDateTime& targetTime);
68-
69-
DECLARE_PRIVATE_MEMBER(SystemClock, hours, setHours, mHours, hoursChanged);
70-
DECLARE_PRIVATE_MEMBER(SystemClock, minutes, setMinutes, mMinutes, minutesChanged);
71-
DECLARE_PRIVATE_MEMBER(SystemClock, seconds, setSeconds, mSeconds, secondsChanged);
7291
};

0 commit comments

Comments
 (0)