Add a flexible QgsInterval constructor for creating from years/months/days/etc

This commit is contained in:
Nyall Dawson 2020-05-09 17:30:24 +10:00
parent 072222d2fd
commit ca64c6cac9
4 changed files with 58 additions and 0 deletions

View File

@ -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;

View File

@ -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 )

View File

@ -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()

View File

@ -166,6 +166,24 @@ class TestQgsInterval(unittest.TestCase):
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()