QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 <chrono>
26
27#include "qgis.h"
28#include "qgis_core.h"
29#include "qgis_sip.h"
30
31#include <QVariant>
32
33#ifdef SIP_RUN
34% ModuleHeaderCode
35#include "qgsunittypes.h"
36% End
37#endif
38class QString;
39
45
46class CORE_EXPORT QgsInterval
47{
48 public:
49
50 // YEAR const value taken from postgres query
51 // SELECT EXTRACT(EPOCH FROM interval '1 year')
53 static const int YEARS = 31557600;
55 static const int MONTHS = 60 * 60 * 24 * 30;
57 static const int WEEKS = 60 * 60 * 24 * 7;
59 static const int DAY = 60 * 60 * 24;
61 static const int HOUR = 60 * 60;
63 static const int MINUTE = 60;
64
68 QgsInterval() = default;
69
74 QgsInterval( double seconds );
75
80 QgsInterval( std::chrono::milliseconds milliseconds ) SIP_SKIP;
81
85 QgsInterval( double duration, Qgis::TemporalUnit unit );
86
96 QgsInterval( double years, double months, double weeks, double days, double hours, double minutes, double seconds );
97
98#ifdef SIP_RUN
99 SIP_PYOBJECT __repr__();
100 % MethodCode
101 QString str;
102 if ( ! sipCpp->isValid() )
103 str = QStringLiteral( "<QgsInterval: invalid>" );
104 else if ( sipCpp->originalUnit() != Qgis::TemporalUnit::Unknown )
105 str = QStringLiteral( "<QgsInterval: %1 %2>" ).arg( sipCpp->originalDuration() ).arg( QgsUnitTypes::toString( sipCpp->originalUnit() ) );
106 else
107 str = QStringLiteral( "<QgsInterval: %1 seconds>" ).arg( sipCpp->seconds() );
108 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
109 % End
110#endif
111
121 double years() const;
122
134 void setYears( double years );
135
146 double months() const;
147
159 void setMonths( double months );
160
170 double weeks() const;
171
183 void setWeeks( double weeks );
184
194 double days() const;
195
207 void setDays( double days );
208
218 double hours() const;
219
231 void setHours( double hours );
232
242 double minutes() const;
243
255 void setMinutes( double minutes );
256
261 double seconds() const { return mSeconds; }
262
274 void setSeconds( double seconds );
275
280 bool isValid() const { return mValid; }
281
287 void setValid( bool valid ) { mValid = valid; }
288
305 double originalDuration() const { return mOriginalDuration; }
306
320 Qgis::TemporalUnit originalUnit() const { return mOriginalUnit; }
321
322 bool operator==( QgsInterval other ) const
323 {
324 if ( !mValid && !other.mValid )
325 return true;
326 else if ( mValid && other.mValid && ( mOriginalUnit != Qgis::TemporalUnit::Unknown || other.mOriginalUnit != Qgis::TemporalUnit::Unknown ) )
327 return mOriginalUnit == other.mOriginalUnit && mOriginalDuration == other.mOriginalDuration;
328 else if ( mValid && other.mValid )
329 return qgsDoubleNear( mSeconds, other.mSeconds );
330 else
331 return false;
332 }
333
334 bool operator!=( QgsInterval other ) const
335 {
336 return !( *this == other );
337 }
338
344 static QgsInterval fromString( const QString &string );
345
347 operator QVariant() const
348 {
349 return QVariant::fromValue( *this );
350 }
351
352 private:
353
355 double mSeconds = 0.0;
356
358 bool mValid = false;
359
361 double mOriginalDuration = 0.0;
362
365};
366
368
369#ifndef SIP_RUN
370
371#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
372
379QgsInterval CORE_EXPORT operator-( const QDateTime &datetime1, const QDateTime &datetime2 );
380
381#endif
382
389QgsInterval CORE_EXPORT operator-( QDate date1, QDate date2 );
390
397QgsInterval CORE_EXPORT operator-( QTime time1, QTime time2 );
398
405QDateTime CORE_EXPORT operator+( const QDateTime &start, const QgsInterval &interval );
406
408QDebug CORE_EXPORT operator<<( QDebug dbg, const QgsInterval &interval );
409
410#endif
411
412#endif // QGSINTERVAL_H
TemporalUnit
Temporal units.
Definition qgis.h:5159
@ Unknown
Unknown time unit.
Definition qgis.h:5171
A representation of the interval between two datetime values.
Definition qgsinterval.h:47
static const int MINUTE
Seconds per minute.
Definition qgsinterval.h:63
double originalDuration() const
Returns the original interval duration.
bool isValid() const
Returns true if the interval is valid.
double days() const
Returns the interval duration in days.
void setMinutes(double minutes)
Sets the interval duration in minutes.
void setValid(bool valid)
Sets whether the interval is valid.
void setWeeks(double weeks)
Sets the interval duration in weeks.
QgsInterval()=default
Default constructor for QgsInterval.
double weeks() const
Returns the interval duration in weeks.
static const int MONTHS
Seconds per month, based on 30 day month.
Definition qgsinterval.h:55
double months() const
Returns the interval duration in months (based on a 30 day month).
bool operator==(QgsInterval other) const
double seconds() const
Returns the interval duration in seconds.
Qgis::TemporalUnit originalUnit() const
Returns the original interval temporal unit.
double years() const
Returns the interval duration in years (based on an average year length).
static const int HOUR
Seconds per hour.
Definition qgsinterval.h:61
static const int WEEKS
Seconds per week.
Definition qgsinterval.h:57
static const int DAY
Seconds per day.
Definition qgsinterval.h:59
double hours() const
Returns the interval duration in hours.
void setHours(double hours)
Sets the interval duration in hours.
bool operator!=(QgsInterval other) const
static const int YEARS
Seconds per year (average).
Definition qgsinterval.h:53
void setYears(double years)
Sets the interval duration in years.
void setDays(double days)
Sets the interval duration in days.
double minutes() const
Returns the interval duration in minutes.
void setMonths(double months)
Sets the interval duration in months.
static Q_INVOKABLE QString toString(Qgis::DistanceUnit unit)
Returns a translated string representing a distance unit.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6607
#define SIP_SKIP
Definition qgis_sip.h:134
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.