19 #include <QStringList>
22 #include <QVariantList>
32 : mStatistics( stats )
51 const auto constValues = values;
52 for (
const QVariant &variant : constValues )
62 if ( value.type() == QVariant::DateTime )
64 testDateTime( value.toDateTime(), value.isNull() );
66 else if ( value.type() == QVariant::Date )
68 QDate date = value.toDate();
69 testDateTime( date.isValid() ? QDateTime( date, QTime( 0, 0, 0 ) )
70 : QDateTime(), value.isNull() );
72 else if ( value.type() == QVariant::Time )
75 QTime time = value.toTime();
76 testDateTime( time.isValid() ? QDateTime( QDate::fromJulianDay( 0 ), time )
77 : QDateTime(), value.isNull() );
94 void QgsDateTimeStatisticalSummary::testDateTime(
const QDateTime &dateTime,
bool isNull )
98 if ( !dateTime.isValid() || isNull )
105 if ( mStatistics &
Min || mStatistics &
Range )
107 if ( mMin.isValid() && dateTime.isValid() )
109 mMin = std::min( mMin, dateTime );
111 else if ( !mMin.isValid() && dateTime.isValid() )
116 if ( mStatistics &
Max || mStatistics &
Range )
118 if ( mMax.isValid() && dateTime.isValid() )
120 mMax = std::max( mMax, dateTime );
122 else if ( !mMax.isValid() && dateTime.isValid() )
136 return mValues.count();
138 return mCountMissing;
140 return mIsTimes ? QVariant( mMin.time() ) : QVariant( mMin );
142 return mIsTimes ? QVariant( mMax.time() ) : QVariant( mMax );
144 return mIsTimes ? QVariant::fromValue( mMax.time() - mMin.time() ) : QVariant::fromValue( mMax - mMin );
156 return QObject::tr(
"Count" );
158 return QObject::tr(
"Count (distinct)" );
160 return QObject::tr(
"Count (missing)" );
162 return QObject::tr(
"Minimum (earliest)" );
164 return QObject::tr(
"Maximum (latest)" );
166 return QObject::tr(
"Range (interval)" );
void calculate(const QVariantList &values)
Calculates summary statistics for a list of variants.
QgsDateTimeStatisticalSummary(QgsDateTimeStatisticalSummary::Statistics stats=All)
Constructor for QgsDateTimeStatisticalSummary.
void addValue(const QVariant &value)
Adds a single datetime to the statistics calculation.
QVariant statistic(QgsDateTimeStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.
void finalize()
Must be called after adding all datetimes with addValue() and before retrieving any calculated dateti...
void reset()
Resets the calculated values.
Statistic
Enumeration of flags that specify statistics to be calculated.
@ Min
Minimum (earliest) datetime value.
@ Max
Maximum (latest) datetime value.
@ CountDistinct
Number of distinct datetime values.
@ Range
Interval between earliest and latest datetime value.
@ CountMissing
Number of missing (null) values.
static QString displayName(QgsDateTimeStatisticalSummary::Statistic statistic)
Returns the friendly display name for a statistic.