Skip to content
Permalink
Browse files

Minor doc correction, tweak equality operator to consider original units

  • Loading branch information
nyalldawson committed Dec 18, 2020
1 parent 6ae1bf2 commit 2f2e410d0d2f2c62cea21eb42f6d0dfa7873d30f
@@ -121,7 +121,7 @@ Changes the original unit to QgsUnitTypes.TemporalMonths
Returns the interval duration in weeks.

If the :py:func:`~QgsInterval.originalUnit` is QgsUnitTypes.TemporalWeeks then this value
will match the exact number of months as returned by :py:func:`~QgsInterval.originalDuration`,
will match the exact number of weeks as returned by :py:func:`~QgsInterval.originalDuration`,
otherwise it will be calculated using the QgsInterval.WEEKS constant.

.. seealso:: :py:func:`setWeeks`
@@ -146,7 +146,7 @@ Changes the original unit to QgsUnitTypes.TemporalWeeks
Returns the interval duration in days.

If the :py:func:`~QgsInterval.originalUnit` is QgsUnitTypes.TemporalDays then this value
will match the exact number of months as returned by :py:func:`~QgsInterval.originalDuration`,
will match the exact number of days as returned by :py:func:`~QgsInterval.originalDuration`,
otherwise it will be calculated using the QgsInterval.DAY constant.

.. seealso:: :py:func:`setDays`
@@ -171,7 +171,7 @@ Changes the original unit to QgsUnitTypes.TemporalDays
Returns the interval duration in hours.

If the :py:func:`~QgsInterval.originalUnit` is QgsUnitTypes.TemporalHours then this value
will match the exact number of months as returned by :py:func:`~QgsInterval.originalDuration`,
will match the exact number of hours as returned by :py:func:`~QgsInterval.originalDuration`,
otherwise it will be calculated using the QgsInterval.HOUR constant.

.. seealso:: :py:func:`setHours`
@@ -196,7 +196,7 @@ The original unit to QgsUnitTypes.TemporalHours
Returns the interval duration in minutes.

If the :py:func:`~QgsInterval.originalUnit` is QgsUnitTypes.TemporalMinutes then this value
will match the exact number of months as returned by :py:func:`~QgsInterval.originalDuration`,
will match the exact number of minutes as returned by :py:func:`~QgsInterval.originalDuration`,
otherwise it will be calculated using the QgsInterval.MINUTE constant.

.. seealso:: :py:func:`setMinutes`
@@ -253,7 +253,6 @@ Sets whether the interval is valid.
.. seealso:: :py:func:`isValid`
%End


double originalDuration() const;
%Docstring
Returns the original interval duration.
@@ -272,7 +271,6 @@ Returns 0.0 if the original duration was not set.
.. versionadded:: 3.18
%End


QgsUnitTypes::TemporalUnit originalUnit() const;
%Docstring
Returns the original interval temporal unit.
@@ -31,17 +31,17 @@
QgsInterval::QgsInterval( double seconds )
: mSeconds( seconds )
, mValid( true )
, mOriginalDuration( seconds )
, mOriginalUnit( QgsUnitTypes::TemporalSeconds )
{
mOriginalDuration = mSeconds;
mOriginalUnit = QgsUnitTypes::TemporalSeconds;
}

QgsInterval::QgsInterval( double duration, QgsUnitTypes::TemporalUnit unit )
: mSeconds( duration * QgsUnitTypes::fromUnitToUnitFactor( unit, QgsUnitTypes::TemporalSeconds ) )
, mValid( true )
, mOriginalDuration( duration )
, mOriginalUnit( unit )
{
mOriginalDuration = duration;
mOriginalUnit = unit;
}

QgsInterval::QgsInterval( double years, double months, double weeks, double days, double hours, double minutes, double seconds )
@@ -89,11 +89,15 @@ QgsInterval::QgsInterval( double years, double months, double weeks, double days
mOriginalDuration = seconds;
mOriginalUnit = QgsUnitTypes::TemporalSeconds;
}
else if ( !years && !months && !weeks && !days && !hours && !minutes && !seconds )
{
mOriginalDuration = 0;
mOriginalUnit = QgsUnitTypes::TemporalSeconds;
}
else
{
mOriginalUnit = QgsUnitTypes::TemporalUnknownUnit;
}

}

