Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add constructor variant to QgsInterval which uses QgsUnitTypes::Tempo…
…ralUnit
  • Loading branch information
nyalldawson committed Mar 12, 2020
1 parent 555d1ef commit 2959276
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions python/core/auto_generated/qgsinterval.sip.in
Expand Up @@ -42,6 +42,11 @@ Default constructor for QgsInterval. Creates an invalid interval.
Constructor for QgsInterval.

:param seconds: duration of interval in seconds
%End

QgsInterval( double duration, QgsUnitTypes::TemporalUnit unit );
%Docstring
Constructor for QgsInterval, using the specified ``duration`` and ``units``.
%End

double years() const;
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsinterval.cpp
Expand Up @@ -33,6 +33,13 @@ QgsInterval::QgsInterval( double seconds )
, mValid( true )
{ }

QgsInterval::QgsInterval( double duration, QgsUnitTypes::TemporalUnit unit )
: mSeconds( duration * QgsUnitTypes::fromUnitToUnitFactor( unit, QgsUnitTypes::TemporalSeconds ) )
, mValid( true )
{

}

bool QgsInterval::operator==( QgsInterval other ) const
{
if ( !mValid && !other.mValid )
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsinterval.h
Expand Up @@ -26,6 +26,7 @@

#include "qgis_sip.h"
#include "qgis_core.h"
#include "qgsunittypes.h"

class QString;

Expand Down Expand Up @@ -66,6 +67,11 @@ class CORE_EXPORT QgsInterval
*/
QgsInterval( double seconds );

/**
* Constructor for QgsInterval, using the specified \a duration and \a units.
*/
QgsInterval( double duration, QgsUnitTypes::TemporalUnit unit );

/**
* Returns the interval duration in years (based on an average year length)
* \see setYears()
Expand Down
7 changes: 6 additions & 1 deletion tests/src/python/test_qgsinterval.py
Expand Up @@ -12,7 +12,7 @@

import qgis # NOQA

from qgis.core import QgsInterval
from qgis.core import QgsInterval, QgsUnitTypes
from qgis.testing import unittest


Expand All @@ -37,6 +37,11 @@ def testIntervalConstructor(self):
self.assertTrue(i.isValid())
self.assertEqual(i.seconds(), 56)

# constructor with unit type
i = QgsInterval(56, QgsUnitTypes.TemporalMilliseconds)
self.assertTrue(i.isValid())
self.assertEqual(i.seconds(), 56000)

def testSettersGetters(self):
# setters and getters
i = QgsInterval()
Expand Down

0 comments on commit 2959276

Please sign in to comment.