timex v2.1.5 Timex.DateTime

A type which represents a date and time with timezone information (optional, UTC will be assumed for date/times with no timezone information provided).

Functions that produce time intervals use UNIX epoch (or simly Epoch) as the default reference date. Epoch is defined as UTC midnight of January 1, 1970.

Time intervals in this module don’t account for leap seconds.

Summary

Functions

See docs for Timex.Comparable.compare/2 or Timex.Comparable.compare/3

See docs for Timex.Comparable.diff/2 or Timex.Comparable.diff/3

Get a DateTime representing the first moment of the UNIX epoch (1970/1/1)

Time interval since year 0 of Epoch expressed in the specified units

Construct a date from an Erlang date or datetime value

Like from/1, but allows providing a timezone to assume/convert for the input date/time

Construct a date from a time interval since Epoch or year 0

Converts an integer value representing days since the reference date (:epoch or :zero) to a DateTime struct representing that moment in time

Converts from an Ecto DateTime tuple to a DateTime struct

Converts from an Erlang datetime tuple (including those with milliseconds) to a DateTime struct

Converts an integer representing microseconds since the reference date (:epoch or :zero) to a DateTime struct representing that moment in time

Converts an integer representing milliseconds since the reference date (:epoch or :zero) to a DateTime struct representing that moment in time

Converts the value of a Phoenix date/time field to a DateTime struct

Converts an integer value representing seconds since the reference date (:epoch or :zero) to a DateTime struct representing that moment in time

Converts an Erlang timestamp to a DateTime struct representing that moment in time

Get current date and time in the local timezone

Convert a DateTime to the local timezone

Get the current date and time

Get representation of the current date and time in seconds or days since Epoch

Return a new DateTime with the specified fields replaced by new values

Shifts the given DateTime based on a series of options

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.DateTime.to_seconds/1

See Timex.DateTime.to_seconds/2

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

Creates a new DateTime struct for today’s date, at the beginning of the day

Get the current date and time in UTC

Convert a DateTime to UTC

Get a DateTime representing the first moment of the first day of year zero (:calendar module’s default reference date)

Types

t :: %Timex.DateTime{calendar: term, day: term, hour: term, millisecond: term, minute: term, month: term, second: term, timezone: term, year: term}

Functions

compare(a, b)

See docs for Timex.Comparable.compare/2 or Timex.Comparable.compare/3

compare(a, b, granularity)

See Timex.Comparable.compare/3.

diff(a, b)

See docs for Timex.Comparable.diff/2 or Timex.Comparable.diff/3

diff(a, b, granularity)

See Timex.Comparable.diff/3.

epoch()

Specs

epoch :: Timex.DateTime.t

Get a DateTime representing the first moment of the UNIX epoch (1970/1/1).

Timex uses the UNIX epoch as it’s default reference date, but this can be overridden on a per-function basis where applicable.

See also zero/0.

Examples

iex> use Timex
...> date = %DateTime{year: 1970, month: 1, day: 1, timezone: %TimezoneInfo{}}
...> Elixir.Timex.DateTime.epoch === date
true
epoch(arg1)

Specs

epoch(:seconds | :days) :: integer
epoch(:timestamp) :: Timex.Types.timestamp

Time interval since year 0 of Epoch expressed in the specified units.

Examples

iex> Elixir.Timex.DateTime.epoch(:timestamp)
{0,0,0}
iex> Elixir.Timex.DateTime.epoch(:seconds)
62167219200
from(date)

Specs

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

Construct a date from an Erlang date or datetime value.

You may specify the date’s time zone as the second argument. If the argument is omitted, UTC time zone is assumed.

When passing {year, month, day} as the first argument, the resulting date will indicate midnight of that day in the specified timezone (UTC by default).

NOTE: When using from the input value is normalized to prevent invalid dates from being accidentally introduced. Use set with validate: false, or create the %DateTime{} by hand if you do not want normalization.

Examples

> DateTime.from(:erlang.universaltime)             #=> %DateTime{...}
> DateTime.from(:erlang.localtime)                 #=> %Datetime{...}
> DateTime.from(:erlang.localtime, :local)         #=> %DateTime{...}
> DateTime.from({2014,3,16}, "America/Chicago")    #=> %DateTime{...}
> DateTime.from(phoenix_datetime_select_params)    #=> %DateTime{...}
from(date, tz)

Specs

from(Timex.Types.valid_datetime | Timex.Types.phoenix_datetime_select_params, Timex.Types.valid_timezone) ::
  Timex.DateTime.t |
  Timex.AmbiguousDateTime.t |
  {:error, term}

Like from/1, but allows providing a timezone to assume/convert for the input date/time.

from(timestamp, atom, ref)

Construct a date from a time interval since Epoch or year 0.

UTC time zone is assumed. This assumption can be modified by setting desired time zone using set/3 after the date is constructed.

Examples

> DateTime.from(13, :seconds)
> DateTime.from(13, :days, :zero)
> DateTime.from(Time.now, :timestamp)
from_days(days, ref \\ :epoch)

Specs

