PostgresInterval class final

A Postgres interval: a span of months, days and microseconds kept apart, the way Postgres stores it.

A month is 28 to 31 days and a day is 23 to 25 hours across a daylight saving change, so none of the three converts exactly into the others and Postgres keeps them separate: date '2024-01-31' + interval '1 month' is February 29 while + interval '30 days' is March 1. A Duration cannot hold that distinction, which is why an interval column is not one.

const PostgresInterval(years: 1, months: 2, days: 3, hours: 4).literal
// 1 year 2 mons 3 days 04:00:00
PostgresInterval.parse('1 mon').toDuration()   // 30 days
PostgresInterval.parse('P1Y2M3DT4H5M6S')       // the iso_8601 style

parse reads every IntervalStyle Postgres can emit, postgres, postgres_verbose, sql_standard and iso_8601; literal renders the postgres style, which Postgres accepts whatever its IntervalStyle is.

Implemented types
Annotations
  • @experimental

Constructors

PostgresInterval({int years = 0, int months = 0, int weeks = 0, int days = 0, int hours = 0, int minutes = 0, int seconds = 0, int milliseconds = 0, int microseconds = 0})
The interval of the given components, folded into months, days and microseconds: years count as 12 months, weeks as 7 days, and the time components add up into microseconds.
const
PostgresInterval.fromDuration(Duration duration)
The interval of exactly duration, with no months or days.
PostgresInterval.parse(String literal)
Parses an interval literal in any of the output styles of Postgres: 1 year 2 mons 3 days 04:05:06.789 (postgres), @ 1 year 2 mons 3 days 4 hours 5 mins 6.789 secs and @ 1 day ago (postgres_verbose), 1-2 3 4:05:06.789, -3 4:05:06 and +1-2 -3 +4:05:06 (sql_standard) and P1Y2M3DT4H5M6.789S (iso_8601). The unit words may also be spelled out, so 3 hours 30 minutes parses too.
factory

Properties

days → int
The calendar days, kept apart from months because a month has no fixed number of them.
final
hashCode → int
The hash code for this object.
no setteroverride
literal → String
The literal in the postgres output style, which Postgres accepts on input whatever its IntervalStyle is: 1 year 2 mons 3 days 04:05:06.789, with a bare 00:00:00 for zero.
no setteroverride
microseconds → int
The clock time, kept apart from days because a day has no fixed number of hours across daylight saving changes.
final
months → int
The calendar months, with 12 to a year.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

compareTo(PostgresInterval other) → int
Orders intervals the way Postgres compares them: every month counts as 30 days and every day as 24 hours, so 30 days and 1 mon compare equal without being equal.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDuration() → Duration
The interval as one span of time, counting a year as 365.25 days and a remaining month as 30, the same convention extract(epoch from …) applies in Postgres.
toString() → String
A string representation of this object.
override

Operators

operator ==(Object other) → bool
The equality operator.
override

Constants

zero → const PostgresInterval
The interval of no length at all.