25#include <QRegularExpression>
36 , mOriginalDuration( seconds )
37 , mOriginalUnit(
Qgis::TemporalUnit::Seconds )
42 : mSeconds( static_cast<double>( milliseconds.count() ) / 1000.0 )
44 , mOriginalDuration( static_cast<double>( milliseconds.count() ) )
45 , mOriginalUnit(
Qgis::TemporalUnit::Milliseconds )
50 : mSeconds( duration *
QgsUnitTypes::fromUnitToUnitFactor( unit,
Qgis::TemporalUnit::Seconds ) )
52 , mOriginalDuration( duration )
53 , mOriginalUnit( unit )
57QgsInterval::QgsInterval(
double years,
double months,
double weeks,
double days,
double hours,
double minutes,
double seconds )
58 : mSeconds( years *
QgsUnitTypes::fromUnitToUnitFactor(
Qgis::TemporalUnit::Years,
Qgis::TemporalUnit::Seconds )
59 + months *
QgsUnitTypes::fromUnitToUnitFactor(
Qgis::TemporalUnit::Months,
Qgis::TemporalUnit::Seconds )
60 + weeks *
QgsUnitTypes::fromUnitToUnitFactor(
Qgis::TemporalUnit::Weeks,
Qgis::TemporalUnit::Seconds )
61 + days *
QgsUnitTypes::fromUnitToUnitFactor(
Qgis::TemporalUnit::Days,
Qgis::TemporalUnit::Seconds )
62 + hours *
QgsUnitTypes::fromUnitToUnitFactor(
Qgis::TemporalUnit::Hours,
Qgis::TemporalUnit::Seconds )
63 + minutes *
QgsUnitTypes::fromUnitToUnitFactor(
Qgis::TemporalUnit::Minutes,
Qgis::TemporalUnit::Seconds )
69 mOriginalDuration =
years;
70 mOriginalUnit = Qgis::TemporalUnit::Years;
74 mOriginalDuration =
months;
75 mOriginalUnit = Qgis::TemporalUnit::Months;
79 mOriginalDuration =
weeks;
80 mOriginalUnit = Qgis::TemporalUnit::Weeks;
84 mOriginalDuration =
days;
85 mOriginalUnit = Qgis::TemporalUnit::Days;
89 mOriginalDuration =
hours;
90 mOriginalUnit = Qgis::TemporalUnit::Hours;
95 mOriginalUnit = Qgis::TemporalUnit::Minutes;
100 mOriginalUnit = Qgis::TemporalUnit::Seconds;
104 mOriginalDuration = 0;
105 mOriginalUnit = Qgis::TemporalUnit::Seconds;
109 mOriginalUnit = Qgis::TemporalUnit::Unknown;
115 if ( mOriginalUnit == Qgis::TemporalUnit::Years )
116 return mOriginalDuration;
118 return mSeconds /
YEARS;
125 mOriginalDuration =
years;
126 mOriginalUnit = Qgis::TemporalUnit::Years;
131 if ( mOriginalUnit == Qgis::TemporalUnit::Months )
132 return mOriginalDuration;
141 mOriginalDuration =
months;
142 mOriginalUnit = Qgis::TemporalUnit::Months;
147 if ( mOriginalUnit == Qgis::TemporalUnit::Weeks )
148 return mOriginalDuration;
150 return mSeconds /
WEEKS;
158 mOriginalDuration =
weeks;
159 mOriginalUnit = Qgis::TemporalUnit::Weeks;
164 if ( mOriginalUnit == Qgis::TemporalUnit::Days )
165 return mOriginalDuration;
167 return mSeconds /
DAY;
175 mOriginalDuration =
days;
176 mOriginalUnit = Qgis::TemporalUnit::Days;
181 if ( mOriginalUnit == Qgis::TemporalUnit::Hours )
182 return mOriginalDuration;
184 return mSeconds /
HOUR;
192 mOriginalDuration =
hours;
193 mOriginalUnit = Qgis::TemporalUnit::Hours;
198 if ( mOriginalUnit == Qgis::TemporalUnit::Minutes )
199 return mOriginalDuration;
209 mOriginalUnit = Qgis::TemporalUnit::Minutes;
217 mOriginalUnit = Qgis::TemporalUnit::Seconds;
223 const thread_local QRegularExpression rx(
"([-+]?\\d*\\.?\\d+\\s+\\S+)", QRegularExpression::CaseInsensitiveOption );
224 const thread_local QRegularExpression rxtime(
".* \\d{1,2}(:)\\d{1,2}(:)\\d{1,2}.*", QRegularExpression::CaseInsensitiveOption );
226 const QRegularExpressionMatch matchtime = rxtime.match(
string );
227 QString modedString = QString(
string );
228 if ( matchtime.hasMatch() )
231 modedString.replace( matchtime.capturedStart( 2 ), 1,
" minutes " );
233 modedString.replace( matchtime.capturedStart( 1 ), 1,
" hours " );
234 modedString.append(
" seconds" );
239 QRegularExpressionMatch match = rx.match( modedString );
240 while ( match.hasMatch() )
242 list << match.captured( 1 );
243 pos = match.capturedStart() + match.capturedLength();
244 match = rx.match( modedString, pos );
247 const thread_local QMap<int, QStringList> map{{
248 {1, QStringList() << QStringLiteral(
"second" ) << QStringLiteral(
"seconds" ) << QObject::tr(
"second|seconds",
"list of words separated by | which reference years" ).split(
'|' )},
249 { 0 +
MINUTE, QStringList() << QStringLiteral(
"minute" ) << QStringLiteral(
"minutes" ) << QObject::tr(
"minute|minutes",
"list of words separated by | which reference minutes" ).split(
'|' ) },
250 {0 +
HOUR, QStringList() << QStringLiteral(
"hour" ) << QStringLiteral(
"hours" ) << QObject::tr(
"hour|hours",
"list of words separated by | which reference minutes hours" ).split(
'|' )},
251 {0 +
DAY, QStringList() << QStringLiteral(
"day" ) << QStringLiteral(
"days" ) << QObject::tr(
"day|days",
"list of words separated by | which reference days" ).split(
'|' )},
252 {0 +
WEEKS, QStringList() << QStringLiteral(
"week" ) << QStringLiteral(
"weeks" ) << QObject::tr(
"week|weeks",
"wordlist separated by | which reference weeks" ).split(
'|' )},
253 {0 +
MONTHS, QStringList() << QStringLiteral(
"month" ) << QStringLiteral(
"months" ) << QStringLiteral(
"mon" ) << QObject::tr(
"month|months|mon",
"list of words separated by | which reference months" ).split(
'|' )},
254 {0 +
YEARS, QStringList() << QStringLiteral(
"year" ) << QStringLiteral(
"years" ) << QObject::tr(
"year|years",
"list of words separated by | which reference years" ).split(
'|' )},
257 const thread_local QRegularExpression splitRx(
"\\s+" );
259 for (
const QString &match : std::as_const( list ) )
261 const QStringList split = match.split( splitRx );
263 const double value = split.at( 0 ).toDouble( &ok );
269 bool matched =
false;
270 QMap<int, QStringList>::const_iterator it = map.constBegin();
271 for ( ; it != map.constEnd(); ++it )
273 const int duration = it.key();
274 const QStringList durationIdentifiers = it.value();
275 for (
const QString &identifier : durationIdentifiers )
277 if ( match.contains( identifier, Qt::CaseInsensitive ) )
302 dbg.nospace() <<
"QgsInterval()";
304 dbg.nospace() <<
"QgsInterval(" << interval.
seconds() <<
")";
305 return dbg.maybeSpace();
308#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
312 const qint64 mSeconds = dt2.msecsTo( dt1 );
320 return start.addMSecs(
static_cast<qint64
>( interval.
seconds() * 1000.0 ) );
325 const qint64 seconds =
static_cast< qint64
>( date2.daysTo( date1 ) ) * 24 * 60 * 60;
331 const qint64 mSeconds = time2.msecsTo( time1 );
The Qgis class provides global constants for use throughout the application.
TemporalUnit
Temporal units.
A representation of the interval between two datetime values.
static const int MINUTE
Seconds per minute.
bool isValid() const
Returns true if the interval is valid.
double days() const
Returns the interval duration in days.
void setMinutes(double minutes)
Sets the interval duration in minutes.
void setWeeks(double weeks)
Sets the interval duration in weeks.
QgsInterval()=default
Default constructor for QgsInterval.
double weeks() const
Returns the interval duration in weeks.
static const int MONTHS
Seconds per month, based on 30 day month.
double months() const
Returns the interval duration in months (based on a 30 day month).
static QgsInterval fromString(const QString &string)
Converts a string to an interval.
double seconds() const
Returns the interval duration in seconds.
double years() const
Returns the interval duration in years (based on an average year length)
static const int HOUR
Seconds per hour.
static const int WEEKS
Seconds per week.
static const int DAY
Seconds per day.
double hours() const
Returns the interval duration in hours.
void setHours(double hours)
Sets the interval duration in hours.
static const int YEARS
Seconds per year (average)
void setYears(double years)
Sets the interval duration in years.
void setSeconds(double seconds)
Sets the interval duration in seconds.
void setDays(double days)
Sets the interval duration in days.
double minutes() const
Returns the interval duration in minutes.
void setMonths(double months)
Sets the interval duration in months.
Helper functions for various unit types.
QDebug operator<<(QDebug dbg, const QgsInterval &interval)
Debug string representation of interval.
QgsInterval operator-(QDate date1, QDate date2)
Returns the interval between two dates.
QDateTime operator+(const QDateTime &start, const QgsInterval &interval)
Adds an interval to a datetime.