from_days(non_neg_integer, :epoch | :zero) ::
  Timex.DateTime.t |
  {:error, atom}

Converts an integer value representing days since the reference date (:epoch or :zero) to a DateTime struct representing that moment in time

from_ecto(date, tz \\ %Timex.TimezoneInfo{})

Converts from an Ecto DateTime tuple to a DateTime struct

from_erl(date)

Converts from an Erlang datetime tuple (including those with milliseconds) to a DateTime struct

from_erl(date, tz)

Specs

from_erl(Timex.Types.date | Timex.Types.datetime, Timex.Types.valid_timezone | nil) ::
  Timex.DateTime.t |
  {:error, term}
from_microseconds(us, type \\ :epoch)

Specs

from_microseconds(non_neg_integer, :epoch | :zero) :: Timex.DateTime.t :: {:error, atom}

Converts an integer representing microseconds since the reference date (:epoch or :zero) to a DateTime struct representing that moment in time

from_milliseconds(ms, type \\ :epoch)

Specs

from_milliseconds(non_neg_integer, :epoch | :zero) :: Timex.DateTime.t :: {:error, atom}

Converts an integer representing milliseconds since the reference date (:epoch or :zero) to a DateTime struct representing that moment in time

from_phoenix_datetime_select(dt)

Specs

from_phoenix_datetime_select(Map.t) ::
  Timex.DateTime.t |
  {:error, atom}

Converts the value of a Phoenix date/time field to a DateTime struct.

An optional timezone provided as a second parameter sets the timezone of the DateTime.

from_phoenix_datetime_select(dt, tz)

Specs

from_phoenix_datetime_select(Map.t, Timex.Types.valid_timezone) ::
  Timex.DateTime.t |
  {:error, atom}
from_seconds(s, ref \\ :epoch)

Specs

from_seconds(non_neg_integer, :epoch | :zero) :: Timex.DateTime.t :: {:error, atom}

Converts an integer value representing seconds since the reference date (:epoch or :zero) to a DateTime struct representing that moment in time

from_timestamp(timestamp, ref \\ :epoch)

Specs

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

Converts an Erlang timestamp to a DateTime struct representing that moment in time

local()

Specs

local :: Timex.DateTime.t

Get current date and time in the local timezone.

See also universal/0.

local(date)

Specs

Convert a DateTime to the local timezone.

See also universal/1.

now()

Specs

Get the current date and time.

now(tz)

Specs

now(:secs | :days) :: integer
now(Timex.TimezoneInfo.t | String.t | :utc | :local) :: Timex.DateTime.t

Get representation of the current date and time in seconds or days since Epoch.

set(date, options)

Specs

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

Return a new DateTime with the specified fields replaced by new values.

Values are automatically validated and clamped to good values by default. If you wish to skip validation, perhaps for performance reasons, pass validate: false.

Values are applied in order, so if you pass [datetime: dt, date: d], the date value from date will override datetime’s date value.

shift(datetime, shifts)

Specs

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

Shifts the given DateTime based on a series of options.

See docs for Timex.shift/2 for details.

to_days(date, reference \\ :epoch)

Specs

to_days(Timex.DateTime.t, :epoch | :zero) ::
  integer |
  {:error, atom}

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

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

Examples

iex> Timex.datetime({1970, 1, 15}) |> Elixir.Timex.DateTime.to_days
14
to_seconds(date, reference \\ :epoch, options \\ [utc: true])

Specs

to_seconds(Timex.DateTime.t, :epoch | :zero, [{:utc, false | true}]) ::
  integer |
  {:error, atom}

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

With to_seconds/3, you can also specify an option utc: false | true, which controls whether the DateTime is converted to UTC prior to calculating the number of seconds from the reference date. By default, UTC conversion is enabled.

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

Examples

iex> Timex.datetime({{1999, 1, 2}, {12,13,14}}) |> Elixir.Timex.DateTime.to_seconds
915279194
to_secs(date)

See Timex.DateTime.to_seconds/1.

to_secs(date, ref)

See Timex.DateTime.to_seconds/2.

to_secs(date, ref, options)

See Timex.DateTime.to_seconds/3.

to_timestamp(date, reference \\ :epoch)

Specs

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

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.DateTime.epoch |> Elixir.Timex.DateTime.to_timestamp
{0,0,0}
today()

Specs

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

Creates a new DateTime struct for today’s date, at the beginning of the day

today(tz)

Specs

today(Timex.Timezone.t | String.t | :utc | :local) ::
  Timex.DateTime.t |
  {:error, term}
universal()

Specs

universal :: Timex.DateTime.t

Get the current date and time in UTC.

See also local/0. Delegates to now/0, since they are identical in behavior

universal(date)

Specs

Convert a DateTime to UTC

See also local/1.

zero()

Specs

Get a DateTime representing the first moment of the first day of year zero (:calendar module’s default reference date).

See also epoch/0.

Examples

iex> use Timex
...> date = %DateTime{year: 0, month: 1, day: 1, timezone: %TimezoneInfo{}}
...> Elixir.Timex.DateTime.zero === date
true