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.
1330class 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
2951public:
@@ -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+
4673signals:
4774 void enabledChanged ();
4875 void precisionChanged ();
49- void hoursChanged ();
50- void minutesChanged ();
51- void secondsChanged ();
76+ void dateChanged ();
5277
5378private slots:
5479 void onTimeout ();
5580
5681private:
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