QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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 "qgis.h"
31
32#ifdef SIP_RUN
33% ModuleHeaderCode
34#include "qgsunittypes.h"
35% End
36#endif
37class QString;
38
45class CORE_EXPORT QgsInterval
46{
47 public:
48
49 // YEAR const value taken from postgres query
50 // SELECT EXTRACT(EPOCH FROM interval '1 year')
52 static const int YEARS = 31557600;
54 static const int MONTHS = 60 * 60 * 24 * 30;
56 static const int WEEKS = 60 * 60 * 24 * 7;
58 static const int DAY = 60 * 60 * 24;
60 static const int HOUR = 60 * 60;
62 static const int MINUTE = 60;
63
67 QgsInterval() = default;
68
73 QgsInterval( double seconds );
74
79 QgsInterval( std::chrono::milliseconds milliseconds ) SIP_SKIP;
80
84 QgsInterval( double duration, Qgis::TemporalUnit unit );
85
95 QgsInterval( double years, double months, double weeks, double days, double hours, double minutes, double seconds );
96
97#ifdef SIP_RUN
98 SIP_PYOBJECT __repr__();
99 % MethodCode
100 QString str;
101 if ( ! sipCpp->isValid() )
102 str = QStringLiteral( "<QgsInterval: invalid>" );
103 else if ( sipCpp->originalUnit() != Qgis::TemporalUnit::Unknown )
104 str = QStringLiteral( "<QgsInterval: %1 %2>" ).arg( sipCpp->originalDuration() ).arg( QgsUnitTypes::toString( sipCpp->originalUnit() ) );
105 else
106 str = QStringLiteral( "<QgsInterval: %1 seconds>" ).arg( sipCpp->seconds() );
107 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
108 % End
109#endif
110
120 double years() const;
121
133 void setYears( double years );
134
145 double months() const;
146
158 void setMonths( double months );
159
169 double weeks() const;
170
182 void setWeeks( double weeks );
183
193 double days() const;
194
206 void setDays( double days );
207
217 double hours() const;
218
230 void setHours( double hours );
231
241 double minutes() const;
242
254 void setMinutes( double minutes );
255
260 double seconds() const { return mSeconds; }
261
273 void setSeconds( double seconds );
274
279 bool isValid() const { return mValid; }
280
286 void setValid( bool valid ) { mValid = valid; }
287
304 double originalDuration() const { return mOriginalDuration; }
305
319 Qgis::TemporalUnit originalUnit() const { return mOriginalUnit; }
320
321 bool operator==( QgsInterval other ) const
322 {
323 if ( !mValid && !other.mValid )
324 return true;
325 else if ( mValid && other.mValid && ( mOriginalUnit != Qgis::TemporalUnit::Unknown || other.mOriginalUnit != Qgis::TemporalUnit::Unknown ) )
326 return mOriginalUnit == other.mOriginalUnit && mOriginalDuration == other.mOriginalDuration;
327 else if ( mValid && other.mValid )
328 return qgsDoubleNear( mSeconds, other.mSeconds );
329 else
330 return false;
331 }
332
333 bool operator!=( QgsInterval other ) const
334 {
335 return !( *this == other );
336 }
337
343 static QgsInterval fromString( const QString &string );
344
346 operator QVariant() const
347 {
348 return QVariant::fromValue( *this );
349 }
350
351 private:
352
354 double mSeconds = 0.0;
355
357 bool mValid = false;
358
360 double mOriginalDuration = 0.0;
361
364};
365
367
368#ifndef SIP_RUN
369
370#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
371
378QgsInterval CORE_EXPORT operator-( const QDateTime &datetime1, const QDateTime &datetime2 );
379
380#endif
381
388QgsInterval CORE_EXPORT operator-( QDate date1, QDate date2 );
389
396QgsInterval CORE_EXPORT operator-( QTime time1, QTime time2 );
397
404QDateTime CORE_EXPORT operator+( const QDateTime &start, const QgsInterval &interval );
405
407QDebug CORE_EXPORT operator<<( QDebug dbg, const QgsInterval &interval );
408
409#endif
410
411#endif // QGSINTERVAL_H
TemporalUnit
Temporal units.
Definition qgis.h:4823
@ Unknown
Unknown time unit.
A representation of the interval between two datetime values.
Definition qgsinterval.h:46
double originalDuration() const
Returns the original interval duration.
bool isValid() const
Returns true if the interval is valid.
void setValid(bool valid)
Sets whether the interval is valid.
QgsInterval()=default
Default constructor for QgsInterval.
bool operator==(QgsInterval other) const
double seconds() const
Returns the interval duration in seconds.
Qgis::TemporalUnit originalUnit() const
Returns the original interval temporal unit.
bool operator!=(QgsInterval other) const
static Q_INVOKABLE QString toString(Qgis::DistanceUnit unit)
Returns a translated string representing a distance unit.
#define str(x)
Definition qgis.cpp:39
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5958
#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.