19 #include <QStringList>
24 #include <QRegularExpression>
35 , mOriginalDuration( seconds )
43 , mOriginalDuration( duration )
44 , mOriginalUnit( unit )
48 QgsInterval::QgsInterval(
double years,
double months,
double weeks,
double days,
double hours,
double minutes,
double seconds )
60 mOriginalDuration =
years;
65 mOriginalDuration =
months;
70 mOriginalDuration =
weeks;
75 mOriginalDuration =
days;
80 mOriginalDuration =
hours;
95 mOriginalDuration = 0;
107 return mOriginalDuration;
109 return mSeconds /
YEARS;
116 mOriginalDuration =
years;
123 return mOriginalDuration;
132 mOriginalDuration =
months;
139 return mOriginalDuration;
141 return mSeconds /
WEEKS;
149 mOriginalDuration =
weeks;
156 return mOriginalDuration;
158 return mSeconds /
DAY;
166 mOriginalDuration =
days;
173 return mOriginalDuration;
175 return mSeconds /
HOUR;
183 mOriginalDuration =
hours;
190 return mOriginalDuration;
214 const thread_local QRegularExpression rx(
"([-+]?\\d*\\.?\\d+\\s+\\S+)", QRegularExpression::CaseInsensitiveOption );
217 QRegularExpressionMatch match = rx.match(
string );
218 while ( match.hasMatch() )
220 list << match.captured( 1 );
221 pos = match.capturedStart() + match.capturedLength();
222 match = rx.match(
string, pos );
225 QMap<int, QStringList> map;
226 map.insert( 1, QStringList() << QStringLiteral(
"second" ) << QStringLiteral(
"seconds" ) << QObject::tr(
"second|seconds",
"list of words separated by | which reference years" ).split(
'|' ) );
227 map.insert( 0 +
MINUTE, QStringList() << QStringLiteral(
"minute" ) << QStringLiteral(
"minutes" ) << QObject::tr(
"minute|minutes",
"list of words separated by | which reference minutes" ).split(
'|' ) );
228 map.insert( 0 +
HOUR, QStringList() << QStringLiteral(
"hour" ) << QStringLiteral(
"hours" ) << QObject::tr(
"hour|hours",
"list of words separated by | which reference minutes hours" ).split(
'|' ) );
229 map.insert( 0 +
DAY, QStringList() << QStringLiteral(
"day" ) << QStringLiteral(
"days" ) << QObject::tr(
"day|days",
"list of words separated by | which reference days" ).split(
'|' ) );
230 map.insert( 0 +
WEEKS, QStringList() << QStringLiteral(
"week" ) << QStringLiteral(
"weeks" ) << QObject::tr(
"week|weeks",
"wordlist separated by | which reference weeks" ).split(
'|' ) );
231 map.insert( 0 +
MONTHS, QStringList() << QStringLiteral(
"month" ) << QStringLiteral(
"months" ) << QObject::tr(
"month|months",
"list of words separated by | which reference months" ).split(
'|' ) );
232 map.insert( 0 +
YEARS, QStringList() << QStringLiteral(
"year" ) << QStringLiteral(
"years" ) << QObject::tr(
"year|years",
"list of words separated by | which reference years" ).split(
'|' ) );
234 const auto constList = list;
235 for (
const QString &match : constList )
237 const thread_local QRegularExpression splitRx(
"\\s+" );
238 const QStringList split = match.split( splitRx );
240 const double value = split.at( 0 ).toDouble( &ok );
246 bool matched =
false;
247 QMap<int, QStringList>::const_iterator it = map.constBegin();
248 for ( ; it != map.constEnd(); ++it )
250 const int duration = it.key();
251 const auto constValue = it.value();
252 for (
const QString &name : constValue )
254 if ( match.contains( name, Qt::CaseInsensitive ) )
279 dbg.nospace() <<
"QgsInterval()";
281 dbg.nospace() <<
"QgsInterval(" << interval.
seconds() <<
")";
282 return dbg.maybeSpace();
287 const qint64 mSeconds = dt2.msecsTo( dt1 );
293 return start.addMSecs(
static_cast<qint64
>( interval.
seconds() * 1000.0 ) );
298 const qint64 seconds =
static_cast< qint64
>( date2.daysTo( date1 ) ) * 24 * 60 * 60;
304 const qint64 mSeconds = time2.msecsTo( time1 );
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.
TemporalUnit
Temporal units.
@ TemporalUnknownUnit
Unknown time unit.
@ TemporalSeconds
Seconds.
@ TemporalMinutes
Minutes.
QDebug operator<<(QDebug dbg, const QgsInterval &interval)
Debug string representation of interval.
QgsInterval operator-(const QDateTime &dt1, const QDateTime &dt2)
Returns the interval between two datetimes.
QDateTime operator+(const QDateTime &start, const QgsInterval &interval)
Adds an interval to a datetime.