Skip to content
Permalink
Browse files

Add a flexible QgsInterval constructor for creating from years/months…

…/days/etc
  • Loading branch information
nyalldawson committed May 9, 2020
1 parent 072222d commit ca64c6cac96935be5fda864b5502734f16e5f9f3
@@ -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;
@@ -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 )
@@ -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()
@@ -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.
You can’t perform that action at this time.