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
See docs for Timex.Comparable.compare/2
or Timex.Comparable.compare/3
See docs for Timex.Comparable.diff/2
or Timex.Comparable.diff/3
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
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
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{...}
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.
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)
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
Converts from an Ecto DateTime tuple to a DateTime struct
Converts from an Erlang datetime tuple (including those with milliseconds) to a DateTime struct
Specs
from_erl(Timex.Types.date | Timex.Types.datetime, Timex.Types.valid_timezone | nil) ::
Timex.DateTime.t |
{:error, term}
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
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
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.
Specs
from_phoenix_datetime_select(Map.t, Timex.Types.valid_timezone) ::
Timex.DateTime.t |
{:error, atom}
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
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
Specs
local :: Timex.DateTime.t
Get current date and time in the local timezone.
See also universal/0
.
Specs
local(date :: Timex.DateTime.t) :: Timex.DateTime.t
Convert a DateTime to the local timezone.
See also universal/1
.
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.
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.
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.
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
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
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}
Specs
today :: Timex.DateTime.t | {:error, term}
Creates a new DateTime struct for today’s date, at the beginning of the day
Specs
today(Timex.Timezone.t | String.t | :utc | :local) ::
Timex.DateTime.t |
{:error, term}
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
Specs
universal(Timex.DateTime.t) :: Timex.DateTime.t
Convert a DateTime to UTC
See also local/1
.
Specs
zero :: Timex.DateTime.t
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