View Source Ecto.UTCDateTimeRange (Ecto DateTimeRange v0.1.1)

An Ecto.Type wrapping a :tstzrange Postgres column. To the application, it appears as a struct with :start_at and :end_at, with :utc_datetime values.

defmodule Core.Thing do
  use Ecto.Schema
  import Ecto.Changeset

  schema "things" do
    field :performed_during, Ecto.UTCDateTimeRange
  end

  @required_attrs ~w[performed_during]a
  def changeset(data \ %__MODULE__{}, attrs) do
    data
    |> cast(attrs, @required_attrs)
    |> validate_required(@required_attrs)
  end
end

Link to this section Summary

Ecto.Type Callbacks

Converts user-provided data (for example from a form) to the Elixir term.

Converts the Elixir term to the Ecto native type.

Declares than when used in embedded schemas, the type will be dumped before being encoded.

Checks if two terms are equal.

Converts the Ecto native type to the Elixir term.

Declares the native type that will be used in the database.

Functions

Create an Ecto.UTCDateTimeRange from two ISO8601 strings.

Create an Ecto.UTCDateTimeRange from two ISO8601 strings.

Link to this section Types

@type t() :: %Ecto.UTCDateTimeRange{end_at: DateTime.t(), start_at: DateTime.t()}

Link to this section Ecto.Type Callbacks

Converts user-provided data (for example from a form) to the Elixir term.

Converts the Elixir term to the Ecto native type.

Declares than when used in embedded schemas, the type will be dumped before being encoded.

Checks if two terms are equal.

Converts the Ecto native type to the Elixir term.

Declares the native type that will be used in the database.

Link to this section Functions

@spec parse(binary()) :: {:ok, t()} | {:error, term()}

Create an Ecto.UTCDateTimeRange from two ISO8601 strings.

example

Example

iex> Ecto.UTCDateTimeRange.parse("2020-02-02T00:01:00Z - 2020-02-02T00:01:01Z")
{:ok, %Ecto.UTCDateTimeRange{start_at: ~U[2020-02-02T00:01:00Z], end_at: ~U[2020-02-02T00:01:01Z]}}

iex> Ecto.UTCDateTimeRange.parse("2020-02-02T00:01:00Z - later")
{:error, "Unable to parse DateTime(s) from input"}
@spec sigil_t(binary(), list()) :: t() | no_return()

Create an Ecto.UTCDateTimeRange from two ISO8601 strings.

example

Example

iex> import Ecto.UTCDateTimeRange, only: [sigil_t: 2]
iex> ~t(2020-02-02T00:01:00Z - 2020-02-02T00:01:01Z)
%Ecto.UTCDateTimeRange{start_at: ~U[2020-02-02T00:01:00Z], end_at: ~U[2020-02-02T00:01:01Z]}
...>
iex> ~t(hi there)
** (ArgumentError) Unable to parse DateTime(s) from input