QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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// clang-format off
38% ModuleHeaderCode
39#include "qgsunittypes.h"
40% End
41// clang-format on
42#endif
43 class QString;
44
50
51class CORE_EXPORT QgsInterval
52{
53 public:
54 // YEAR const value taken from postgres query
55 // SELECT EXTRACT(EPOCH FROM interval '1 year')
57 static const int YEARS = 31557600;
59 static const int MONTHS = 60 * 60 * 24 * 30;
61 static const int WEEKS = 60 * 60 * 24 * 7;
63 static const int DAY = 60 * 60 * 24;
65 static const int HOUR = 60 * 60;
67 static const int MINUTE = 60;
68
72 QgsInterval() = default;
73
78 QgsInterval( double seconds );
79
84 QgsInterval( std::chrono::milliseconds milliseconds ) SIP_SKIP;
85
89 QgsInterval( double duration, Qgis::TemporalUnit unit );
90
100 QgsInterval( double years, double months, double weeks, double days, double hours, double minutes, double seconds );
101
102#ifdef SIP_RUN
103 // clang-format off
104 SIP_PYOBJECT __repr__();
105 % MethodCode
106 QString str;
107 if ( ! sipCpp->isValid() )
108 str = u"<QgsInterval: invalid>"_s;
109 else if ( sipCpp->originalUnit() != Qgis::TemporalUnit::Unknown )
110 str = u"<QgsInterval: %1 %2>"_s.arg( sipCpp->originalDuration() ).arg( QgsUnitTypes::toString( sipCpp->originalUnit() ) );
111 else
112 str = u"<QgsInterval: %1 seconds>"_s.arg( sipCpp->seconds() );
113 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
114 % End
115// clang-format on
116#endif
117
127 double years() const;
128
140 void setYears( double years );
141
152 double months() const;
153
165 void setMonths( double months );
166
176 double weeks() const;
177
189 void setWeeks( double weeks );
190
200 double days() const;
201
213 void setDays( double days );
214
224 double hours() const;
225
237 void setHours( double hours );
238
248 double minutes() const;
249
261 void setMinutes( double minutes );
262
267 double seconds() const { return mSeconds; }
268
280 void setSeconds( double seconds );
281
286 bool isValid() const { return mValid; }
287
293 void setValid( bool valid ) { mValid = valid; }
294
311 double originalDuration() const { return mOriginalDuration; }
312
326 Qgis::TemporalUnit originalUnit() const { return mOriginalUnit; }
327
328 bool operator==( QgsInterval other ) const
329 {
330 if ( !mValid && !other.mValid )
331 return true;
332 else if ( mValid && other.mValid && ( mOriginalUnit != Qgis::TemporalUnit::Unknown || other.mOriginalUnit != Qgis::TemporalUnit::Unknown ) )
333 return mOriginalUnit == other.mOriginalUnit && mOriginalDuration == other.mOriginalDuration;
334 else if ( mValid && other.mValid )
335 return qgsDoubleNear( mSeconds, other.mSeconds );
336 else
337 return false;
338 }
339
340 bool operator!=( QgsInterval other ) const { return !( *this == other ); }
341
347 static QgsInterval fromString( const QString &string );
348
350 operator QVariant() const { return QVariant::fromValue( *this ); }
351
352 private:
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
377QgsInterval CORE_EXPORT operator-( QDate date1, QDate date2 );
378
385QgsInterval CORE_EXPORT operator-( QTime time1, QTime time2 );
386
393QDateTime CORE_EXPORT operator+( const QDateTime &start, const QgsInterval &interval );
394
396QDebug CORE_EXPORT operator<<( QDebug dbg, const QgsInterval &interval );
397
398#endif
399
400#endif // QGSINTERVAL_H
TemporalUnit
Temporal units.
Definition qgis.h:5316
@ Unknown
Unknown time unit.
Definition qgis.h:5328
A representation of the interval between two datetime values.
Definition qgsinterval.h:52
static const int MINUTE
Seconds per minute.
Definition qgsinterval.h:67
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:59
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:65
static const int WEEKS
Seconds per week.
Definition qgsinterval.h:61
static const int DAY
Seconds per day.
Definition qgsinterval.h:63
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:57
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:6975
#define SIP_SKIP
Definition qgis_sip.h:133
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.