Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a flexible QgsInterval constructor for creating from years/months…
…/days/etc
  • Loading branch information
nyalldawson committed May 9, 2020
1 parent 072222d commit ca64c6c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
16 changes: 16 additions & 0 deletions python/core/auto_generated/qgsinterval.sip.in
Expand Up @@ -47,6 +47,22 @@ Constructor for QgsInterval.
QgsInterval( double duration, QgsUnitTypes::TemporalUnit unit );
%Docstring
Constructor for QgsInterval, using the specified ``duration`` and ``units``.
%End

QgsInterval( double years, double months, double weeks, double days, double hours, double minutes, double seconds );
%Docstring
Constructor for QgsInterval, using the specified ``years``, ``months``,
``weeks``, ``days``, ``hours``, ``minutes`` and ``seconds``.

.. note::

Month units assumes a 30 day month length.

.. note::

Year units assumes a 365.25 day year length.

.. versionadded:: 3.14
%End

double years() const;
Expand Down
13 changes: 13 additions & 0 deletions src/core/qgsinterval.cpp
Expand Up @@ -40,6 +40,19 @@ QgsInterval::QgsInterval( double duration, QgsUnitTypes::TemporalUnit unit )

}

QgsInterval::QgsInterval( double years, double months, double weeks, double days, double hours, double minutes, double seconds )
: mSeconds( years * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalYears, QgsUnitTypes::TemporalSeconds )
+ months * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalMonths, QgsUnitTypes::TemporalSeconds )
+ weeks * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalWeeks, QgsUnitTypes::TemporalSeconds )
+ days * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalDays, QgsUnitTypes::TemporalSeconds )
+ hours * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalHours, QgsUnitTypes::TemporalSeconds )
+ minutes * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalMinutes, QgsUnitTypes::TemporalSeconds )
+ seconds )
, mValid( true )
{

}

bool QgsInterval::operator==( QgsInterval other ) const
{
if ( !mValid && !other.mValid )
Expand Down
11 changes: 11 additions & 0 deletions src/core/qgsinterval.h
Expand Up @@ -72,6 +72,17 @@ class CORE_EXPORT QgsInterval
*/
QgsInterval( double duration, QgsUnitTypes::TemporalUnit unit );

/**
* Constructor for QgsInterval, using the specified \a years, \a months,
* \a weeks, \a days, \a hours, \a minutes and \a seconds.
*
* \note Month units assumes a 30 day month length.
* \note Year units assumes a 365.25 day year length.
*
* \since QGIS 3.14
*/
QgsInterval( double years, double months, double weeks, double days, double hours, double minutes, double seconds );

/**
* Returns the interval duration in years (based on an average year length)
* \see setYears()
Expand Down
18 changes: 18 additions & 0 deletions tests/src/python/test_qgsinterval.py
Expand Up @@ -166,6 +166,24 @@ def testFromString(self):
i = QgsInterval.fromString('bad')
self.assertFalse(i.isValid())

def testFromUnits(self):
i = QgsInterval(2, 0, 0, 0, 0, 0, 0)
self.assertEqual(i.seconds(), 63115200.0)
i = QgsInterval(0, 2, 0, 0, 0, 0, 0)
self.assertEqual(i.seconds(), 5184000.0)
i = QgsInterval(0, 0, 2, 0, 0, 0, 0)
self.assertEqual(i.seconds(), 1209600.0)
i = QgsInterval(0, 0, 0, 2, 0, 0, 0)
self.assertEqual(i.seconds(), 172800.0)
i = QgsInterval(0, 0, 0, 0, 2, 0, 0)
self.assertEqual(i.seconds(), 7200.0)
i = QgsInterval(0, 0, 0, 0, 0, 2, 0)
self.assertEqual(i.seconds(), 120.0)
i = QgsInterval(0, 0, 0, 0, 0, 0, 2)
self.assertEqual(i.seconds(), 2)
i = QgsInterval(1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 2)
self.assertEqual(i.seconds(), 56342192.0)


if __name__ == '__main__':
unittest.main()

0 comments on commit ca64c6c

Please sign in to comment.