swift
Loading...
Searching...
No Matches
timestampbased.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QByteArray>
7#include <QDateTime>
8
10#include "misc/stringutils.h"
11#include "misc/verify.h"
12
13namespace swift::misc
14{
16
18 : m_timestampMSecsSinceEpoch(timestamp.toMSecsSinceEpoch())
19 {}
20
29
31
33 {
34 // yyyy MM dd hh mm ss zzz
35 // 0123 45 67 89 01 23 456
36 // 1234 56 78 90 12 34 567
37
38 const QString s(removeDateTimeSeparators(yyyyMMddhhmmsszzz));
40 this->setUtcTimestamp(dt);
41 }
42
44 {
45 m_timestampMSecsSinceEpoch = timestamp.isValid() ? timestamp.toMSecsSinceEpoch() : -1;
46 }
47
48 bool ITimestampBased::isNewerThan(const ITimestampBased &otherTimestampObj) const
49 {
51 }
52
53 bool ITimestampBased::isNewerThan(qint64 mSecsSinceEpoch) const
54 {
55 return m_timestampMSecsSinceEpoch > mSecsSinceEpoch;
56 }
57
58 bool ITimestampBased::isOlderThan(const ITimestampBased &otherTimestampObj) const
59 {
61 }
62
63 bool ITimestampBased::isOlderThan(qint64 mSecsSinceEpoch) const
64 {
65 return m_timestampMSecsSinceEpoch < mSecsSinceEpoch;
66 }
67
69 {
70 if (offsetMs <= 0)
71 {
72 return false;
73 }
75 }
76
77 bool ITimestampBased::isSame(const ITimestampBased &otherTimestampObj) const
78 {
80 }
81
82 qint64 ITimestampBased::msecsTo(const ITimestampBased &otherTimestampObj) const
83 {
85 }
86
87 qint64 ITimestampBased::absMsecsTo(const ITimestampBased &otherTimestampObj) const
88 {
89 qint64 dt = this->msecsTo(otherTimestampObj);
90 return dt > 0 ? dt : dt * -1;
91 }
92
98
100
102
104 {
105 return this->hasValidTimestamp() ? this->getUtcTimestamp().toString("MM-dd hh:mm:ss") : "";
106 }
107
109 {
110 return this->hasValidTimestamp() ? this->getUtcTimestamp().toString("MM-dd hh:mm:ss.zzz") : "";
111 }
112
114 {
115 return this->hasValidTimestamp() ? this->getUtcTimestamp().toString("dd hh:mm:ss") : "";
116 }
117
119 {
120 return this->hasValidTimestamp() ? this->getUtcTimestamp().toString("hh:mm:ss") : "";
121 }
122
124 {
125 return this->hasValidTimestamp() ? this->getUtcTimestamp().toString("hh:mm:ss.zzz") : "";
126 }
127
129 {
130 return this->hasValidTimestamp() ? this->getUtcTimestamp().toString("hh::mm") : "";
131 }
132
134 {
135 return this->hasValidTimestamp() ? this->getUtcTimestamp().toString("yyyy-MM-dd HH:mm:ss") : "";
136 }
137
139 {
140 return this->hasValidTimestamp() ? this->getUtcTimestamp().toString("yyyy-MM-dd HH:mm:ss.zzz") : "";
141 }
142
144
146 {
147 return (index >= static_cast<int>(IndexUtcTimestamp)) && (index <= static_cast<int>(IndexMSecsSinceEpoch));
148 }
149
151 {
152 if (index.isEmpty())
153 {
154 return false;
155 }
156 const int i = index.frontCasted<int>();
157 return isAnyTimestampIndex(i);
158 }
159
161 {
162 if (!index.isEmpty())
163 {
164 const auto i = index.frontCasted<ColumnIndex>();
165 switch (i)
166 {
167 case IndexUtcTimestamp: return QVariant::fromValue(this->getUtcTimestamp());
168 case IndexMSecsSinceEpoch: return QVariant::fromValue(this->getMSecsSinceEpoch());
169 case IndexUtcTimestampFormattedDhms: return QVariant::fromValue(this->getFormattedUtcTimestampDhms());
170 case IndexUtcTimestampFormattedHm: return QVariant::fromValue(this->getFormattedUtcTimestampHm());
171 case IndexUtcTimestampFormattedHms: return QVariant::fromValue(this->getFormattedUtcTimestampHms());
172 case IndexUtcTimestampFormattedYmdhms: return QVariant::fromValue(this->getFormattedUtcTimestampYmdhms());
173 case IndexUtcTimestampFormattedYmdhmsz: return QVariant::fromValue(this->getFormattedUtcTimestampYmdhmsz());
174 case IndexUtcTimestampFormattedMdhms: return QVariant::fromValue(this->getFormattedUtcTimestampMdhms());
175 case IndexUtcTimestampFormattedMdhmsz: return QVariant::fromValue(this->getFormattedUtcTimestampMdhmsz());
176 default: break;
177 }
178 }
179 const QString m = QStringLiteral("Cannot handle index %1").arg(index.toQString());
180 SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable(m));
181 return QVariant::fromValue(m);
182 }
183
185 {
186 if (!index.isEmpty())
187 {
188 const auto i = index.frontCasted<ColumnIndex>();
189 switch (i)
190 {
191 case IndexUtcTimestamp: this->setUtcTimestamp(variant.toDateTime()); return;
192 case IndexMSecsSinceEpoch: this->setMSecsSinceEpoch(variant.toLongLong()); return;
193 case IndexUtcTimestampFormattedYmdhms:
194 case IndexUtcTimestampFormattedYmdhmsz:
195 case IndexUtcTimestampFormattedHm:
196 case IndexUtcTimestampFormattedHms:
197 case IndexUtcTimestampFormattedDhms:
198 {
199 const QDateTime dt = fromStringUtc(variant.toString());
201 }
202 return;
203 default: break;
204 }
205 }
206 const QString m = QStringLiteral("Cannot handle index %1").arg(index.toQString());
207 SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable(m));
208 }
209
211 {
212 Q_UNUSED(index);
214 }
215
223
225 {
226 static const QString ts("%1 (%2)");
227 if (!this->hasValidTimestamp())
228 {
229 return ts.arg("-", this->getTimeOffsetWithUnit());
230 }
231 if (formatted)
232 {
233 return ts.arg(this->getFormattedUtcTimestampHms(), this->getTimeOffsetWithUnit());
234 }
235 return ts.arg(m_timeOffsetMs).arg(this->getTimeOffsetWithUnit());
236 }
237
239 {
240 if (!includeRawTimestamp)
241 {
242 return this->getTimestampAndOffset(true);
243 }
244 static const QString ts("%1/%2 (%3)");
245 if (!this->hasValidTimestamp())
246 {
247 return ts.arg("-", "-", this->getTimeOffsetWithUnit());
248 }
249 return ts.arg(this->getFormattedUtcTimestampHmsz())
251 .arg(this->getTimeOffsetWithUnit());
252 }
253
255 {
256 return this->getAdjustedMSecsSinceEpoch() > otherTimestampObj.getAdjustedMSecsSinceEpoch();
257 }
258
259 bool ITimestampWithOffsetBased::isNewerThanAdjusted(qint64 mSecsSinceEpoch) const
260 {
261 return this->getAdjustedMSecsSinceEpoch() > mSecsSinceEpoch;
262 }
263
265 {
266 return this->getAdjustedMSecsSinceEpoch() < otherTimestampObj.getAdjustedMSecsSinceEpoch();
267 }
268
269 bool ITimestampWithOffsetBased::isOlderThanAdjusted(qint64 mSecsSinceEpoch) const
270 {
271 return this->getAdjustedMSecsSinceEpoch() < mSecsSinceEpoch;
272 }
273
275 {
277 {
278 return true;
279 }
280 if (index.isEmpty())
281 {
282 return false;
283 }
284 const int i = index.frontCasted<int>();
285 return (i >= static_cast<int>(IndexOffsetMs)) && (i <= static_cast<int>(IndexOffsetWithUnit));
286 }
287
289
291
293 {
294 return QStringLiteral("%1ms").arg(this->getTimeOffsetMs());
295 }
296
298 {
300 {
302 }
303 if (!index.isEmpty())
304 {
305 const auto i = index.frontCasted<ColumnIndex>();
306 switch (i)
307 {
308 case IndexOffsetMs:
309 {
311 }
312 case IndexAdjustedMsWithOffset:
313 {
315 }
316 case IndexOffsetWithUnit:
317 {
319 }
320 default: break;
321 }
322 }
323 const QString m = QStringLiteral("Cannot handle index %1").arg(index.toQString());
324 SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable(m));
325 return {};
326 }
327
329 {
331 {
333 return;
334 }
335 if (!index.isEmpty())
336 {
337 const auto i = index.frontCasted<ColumnIndex>();
338 switch (i)
339 {
340 case IndexOffsetMs:
341 {
342 m_timeOffsetMs = variant.value<qint64>();
343 return;
344 }
345 case IndexAdjustedMsWithOffset: return; // read only
346 case IndexOffsetWithUnit: return; // read only
347 default: break;
348 }
349 }
350 const QString m = QStringLiteral("Cannot handle index %1").arg(index.toQString());
351 SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable(m));
352 }
353
355 const ITimestampWithOffsetBased &compareValue) const
356 {
358 {
359 return ITimestampBased::comparePropertyByIndex(index, compareValue);
360 }
361 if (!index.isEmpty())
362 {
363 const auto i = index.frontCasted<ColumnIndex>();
364 switch (i)
365 {
366 case IndexOffsetWithUnit:
367 case IndexOffsetMs:
368 {
369 return Compare::compare(m_timeOffsetMs, compareValue.m_timeOffsetMs);
370 }
371 case IndexAdjustedMsWithOffset:
372 {
374 }
375 default: break;
376 }
377 }
378 const QString m = QStringLiteral("Cannot handle index %1").arg(index.toQString());
379 SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable(m));
380 return 0;
381 }
382} // namespace swift::misc
Non-owning reference to a CPropertyIndex with a subset of its features.
QString toQString(bool i18n=false) const
Cast as QString.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
qint64 getMSecsSinceEpoch() const
Timestamp as ms value.
bool isNewerThan(const ITimestampBased &otherTimestampObj) const
Is this newer than other?
bool isOlderThanNowMinusOffset(int offsetMs) const
Older than now-offset.
static bool isAnyTimestampIndex(int index)
Any of the timestamp indexes.
void updateMissingParts(const ITimestampBased &other)
Update missing parts.
void setTimestampToNull()
Set to null.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
int comparePropertyByIndex(CPropertyIndexRef index, const ITimestampBased &compareValue) const
Compare for index.
QString getFormattedUtcTimestampYmdhms() const
As yyyy MM dd HH mm ss.
ITimestampBased()=default
Constructor.
void setByYearMonthDayHourMinute(const QString &yyyyMMddhhmmss)
Set by value such as "20141003231045".
static bool canHandleIndex(CPropertyIndexRef index)
Can given index be handled.
QString getFormattedUtcTimestampHmsz() const
As hh:mm:ss.zzz.
qint64 m_timestampMSecsSinceEpoch
timestamp value
void setCurrentUtcTime()
Set the current time as timestamp.
QString getFormattedUtcTimestampHm() const
As hh:mm.
QDateTime getUtcTimestamp() const
Get timestamp.
bool isOlderThan(const ITimestampBased &otherTimestampObj) const
Is this older than other?
QString getFormattedUtcTimestampHms() const
As hh:mm:ss.
ColumnIndex
Properties by index.
qint64 msecsToNow() const
Milliseconds to now.
bool isSame(const ITimestampBased &otherTimestampObj) const
Same timestamp.
QString getFormattedUtcTimestampMdhms() const
As MM dd HH mm ss.
QString getFormattedUtcTimestampDhms() const
As dd HH mm ss.
void setUtcTimestamp(const QDateTime &timestamp)
Set timestamp.
bool hasValidTimestamp() const
Valid timestamp?
qint64 msecsTo(const ITimestampBased &otherTimestampObj) const
Time difference.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
QString getFormattedUtcTimestampYmdhmsz() const
As yyyy MM dd HH mm ss.zzz.
qint64 absMsecsTo(const ITimestampBased &otherTimestampObj) const
Time difference.
QString getFormattedUtcTimestampMdhmsz() const
As MM dd HH mm ss.zzz.
void setMSecsSinceEpoch(qint64 mSecsSinceEpoch)
Timestamp as ms value.
void addMsecs(qint64 ms)
Add the given number of milliseconds to the timestamp.
qint64 getAdjustedMSecsSinceEpoch() const
Timestamp with offset added for interpolation.
static bool canHandleIndex(CPropertyIndexRef index)
Can given index be handled.
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
QString getTimeOffsetWithUnit() const
Offset with unit.
qint64 getTimeOffsetMs() const
Milliseconds to add to timestamp for interpolation.
int comparePropertyByIndex(CPropertyIndexRef index, const ITimestampWithOffsetBased &compareValue) const
Compare for index.
bool hasNonZeroOffsetTime() const
Having a valid offset time.
ITimestampWithOffsetBased()=default
Constructor.
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
bool isNewerThanAdjusted(const ITimestampWithOffsetBased &otherTimestampObj) const
Is this newer than other?
QString getTimestampAndOffset(bool formatted) const
Timestamp and offset.
bool isOlderThanAdjusted(const ITimestampWithOffsetBased &otherTimestampObj) const
Is this older than other?
void addMsecsToOffsetTime(qint64 msToAdd)
Adds a value to offset time.
QString getFormattedTimestampAndOffset(bool includeRawTimestamp) const
Timestamp and offset.
int compare(T a, T b)
Compare arithmetic values.
Free functions in swift::misc.
SWIFT_MISC_EXPORT QString removeDateTimeSeparators(const QString &s)
Remove the typical separators such as "-", " ".
SWIFT_MISC_EXPORT QDateTime fromStringUtc(const QString &dateTimeString, const QString &format)
Same as QDateTime::fromString but QDateTime will be set to UTC.
SWIFT_MISC_EXPORT QDateTime parseDateTimeStringOptimized(const QString &dateTimeString)
Parse yyyyMMddHHmmsszzz strings optimized.
qint64 currentMSecsSinceEpoch()
QDateTime fromMSecsSinceEpoch(qint64 msecs)
bool isValid() const const
qint64 toMSecsSinceEpoch() const const
QString toString(QStringView format) const const
QString arg(Args &&... args) const const
QVariant fromValue(T &&value)
QDateTime toDateTime() const const
qlonglong toLongLong(bool *ok) const const
QString toString() const const
T value() const &const
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.
Definition verify.h:26