timex v2.0.0 Timex.Date

This module represents all functions specific to creating/manipulating/comparing Dates (year/month/day)

Summary

Functions

Compare two dates returning one of the following values

Calculate time interval between two dates. If the second date comes after the first one in time, return value will be positive; and negative otherwise

Returns a Date representing the date of the UNIX epoch

Converts from a date/time value to a Date struct representing that date

WARNING: This is here to ease the migration to 2.x, but is deprecated

Given an integer value representing days since the reference date (:epoch or :zero), returns a Date struct representing that date

Like from/1, but more explicit about it’s inputs (Erlang date/datetime tuples only)

Given an integer value representing microseconds since the reference date (:epoch or :zero), returns a Date struct representing that date

Given an integer value representing milliseconds since the reference date (:epoch or :zero), returns a Date struct representing that date

Given an integer value representing seconds since the reference date (:epoch or :zero), returns a Date struct representing that date

Given an Erlang timestamp, converts it to a Date struct representing the date of that timestamp

Returns today’s date as a Date struct. If given a timezone, returns whatever “today” is in that timezone

See docs for Timex.set/2 for details

Shifts the given Date based on the provided options. See Timex.shift/2 for more information

Converts a Date to a DateTime in UTC

Convert the date to an integer number of days since Epoch or year 0

Convert a date to an integer number of seconds since Epoch or year 0

See Timex.Date.to_seconds/1

Convert a date to a timestamp value consumable by the Time module

Returns today’s date as a Date struct. If given a timezone, returns whatever “today” is in that timezone

Returns a Date representing the first day of year zero

Types

t :: %Timex.Date{calendar: term, day: term, month: term, year: term}

Functions

compare(date, b)

Specs

compare(Timex.Date.t, Timex.Date.t | :epoch | :zero | :distant_past | :distant_future) ::
  -1 |
  0 |
  1 |
  {:error, term}

Compare two dates returning one of the following values:

  • -1 — the first date comes before the second one
  • 0 — both arguments represent the same date when coalesced to the same timezone.
  • 1 — the first date comes after the second one

See the docs for Timex.compare/2 or Timex.compare/3 for more details.

compare(a, b, granularity)

Specs

compare(Timex.Date.t, Timex.Date.t, :years | :months | :weeks | :days | :hours | :minutes | :seconds | :timestamp) ::
  -1 |
  0 |
  1 |
  {:error, term}
diff(this, other, type)

Specs

diff(Timex.Date.t, Timex.Date.t, :seconds | :minutes | :hours | :days | :weeks | :months | :years) ::
  integer |
  {:error, term}
diff(Timex.Date.t, Timex.Date.t, :timestamp) ::
  Types.timestamp |
  {:error, term}

Calculate time interval between two dates. If the second date comes after the first one in time, return value will be positive; and negative otherwise.

See docs for Timex.diff/3 for more details.

epoch()

Specs

epoch :: Timex.DateTime.t

Returns a Date representing the date of the UNIX epoch

epoch(atom)

Specs

epoch(:seconds) :: Timex.DateTime.t
from(date)

Specs

from(Types.valid_datetime | Types.dtz | Types.phoenix_datetime_select_params) ::
  Timex.Date.t |
  {:error, term}

Converts from a date/time value to a Date struct representing that date

from(value, type, ref \\ :epoch)

WARNING: This is here to ease the migration to 2.x, but is deprecated.

Converts a value of the provided type to a Date struct, relative to the reference date (:epoch or :zero)

from_days(n, ref \\ :epoch)

Specs

from_days(non_neg_integer, :epoch | :zero) ::
  Timex.Date.t |
  {:error, term}

Given an integer value representing days since the reference date (:epoch or :zero), returns a Date struct representing that date

from_erl(arg1)

Like from/1, but more explicit about it’s inputs (Erlang date/datetime tuples only).

from_microseconds(n, ref \\ :epoch)

Specs

from_microseconds(non_neg_integer, :epoch | :zero) ::
  Timex.Date.t |
  {:error, term}

Given an integer value representing microseconds since the reference date (:epoch or :zero), returns a Date struct representing that date

from_millisecond(, )
from_milliseconds(n, ref \\ :epoch)

Specs

from_milliseconds(non_neg_integer, :epoch | :zero) ::
  Timex.Date.t |
  {:error, term}

Given an integer value representing milliseconds since the reference date (:epoch or :zero), returns a Date struct representing that date

from_seconds(n, ref \\ :epoch)

Specs

from_seconds(non_neg_integer, :epoch | :zero) ::
  Timex.Date.t |
  {:error, term}

Given an integer value representing seconds since the reference date (:epoch or :zero), returns a Date struct representing that date

from_timestamp(timestamp, ref \\ :epoch)

Specs

from_timestamp(Types.timestamp, :epoch | :zero) ::
  Timex.Date.t |
  {:error, term}

Given an Erlang timestamp, converts it to a Date struct representing the date of that timestamp

now()

Specs

now :: Timex.Date.t | {:error, term}

Returns today’s date as a Date struct. If given a timezone, returns whatever “today” is in that timezone

now(tz)

Specs

now(Types.valid_timezone) ::
  Timex.DateTime.t |
  {:error, term}
set(date, options)

Specs

set(Timex.Date.t, [{atom, term}]) ::
  Timex.Date.t |
  {:error, term}

See docs for Timex.set/2 for details.

shift(date, options)

Specs

shift(Timex.Date.t, [{atom, term}]) ::
  Timex.Date.t |
  {:error, term}

Shifts the given Date based on the provided options. See Timex.shift/2 for more information.

to_datetime(dt)

Specs

to_datetime(Timex.Date.t) ::
  Timex.DateTime.t |
  {:error, term}

Converts a Date to a DateTime in UTC

to_days(date, ref \\ :epoch)

Specs

to_days(Timex.Date.t, :epoch | :zero) ::
  integer |
  {:error, term}

Convert the date to an integer number of days since Epoch or year 0.

See also Timex.diff/3 if you want to specify an arbitray reference date.

Examples

iex> Timex.date({1970, 1, 15}) |> Elixir.Timex.Date.to_days
14
to_seconds(date, ref \\ :epoch)

Specs

to_seconds(Timex.Date.t, :epoch | :zero) ::
  integer |
  {:error, term}

Convert a date to an integer number of seconds since Epoch or year 0.

See also Timex.diff/3 if you want to specify an arbitrary reference date.

Examples

iex> Timex.date({1999, 1, 2}) |> Elixir.Timex.Date.to_seconds
915235200
to_secs(date)

See Timex.Date.to_seconds/1.

to_secs(date, ref)

See Timex.Date.to_seconds/2.

to_timestamp(date, ref \\ :epoch)

Specs

to_timestamp(Timex.Date.t, :epoch | :zero) ::
  Types.timestamp |
  {:error, term}

Convert a date to a timestamp value consumable by the Time module.

See also diff/2 if you want to specify an arbitrary reference date.

Examples

iex> Elixir.Timex.Date.epoch |> Elixir.Timex.Date.to_timestamp {0,0,0}

today()

Specs

today :: Timex.Date.t | {:error, term}

Returns today’s date as a Date struct. If given a timezone, returns whatever “today” is in that timezone

today(tz)

Specs

today(Types.valid_timezone) ::
  Timex.Date.t |
  {:error, term}
zero()

Specs

zero :: Timex.Date.t

Returns a Date representing the first day of year zero