QGIS API Documentation 3.29.0-Master (19d7edcfed)
qgsinterval.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsinterval.h
3 -------------
4 Date : May 2016
5 Copyright : (C) 2016 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSINTERVAL_H
17#define QGSINTERVAL_H
18
19/***************************************************************************
20 * This class is considered CRITICAL and any change MUST be accompanied with
21 * full unit tests in test_qgsinterval.py.
22 * See details in QEP #17
23 ****************************************************************************/
24
25#include <QVariant>
26#include <chrono>
27
28#include "qgis_sip.h"
29#include "qgis_core.h"
30#include "qgsunittypes.h"
31#include "qgis.h"
32
33class QString;
34
42class CORE_EXPORT QgsInterval
43{
44 public:
45
46 // YEAR const value taken from postgres query
47 // SELECT EXTRACT(EPOCH FROM interval '1 year')
49 static const int YEARS = 31557600;
51 static const int MONTHS = 60 * 60 * 24 * 30;
53 static const int WEEKS = 60 * 60 * 24 * 7;
55 static const int DAY = 60 * 60 * 24;
57 static const int HOUR = 60 * 60;
59 static const int MINUTE = 60;
60
64 QgsInterval() = default;
65
70 QgsInterval( double seconds );
71
76 QgsInterval( std::chrono::milliseconds milliseconds ) SIP_SKIP;
77
81 QgsInterval( double duration, QgsUnitTypes::TemporalUnit unit );
82
92 QgsInterval( double years, double months, double weeks, double days, double hours, double minutes, double seconds );
93
103 double years() const;
104
116 void setYears( double years );
117
128 double months() const;
129
141 void setMonths( double months );
142
152 double weeks() const;
153
165 void setWeeks( double weeks );
166
176 double days() const;
177
189 void setDays( double days );
190
200 double hours() const;
201
213 void setHours( double hours );
214
224 double minutes() const;
225
237 void setMinutes( double minutes );
238
243 double seconds() const { return mSeconds; }
244
256 void setSeconds( double seconds );
257
262 bool isValid() const { return mValid; }
263
269 void setValid( bool valid ) { mValid = valid; }
270
287 double originalDuration() const { return mOriginalDuration; }
288
302 QgsUnitTypes::TemporalUnit originalUnit() const { return mOriginalUnit; }
303
304 bool operator==( QgsInterval other ) const
305 {
306 if ( !mValid && !other.mValid )
307 return true;
308 else if ( mValid && other.mValid && ( mOriginalUnit != QgsUnitTypes::TemporalUnknownUnit || other.mOriginalUnit != QgsUnitTypes::TemporalUnknownUnit ) )
309 return mOriginalUnit == other.mOriginalUnit && mOriginalDuration == other.mOriginalDuration;
310 else if ( mValid && other.mValid )
311 return qgsDoubleNear( mSeconds, other.mSeconds );
312 else
313 return false;
314 }
315
316 bool operator!=( QgsInterval other ) const
317 {
318 return !( *this == other );
319 }
320
326 static QgsInterval fromString( const QString &string );
327
329 operator QVariant() const
330 {
331 return QVariant::fromValue( *this );
332 }
333
334 private:
335
337 double mSeconds = 0.0;
338
340 bool mValid = false;
341
343 double mOriginalDuration = 0.0;
344
347};
348
350
351#ifndef SIP_RUN
352
353#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
354
362QgsInterval CORE_EXPORT operator-( const QDateTime &datetime1, const QDateTime &datetime2 );
363
364#endif
365
373QgsInterval CORE_EXPORT operator-( QDate date1, QDate date2 );
374
382QgsInterval CORE_EXPORT operator-( QTime time1, QTime time2 );
383
391QDateTime CORE_EXPORT operator+( const QDateTime &start, const QgsInterval &interval );
392
394QDebug CORE_EXPORT operator<<( QDebug dbg, const QgsInterval &interval );
395
396#endif
397
398#endif // QGSINTERVAL_H
A representation of the interval between two datetime values.
Definition: qgsinterval.h:43
double originalDuration() const
Returns the original interval duration.
Definition: qgsinterval.h:287
bool isValid() const
Returns true if the interval is valid.
Definition: qgsinterval.h:262
void setValid(bool valid)
Sets whether the interval is valid.
Definition: qgsinterval.h:269
QgsInterval()=default
Default constructor for QgsInterval.
bool operator==(QgsInterval other) const
Definition: qgsinterval.h:304
double seconds() const
Returns the interval duration in seconds.
Definition: qgsinterval.h:243
bool operator!=(QgsInterval other) const
Definition: qgsinterval.h:316
QgsUnitTypes::TemporalUnit originalUnit() const
Returns the original interval temporal unit.
Definition: qgsinterval.h:302
TemporalUnit
Temporal units.
Definition: qgsunittypes.h:150
@ TemporalUnknownUnit
Unknown time unit.
Definition: qgsunittypes.h:162
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:3077
#define SIP_SKIP
Definition: qgis_sip.h:126
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
QDateTime CORE_EXPORT operator+(const QDateTime &start, const QgsInterval &interval)
Adds an interval to a datetime.
QgsInterval CORE_EXPORT operator-(QDate date1, QDate date2)
Returns the interval between two dates.
QDebug CORE_EXPORT operator<<(QDebug dbg, const QgsInterval &interval)
Debug string representation of interval.