-
Notifications
You must be signed in to change notification settings - Fork 11
/
clocktime.cpp
215 lines (179 loc) · 4.93 KB
/
clocktime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
** Copyright (C) 2023 Victron Energy B.V.
** See LICENSE.txt for license information.
*/
#include "clocktime.h"
#if !defined(VENUS_WEBASSEMBLY_BUILD)
#include <QTimeZone>
#endif
using namespace Victron::VenusOS;
ClockTime::ClockTime(QObject *parent)
: QObject(parent)
{
}
ClockTime* ClockTime::create(QQmlEngine *engine, QJSEngine *)
{
static ClockTime* clockTime = new ClockTime(nullptr);
return clockTime;
}
QDateTime ClockTime::dateTime() const
{
return m_dateTime;
}
void ClockTime::setDateTime(const QDateTime &dt)
{
if (m_dateTime != dt || m_dateTime.timeZoneAbbreviation() != dt.timeZoneAbbreviation()) {
const QDateTime old = m_dateTime;
m_dateTime = dt;
emit dateTimeChanged();
emit clockTimeChanged();
if (old.date().year() != dt.date().year()) emit yearChanged();
if (old.date().month() != dt.date().month()) emit monthChanged();
if (old.date().day() != dt.date().day()) emit dayChanged();
if (old.time().hour() != dt.time().hour()) emit hourChanged();
if (old.time().minute() != dt.time().minute()) emit minuteChanged();
if (old.time().second() != dt.time().second()) emit secondChanged();
if (old.time().msec() != dt.time().msec()) emit msecChanged();
if (old.date().year() != dt.date().year()
|| old.date().month() != dt.date().month()
|| old.date().day() != dt.date().day()) emit currentDateChanged();
if (old.time().hour() != dt.time().hour()
|| old.time().minute() != dt.time().minute()) emit currentTimeChanged();
emit currentDateTimeUtcChanged();
}
}
int ClockTime::year() const
{
return m_dateTime.date().year();
}
int ClockTime::month() const
{
return m_dateTime.date().month();
}
int ClockTime::day() const
{
return m_dateTime.date().day();
}
int ClockTime::hour() const
{
return m_dateTime.time().hour();
}
int ClockTime::minute() const
{
return m_dateTime.time().minute();
}
int ClockTime::second() const
{
return m_dateTime.time().second();
}
int ClockTime::msec() const
{
return m_dateTime.time().msec();
}
qint64 ClockTime::clockTime() const
{
return m_dateTime.toSecsSinceEpoch();
}
void ClockTime::setClockTime(qint64 secondsSinceEpoch)
{
if (clockTime() == secondsSinceEpoch) {
return;
}
updateTime(secondsSinceEpoch);
// Wait until just after the next clock minute, then update the time properties.
const int secsUntilNextMin = 60 - m_dateTime.time().second();
scheduleNextTimeCheck((secsUntilNextMin + 1) * 1000);
}
QString ClockTime::systemTimeZone() const
{
return m_systemTimeZone;
}
void ClockTime::setSystemTimeZone(const QString &tz)
{
if (m_systemTimeZone != tz) {
m_systemTimeZone = tz;
updateTime(clockTime());
emit systemTimeZoneChanged();
}
}
QString ClockTime::currentDate() const
{
return m_dateTime.toString("yyyy-MM-dd");
}
QString ClockTime::currentTime() const
{
return m_dateTime.toString("hh:mm");
}
QString ClockTime::currentDateTimeUtc() const
{
return m_dateTime.toUTC().toString("yyyy-MM-dd hh:mm");
}
QString ClockTime::formatTime(int hour, int minute) const
{
QTime t(hour, minute);
return t.toString("hh:mm");
}
QString ClockTime::formatDeltaDate(qint64 secondsDelta, const QString &format) const
{
return m_dateTime.addSecs(secondsDelta).toString(format);
}
qint64 ClockTime::otherClockTime(int year, int month, int day, int hour, int minute) const
{
// assume that the specified date/time is in our current clock time's timezone.
QDateTime other = m_dateTime;
QDate d = other.date();
d.setDate(year, month, day);
other.setDate(d);
QTime t = other.time();
t.setHMS(hour, minute, t.second(), t.msec());
other.setTime(t);
return other.toSecsSinceEpoch();
}
bool ClockTime::isDateValid(int year, int month, int day) const
{
static const QCalendar calendar;
return calendar.isDateValid(year, month, day);
}
int ClockTime::daysInMonth(int month, int year) const
{
static const QCalendar calendar;
return calendar.daysInMonth(month, year);
}
void ClockTime::timerEvent(QTimerEvent *)
{
qint64 secsSinceEpoch = clockTime();
if (secsSinceEpoch <= 0) {
return;
}
secsSinceEpoch += (m_timerInterval / 1000);
updateTime(secsSinceEpoch);
scheduleNextTimeCheck(60 * 1000);
}
void ClockTime::updateTime(qint64 secsSinceEpoch)
{
if (secsSinceEpoch <= 0) {
return;
}
const QDateTime currentUtc = QDateTime::fromSecsSinceEpoch(secsSinceEpoch, Qt::UTC);
if (m_systemTimeZone.compare(QStringLiteral("/UTC"), Qt::CaseInsensitive) == 0
|| m_systemTimeZone.compare(QStringLiteral("UTC"), Qt::CaseInsensitive) == 0) {
setDateTime(currentUtc);
} else {
#if defined(VENUS_WEBASSEMBLY_BUILD)
// Cannot use QTimeZone in Emscripten builds.
// We thus cannot convert to the specified timezone offset.
// The local time will be the local time of the browser.
setDateTime(currentUtc.toLocalTime());
#else
setDateTime(currentUtc.toTimeZone(QTimeZone(m_systemTimeZone.toUtf8())));
#endif
}
}
void ClockTime::scheduleNextTimeCheck(int interval)
{
if (m_timerId > 0) {
killTimer(m_timerId);
}
m_timerInterval = interval;
m_timerId = startTimer(interval);
}