Skip to content
Permalink
Browse files

Add API to set/retrieve original units in QgsInterval

  • Loading branch information
Samweli authored and nyalldawson committed Dec 18, 2020
1 parent 2a81ebf commit 6ae1bf22c68737380b802b27574248609d97f2b4
Showing with 494 additions and 15 deletions.
  1. +95 −0 python/core/auto_generated/qgsinterval.sip.in
  2. +152 −2 src/core/qgsinterval.cpp
  3. +127 −13 src/core/qgsinterval.h
  4. +120 −0 tests/src/python/test_qgsinterval.py
@@ -69,13 +69,22 @@ Constructor for QgsInterval, using the specified ``years``, ``months``,
%Docstring
Returns the interval duration in years (based on an average year length)

If the :py:func:`~QgsInterval.originalUnit` is QgsUnitTypes.TemporalYears then this value
will match the exact number of months as returned by :py:func:`~QgsInterval.originalDuration`,
otherwise it will be calculated using the average year length (31557600 seconds).

.. seealso:: :py:func:`setYears`
%End

void setYears( double years );
%Docstring
Sets the interval duration in years.

Replaces the interval size and changes the original interval unit
and duration, :py:func:`originalDuration` and \see originalUnit().

Changes the original unit to QgsUnitTypes.TemporalYears

:param years: duration in years (based on average year length)

.. seealso:: :py:func:`years`
@@ -85,13 +94,23 @@ Sets the interval duration in years.
%Docstring
Returns the interval duration in months (based on a 30 day month).

If the :py:func:`~QgsInterval.originalUnit` is QgsUnitTypes.TemporalMonths then this value
will match the exact number of months as returned by :py:func:`~QgsInterval.originalDuration`,
otherwise it will be calculated using the assumption that
a month consists of exactly 30 days.

.. seealso:: :py:func:`setMonths`
%End

void setMonths( double months );
%Docstring
Sets the interval duration in months.

Replaces the interval size and changes the original interval unit
and duration, :py:func:`originalDuration` and \see originalUnit().

Changes the original unit to QgsUnitTypes.TemporalMonths

:param months: duration in months (based on a 30 day month)

.. seealso:: :py:func:`months`
@@ -101,13 +120,22 @@ Sets the interval duration in months.
%Docstring
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`,
otherwise it will be calculated using the QgsInterval.WEEKS constant.

.. seealso:: :py:func:`setWeeks`
%End

void setWeeks( double weeks );
%Docstring
Sets the interval duration in weeks.

Replaces the interval size and changes the original interval unit
and duration, :py:func:`originalDuration` and \see originalUnit().

Changes the original unit to QgsUnitTypes.TemporalWeeks

:param weeks: duration in weeks

.. seealso:: :py:func:`weeks`
@@ -117,13 +145,22 @@ Sets the interval duration in weeks.
%Docstring
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`,
otherwise it will be calculated using the QgsInterval.DAY constant.

.. seealso:: :py:func:`setDays`
%End

void setDays( double days );
%Docstring
Sets the interval duration in days.

Replaces the interval size and changes the original interval unit
and duration, :py:func:`originalDuration` and \see originalUnit().

Changes the original unit to QgsUnitTypes.TemporalDays

:param days: duration in days

.. seealso:: :py:func:`days`
@@ -133,13 +170,22 @@ Sets the interval duration in days.
%Docstring
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`,
otherwise it will be calculated using the QgsInterval.HOUR constant.

.. seealso:: :py:func:`setHours`
%End

void setHours( double hours );
%Docstring
Sets the interval duration in hours.

Replaces the interval size and changes the original interval unit
and duration, :py:func:`originalDuration` and \see originalUnit().

The original unit to QgsUnitTypes.TemporalHours

:param hours: duration in hours

.. seealso:: :py:func:`hours`
@@ -149,13 +195,22 @@ Sets the interval duration in hours.
%Docstring
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`,
otherwise it will be calculated using the QgsInterval.MINUTE constant.

.. seealso:: :py:func:`setMinutes`
%End

void setMinutes( double minutes );
%Docstring
Sets the interval duration in minutes.

Replaces the interval size and changes the original interval unit
and duration, :py:func:`originalDuration` and \see originalUnit().

Changes the original unit to QgsUnitTypes.TemporalMinutes

:param minutes: duration in minutes

.. seealso:: :py:func:`minutes`
@@ -172,6 +227,11 @@ Returns the interval duration in seconds.
%Docstring
Sets the interval duration in seconds.

Replaces the interval size and changes the original interval unit
and duration, :py:func:`originalDuration` and \see originalUnit().

Changes the original unit to QgsUnitTypes.TemporalSeconds

:param seconds: duration in seconds

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


double originalDuration() const;
%Docstring
Returns the original interval duration.

This original interval duration can be updated through calling
QgsInterval setter methods.

.. seealso:: :py:func:`originalUnit`

If the original interval duration is not available or
interval was set with a mix of units, calling :py:func:`~QgsInterval.originalUnit`
will return QgsUnitTypes.TemporalUnknownUnit

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.

