QGIS API Documentation  3.23.0-Master (dd0cd13a00)
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 #include "qgis.h"
31 
32 class QString;
33 
41 class CORE_EXPORT QgsInterval
42 {
43  public:
44 
45  // YEAR const value taken from postgres query
46  // SELECT EXTRACT(EPOCH FROM interval '1 year')
48  static const int YEARS = 31557600;
50  static const int MONTHS = 60 * 60 * 24 * 30;
52  static const int WEEKS = 60 * 60 * 24 * 7;
54  static const int DAY = 60 * 60 * 24;
56  static const int HOUR = 60 * 60;
58  static const int MINUTE = 60;
59 
63  QgsInterval() = default;
64 
69  QgsInterval( double seconds );
70 
74  QgsInterval( double duration, QgsUnitTypes::TemporalUnit unit );
75 
85  QgsInterval( double years, double months, double weeks, double days, double hours, double minutes, double seconds );
86 
96  double years() const;
97 
109  void setYears( double years );
110 
121  double months() const;
122 
134  void setMonths( double months );
135 
145  double weeks() const;
146 
158  void setWeeks( double weeks );
159 
169  double days() const;
170 
182  void setDays( double days );
183 
193  double hours() const;
194 
206  void setHours( double hours );
207 
217  double minutes() const;
218 
230  void setMinutes( double minutes );
231 
236  double seconds() const { return mSeconds; }
237 
249  void setSeconds( double seconds );
250 
255  bool isValid() const { return mValid; }
256 
262  void setValid( bool valid ) { mValid = valid; }
263 
280  double originalDuration() const { return mOriginalDuration; }
281 
295  QgsUnitTypes::TemporalUnit originalUnit() const { return mOriginalUnit; }
296 
297  bool operator==( QgsInterval other ) const
298  {
299  if ( !mValid && !other.mValid )
300  return true;
301  else if ( mValid && other.mValid && ( mOriginalUnit != QgsUnitTypes::TemporalUnknownUnit || other.mOriginalUnit != QgsUnitTypes::TemporalUnknownUnit ) )
302  return mOriginalUnit == other.mOriginalUnit && mOriginalDuration == other.mOriginalDuration;
303  else if ( mValid && other.mValid )
304  return qgsDoubleNear( mSeconds, other.mSeconds );
305  else
306  return false;
307  }
308 
309  bool operator!=( QgsInterval other ) const
310  {
311  return !( *this == other );
312  }
313 
319  static QgsInterval fromString( const QString &string );
320 
322  operator QVariant() const
323  {
324  return QVariant::fromValue( *this );
325  }
326 
327  private:
328 
330  double mSeconds = 0.0;
331 
333  bool mValid = false;
334 
336  double mOriginalDuration = 0.0;
337 
340 };
341 
343 
344 #ifndef SIP_RUN
345 
353 QgsInterval CORE_EXPORT operator-( const QDateTime &datetime1, const QDateTime &datetime2 );
354 
362 QgsInterval CORE_EXPORT operator-( QDate date1, QDate date2 );
363 
371 QgsInterval CORE_EXPORT operator-( QTime time1, QTime time2 );
372 
380 QDateTime CORE_EXPORT operator+( const QDateTime &start, const QgsInterval &interval );
381 
383 QDebug CORE_EXPORT operator<<( QDebug dbg, const QgsInterval &interval );
384 \
385 #endif
386 
387 #endif // QGSINTERVAL_H
A representation of the interval between two datetime values.
Definition: qgsinterval.h:42
double originalDuration() const
Returns the original interval duration.
Definition: qgsinterval.h:280
bool isValid() const
Returns true if the interval is valid.
Definition: qgsinterval.h:255
void setValid(bool valid)
Sets whether the interval is valid.
Definition: qgsinterval.h:262
QgsInterval()=default
Default constructor for QgsInterval.
bool operator==(QgsInterval other) const
Definition: qgsinterval.h:297
double seconds() const
Returns the interval duration in seconds.
Definition: qgsinterval.h:236
bool operator!=(QgsInterval other) const
Definition: qgsinterval.h:309
QgsUnitTypes::TemporalUnit originalUnit() const
Returns the original interval temporal unit.
Definition: qgsinterval.h:295
TemporalUnit
Temporal units.
Definition: qgsunittypes.h:150
@ TemporalUnknownUnit
Unknown time unit.
Definition: qgsunittypes.h:162
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:1504
QDateTime CORE_EXPORT operator+(const QDateTime &start, const QgsInterval &interval)
Adds an interval to a datetime.
QgsInterval CORE_EXPORT operator-(const QDateTime &datetime1, const QDateTime &datetime2)
Returns the interval between two datetimes.
QDebug CORE_EXPORT operator<<(QDebug dbg, const QgsInterval &interval)
Debug string representation of interval.
Q_DECLARE_METATYPE(QgsMeshTimeSettings)