24#include <QRegularExpression>
35 , mOriginalDuration( seconds )
43 , mOriginalDuration( duration )
44 , mOriginalUnit( unit )
48QgsInterval::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 );
215 const thread_local QRegularExpression rxtime(
".* \\d{1,2}(:)\\d{1,2}(:)\\d{1,2}.*", QRegularExpression::CaseInsensitiveOption );
217 const QRegularExpressionMatch matchtime = rxtime.match(
string );
218 QString modedString = QString(
string );
219 if ( matchtime.hasMatch() )
222 modedString.replace( matchtime.capturedStart( 2 ), 1,
" minutes " );
224 modedString.replace( matchtime.capturedStart( 1 ), 1,
" hours " );
225 modedString.append(
" seconds" );
230 QRegularExpressionMatch match = rx.match( modedString );
231 while ( match.hasMatch() )
233 list << match.captured( 1 );
234 pos = match.capturedStart() + match.capturedLength();
235 match = rx.match( modedString, pos );
238 const thread_local QMap<int, QStringList> map{{
239 {1, QStringList() << QStringLiteral(
"second" ) << QStringLiteral(
"seconds" ) << QObject::tr(
"second|seconds",
"list of words separated by | which reference years" ).split(
'|' )},
240 { 0 +
MINUTE, QStringList() << QStringLiteral(
"minute" ) << QStringLiteral(
"minutes" ) << QObject::tr(
"minute|minutes",
"list of words separated by | which reference minutes" ).split(
'|' ) },
241 {0 +
HOUR, QStringList() << QStringLiteral(
"hour" ) << QStringLiteral(
"hours" ) << QObject::tr(
"hour|hours",
"list of words separated by | which reference minutes hours" ).split(
'|' )},
242 {0 +
DAY, QStringList() << QStringLiteral(
"day" ) << QStringLiteral(
"days" ) << QObject::tr(
"day|days",
"list of words separated by | which reference days" ).split(
'|' )},
243 {0 +
WEEKS, QStringList() << QStringLiteral(
"week" ) << QStringLiteral(
"weeks" ) << QObject::tr(
"week|weeks",
"wordlist separated by | which reference weeks" ).split(
'|' )},
244 {0 +
MONTHS, QStringList() << QStringLiteral(
"month" ) << QStringLiteral(
"months" ) << QStringLiteral(
"mon" ) << QObject::tr(
"month|months|mon",
"list of words separated by | which reference months" ).split(
'|' )},
245 {0 +
YEARS, QStringList() << QStringLiteral(
"year" ) << QStringLiteral(
"years" ) << QObject::tr(
"year|years",
"list of words separated by | which reference years" ).split(
'|' )},
248 const thread_local QRegularExpression splitRx(
"\\s+" );
250 for (
const QString &match : std::as_const( list ) )
252 const QStringList split = match.split( splitRx );
254 const double value = split.at( 0 ).toDouble( &ok );
260 bool matched =
false;
261 QMap<int, QStringList>::const_iterator it = map.constBegin();
262 for ( ; it != map.constEnd(); ++it )
264 const int duration = it.key();
265 const QStringList durationIdentifiers = it.value();
266 for (
const QString &identifier : durationIdentifiers )
268 if ( match.contains( identifier, Qt::CaseInsensitive ) )
293 dbg.nospace() <<
"QgsInterval()";
295 dbg.nospace() <<
"QgsInterval(" << interval.
seconds() <<
")";
296 return dbg.maybeSpace();
301 const qint64 mSeconds = dt2.msecsTo( dt1 );
307 return start.addMSecs(
static_cast<qint64
>( interval.
seconds() * 1000.0 ) );
312 const qint64 seconds =
static_cast< qint64
>( date2.daysTo( date1 ) ) * 24 * 60 * 60;
318 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.