swift
timestampbased.h
Go to the documentation of this file.
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 #ifndef SWIFT_MISC_TIMESTAMPBASED_H
7 #define SWIFT_MISC_TIMESTAMPBASED_H
8 
9 #include <QDateTime>
10 #include <QString>
11 #include <QVariant>
12 #include <QtGlobal>
13 
14 #include "misc/propertyindexref.h"
15 #include "misc/swiftmiscexport.h"
16 
17 namespace swift::misc
18 {
21  {
22  public:
25  {
26  IndexUtcTimestamp = CPropertyIndexRef::GlobalIndexITimestampBased,
27  IndexUtcTimestampFormattedYmdhms,
28  IndexUtcTimestampFormattedYmdhmsz,
29  IndexUtcTimestampFormattedMdhms,
30  IndexUtcTimestampFormattedMdhmsz,
31  IndexUtcTimestampFormattedDhms,
32  IndexUtcTimestampFormattedHms,
33  IndexUtcTimestampFormattedHm,
34  IndexMSecsSinceEpoch // keep this as last item
35  };
36 
38  QDateTime getUtcTimestamp() const;
39 
41  qint64 getMSecsSinceEpoch() const { return m_timestampMSecsSinceEpoch; }
42 
44  qint64 getTimeDifferenceMs(qint64 compareTime) const { return compareTime - this->getMSecsSinceEpoch(); }
45 
47  qint64 getTimeDifferenceAbsMs(qint64 compareTime) const
48  {
49  return qAbs(compareTime - this->getMSecsSinceEpoch());
50  }
51 
53  qint64 getTimeDifferenceToNowMs() const
54  {
55  return this->getTimeDifferenceMs(QDateTime::currentMSecsSinceEpoch());
56  }
57 
59  qint64 getTimeDifferenceMs(const ITimestampBased &compare) const
60  {
61  return compare.getMSecsSinceEpoch() - this->getMSecsSinceEpoch();
62  }
63 
65  qint64 getAbsTimeDifferenceMs(qint64 compareTime) const { return qAbs(this->getTimeDifferenceMs(compareTime)); }
66 
68  qint64 getAbsTimeDifferenceMs(const ITimestampBased &compare) const
69  {
70  return qAbs(this->getTimeDifferenceMs(compare));
71  }
72 
74  void setTimestampToNull();
75 
77  void setMSecsSinceEpoch(qint64 mSecsSinceEpoch) { m_timestampMSecsSinceEpoch = mSecsSinceEpoch; }
78 
80  void setByYearMonthDayHourMinute(const QString &yyyyMMddhhmmss);
81 
83  void setUtcTimestamp(const QDateTime &timestamp);
84 
86  bool isNewerThan(const ITimestampBased &otherTimestampObj) const;
87 
89  bool isNewerThan(qint64 mSecsSinceEpoch) const;
90 
92  bool isOlderThan(const ITimestampBased &otherTimestampObj) const;
93 
95  bool isOlderThan(qint64 mSecsSinceEpoch) const;
96 
98  bool isOlderThanNowMinusOffset(int offsetMs) const;
99 
101  bool isSame(const ITimestampBased &otherTimestampObj) const;
102 
104  qint64 msecsTo(const ITimestampBased &otherTimestampObj) const;
105 
107  qint64 absMsecsTo(const ITimestampBased &otherTimestampObj) const;
108 
110  qint64 msecsToNow() const;
111 
113  void setCurrentUtcTime();
114 
116  void addMsecs(qint64 ms);
117 
119  QString getFormattedUtcTimestampHms() const;
120 
122  QString getFormattedUtcTimestampHmsz() const;
123 
125  QString getFormattedUtcTimestampHm() const;
126 
128  QString getFormattedUtcTimestampDhms() const;
129 
131  QString getFormattedUtcTimestampMdhms() const;
132 
134  QString getFormattedUtcTimestampMdhmsz() const;
135 
137  QString getFormattedUtcTimestampYmdhms() const;
138 
140  QString getFormattedUtcTimestampYmdhmsz() const;
141 
143  bool hasValidTimestamp() const;
144 
146  static bool isAnyTimestampIndex(int index);
147 
149  static bool canHandleIndex(CPropertyIndexRef index);
150 
151  protected:
153  ITimestampBased();
154 
156  ITimestampBased(qint64 msSincePoch);
157 
159  ITimestampBased(const QDateTime &timestamp);
160 
162  QVariant propertyByIndex(CPropertyIndexRef index) const;
163 
165  void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant);
166 
168  int comparePropertyByIndex(CPropertyIndexRef index, const ITimestampBased &compareValue) const;
169 
171  void updateMissingParts(const ITimestampBased &other);
172 
173  qint64 m_timestampMSecsSinceEpoch = -1;
174  };
175 
178  {
179  public:
182  {
183  IndexOffsetMs = static_cast<int>(CPropertyIndexRef::GlobalIndexITimestampBased) +
184  ITimestampBased::IndexMSecsSinceEpoch + 1,
185  IndexAdjustedMsWithOffset,
186  IndexOffsetWithUnit // keep this as last item
187  };
188 
190  void setTimeOffsetMs(qint64 offset) { m_timeOffsetMs = offset; }
191 
193  qint64 getTimeOffsetMs() const { return m_timeOffsetMs; }
194 
196  bool hasNonZeroOffsetTime() const;
197 
199  void addMsecsToOffsetTime(qint64 msToAdd);
200 
202  QString getTimeOffsetWithUnit() const;
203 
205  qint64 getAdjustedMSecsSinceEpoch() const { return this->getMSecsSinceEpoch() + this->getTimeOffsetMs(); }
206 
208  void setAdjustedMSecsSinceEpoch(qint64 adjustedTimeMs)
209  {
210  this->setMSecsSinceEpoch(adjustedTimeMs - this->getTimeOffsetMs());
211  }
212 
214  qint64 getAdjustedTimeDifferenceMs(qint64 compareTime) const
215  {
216  return this->getAdjustedMSecsSinceEpoch() - compareTime;
217  }
218 
220  QString getTimestampAndOffset(bool formatted) const;
221 
223  QString getFormattedTimestampAndOffset(bool includeRawTimestamp) const;
224 
226  bool isNewerThanAdjusted(const ITimestampWithOffsetBased &otherTimestampObj) const;
227 
229  bool isNewerThanAdjusted(qint64 mSecsSinceEpoch) const;
230 
232  bool isOlderThanAdjusted(const ITimestampWithOffsetBased &otherTimestampObj) const;
233 
235  bool isOlderThanAdjusted(qint64 mSecsSinceEpoch) const;
236 
237  protected:
239  static bool canHandleIndex(CPropertyIndexRef index);
240 
243 
245  ITimestampWithOffsetBased(qint64 msSincePoch) : ITimestampBased(msSincePoch) {}
246 
248  ITimestampWithOffsetBased(const QDateTime &timestamp) : ITimestampBased(timestamp) {}
249 
251  QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const;
252 
254  void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant);
255 
257  int comparePropertyByIndex(CPropertyIndexRef index, const ITimestampWithOffsetBased &compareValue) const;
258 
259  qint64 m_timeOffsetMs = 0;
260  };
261 } // namespace swift::misc
262 
263 #endif // SWIFT_MISC_TIMESTAMPBASED_H
Non-owning reference to a CPropertyIndex with a subset of its features.
Entity with timestamp.
qint64 getMSecsSinceEpoch() const
Timestamp as ms value.
qint64 getTimeDifferenceMs(qint64 compareTime) const
Time difference in ms.
qint64 getAbsTimeDifferenceMs(const ITimestampBased &compare) const
Time difference in ms.
ColumnIndex
Properties by index.
qint64 getTimeDifferenceToNowMs() const
Time difference to now.
qint64 getTimeDifferenceMs(const ITimestampBased &compare) const
Time difference in ms.
qint64 getTimeDifferenceAbsMs(qint64 compareTime) const
Time difference in ms.
qint64 getAbsTimeDifferenceMs(qint64 compareTime) const
Time difference in ms.
void setMSecsSinceEpoch(qint64 mSecsSinceEpoch)
Timestamp as ms value.
qint64 getAdjustedMSecsSinceEpoch() const
Timestamp with offset added for interpolation.
ITimestampWithOffsetBased(const QDateTime &timestamp)
Constructor.
void setAdjustedMSecsSinceEpoch(qint64 adjustedTimeMs)
Set timestamp with offset added for interpolation.
qint64 getTimeOffsetMs() const
Milliseconds to add to timestamp for interpolation.
qint64 getAdjustedTimeDifferenceMs(qint64 compareTime) const
Time difference in ms (this -> compare)
ITimestampWithOffsetBased(qint64 msSincePoch)
Constructor.
void setTimeOffsetMs(qint64 offset)
Milliseconds to add to timestamp for interpolation.
Free functions in swift::misc.
#define SWIFT_MISC_EXPORT
Export a class or function from the library.