23#include <QVariantList>
33 : mStatistics( stats )
52 const auto constValues = values;
53 for (
const QVariant &variant : constValues )
63 if ( value.type() == QVariant::DateTime )
67 else if ( value.type() == QVariant::Date )
69 const QDate date = value.toDate();
70 testDateTime( date.isValid() ? QDateTime( date, QTime( 0, 0, 0 ) )
73 else if ( value.type() == QVariant::Time )
76 const QTime time = value.toTime();
77 testDateTime( time.isValid() ? QDateTime( QDate::fromJulianDay( 0 ), time )
95void QgsDateTimeStatisticalSummary::testDateTime(
const QDateTime &dateTime,
bool isNull )
99 if ( !dateTime.isValid() || isNull )
106 if ( mStatistics &
Min || mStatistics &
Range )
108 if ( mMin.isValid() && dateTime.isValid() )
110 mMin = std::min( mMin, dateTime );
112 else if ( !mMin.isValid() && dateTime.isValid() )
117 if ( mStatistics &
Max || mStatistics &
Range )
119 if ( mMax.isValid() && dateTime.isValid() )
121 mMax = std::max( mMax, dateTime );
123 else if ( !mMax.isValid() && dateTime.isValid() )
137 return mValues.count();
139 return mCountMissing;
141 return mIsTimes ? QVariant( mMin.time() ) : QVariant( mMin );
143 return mIsTimes ? QVariant( mMax.time() ) : QVariant( mMax );
145 return mIsTimes ? QVariant::fromValue( mMax.time() - mMin.time() ) : QVariant::fromValue( mMax - mMin );
157 return QObject::tr(
"Count" );
159 return QObject::tr(
"Count (distinct)" );
161 return QObject::tr(
"Count (missing)" );
163 return QObject::tr(
"Minimum (earliest)" );
165 return QObject::tr(
"Maximum (latest)" );
167 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.
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.