QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
27 #include "qgis_sip.h"
28 #include "qgis_core.h"
29 #include "qgsunittypes.h"
30 
31 class QString;
32 
40 class CORE_EXPORT QgsInterval
41 {
42  public:
43 
44  // YEAR const value taken from postgres query
45  // SELECT EXTRACT(EPOCH FROM interval '1 year')
47  static const int YEARS = 31557600;
49  static const int MONTHS = 60 * 60 * 24 * 30;
51  static const int WEEKS = 60 * 60 * 24 * 7;
53  static const int DAY = 60 * 60 * 24;
55  static const int HOUR = 60 * 60;
57  static const int MINUTE = 60;
58 
62  QgsInterval() = default;
63 
68  QgsInterval( double seconds );
69 
73  QgsInterval( double duration, QgsUnitTypes::TemporalUnit unit );
74 
84  QgsInterval( double years, double months, double weeks, double days, double hours, double minutes, double seconds );
85 
90  double years() const { return mSeconds / YEARS; }
91 
97  void setYears( double years ) { mSeconds = years * YEARS; mValid = true; }
98 
103  double months() const { return mSeconds / MONTHS; }
104 
110  void setMonths( double months ) { mSeconds = months * MONTHS; mValid = true; }
111 
116  double weeks() const { return mSeconds / WEEKS; }
117 
123  void setWeeks( double weeks ) { mSeconds = weeks * WEEKS; mValid = true; }
124 
129  double days() const { return mSeconds / DAY; }
130 
136  void setDays( double days ) { mSeconds = days * DAY; mValid = true; }
137 
142  double hours() const { return mSeconds / HOUR; }
143 
149  void setHours( double hours ) { mSeconds = hours * HOUR; mValid = true; }
150 
155  double minutes() const { return mSeconds / MINUTE; }
156 
162  void setMinutes( double minutes ) { mSeconds = minutes * MINUTE; mValid = true; }
163 
168  double seconds() const { return mSeconds; }
169 
175  void setSeconds( double seconds ) { mSeconds = seconds; mValid = true; }
176 
181  bool isValid() const { return mValid; }
182 
188  void setValid( bool valid ) { mValid = valid; }
189 
190  bool operator==( QgsInterval other ) const;
191 
197  static QgsInterval fromString( const QString &string );
198 
200  operator QVariant() const
201  {
202  return QVariant::fromValue( *this );
203  }
204 
205  private:
206 
208  double mSeconds = 0.0;
209 
211  bool mValid = false;
212 };
213 
215 
216 #ifndef SIP_RUN
217 
225 QgsInterval CORE_EXPORT operator-( const QDateTime &datetime1, const QDateTime &datetime2 );
226 
234 QgsInterval CORE_EXPORT operator-( QDate date1, QDate date2 );
235 
243 QgsInterval CORE_EXPORT operator-( QTime time1, QTime time2 );
244 
252 QDateTime CORE_EXPORT operator+( const QDateTime &start, const QgsInterval &interval );
253 
255 QDebug operator<<( QDebug dbg, const QgsInterval &interval );
256 \
257 #endif
258 
259 #endif // QGSINTERVAL_H
operator<<
QDebug operator<<(QDebug dbg, const QgsInterval &interval)
Debug string representation of interval.
Definition: qgsinterval.cpp:129
QgsInterval::seconds
double seconds() const
Returns the interval duration in seconds.
Definition: qgsinterval.h:168
QgsInterval::setYears
void setYears(double years)
Sets the interval duration in years.
Definition: qgsinterval.h:97
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:399
qgsunittypes.h
QgsInterval::months
double months() const
Returns the interval duration in months (based on a 30 day month).
Definition: qgsinterval.h:103
QgsUnitTypes::TemporalUnit
TemporalUnit
Temporal units.
Definition: qgsunittypes.h:149
QgsInterval::setWeeks
void setWeeks(double weeks)
Sets the interval duration in weeks.
Definition: qgsinterval.h:123
QgsInterval::setHours
void setHours(double hours)
Sets the interval duration in hours.
Definition: qgsinterval.h:149
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
QgsInterval::setMonths
void setMonths(double months)
Sets the interval duration in months.
Definition: qgsinterval.h:110
qgis_sip.h
QgsInterval::setDays
void setDays(double days)
Sets the interval duration in days.
Definition: qgsinterval.h:136
QgsInterval::minutes
double minutes() const
Returns the interval duration in minutes.
Definition: qgsinterval.h:155
QgsInterval::setSeconds
void setSeconds(double seconds)
Sets the interval duration in seconds.
Definition: qgsinterval.h:175
QgsInterval::weeks
double weeks() const
Returns the interval duration in weeks.
Definition: qgsinterval.h:116
QgsInterval::setValid
void setValid(bool valid)
Sets whether the interval is valid.
Definition: qgsinterval.h:188
QgsInterval
A representation of the interval between two datetime values.
Definition: qgsinterval.h:40
QgsInterval::years
double years() const
Returns the interval duration in years (based on an average year length)
Definition: qgsinterval.h:90
QgsInterval::days
double days() const
Returns the interval duration in days.
Definition: qgsinterval.h:129
QgsInterval::hours
double hours() const
Returns the interval duration in hours.
Definition: qgsinterval.h:142
operator-
QgsInterval CORE_EXPORT operator-(const QDateTime &datetime1, const QDateTime &datetime2)
Returns the interval between two datetimes.
Definition: qgsinterval.cpp:138
QgsInterval::isValid
bool isValid() const
Returns true if the interval is valid.
Definition: qgsinterval.h:181
QgsInterval::setMinutes
void setMinutes(double minutes)
Sets the interval duration in minutes.
Definition: qgsinterval.h:162
operator+
QDateTime CORE_EXPORT operator+(const QDateTime &start, const QgsInterval &interval)
Adds an interval to a datetime.
Definition: qgsinterval.cpp:144