24#include <QRegularExpression>
28using namespace Qt::StringLiterals;
40 , mOriginalUnit(
Qgis::TemporalUnit::Seconds )
45 : mSeconds( static_cast<double>( milliseconds.count() ) / 1000.0 )
47 , mOriginalDuration( static_cast<double>( milliseconds.count() ) )
48 , mOriginalUnit(
Qgis::TemporalUnit::Milliseconds )
53 : mSeconds( duration *
QgsUnitTypes::fromUnitToUnitFactor( unit,
Qgis::TemporalUnit::Seconds ) )
55 , mOriginalDuration( duration )
56 , mOriginalUnit( unit )
72 mOriginalDuration =
years;
77 mOriginalDuration =
months;
82 mOriginalDuration =
weeks;
87 mOriginalDuration =
days;
92 mOriginalDuration =
hours;
107 mOriginalDuration = 0;
119 return mOriginalDuration;
121 return mSeconds /
YEARS;
128 mOriginalDuration =
years;
135 return mOriginalDuration;
144 mOriginalDuration =
months;
151 return mOriginalDuration;
153 return mSeconds /
WEEKS;
161 mOriginalDuration =
weeks;
168 return mOriginalDuration;
170 return mSeconds /
DAY;
178 mOriginalDuration =
days;
185 return mOriginalDuration;
187 return mSeconds /
HOUR;
195 mOriginalDuration =
hours;
202 return mOriginalDuration;
226 const thread_local QRegularExpression rx(
"([-+]?\\d*\\.?\\d+\\s+\\S+)", QRegularExpression::CaseInsensitiveOption );
227 const thread_local QRegularExpression rxtime(
".* \\d{1,2}(:)\\d{1,2}(:)\\d{1,2}.*", QRegularExpression::CaseInsensitiveOption );
229 const QRegularExpressionMatch matchtime = rxtime.match(
string );
230 QString modedString = QString(
string );
231 if ( matchtime.hasMatch() )
234 modedString.replace( matchtime.capturedStart( 2 ), 1,
" minutes " );
236 modedString.replace( matchtime.capturedStart( 1 ), 1,
" hours " );
237 modedString.append(
" seconds" );
242 QRegularExpressionMatch match = rx.match( modedString );
243 while ( match.hasMatch() )
245 list << match.captured( 1 );
246 pos = match.capturedStart() + match.capturedLength();
247 match = rx.match( modedString, pos );
250 const thread_local QMap<int, QStringList> map{{
251 {1, QStringList() << u
"second"_s << u
"seconds"_s << QObject::tr(
"second|seconds",
"list of words separated by | which reference years" ).split(
'|' )},
252 { 0 +
MINUTE, QStringList() << u
"minute"_s << u
"minutes"_s << QObject::tr(
"minute|minutes",
"list of words separated by | which reference minutes" ).split(
'|' ) },
253 {0 +
HOUR, QStringList() << u
"hour"_s << u
"hours"_s << QObject::tr(
"hour|hours",
"list of words separated by | which reference minutes hours" ).split(
'|' )},
254 {0 +
DAY, QStringList() << u
"day"_s << u
"days"_s << QObject::tr(
"day|days",
"list of words separated by | which reference days" ).split(
'|' )},
255 {0 +
WEEKS, QStringList() << u
"week"_s << u
"weeks"_s << QObject::tr(
"week|weeks",
"wordlist separated by | which reference weeks" ).split(
'|' )},
256 {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(
'|' )},
257 {0 +
YEARS, QStringList() << u
"year"_s << u
"years"_s << QObject::tr(
"year|years",
"list of words separated by | which reference years" ).split(
'|' )},
260 const thread_local QRegularExpression splitRx(
"\\s+" );
262 for (
const QString &match : std::as_const( list ) )
264 const QStringList split = match.split( splitRx );
266 const double value = split.at( 0 ).toDouble( &ok );
272 bool matched =
false;
273 QMap<int, QStringList>::const_iterator it = map.constBegin();
274 for ( ; it != map.constEnd(); ++it )
276 const int duration = it.key();
277 const QStringList durationIdentifiers = it.value();
278 for (
const QString &identifier : durationIdentifiers )
280 if ( match.contains( identifier, Qt::CaseInsensitive ) )
305 dbg.nospace() <<
"QgsInterval()";
307 dbg.nospace() <<
"QgsInterval(" << interval.
seconds() <<
")";
308 return dbg.maybeSpace();
311#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
315 const qint64 mSeconds = dt2.msecsTo( dt1 );
323 return start.addMSecs(
static_cast<qint64
>( interval.
seconds() * 1000.0 ) );
328 const qint64 seconds =
static_cast< qint64
>( date2.daysTo( date1 ) ) * 24 * 60 * 60;
334 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.