Skip to content
Permalink
Browse files
Implement time stats for QgsDateTimeStatisticalSummary
  • Loading branch information
nyalldawson committed Nov 30, 2016
1 parent 6a457c2 commit ecd876a56168d64acced754263112098cba46b9b
Showing with 32 additions and 3 deletions.
  1. +11 −3 src/core/qgsdatetimestatisticalsummary.cpp
  2. +1 −0 src/core/qgsdatetimestatisticalsummary.h
  3. +20 −0 tests/src/python/test_qgsdatetimestatisticalsummary.py
@@ -41,6 +41,7 @@ void QgsDateTimeStatisticalSummary::reset()
mCountMissing = 0;
mMin = QDateTime();
mMax = QDateTime();
mIsTimes = false;
}

void QgsDateTimeStatisticalSummary::calculate( const QVariantList& values )
@@ -66,6 +67,13 @@ void QgsDateTimeStatisticalSummary::addValue( const QVariant& value )
testDateTime( date.isValid() ? QDateTime( date, QTime( 0, 0, 0 ) )
: QDateTime() );
}
else if ( value.type() == QVariant::Time )
{
mIsTimes = true;
QTime time = value.toTime();
testDateTime( time.isValid() ? QDateTime( QDate::fromJulianDay( 0 ), time )
: QDateTime() );
}
else //not a date
{
mCountMissing++;
@@ -126,11 +134,11 @@ QVariant QgsDateTimeStatisticalSummary::statistic( QgsDateTimeStatisticalSummary
case CountMissing:
return mCountMissing;
case Min:
return mMin;
return mIsTimes ? QVariant( mMin.time() ) : QVariant( mMin );
case Max:
return mMax;
return mIsTimes ? QVariant( mMax.time() ) : QVariant( mMax );
case Range:
return QVariant::fromValue( mMax - mMin );
return mIsTimes ? QVariant::fromValue( mMax.time() - mMin.time() ) : QVariant::fromValue( mMax - mMin );
case All:
return 0;
}
@@ -154,6 +154,7 @@ class CORE_EXPORT QgsDateTimeStatisticalSummary
int mCountMissing;
QDateTime mMin;
QDateTime mMax;
bool mIsTimes;

void testDateTime( const QDateTime& dateTime );
};
@@ -157,6 +157,26 @@ def testDates(self):
self.assertEqual(s.max(), QDateTime(QDate(2019, 12, 28), QTime()))
self.assertEqual(s.range(), QgsInterval(693792000))

def testTimes(self):
""" test with time values """
s = QgsDateTimeStatisticalSummary()
self.assertEqual(s.statistics(), QgsDateTimeStatisticalSummary.All)
s.calculate([QTime(11, 3, 4),
QTime(15, 3, 4),
QTime(19, 12, 28),
QTime(),
QTime(8, 1, 2),
QTime(),
QTime(19, 12, 28)])
self.assertEqual(s.count(), 7)
self.assertEqual(s.countDistinct(), 5)
self.assertEqual(s.countMissing(), 2)
self.assertEqual(s.min().time(), QTime(8, 1, 2))
self.assertEqual(s.max().time(), QTime(19, 12, 28))
self.assertEqual(s.statistic(QgsDateTimeStatisticalSummary.Min), QTime(8, 1, 2))
self.assertEqual(s.statistic(QgsDateTimeStatisticalSummary.Max), QTime(19, 12, 28))
self.assertEqual(s.range(), QgsInterval(40286))

def testMissing(self):
s = QgsDateTimeStatisticalSummary()
s.calculate([NULL,

0 comments on commit ecd876a

Please sign in to comment.