QGIS API Documentation  3.0.2-Girona (307d082)
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.h"
28 #include "qgis_core.h"
29 
30 class QString;
31 
39 class CORE_EXPORT QgsInterval
40 {
41  public:
42 
43  // YEAR const value taken from postgres query
44  // SELECT EXTRACT(EPOCH FROM interval '1 year')
46  static const int YEARS = 31557600;
48  static const int MONTHS = 60 * 60 * 24 * 30;
50  static const int WEEKS = 60 * 60 * 24 * 7;
52  static const int DAY = 60 * 60 * 24;
54  static const int HOUR = 60 * 60;
56  static const int MINUTE = 60;
57 
61  QgsInterval() = default;
62 
67  QgsInterval( double seconds );
68 
73  double years() const { return mSeconds / YEARS; }
74 
80  void setYears( double years ) { mSeconds = years * YEARS; mValid = true; }
81 
86  double months() const { return mSeconds / MONTHS; }
87 
93  void setMonths( double months ) { mSeconds = months * MONTHS; mValid = true; }
94 
99  double weeks() const { return mSeconds / WEEKS; }
100 
106  void setWeeks( double weeks ) { mSeconds = weeks * WEEKS; mValid = true; }
107 
112  double days() const { return mSeconds / DAY; }
113 
119  void setDays( double days ) { mSeconds = days * DAY; mValid = true; }
120 
125  double hours() const { return mSeconds / HOUR; }
126 
132  void setHours( double hours ) { mSeconds = hours * HOUR; mValid = true; }
133 
138  double minutes() const { return mSeconds / MINUTE; }
139 
145  void setMinutes( double minutes ) { mSeconds = minutes * MINUTE; mValid = true; }
146 
151  double seconds() const { return mSeconds; }
152 
158  void setSeconds( double seconds ) { mSeconds = seconds; mValid = true; }
159 
164  bool isValid() const { return mValid; }
165 
171  void setValid( bool valid ) { mValid = valid; }
172 
173  bool operator==( QgsInterval other ) const;
174 
180  static QgsInterval fromString( const QString &string );
181 
183  operator QVariant() const
184  {
185  return QVariant::fromValue( *this );
186  }
187 
188  private:
189 
191  double mSeconds = 0.0;
192 
194  bool mValid = false;
195 };
196 
198 
199 #ifndef SIP_RUN
200 
208 QgsInterval CORE_EXPORT operator-( const QDateTime &datetime1, const QDateTime &datetime2 );
209 
217 QgsInterval CORE_EXPORT operator-( QDate date1, QDate date2 );
218 
226 QgsInterval CORE_EXPORT operator-( QTime time1, QTime time2 );
227 
235 QDateTime CORE_EXPORT operator+( const QDateTime &start, const QgsInterval &interval );
236 
238 QDebug operator<<( QDebug dbg, const QgsInterval &interval );
239 \
240 #endif
241 
242 #endif // QGSINTERVAL_H
void setMinutes(double minutes)
Sets the interval duration in minutes.
Definition: qgsinterval.h:145
bool isValid() const
Returns true if the interval is valid.
Definition: qgsinterval.h:164
double months() const
Returns the interval duration in months (based on a 30 day month).
Definition: qgsinterval.h:86
void setYears(double years)
Sets the interval duration in years.
Definition: qgsinterval.h:80
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
double seconds() const
Returns the interval duration in seconds.
Definition: qgsinterval.h:151
void setHours(double hours)
Sets the interval duration in hours.
Definition: qgsinterval.h:132
QDebug operator<<(QDebug dbg, const QgsInterval &interval)
Debug string representation of interval.
Q_DECLARE_METATYPE(QModelIndex)
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:106
void setMonths(double months)
Sets the interval duration in months.
Definition: qgsinterval.h:93
double weeks() const
Returns the interval duration in weeks.
Definition: qgsinterval.h:99
void setDays(double days)
Sets the interval duration in days.
Definition: qgsinterval.h:119
A representation of the interval between two datetime values.
Definition: qgsinterval.h:39
double days() const
Returns the interval duration in days.
Definition: qgsinterval.h:112
void setValid(bool valid)
Sets whether the interval is valid.
Definition: qgsinterval.h:171
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:73
double minutes() const
Returns the interval duration in minutes.
Definition: qgsinterval.h:138
double hours() const
Returns the interval duration in hours.
Definition: qgsinterval.h:125
void setSeconds(double seconds)
Sets the interval duration in seconds.
Definition: qgsinterval.h:158