24#include <QRegularExpression>
38 , mOriginalUnit(
Qgis::TemporalUnit::Seconds )
43 : mSeconds( static_cast<double>( milliseconds.count() ) / 1000.0 )
45 , mOriginalDuration( static_cast<double>( milliseconds.count() ) )
46 , mOriginalUnit(
Qgis::TemporalUnit::Milliseconds )
51 : mSeconds( duration *
QgsUnitTypes::fromUnitToUnitFactor( unit,
Qgis::TemporalUnit::Seconds ) )
53 , mOriginalDuration( duration )
54 , mOriginalUnit( unit )
70 mOriginalDuration =
years;
75 mOriginalDuration =
months;
80 mOriginalDuration =
weeks;
85 mOriginalDuration =
days;
90 mOriginalDuration =
hours;
105 mOriginalDuration = 0;
117 return mOriginalDuration;
119 return mSeconds /
YEARS;
126 mOriginalDuration =
years;
133 return mOriginalDuration;
142 mOriginalDuration =
months;
149 return mOriginalDuration;
151 return mSeconds /
WEEKS;
159 mOriginalDuration =
weeks;
166 return mOriginalDuration;
168 return mSeconds /
DAY;
176 mOriginalDuration =
days;
183 return mOriginalDuration;
185 return mSeconds /
HOUR;
193 mOriginalDuration =
hours;
200 return mOriginalDuration;
224 const thread_local QRegularExpression rx(
"([-+]?\\d*\\.?\\d+\\s+\\S+)", QRegularExpression::CaseInsensitiveOption );
225 const thread_local QRegularExpression rxtime(
".* \\d{1,2}(:)\\d{1,2}(:)\\d{1,2}.*", QRegularExpression::CaseInsensitiveOption );
227 const QRegularExpressionMatch matchtime = rxtime.match(
string );
228 QString modedString = QString(
string );
229 if ( matchtime.hasMatch() )
232 modedString.replace( matchtime.capturedStart( 2 ), 1,
" minutes " );
234 modedString.replace( matchtime.capturedStart( 1 ), 1,
" hours " );
235 modedString.append(
" seconds" );
240 QRegularExpressionMatch match = rx.match( modedString );
241 while ( match.hasMatch() )
243 list << match.captured( 1 );
244 pos = match.capturedStart() + match.capturedLength();
245 match = rx.match( modedString, pos );
248 const thread_local QMap<int, QStringList> map{{
249 {1, QStringList() << u
"second"_s << u
"seconds"_s << QObject::tr(
"second|seconds",
"list of words separated by | which reference years" ).split(
'|' )},
250 { 0 +
MINUTE, QStringList() << u
"minute"_s << u
"minutes"_s << QObject::tr(
"minute|minutes",
"list of words separated by | which reference minutes" ).split(
'|' ) },
251 {0 +
HOUR, QStringList() << u
"hour"_s << u
"hours"_s << QObject::tr(
"hour|hours",
"list of words separated by | which reference minutes hours" ).split(
'|' )},
252 {0 +
DAY, QStringList() << u
"day"_s << u
"days"_s << QObject::tr(
"day|days",
"list of words separated by | which reference days" ).split(
'|' )},
253 {0 +
WEEKS, QStringList() << u
"week"_s << u
"weeks"_s << QObject::tr(
"week|weeks",
"wordlist separated by | which reference weeks" ).split(
'|' )},
254 {0 +
MONTHS, QStringList() << u
"month"_s << u
"months"_s << u
"mon"_s << QObject::tr(
"month|months|mon",
"list of words separated by | which reference months" ).split(
'|' )},
255 {0 +
YEARS, QStringList() << u
"year"_s << u
"years"_s << QObject::tr(
"year|years",
"list of words separated by | which reference years" ).split(
'|' )},
258 const thread_local QRegularExpression splitRx(
"\\s+" );
260 for (
const QString &match : std::as_const( list ) )
262 const QStringList split = match.split( splitRx );
264 const double value = split.at( 0 ).toDouble( &ok );
270 bool matched =
false;
271 QMap<int, QStringList>::const_iterator it = map.constBegin();
272 for ( ; it != map.constEnd(); ++it )
274 const int duration = it.key();
275 const QStringList durationIdentifiers = it.value();
276 for (
const QString &identifier : durationIdentifiers )
278 if ( match.contains( identifier, Qt::CaseInsensitive ) )
303 dbg.nospace() <<
"QgsInterval()";
305 dbg.nospace() <<
"QgsInterval(" << interval.
seconds() <<
")";
306 return dbg.maybeSpace();
309#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
313 const qint64 mSeconds = dt2.msecsTo( dt1 );
321 return start.addMSecs(
static_cast<qint64
>( interval.
seconds() * 1000.0 ) );
326 const qint64 seconds =
static_cast< qint64
>( date2.daysTo( date1 ) ) * 24 * 60 * 60;
332 const qint64 mSeconds = time2.msecsTo( time1 );
Provides global constants and enumerations for use throughout the application.
TemporalUnit
Temporal units.
@ Unknown
Unknown time unit.
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.