24#include <QRegularExpression>
28using namespace Qt::StringLiterals;
40 , mOriginalUnit(
Qgis::TemporalUnit::Seconds )
44 : mSeconds( static_cast<double>( milliseconds.count() ) / 1000.0 )
46 , mOriginalDuration( static_cast<double>( milliseconds.count() ) )
47 , mOriginalUnit(
Qgis::TemporalUnit::Milliseconds )
51 : mSeconds( duration *
QgsUnitTypes::fromUnitToUnitFactor( unit,
Qgis::TemporalUnit::Seconds ) )
53 , mOriginalDuration( duration )
54 , mOriginalUnit( unit )
71 mOriginalDuration =
years;
76 mOriginalDuration =
months;
81 mOriginalDuration =
weeks;
86 mOriginalDuration =
days;
91 mOriginalDuration =
hours;
106 mOriginalDuration = 0;
118 return mOriginalDuration;
120 return mSeconds /
YEARS;
127 mOriginalDuration =
years;
134 return mOriginalDuration;
143 mOriginalDuration =
months;
150 return mOriginalDuration;
152 return mSeconds /
WEEKS;
160 mOriginalDuration =
weeks;
167 return mOriginalDuration;
169 return mSeconds /
DAY;
177 mOriginalDuration =
days;
184 return mOriginalDuration;
186 return mSeconds /
HOUR;
194 mOriginalDuration =
hours;
201 return mOriginalDuration;
225 const thread_local QRegularExpression rx(
"([-+]?\\d*\\.?\\d+\\s+\\S+)", QRegularExpression::CaseInsensitiveOption );
226 const thread_local QRegularExpression rxtime(
".* \\d{1,2}(:)\\d{1,2}(:)\\d{1,2}.*", QRegularExpression::CaseInsensitiveOption );
228 const QRegularExpressionMatch matchtime = rxtime.match(
string );
229 QString modedString = QString(
string );
230 if ( matchtime.hasMatch() )
233 modedString.replace( matchtime.capturedStart( 2 ), 1,
" minutes " );
235 modedString.replace( matchtime.capturedStart( 1 ), 1,
" hours " );
236 modedString.append(
" seconds" );
241 QRegularExpressionMatch match = rx.match( modedString );
242 while ( match.hasMatch() )
244 list << match.captured( 1 );
245 pos = match.capturedStart() + match.capturedLength();
246 match = rx.match( modedString, pos );
249 const thread_local QMap<int, QStringList> map { {
250 { 1, QStringList() << u
"second"_s << u
"seconds"_s << QObject::tr(
"second|seconds",
"list of words separated by | which reference years" ).split(
'|' ) },
251 { 0 +
MINUTE, QStringList() << u
"minute"_s << u
"minutes"_s << QObject::tr(
"minute|minutes",
"list of words separated by | which reference minutes" ).split(
'|' ) },
252 { 0 +
HOUR, QStringList() << u
"hour"_s << u
"hours"_s << QObject::tr(
"hour|hours",
"list of words separated by | which reference minutes hours" ).split(
'|' ) },
253 { 0 +
DAY, QStringList() << u
"day"_s << u
"days"_s << QObject::tr(
"day|days",
"list of words separated by | which reference days" ).split(
'|' ) },
254 { 0 +
WEEKS, QStringList() << u
"week"_s << u
"weeks"_s << QObject::tr(
"week|weeks",
"wordlist separated by | which reference weeks" ).split(
'|' ) },
255 { 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(
'|' ) },
256 { 0 +
YEARS, QStringList() << u
"year"_s << u
"years"_s << QObject::tr(
"year|years",
"list of words separated by | which reference years" ).split(
'|' ) },
259 const thread_local QRegularExpression splitRx(
"\\s+" );
261 for (
const QString &match : std::as_const( list ) )
263 const QStringList split = match.split( splitRx );
265 const double value = split.at( 0 ).toDouble( &ok );
271 bool matched =
false;
272 QMap<int, QStringList>::const_iterator it = map.constBegin();
273 for ( ; it != map.constEnd(); ++it )
275 const int duration = it.key();
276 const QStringList durationIdentifiers = it.value();
277 for (
const QString &identifier : durationIdentifiers )
279 if ( match.contains( identifier, Qt::CaseInsensitive ) )
304 dbg.nospace() <<
"QgsInterval()";
306 dbg.nospace() <<
"QgsInterval(" << interval.
seconds() <<
")";
307 return dbg.maybeSpace();
312 return start.addMSecs(
static_cast<qint64
>( interval.
seconds() * 1000.0 ) );
317 const qint64 seconds =
static_cast< qint64
>( date2.daysTo( date1 ) ) * 24 * 60 * 60;
323 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.