18 #include <QStringList>
33 , mOriginalDuration( seconds )
41 , mOriginalDuration( duration )
42 , mOriginalUnit( unit )
46 QgsInterval::QgsInterval(
double years,
double months,
double weeks,
double days,
double hours,
double minutes,
double seconds )
58 mOriginalDuration =
years;
63 mOriginalDuration =
months;
68 mOriginalDuration =
weeks;
73 mOriginalDuration =
days;
78 mOriginalDuration =
hours;
93 mOriginalDuration = 0;
105 return mOriginalDuration;
107 return mSeconds /
YEARS;
114 mOriginalDuration =
years;
121 return mOriginalDuration;
130 mOriginalDuration =
months;
137 return mOriginalDuration;
139 return mSeconds /
WEEKS;
147 mOriginalDuration =
weeks;
154 return mOriginalDuration;
156 return mSeconds /
DAY;
164 mOriginalDuration =
days;
171 return mOriginalDuration;
173 return mSeconds /
HOUR;
181 mOriginalDuration =
hours;
188 return mOriginalDuration;
212 QRegExp rx(
"([-+]?\\d*\\.?\\d+\\s+\\S+)", Qt::CaseInsensitive );
216 while ( ( pos = rx.indexIn(
string, pos ) ) != -1 )
219 pos += rx.matchedLength();
222 QMap<int, QStringList> map;
223 map.insert( 1, QStringList() << QStringLiteral(
"second" ) << QStringLiteral(
"seconds" ) << QObject::tr(
"second|seconds",
"list of words separated by | which reference years" ).split(
'|' ) );
224 map.insert( 0 +
MINUTE, QStringList() << QStringLiteral(
"minute" ) << QStringLiteral(
"minutes" ) << QObject::tr(
"minute|minutes",
"list of words separated by | which reference minutes" ).split(
'|' ) );
225 map.insert( 0 +
HOUR, QStringList() << QStringLiteral(
"hour" ) << QStringLiteral(
"hours" ) << QObject::tr(
"hour|hours",
"list of words separated by | which reference minutes hours" ).split(
'|' ) );
226 map.insert( 0 +
DAY, QStringList() << QStringLiteral(
"day" ) << QStringLiteral(
"days" ) << QObject::tr(
"day|days",
"list of words separated by | which reference days" ).split(
'|' ) );
227 map.insert( 0 +
WEEKS, QStringList() << QStringLiteral(
"week" ) << QStringLiteral(
"weeks" ) << QObject::tr(
"week|weeks",
"wordlist separated by | which reference weeks" ).split(
'|' ) );
228 map.insert( 0 +
MONTHS, QStringList() << QStringLiteral(
"month" ) << QStringLiteral(
"months" ) << QObject::tr(
"month|months",
"list of words separated by | which reference months" ).split(
'|' ) );
229 map.insert( 0 +
YEARS, QStringList() << QStringLiteral(
"year" ) << QStringLiteral(
"years" ) << QObject::tr(
"year|years",
"list of words separated by | which reference years" ).split(
'|' ) );
231 const auto constList = list;
232 for (
const QString &match : constList )
234 QStringList split = match.split( QRegExp(
"\\s+" ) );
236 double value = split.at( 0 ).toDouble( &ok );
242 bool matched =
false;
243 QMap<int, QStringList>::const_iterator it = map.constBegin();
244 for ( ; it != map.constEnd(); ++it )
246 int duration = it.key();
247 const auto constValue = it.value();
248 for (
const QString &name : constValue )
250 if ( match.contains( name, Qt::CaseInsensitive ) )
275 dbg.nospace() <<
"QgsInterval()";
277 dbg.nospace() <<
"QgsInterval(" << interval.
seconds() <<
")";
278 return dbg.maybeSpace();
283 qint64 mSeconds = dt2.msecsTo( dt1 );
289 return start.addMSecs(
static_cast<qint64
>( interval.
seconds() * 1000.0 ) );
294 qint64 seconds =
static_cast< qint64
>( date2.daysTo( date1 ) ) * 24 * 60 * 60;
300 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.