double QgsInterval::years() const
@@ -207,6 +211,8 @@ bool QgsInterval::operator==( QgsInterval other ) const
{
if ( !mValid && !other.mValid )
return true;
else if ( mValid && other.mValid && ( mOriginalUnit != QgsUnitTypes::TemporalUnknownUnit || other.mOriginalUnit != QgsUnitTypes::TemporalUnknownUnit ) )
return mOriginalUnit == other.mOriginalUnit && mOriginalDuration == other.mOriginalDuration;
else if ( mValid && other.mValid )
return qgsDoubleNear( mSeconds, other.mSeconds );
else
@@ -136,7 +136,7 @@ class CORE_EXPORT QgsInterval
* Returns the interval duration in weeks.
*
* If the originalUnit() is QgsUnitTypes::TemporalWeeks then this value
* will match the exact number of months as returned by originalDuration(),
* will match the exact number of weeks as returned by originalDuration(),
* otherwise it will be calculated using the QgsInterval::WEEKS constant.
*
* \see setWeeks()
@@ -160,7 +160,7 @@ class CORE_EXPORT QgsInterval
* Returns the interval duration in days.
*
* If the originalUnit() is QgsUnitTypes::TemporalDays then this value
* will match the exact number of months as returned by originalDuration(),
* will match the exact number of days as returned by originalDuration(),
* otherwise it will be calculated using the QgsInterval::DAY constant.
*
* \see setDays()
@@ -184,7 +184,7 @@ class CORE_EXPORT QgsInterval
* Returns the interval duration in hours.
*
* If the originalUnit() is QgsUnitTypes::TemporalHours then this value
* will match the exact number of months as returned by originalDuration(),
* will match the exact number of hours as returned by originalDuration(),
* otherwise it will be calculated using the QgsInterval::HOUR constant.
*
* \see setHours()
@@ -208,7 +208,7 @@ class CORE_EXPORT QgsInterval
* Returns the interval duration in minutes.
*
* If the originalUnit() is QgsUnitTypes::TemporalMinutes then this value
* will match the exact number of months as returned by originalDuration(),
* will match the exact number of minutes as returned by originalDuration(),
* otherwise it will be calculated using the QgsInterval::MINUTE constant.
*
* \see setMinutes()
@@ -276,7 +276,6 @@ class CORE_EXPORT QgsInterval
*
* \since 3.18
*/

double originalDuration() const { return mOriginalDuration; }

/**
@@ -292,7 +291,6 @@ class CORE_EXPORT QgsInterval
*
* \since 3.18
*/

QgsUnitTypes::TemporalUnit originalUnit() const { return mOriginalUnit; }

bool operator==( QgsInterval other ) const;
@@ -105,6 +105,24 @@ def testEquality(self):
i1.setSeconds(6)
self.assertNotEqual(i1, i2)

# using original units

i1 = QgsInterval(1, QgsUnitTypes.TemporalMonths)
self.assertEqual(i1, QgsInterval(1, QgsUnitTypes.TemporalMonths))
i2 = QgsInterval(720, QgsUnitTypes.TemporalHours)
self.assertEqual(i2, QgsInterval(720, QgsUnitTypes.TemporalHours))
# these QgsInterval would be equal if we test on the approximated seconds value alone, but should be treated as not equal
# as their original units differ and we don't want the odd situation that the QgsInterval objects report equality
# but i1.months() != i2.months()!!
self.assertEqual(i1.seconds(), i2.seconds())
self.assertNotEqual(i1, i2)

# these should be treated as equal - they have unknown original units, so we are just comparing
# their computed seconds values
i1 = QgsInterval(0, 0, 0, 1, 0, 0, 1)
i2 = QgsInterval(0, 0, 0, 0, 24, 0, 1)
self.assertEqual(i1, i2)

def testFromString(self):
i = QgsInterval.fromString('1 Year 1 Month 1 Week 1 Hour 1 Minute')
self.assertTrue(i.isValid())

0 comments on commit 2f2e410

Please sign in to comment.
You can’t perform that action at this time.