View Source EctoRange.Timestamp (ecto_range v0.2.1)

A custom type for working with the Postgres tsrange type.

iex> range = {~N[2020-10-31 09:30:00], ~N[2020-11-02 10:00:00]}
iex> cs = TestApp.Table.changeset(%TestApp.Table{name: "EctoRange.Timestamp"}, %{tsrange: range})
iex> cs.changes
%{tsrange: %Postgrex.Range{lower: ~N[2020-10-31 09:30:00], upper: ~N[2020-11-02 10:00:00], lower_inclusive: true, upper_inclusive: true}}

tsrange in Postgres is a continuous range, and does not have any equivalent Elixir struct.

casting

Casting

EctoRange.Timestamp provides a couple of conveniences when casting data. All valid data will be cast into a Postgrex.Range.t/0 struct. When supplied to an Ecto.Changeset, the following types are valid

  • {t:timestamp() | t:String.t/0, t:timestamp() | t:String.t/0} can be used to express unbounded ranges, where nil represents an unbounded endpoint

  • Postgrex.Range.t/0 will be treated as a valid range representation

loading

Loading

All data loaded from the database will be normalized into an inclusive range to align with the semantics of Date.Range.t()

Link to this section Summary

Functions

Callback implementation for Ecto.Type.embed_as/1.

Callback implementation for Ecto.Type.equal?/2.

Converts a Postgrex.Range.t() into a normalized form. For bounded ranges, it will make the lower and upper bounds inclusive to align with the semantics of Date.Range.t()

Converts valid NaiveDateTime.t() tuples into a Postgrex.Range.t()

Link to this section Types

@type timestamp() :: nil | NaiveDateTime.t()

Link to this section Functions

Callback implementation for Ecto.Type.embed_as/1.

Callback implementation for Ecto.Type.equal?/2.

Converts a Postgrex.Range.t() into a normalized form. For bounded ranges, it will make the lower and upper bounds inclusive to align with the semantics of Date.Range.t()

iex> range = %Postgrex.Range{lower: ~D[1989-09-22], upper: ~D[2021-03-02], lower_inclusive: true, upper_inclusive: false}
iex> EctoRange.Date.normalize_range(range)
%Postgrex.Range{lower: ~D[1989-09-22], upper: ~D[2021-03-01], lower_inclusive: true, upper_inclusive: true}

iex> range = %Postgrex.Range{lower: ~D[1989-09-21], upper: ~D[2021-03-01], lower_inclusive: false, upper_inclusive: true}
iex> EctoRange.Date.normalize_range(range)
%Postgrex.Range{lower: ~D[1989-09-22], upper: ~D[2021-03-01], lower_inclusive: true, upper_inclusive: true}
Link to this function

to_postgrex_range(range)

View Source
@spec to_postgrex_range(Postgrex.Range.t() | {timestamp(), timestamp()}) ::
  Postgrex.Range.t()

Converts valid NaiveDateTime.t() tuples into a Postgrex.Range.t()

iex> EctoRange.Date.to_postgrex_range({~N[2021-03-01 08:30:00], ~N[2023-03-30 10:30:00]})
%Postgrex.Range{lower: ~N[2021-03-01 08:30:00], upper: ~N[2023-03-30 10:30:00], lower_inclusive: true, upper_inclusive: true}