View Source Want.DateTime (want v1.18.0)

Provides conversions to and from Elixir DateTime structs.

Summary

Functions

Cast an input to a DateTime.

Types

@type result() ::
  {:ok, t()}
  | {:error, binary()}
  | {:error, atom()}
  | {:gap, t(), t()}
  | {:ambiguous, t(), t()}
@type t() :: DateTime.t()

Functions

@spec cast(input :: any(), opts :: Keyword.t()) :: result()

Cast an input to a DateTime.

Options

Options

  • :decode - If set to :uri, executes a URI decode on the input value before casting it.
  • :format - If set to :unix_ms, interprets the input as a unix timestamp with millisecond accuracy. Otherwise as seconds since the epoch

Examples

iex> Want.DateTime.cast("2024-11-18T09:51:39.0Z", [])
{:ok, %DateTime{time_zone: "Etc/UTC", zone_abbr: "UTC", utc_offset: 0, std_offset: 0, year: 2024, month: 11, day: 18, hour: 09, minute: 51, second: 39, microsecond: {0, 1}}}

iex> Want.DateTime.cast(1731671182000, format: :unix_ms)
{:ok, %DateTime{time_zone: "Etc/UTC", zone_abbr: "UTC", utc_offset: 0, std_offset: 0, year: 2024, month: 11, day: 15, hour: 11, minute: 46, second: 22}}

iex> Want.DateTime.cast(1731671182, [])
{:ok, %DateTime{time_zone: "Etc/UTC", zone_abbr: "UTC", utc_offset: 0, std_offset: 0, year: 2024, month: 11, day: 15, hour: 11, minute: 46, second: 22}}