View Source Ecto.DateTimeRange.NaiveDateTime (Ecto DateTimeRange v1.2.2)

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

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

  schema "things" do
    field :performed_during, Ecto.DateTimeRange.NaiveDateTime
  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

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

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

Link to this section Types

@type t() :: %Ecto.DateTimeRange.NaiveDateTime{
  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

Link to this function

contains?(naive_date_time, time)

View Source
@spec contains?(t(), NaiveDateTime.t()) :: boolean()

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

example

Example

iex> import Ecto.DateTimeRange
iex> range = ~t[2020-01-01T01:00:00..2020-01-02T01:00:00]N
...>
iex> Ecto.DateTimeRange.NaiveDateTime.contains?(range, ~N[2020-01-01T00:59:59Z])
false
iex> Ecto.DateTimeRange.NaiveDateTime.contains?(range, ~N[2020-01-01T01:00:00Z])
true
iex> Ecto.DateTimeRange.NaiveDateTime.contains?(range, ~N[2020-01-02T00:59:59Z])
true
iex> Ecto.DateTimeRange.NaiveDateTime.contains?(range, ~N[2020-01-02T01:00:00Z])
false
iex> Ecto.DateTimeRange.NaiveDateTime.contains?(range, ~N[2020-01-03T01:00:00Z])
false
@spec parse(binary()) :: {:ok, t()} | {:error, term()}

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

example

Example

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

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