Skip to content

Commit

Permalink
New class QgsInterval for storing durations between datetimes
Browse files Browse the repository at this point in the history
Move the QgsExpression::Interval class out to its own QgsInterval
class, extend with new methods and add tests

Add a typedef to keep API compatibility for 2.16
  • Loading branch information
nyalldawson committed May 10, 2016
1 parent 3340d8e commit ceba526
Show file tree
Hide file tree
Showing 12 changed files with 676 additions and 160 deletions.
1 change: 1 addition & 0 deletions python/core/core.sip
Expand Up @@ -57,6 +57,7 @@
%Include qgsgml.sip
%Include qgsgmlschema.sip
%Include qgshistogram.sip
%Include qgsinterval.sip
%Include qgsjsonutils.sip
%Include qgsmaptopixelgeometrysimplifier.sip
%Include qgstransaction.sip
Expand Down
29 changes: 17 additions & 12 deletions python/core/qgsexpression.sip
Expand Up @@ -671,33 +671,33 @@ class QgsExpression
virtual QString dump() const;
};

//TODO QGIS 3.0 - remove
//! @deprecated use QgsInterval instead
class Interval
{
{
public:
Interval( int seconds = 0 );

//! interval length in years
double years();
double years() const;
//! interval length in months
double months();
double months() const;
//! interval length in weeks
double weeks();
double weeks() const;
//! interval length in days
double days();
double days() const;
//! interval length in hours
double hours();
double hours() const;
//! interval length in minutus
double minutes();
double minutes() const;
//! interval length in seconds
double seconds();
double seconds() const;
//! getter interval validity
bool isValid();
bool isValid() const;
//! setter interval validity
void setValid( bool valid );
//! compare two intervals
bool operator==( QgsExpression::Interval other ) const;
//! return an invalid interval
static QgsExpression::Interval invalidInterVal();
//! convert a string to an interval
static QgsExpression::Interval fromString( const QString& string );
};
Expand Down Expand Up @@ -749,7 +749,12 @@ class QgsExpression
bool compare( double diff );
int computeInt( int x, int y );
double computeDouble( double x, double y );
QDateTime computeDateTimeFromInterval( const QDateTime& d, QgsExpression::Interval *i );

/** Computes the result date time calculation from a start datetime and an interval
* @param d start datetime
* @param i interval to add or subtract (depending on mOp)
*/
QDateTime computeDateTimeFromInterval( const QDateTime& d, QgsInterval *i );
};

class NodeInOperator : QgsExpression::Node
Expand Down
136 changes: 136 additions & 0 deletions python/core/qgsinterval.sip
@@ -0,0 +1,136 @@
/** \ingroup core
* \class QgsInterval
* \brief A representation of the interval between two datetime values.
* \note Added in version 2.16
*/

class QgsInterval
{
%TypeHeaderCode
#include <qgsinterval.h>
%End

public:

// YEAR const value taken from postgres query
// SELECT EXTRACT(EPOCH FROM interval '1 year')
//! Seconds per year (average)
static const int YEARS;
//! Seconds per month, based on 30 day month
static const int MONTHS;
//! Seconds per week
static const int WEEKS;
//! Seconds per day
static const int DAY;
//! Seconds per hour
static const int HOUR;
//! Seconds per minute
static const int MINUTE;

/** Default constructor for QgsInterval. Creates an invalid interval.
*/
QgsInterval();

/** Constructor for QgsInterval.
* @param seconds duration of interval in seconds
*/
QgsInterval( double seconds );

/** Returns the interval duration in years (based on an average year length)
* @see setYears()
*/
double years() const;

/** Sets the interval duration in years.
* @param years duration in years (based on average year length)
* @see years()
*/
void setYears( double years );

/** Returns the interval duration in months (based on a 30 day month).
* @see setMonths()
*/
double months() const;

/** Sets the interval duration in months.
* @param months duration in months (based on a 30 day month)
* @see months()
*/
void setMonths( double months );

/** Returns the interval duration in weeks.
* @see setWeeks()
*/
double weeks() const;

/** Sets the interval duration in weeks.
* @param weeks duration in weeks
* @see weeks()
*/
void setWeeks( double weeks );

/** Returns the interval duration in days.
* @see setDays()
*/
double days() const;

/** Sets the interval duration in days.
* @param days duration in days
* @see days()
*/
void setDays( double days );

/** Returns the interval duration in hours.
* @see setHours()
*/
double hours() const;

/** Sets the interval duration in hours.
* @param hours duration in hours
* @see hours()
*/
void setHours( double hours );

/** Returns the interval duration in minutes.
* @see setMinutes()
*/
double minutes() const;

/** Sets the interval duration in minutes.
* @param minutes duration in minutes
* @see minutes()
*/
void setMinutes( double minutes );

/** Returns the interval duration in seconds.
* @see setSeconds()
*/
double seconds() const;

/** Sets the interval duration in seconds.
* @param seconds duration in seconds
* @see seconds()
*/
void setSeconds( double seconds );

/** Returns true if the interval is valid.
* @see setValid()
*/
bool isValid() const;

/** Sets whether the interval is valid.
* @param valid set to true to set the interval as valid.
* @see isValid()
*/
void setValid( bool valid );

bool operator==( const QgsInterval& other ) const;

/** Converts a string to an interval
* @param string string to parse
* @returns interval, or invalid interval if string could not be parsed
*/
static QgsInterval fromString( const QString& string );

};

2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -121,6 +121,7 @@ SET(QGIS_CORE_SRCS
qgsgml.cpp
qgsgmlschema.cpp
qgshistogram.cpp
qgsinterval.cpp
qgsjsonutils.cpp
qgslabel.cpp
qgslabelattributes.cpp
Expand Down Expand Up @@ -637,6 +638,7 @@ SET(QGIS_CORE_HDRS
qgsgeometrycache.h
qgshistogram.h
qgsindexedfeature.h
qgsinterval.h
qgsjsonutils.h
qgslayerdefinition.h
qgslabel.h
Expand Down

0 comments on commit ceba526

Please sign in to comment.