QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 class QString;
27 
34 class CORE_EXPORT QgsInterval
35 {
36  public:
37 
38  // YEAR const value taken from postgres query
39  // SELECT EXTRACT(EPOCH FROM interval '1 year')
41  static const int YEARS = 31557600;
43  static const int MONTHS = 60 * 60 * 24 * 30;
45  static const int WEEKS = 60 * 60 * 24 * 7;
47  static const int DAY = 60 * 60 * 24;
49  static const int HOUR = 60 * 60;
51  static const int MINUTE = 60;
52 
55  QgsInterval();
56 
60  QgsInterval( double seconds );
61 
65  double years() const { return mSeconds / YEARS; }
66 
71  void setYears( double years ) { mSeconds = years * YEARS; mValid = true; }
72 
76  double months() const { return mSeconds / MONTHS; }
77 
82  void setMonths( double months ) { mSeconds = months * MONTHS; mValid = true; }
83 
87  double weeks() const { return mSeconds / WEEKS; }
88 
93  void setWeeks( double weeks ) { mSeconds = weeks * WEEKS; mValid = true; }
94 
98  double days() const { return mSeconds / DAY; }
99 
104  void setDays( double days ) { mSeconds = days * DAY; mValid = true; }
105 
109  double hours() const { return mSeconds / HOUR; }
110 
115  void setHours( double hours ) { mSeconds = hours * HOUR; mValid = true; }
116 
120  double minutes() const { return mSeconds / MINUTE; }
121 
126  void setMinutes( double minutes ) { mSeconds = minutes * MINUTE; mValid = true; }
127 
131  double seconds() const { return mSeconds; }
132 
137  void setSeconds( double seconds ) { mSeconds = seconds; mValid = true; }
138 
142  bool isValid() const { return mValid; }
143 
148  void setValid( bool valid ) { mValid = valid; }
149 
150  bool operator==( const QgsInterval& other ) const;
151 
156  static QgsInterval fromString( const QString& string );
157 
159  operator QVariant() const
160  {
161  return QVariant::fromValue( *this );
162  }
163 
164  private:
165 
167  double mSeconds;
168 
170  bool mValid;
171 };
172 
174 
175 
181 QgsInterval CORE_EXPORT operator-( const QDateTime& datetime1, const QDateTime& datetime2 );
182 
189 QgsInterval CORE_EXPORT operator-( const QDate& date1, const QDate& date2 );
190 
197 QgsInterval CORE_EXPORT operator-( const QTime& time1, const QTime& time2 );
198 
205 QDateTime CORE_EXPORT operator+( const QDateTime& start, const QgsInterval& interval );
206 
208 QDebug operator<<( QDebug dbg, const QgsInterval& interval );
209 \
210 #endif // QGSINTERVAL_H
void setMinutes(double minutes)
Sets the interval duration in minutes.
Definition: qgsinterval.h:126
bool isValid() const
Returns true if the interval is valid.
Definition: qgsinterval.h:142
double months() const
Returns the interval duration in months (based on a 30 day month).
Definition: qgsinterval.h:76
void setYears(double years)
Sets the interval duration in years.
Definition: qgsinterval.h:71
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
double seconds() const
Returns the interval duration in seconds.
Definition: qgsinterval.h:131
Q_DECLARE_METATYPE(QgsMimeDataUtils::UriList)
void setHours(double hours)
Sets the interval duration in hours.
Definition: qgsinterval.h:115
QDebug operator<<(QDebug dbg, const QgsInterval &interval)
Debug string representation of interval.
QDateTime CORE_EXPORT operator+(const QDateTime &start, const QgsInterval &interval)
Adds an interval to a datetime.
void setWeeks(double weeks)
Sets the interval duration in weeks.
Definition: qgsinterval.h:93
void setMonths(double months)
Sets the interval duration in months.
Definition: qgsinterval.h:82
double weeks() const
Returns the interval duration in weeks.
Definition: qgsinterval.h:87
void setDays(double days)
Sets the interval duration in days.
Definition: qgsinterval.h:104
QVariant fromValue(const T &value)
A representation of the interval between two datetime values.
Definition: qgsinterval.h:34
double days() const
Returns the interval duration in days.
Definition: qgsinterval.h:98
void setValid(bool valid)
Sets whether the interval is valid.
Definition: qgsinterval.h:148
QgsInterval CORE_EXPORT operator-(const QDateTime &datetime1, const QDateTime &datetime2)
Returns the interval between two datetimes.
double years() const
Returns the interval duration in years (based on an average year length)
Definition: qgsinterval.h:65
double minutes() const
Returns the interval duration in minutes.
Definition: qgsinterval.h:120
double hours() const
Returns the interval duration in hours.
Definition: qgsinterval.h:109
void setSeconds(double seconds)
Sets the interval duration in seconds.
Definition: qgsinterval.h:137