Class: QgsInterval

class qgis.core.QgsInterval

Bases: sip.wrapper

A representation of the interval between two datetime values.

New in version 2.16.

QgsInterval() Default constructor for QgsInterval. Creates an invalid interval.

QgsInterval(seconds: float) Constructor for QgsInterval.

Parameters

seconds – duration of interval in seconds

QgsInterval(duration: float, unit: QgsUnitTypes.TemporalUnit) Constructor for QgsInterval, using the specified duration and units.

QgsInterval(years: float, months: float, weeks: float, days: float, hours: float, minutes: float, seconds: float) Constructor for QgsInterval, using the specified years, months, weeks, days, hours, minutes and seconds.

Note

Month units assumes a 30 day month length.

Note

Year units assumes a 365.25 day year length.

New in version 3.14.

QgsInterval(QgsInterval)

Methods

days

Returns the interval duration in days.

fromString

Converts a string to an interval

hours

Returns the interval duration in hours.

isValid

Returns True if the interval is valid.

minutes

Returns the interval duration in minutes.

months

Returns the interval duration in months (based on a 30 day month).

originalDuration

Returns the original interval duration.

originalUnit

Returns the original interval temporal unit.

seconds

Returns the interval duration in seconds.

setDays

Sets the interval duration in days.

setHours

Sets the interval duration in hours.

setMinutes

Sets the interval duration in minutes.

setMonths

Sets the interval duration in months.

setSeconds

Sets the interval duration in seconds.

setValid

Sets whether the interval is valid.

setWeeks

Sets the interval duration in weeks.

setYears

Sets the interval duration in years.

weeks

Returns the interval duration in weeks.

years

Returns the interval duration in years (based on an average year length)

Attributes

DAY

HOUR

MINUTE

MONTHS

WEEKS

YEARS

DAY = 86400
HOUR = 3600
MINUTE = 60
MONTHS = 2592000
WEEKS = 604800
YEARS = 31557600
days(self)float

Returns the interval duration in days.

If the originalUnit() is QgsUnitTypes.TemporalDays then this value will match the exact number of days as returned by originalDuration(), otherwise it will be calculated using the QgsInterval.DAY constant.

See also

setDays()

Return type

float

fromString(string: str)QgsInterval

Converts a string to an interval

Parameters

string (str) – string to parse

Return type

QgsInterval

Returns

interval, or invalid interval if string could not be parsed

hours(self)float

Returns the interval duration in hours.

If the originalUnit() is QgsUnitTypes.TemporalHours then this value will match the exact number of hours as returned by originalDuration(), otherwise it will be calculated using the QgsInterval.HOUR constant.

See also

setHours()

Return type

float

isValid(self)bool

Returns True if the interval is valid.

See also

setValid()

Return type

bool

minutes(self)float

Returns the interval duration in minutes.

If the originalUnit() is QgsUnitTypes.TemporalMinutes then this value will match the exact number of minutes as returned by originalDuration(), otherwise it will be calculated using the QgsInterval.MINUTE constant.

See also

setMinutes()

Return type

float

months(self)float

Returns the interval duration in months (based on a 30 day month).

If the originalUnit() is QgsUnitTypes.TemporalMonths then this value will match the exact number of months as returned by originalDuration(), otherwise it will be calculated using the assumption that a month consists of exactly 30 days.

See also

setMonths()

Return type

float

originalDuration(self)float

Returns the original interval duration.

This original interval duration can be updated through calling QgsInterval setter methods.

See also

originalUnit()

If the original interval duration is not available or interval was set with a mix of units, calling originalUnit() will return QgsUnitTypes.TemporalUnknownUnit

Returns 0.0 if the original duration was not set.

New in version 3.18.

Return type

float

originalUnit(self)QgsUnitTypes.TemporalUnit

Returns the original interval temporal unit.

The interval temporal unit can be set through the QgsInterval constructors or through the available setter methods.

Returns QgsUnitTypes.TemporalUnknownUnit if unit was not set when creating the QgsInterval instance or interval was set with a mix of units.

New in version 3.18.

Return type

QgsUnitTypes.TemporalUnit

seconds(self)float

Returns the interval duration in seconds.

See also

setSeconds()

Return type

float

setDays(self, days: float)

Sets the interval duration in days.

Replaces the interval size and changes the original interval unit and duration, originalDuration() and see originalUnit().

Changes the original unit to QgsUnitTypes.TemporalDays

Parameters

days (float) – duration in days

See also

days()

setHours(self, hours: float)

Sets the interval duration in hours.

Replaces the interval size and changes the original interval unit and duration, originalDuration() and see originalUnit().

The original unit to QgsUnitTypes.TemporalHours

Parameters

hours (float) – duration in hours

See also

hours()

setMinutes(self, minutes: float)

Sets the interval duration in minutes.

Replaces the interval size and changes the original interval unit and duration, originalDuration() and see originalUnit().

Changes the original unit to QgsUnitTypes.TemporalMinutes

Parameters

minutes (float) – duration in minutes

See also

minutes()

setMonths(self, months: float)

Sets the interval duration in months.

Replaces the interval size and changes the original interval unit and duration, originalDuration() and see originalUnit().

Changes the original unit to QgsUnitTypes.TemporalMonths

Parameters

months (float) – duration in months (based on a 30 day month)

See also

months()

setSeconds(self, seconds: float)

Sets the interval duration in seconds.

Replaces the interval size and changes the original interval unit and duration, originalDuration() and see originalUnit().

Changes the original unit to QgsUnitTypes.TemporalSeconds

Parameters

seconds (float) – duration in seconds

See also

seconds()

setValid(self, valid: bool)

Sets whether the interval is valid.

Parameters

valid (bool) – set to True to set the interval as valid.

See also

isValid()

setWeeks(self, weeks: float)

Sets the interval duration in weeks.

Replaces the interval size and changes the original interval unit and duration, originalDuration() and see originalUnit().

Changes the original unit to QgsUnitTypes.TemporalWeeks

Parameters

weeks (float) – duration in weeks

See also

weeks()

setYears(self, years: float)

Sets the interval duration in years.

Replaces the interval size and changes the original interval unit and duration, originalDuration() and see originalUnit().

Changes the original unit to QgsUnitTypes.TemporalYears

Parameters

years (float) – duration in years (based on average year length)

See also

years()

weeks(self)float

Returns the interval duration in weeks.

If the originalUnit() is QgsUnitTypes.TemporalWeeks then this value will match the exact number of weeks as returned by originalDuration(), otherwise it will be calculated using the QgsInterval.WEEKS constant.

See also

setWeeks()

Return type

float

years(self)float

Returns the interval duration in years (based on an average year length)

If the originalUnit() is QgsUnitTypes.TemporalYears then this value will match the exact number of months as returned by originalDuration(), otherwise it will be calculated using the average year length (31557600 seconds).

See also

setYears()

Return type

float