QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
32class QString;
33
41class CORE_EXPORT QgsInterval
42{
43 public:
44
45 // YEAR const value taken from postgres query
46 // SELECT EXTRACT(EPOCH FROM interval '1 year')
48 static const int YEARS = 31557600;
50 static const int MONTHS = 60 * 60 * 24 * 30;
52 static const int WEEKS = 60 * 60 * 24 * 7;
54 static const int DAY = 60 * 60 * 24;
56 static const int HOUR = 60 * 60;
58 static const int MINUTE = 60;
59
63 QgsInterval() = default;
64
69 QgsInterval( double seconds );
70
75 QgsInterval( std::chrono::milliseconds milliseconds ) SIP_SKIP;
76
80 QgsInterval( double duration, Qgis::TemporalUnit unit );
81
91 QgsInterval( double years, double months, double weeks, double days, double hours, double minutes, double seconds );
92
102 double years() const;
103
115 void setYears( double years );
116
127 double months() const;
128
140 void setMonths( double months );
141
151 double weeks() const;
152
164 void setWeeks( double weeks );
165
175 double days() const;
176
188 void setDays( double days );
189
199 double hours() const;
200
212 void setHours( double hours );
213
223 double minutes() const;
224
236 void setMinutes( double minutes );
237
242 double seconds() const { return mSeconds; }
243
255 void setSeconds( double seconds );
256
261 bool isValid() const { return mValid; }
262
268 void setValid( bool valid ) { mValid = valid; }
269
286 double originalDuration() const { return mOriginalDuration; }
287
301 Qgis::TemporalUnit originalUnit() const { return mOriginalUnit; }
302
303 bool operator==( QgsInterval other ) const
304 {
305 if ( !mValid && !other.mValid )
306 return true;
307 else if ( mValid && other.mValid && ( mOriginalUnit != Qgis::TemporalUnit::Unknown || other.mOriginalUnit != Qgis::TemporalUnit::Unknown ) )
308 return mOriginalUnit == other.mOriginalUnit && mOriginalDuration == other.mOriginalDuration;
309 else if ( mValid && other.mValid )
310 return qgsDoubleNear( mSeconds, other.mSeconds );
311 else
312 return false;
313 }
314
315 bool operator!=( QgsInterval other ) const
316 {
317 return !( *this == other );
318 }
319
325 static QgsInterval fromString( const QString &string );
326
328 operator QVariant() const
329 {
330 return QVariant::fromValue( *this );
331 }
332
333 private:
334
336 double mSeconds = 0.0;
337
339 bool mValid = false;
340
342 double mOriginalDuration = 0.0;
343
345 Qgis::TemporalUnit mOriginalUnit = Qgis::TemporalUnit::Unknown;
346};
347
349
350#ifndef SIP_RUN
351
352#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
353
361QgsInterval CORE_EXPORT operator-( const QDateTime &datetime1, const QDateTime &datetime2 );
362
363#endif
364
372QgsInterval CORE_EXPORT operator-( QDate date1, QDate date2 );
373
381QgsInterval CORE_EXPORT operator-( QTime time1, QTime time2 );
382
390QDateTime CORE_EXPORT operator+( const QDateTime &start, const QgsInterval &interval );
391
393QDebug CORE_EXPORT operator<<( QDebug dbg, const QgsInterval &interval );
394
395#endif
396
397#endif // QGSINTERVAL_H
TemporalUnit
Temporal units.
Definition: qgis.h:3152
A representation of the interval between two datetime values.
Definition: qgsinterval.h:42
double originalDuration() const
Returns the original interval duration.
Definition: qgsinterval.h:286
bool isValid() const
Returns true if the interval is valid.
Definition: qgsinterval.h:261
void setValid(bool valid)
Sets whether the interval is valid.
Definition: qgsinterval.h:268
QgsInterval()=default
Default constructor for QgsInterval.
bool operator==(QgsInterval other) const
Definition: qgsinterval.h:303
double seconds() const
Returns the interval duration in seconds.
Definition: qgsinterval.h:242
Qgis::TemporalUnit originalUnit() const
Returns the original interval temporal unit.
Definition: qgsinterval.h:301
bool operator!=(QgsInterval other) const
Definition: qgsinterval.h:315
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:3509
#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.