19 #include <QStringList> 38 if ( !mValid && !other.mValid )
40 else if ( mValid && other.mValid )
49 QRegExp rx(
"([-+]?\\d*\\.?\\d+\\s+\\S+)", Qt::CaseInsensitive );
53 while ( ( pos = rx.indexIn(
string, pos ) ) != -1 )
56 pos += rx.matchedLength();
59 QMap<int, QStringList> map;
60 map.insert( 1, QStringList() << QStringLiteral(
"second" ) << QStringLiteral(
"seconds" ) << QObject::tr(
"second|seconds",
"list of words separated by | which reference years" ).split(
'|' ) );
61 map.insert( 0 +
MINUTE, QStringList() << QStringLiteral(
"minute" ) << QStringLiteral(
"minutes" ) << QObject::tr(
"minute|minutes",
"list of words separated by | which reference minutes" ).split(
'|' ) );
62 map.insert( 0 +
HOUR, QStringList() << QStringLiteral(
"hour" ) << QStringLiteral(
"hours" ) << QObject::tr(
"hour|hours",
"list of words separated by | which reference minutes hours" ).split(
'|' ) );
63 map.insert( 0 +
DAY, QStringList() << QStringLiteral(
"day" ) << QStringLiteral(
"days" ) << QObject::tr(
"day|days",
"list of words separated by | which reference days" ).split(
'|' ) );
64 map.insert( 0 +
WEEKS, QStringList() << QStringLiteral(
"week" ) << QStringLiteral(
"weeks" ) << QObject::tr(
"week|weeks",
"wordlist separated by | which reference weeks" ).split(
'|' ) );
65 map.insert( 0 +
MONTHS, QStringList() << QStringLiteral(
"month" ) << QStringLiteral(
"months" ) << QObject::tr(
"month|months",
"list of words separated by | which reference months" ).split(
'|' ) );
66 map.insert( 0 +
YEARS, QStringList() << QStringLiteral(
"year" ) << QStringLiteral(
"years" ) << QObject::tr(
"year|years",
"list of words separated by | which reference years" ).split(
'|' ) );
68 const auto constList = list;
69 for (
const QString &match : constList )
71 QStringList split = match.split( QRegExp(
"\\s+" ) );
73 double value = split.at( 0 ).toDouble( &ok );
80 QMap<int, QStringList>::const_iterator it = map.constBegin();
81 for ( ; it != map.constEnd(); ++it )
83 int duration = it.key();
84 const auto constValue = it.value();
85 for (
const QString &name : constValue )
87 if ( match.contains( name, Qt::CaseInsensitive ) )
96 seconds += value * duration;
112 dbg.nospace() <<
"QgsInterval()";
114 dbg.nospace() <<
"QgsInterval(" << interval.
seconds() <<
")";
115 return dbg.maybeSpace();
120 qint64 mSeconds = dt2.msecsTo( dt1 );
126 return start.addMSecs( static_cast<qint64>( interval.
seconds() * 1000.0 ) );
131 qint64
seconds =
static_cast< qint64
>( date2.daysTo( date1 ) ) * 24 * 60 * 60;
137 qint64 mSeconds = time2.msecsTo( time1 );
static const int YEARS
Seconds per year (average)
bool operator==(QgsInterval other) const
bool isValid() const
Returns true if the interval is valid.
static const int MONTHS
Seconds per month, based on 30 day month.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
double seconds() const
Returns the interval duration in seconds.
static QgsInterval fromString(const QString &string)
Converts a string to an interval.
static const int HOUR
Seconds per hour.
static const int WEEKS
Seconds per week.
QDateTime operator+(const QDateTime &start, QgsInterval interval)
static const int DAY
Seconds per day.
QgsInterval operator-(const QDateTime &dt1, const QDateTime &dt2)
Returns the interval between two datetimes.
QDebug operator<<(QDebug dbg, const QgsInterval &interval)
Debug string representation of interval.
A representation of the interval between two datetime values.
static const int MINUTE
Seconds per minute.
QgsInterval()=default
Default constructor for QgsInterval.