View Source Ecto.DateTimeRange.UTCDateTime (Ecto DateTimeRange v2.0.0)

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.DateTimeRange.UTCDateTime
  end

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

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

Returns true or false depending on whether the time is falls within the specified range.

Create an Ecto.DateTimeRange.UTCDateTime from two ISO8601 strings.

Ecto.Type Callbacks

cast(arg1)

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

dump(arg1)

Converts the Elixir term to the Ecto native type.

embed_as(_)

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

equal?(first, second)

Checks if two terms are equal.

load(arg1)

Converts the Ecto native type to the Elixir term.

type()

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

Types

t()

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

Functions

contains?(utc_date_time, time)

@spec contains?(t(), DateTime.t()) :: boolean()

Returns true or false depending on whether the time is falls within the specified range.

Example

iex> import Ecto.DateTimeRange
iex> range = ~t[2020-01-01T01:00:00Z..2020-01-02T01:00:00Z]U
...>
iex> Ecto.DateTimeRange.UTCDateTime.contains?(range, ~U[2020-01-01T00:59:59Z])
false
iex> Ecto.DateTimeRange.UTCDateTime.contains?(range, ~U[2020-01-01T01:00:00Z])
true
iex> Ecto.DateTimeRange.UTCDateTime.contains?(range, ~U[2020-01-02T00:59:59Z])
true
iex> Ecto.DateTimeRange.UTCDateTime.contains?(range, ~U[2020-01-02T01:00:00Z])
false
iex> Ecto.DateTimeRange.UTCDateTime.contains?(range, ~U[2020-01-03T01:00:00Z])
false

parse(string)

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

Create an Ecto.DateTimeRange.UTCDateTime from two ISO8601 strings.

Example

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

iex> Ecto.DateTimeRange.UTCDateTime.parse("2020-02-02T00:01:00Z..later")
{:error, "Unable to parse DateTime(s) from input"}