23#include <QVariantList>
33 : mStatistics( stats )
52 const auto constValues = values;
53 for (
const QVariant &variant : constValues )
63 if ( value.userType() == QMetaType::Type::QDateTime )
67 else if ( value.userType() == QMetaType::Type::QDate )
69 const QDate date = value.toDate();
70 testDateTime( date.isValid() ? QDateTime( date, QTime( 0, 0, 0 ) )
73 else if ( value.userType() == QMetaType::Type::QTime )
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 )
108 if ( mMin.isValid() && dateTime.isValid() )
110 mMin = std::min( mMin, dateTime );
112 else if ( !mMin.isValid() && dateTime.isValid() )
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#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
146 return mIsTimes ? QVariant::fromValue( mMax.time() - mMin.time() ) : QVariant::fromValue( mMax - mMin );
148 return mIsTimes ? QVariant::fromValue( mMax.time() - mMin.time() ) : QVariant::fromValue(
QgsInterval(
static_cast< double >( ( mMax - mMin ).
count() ) / 1000.0 ) );
158#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
161 return QgsInterval(
static_cast< double >( ( mMax - mMin ).
count() ) / 1000.0 );
170 return QObject::tr(
"Count" );
172 return QObject::tr(
"Count (distinct)" );
174 return QObject::tr(
"Count (missing)" );
176 return QObject::tr(
"Minimum (earliest)" );
178 return QObject::tr(
"Maximum (latest)" );
180 return QObject::tr(
"Range (interval)" );
QFlags< DateTimeStatistic > DateTimeStatistics
Statistics to be calculated for date/time values.
DateTimeStatistic
Available date/time statistics.
@ Max
Maximum (latest) datetime value.
@ Min
Minimum (earliest) datetime value.
@ Range
Interval between earliest and latest datetime value.
@ CountMissing
Number of missing (null) values.
@ CountDistinct
Number of distinct datetime values.
QVariant statistic(Qgis::DateTimeStatistic stat) const
Returns the value of a specified statistic.
QgsDateTimeStatisticalSummary(Qgis::DateTimeStatistics stats=Qgis::DateTimeStatistic::All)
Constructor for QgsDateTimeStatisticalSummary.
QgsInterval range() const
Returns the range (interval between earliest and latest non-null datetime values).
void calculate(const QVariantList &values)
Calculates summary statistics for a list of variants.
static QString displayName(Qgis::DateTimeStatistic statistic)
Returns the friendly display name for a statistic.
void addValue(const QVariant &value)
Adds a single datetime to the statistics calculation.
void finalize()
Must be called after adding all datetimes with addValue() and before retrieving any calculated dateti...
void reset()
Resets the calculated values.
int count() const
Returns the calculated count of values.
A representation of the interval between two datetime values.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.