The interval temporal unit can be set through the
QgsInterval constructors or through the available setter methods.

Returns QgsUnitTypes.TemporalUnknownUnit if unit was not set when creating the
QgsInterval instance or interval was set with a mix of units.

.. seealso:: :py:func:`originalDuration`

.. versionadded:: 3.18
%End

bool operator==( QgsInterval other ) const;

static QgsInterval fromString( const QString &string );
@@ -31,13 +31,17 @@
QgsInterval::QgsInterval( double seconds )
: mSeconds( seconds )
, mValid( true )
{ }
{
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;
}

QgsInterval::QgsInterval( double years, double months, double weeks, double days, double hours, double minutes, double seconds )
@@ -50,7 +54,153 @@ QgsInterval::QgsInterval( double years, double months, double weeks, double days
+ seconds )
, mValid( true )
{
if ( years && !months && !weeks && !days && !hours && !minutes && !seconds )
{
mOriginalDuration = years;
mOriginalUnit = QgsUnitTypes::TemporalYears;
}
else if ( !years && months && !weeks && !days && !hours && !minutes && !seconds )
{
mOriginalDuration = months;
mOriginalUnit = QgsUnitTypes::TemporalMonths;
}
else if ( !years && !months && weeks && !days && !hours && !minutes && !seconds )
{
mOriginalDuration = weeks;
mOriginalUnit = QgsUnitTypes::TemporalWeeks;
}
else if ( !years && !months && !weeks && days && !hours && !minutes && !seconds )
{
mOriginalDuration = days;
mOriginalUnit = QgsUnitTypes::TemporalDays;
}
else if ( !years && !months && !weeks && !days && hours && !minutes && !seconds )
{
mOriginalDuration = hours;
mOriginalUnit = QgsUnitTypes::TemporalHours;
}
else if ( !years && !months && !weeks && !days && !hours && minutes && !seconds )
{
mOriginalDuration = minutes;
mOriginalUnit = QgsUnitTypes::TemporalMinutes;
}
else if ( !years && !months && !weeks && !days && !hours && !minutes && seconds )
{
mOriginalDuration = seconds;
mOriginalUnit = QgsUnitTypes::TemporalSeconds;
}
else
{
mOriginalUnit = QgsUnitTypes::TemporalUnknownUnit;
}

}

double QgsInterval::years() const
{
if ( mOriginalUnit == QgsUnitTypes::TemporalYears )
return mOriginalDuration;

return mSeconds / YEARS;
}

void QgsInterval::setYears( double years )
{
mSeconds = years * YEARS;
mValid = true;
mOriginalDuration = years;
mOriginalUnit = QgsUnitTypes::TemporalYears;
}

double QgsInterval::months() const
{
if ( mOriginalUnit == QgsUnitTypes::TemporalMonths )
return mOriginalDuration;

return mSeconds / MONTHS;
}

void QgsInterval::setMonths( double months )
{
mSeconds = months * MONTHS;
mValid = true;
mOriginalDuration = months;
mOriginalUnit = QgsUnitTypes::TemporalMonths;
}

double QgsInterval::weeks() const
{
if ( mOriginalUnit == QgsUnitTypes::TemporalWeeks )
return mOriginalDuration;

return mSeconds / WEEKS;
}


void QgsInterval::setWeeks( double weeks )
{
mSeconds = weeks * WEEKS;
mValid = true;
mOriginalDuration = weeks;
mOriginalUnit = QgsUnitTypes::TemporalWeeks;
}

double QgsInterval::days() const
{
if ( mOriginalUnit == QgsUnitTypes::TemporalDays )
return mOriginalDuration;

return mSeconds / DAY;
}


void QgsInterval::setDays( double days )
{
mSeconds = days * DAY;
mValid = true;
mOriginalDuration = days;
mOriginalUnit = QgsUnitTypes::TemporalDays;
}

double QgsInterval::hours() const
{
if ( mOriginalUnit == QgsUnitTypes::TemporalHours )
return mOriginalDuration;

return mSeconds / HOUR;
}


void QgsInterval::setHours( double hours )
{
mSeconds = hours * HOUR;
mValid = true;
mOriginalDuration = hours;
mOriginalUnit = QgsUnitTypes::TemporalHours;
}

double QgsInterval::minutes() const
{
if ( mOriginalUnit == QgsUnitTypes::TemporalMinutes )
return mOriginalDuration;

return mSeconds / MINUTE;
}

void QgsInterval::setMinutes( double minutes )
{
mSeconds = minutes * MINUTE;
mValid = true;
mOriginalDuration = minutes;
mOriginalUnit = QgsUnitTypes::TemporalMinutes;
}

void QgsInterval::setSeconds( double seconds )
{
mSeconds = seconds;
mValid = true;
mOriginalDuration = seconds;
mOriginalUnit = QgsUnitTypes::TemporalSeconds;
}

bool QgsInterval::operator==( QgsInterval other ) const

0 comments on commit 6ae1bf2

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