Tempo.Ecto.Tempo (Tempo SQL v0.2.0)

Copy Markdown View Source

Ecto.ParameterizedType for persisting a bare Tempo.t/0 as a PostgreSQL tstzrange.

A bare Tempo value is an implicit span — ~o"2026Y" spans the whole of 2026, ~o"2026Y-06M" spans June 2026. This type materialises the implicit span via Tempo.to_interval/1 and then delegates to Tempo.Ecto.Interval.

Usage

schema "years" do
  field :reporting_year, Tempo.Ecto.Tempo, resolution: :year
end

Options

  • :resolution — same as Tempo.Ecto.Interval. With :year, loaded values come back as year-resolution Tempos (the closest shape-preserving round-trip available for implicit spans).

Round-trip caveat

Implicit spans do not round-trip as implicit spans without help. A stored ~o"2026Y" loads back as a %Tempo.Interval{} — there is no way to recover "it was just a year token" from a tstzrange alone. Setting resolution: :year gives the closest approximation by returning an interval whose endpoints are year-resolution Tempos. See the storage contract guide.

Tempo values that materialise to an IntervalSet (any value with a recurrence rule) are rejected — use a Tempo.Ecto.IntervalSet column instead.

Summary

Functions

Builds the parameterized-type tuple for use outside a schema.

Functions

cast_type(options \\ [])

Builds the parameterized-type tuple for use outside a schema.

See Tempo.Ecto.Interval.cast_type/1 for the schema-side equivalent and the full discussion.

Arguments

  • options is a keyword list of options.

Options

  • :resolution truncates the materialised endpoints on load to the given component. One of :year, :month, :day, :hour, :minute, or :second. Defaults to :second.

Returns

Examples

iex> Tempo.Ecto.Tempo.cast_type()
{:parameterized, {Tempo.Ecto.Tempo, %{resolution: :second}}}

iex> Tempo.Ecto.Tempo.cast_type(resolution: :day)
{:parameterized, {Tempo.Ecto.Tempo, %{resolution: :day}}}