View Source Ecto.DateTimeRange.Time (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 t:Time.t() values.

This type does not do any time zone conversions--times going in will be times coming out.

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

  schema "things" do
    field :performed_during, Ecto.DateTimeRange.Time
  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. Note: this assumes that the range is in the same time zone as whatever time or date time it is compared against.

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

Link to this section Types

@type t() :: %Ecto.DateTimeRange.Time{end_at: Time.t(), start_at: Time.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 contains?(t(), DateTime.t() | NaiveDateTime.t() | Time.t()) :: boolean()

Returns true or false depending on whether the time is falls within the specified range. Note: this assumes that the range is in the same time zone as whatever time or date time it is compared against.

example

Example

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

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

example

Example

iex> Ecto.DateTimeRange.Time.parse("00:01:00..00:01:01")
{:ok, %Ecto.DateTimeRange.Time{start_at: ~T[00:01:00], end_at: ~T[00:01:01]}}

iex> Ecto.DateTimeRange.Time.parse("00:01:00..later")
{:error, "Unable to parse Time(s) from input"}