QGIS API Documentation 3.99.0-Master (d270888f95f)
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 <QString>
32#include <QVariant>
33
34using namespace Qt::StringLiterals;
35
36#ifdef SIP_RUN
37% ModuleHeaderCode
38#include "qgsunittypes.h"
39% End
40#endif
41class QString;
42
48
49class CORE_EXPORT QgsInterval
50{
51 public:
52
53 // YEAR const value taken from postgres query
54 // SELECT EXTRACT(EPOCH FROM interval '1 year')
56 static const int YEARS = 31557600;
58 static const int MONTHS = 60 * 60 * 24 * 30;
60 static const int WEEKS = 60 * 60 * 24 * 7;
62 static const int DAY = 60 * 60 * 24;
64 static const int HOUR = 60 * 60;
66 static const int MINUTE = 60;
67
71 QgsInterval() = default;
72
77 QgsInterval( double seconds );
78
83 QgsInterval( std::chrono::milliseconds milliseconds ) SIP_SKIP;
84
88 QgsInterval( double duration, Qgis::TemporalUnit unit );
89
99 QgsInterval( double years, double months, double weeks, double days, double hours, double minutes, double seconds );
100
101#ifdef SIP_RUN
102 SIP_PYOBJECT __repr__();
103 % MethodCode
104 QString str;
105 if ( ! sipCpp->isValid() )
106 str = u"<QgsInterval: invalid>"_s;
107 else if ( sipCpp->originalUnit() != Qgis::TemporalUnit::Unknown )
108 str = u"<QgsInterval: %1 %2>"_s.arg( sipCpp->originalDuration() ).arg( QgsUnitTypes::toString( sipCpp->originalUnit() ) );
109 else
110 str = u"<QgsInterval: %1 seconds>"_s.arg( sipCpp->seconds() );
111 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
112 % End
113#endif
114
124 double years() const;
125
137 void setYears( double years );
138
149 double months() const;
150
162 void setMonths( double months );
163
173 double weeks() const;
174
186 void setWeeks( double weeks );
187
197 double days() const;
198
210 void setDays( double days );
211
221 double hours() const;
222
234 void setHours( double hours );
235
245 double minutes() const;
246
258 void setMinutes( double minutes );
259
264 double seconds() const { return mSeconds; }
265
277 void setSeconds( double seconds );
278
283 bool isValid() const { return mValid; }
284
290 void setValid( bool valid ) { mValid = valid; }
291
308 double originalDuration() const { return mOriginalDuration; }
309
323 Qgis::TemporalUnit originalUnit() const { return mOriginalUnit; }
324
325 bool operator==( QgsInterval other ) const
326 {
327 if ( !mValid && !other.mValid )
328 return true;
329 else if ( mValid && other.mValid && ( mOriginalUnit != Qgis::TemporalUnit::Unknown || other.mOriginalUnit != Qgis::TemporalUnit::Unknown ) )
330 return mOriginalUnit == other.mOriginalUnit && mOriginalDuration == other.mOriginalDuration;
331 else if ( mValid && other.mValid )
332 return qgsDoubleNear( mSeconds, other.mSeconds );
333 else
334 return false;
335 }
336
337 bool operator!=( QgsInterval other ) const
338 {
339 return !( *this == other );
340 }
341
347 static QgsInterval fromString( const QString &string );
348
350 operator QVariant() const
351 {
352 return QVariant::fromValue( *this );
353 }
354
355 private:
356
358 double mSeconds = 0.0;
359
361 bool mValid = false;
362
364 double mOriginalDuration = 0.0;
365
368};
369
371
372#ifndef SIP_RUN
373
374#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
375
382QgsInterval CORE_EXPORT operator-( const QDateTime &datetime1, const QDateTime &datetime2 );
383
384#endif
385
392QgsInterval CORE_EXPORT operator-( QDate date1, QDate date2 );
393
400QgsInterval CORE_EXPORT operator-( QTime time1, QTime time2 );
401
408QDateTime CORE_EXPORT operator+( const QDateTime &start, const QgsInterval &interval );
409
411QDebug CORE_EXPORT operator<<( QDebug dbg, const QgsInterval &interval );
412
413#endif
414
415#endif // QGSINTERVAL_H
TemporalUnit
Temporal units.
Definition qgis.h:5231
@ Unknown
Unknown time unit.
Definition qgis.h:5243
A representation of the interval between two datetime values.
Definition qgsinterval.h:50
static const int MINUTE
Seconds per minute.
Definition qgsinterval.h:66
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:58
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:64
static const int WEEKS
Seconds per week.
Definition qgsinterval.h:60
static const int DAY
Seconds per day.
Definition qgsinterval.h:62
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:56
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:6900
#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.