Time (Elixir v1.20.5)

Copy Markdown View Source

A Time struct and functions.

The Time struct contains the fields hour, minute, second and microseconds. New times can be built with the new/4 function or using the ~T (see sigil_T/2) sigil:

iex> ~T[23:00:07.001]
~T[23:00:07.001]

Both new/4 and sigil return a struct where the time fields can be accessed directly:

iex> time = ~T[23:00:07.001]
iex> time.hour
23
iex> time.microsecond
{1000, 3}

The functions on this module work with the Time struct as well as any struct that contains the same fields as the Time struct, such as NaiveDateTime and DateTime. Such functions expect Calendar.time/0 in their typespecs (instead of t/0).

Developers should avoid creating the Time structs directly and instead rely on the functions provided by this module as well as the ones in third-party calendar libraries.

Comparing times

Comparisons in Elixir using ==/2, >/2, </2 and similar are structural and based on the Time struct fields. For proper comparison between times, use the compare/2, after?/2 and before?/2 functions. The existence of the compare/2 function in this module also allows using Enum.min/2 and Enum.max/2 functions to get the minimum and maximum time of an Enum. For example:

iex> Enum.min([~T[23:00:07.001], ~T[10:00:07.001]], Time)
~T[10:00:07.001]

Summary

Functions

Adds the amount_to_add of units to the given time.

Returns true if the first time is strictly later than the second.

Returns true if the first time is strictly earlier than the second.

Compares two time structs.

Converts given time to a different calendar.

Similar to Time.convert/2, but raises an ArgumentError if the conversion between the two calendars is not possible.

Returns the difference between two times, considering only the hour, minute, second and microsecond.

Converts an Erlang time tuple to a Time struct.

Converts an Erlang time tuple to a Time struct.

Parses the extended "Local time" format described by ISO 8601:2019.

Parses the extended "Local time" format described by ISO 8601:2019.

Converts a number of seconds after midnight to a Time struct.

Shifts given time by duration according to its calendar.

Converts given time to an Erlang time tuple.

Converts a Time struct to a number of seconds after midnight.

Converts the given time to a string.

Returns the given time with the microsecond field truncated to the given precision (:microsecond, millisecond or :second).

Returns the current time in UTC.

Returns the current time in UTC, supporting a precision and a specific calendar.

Types

t()

@type t() :: %Time{
  calendar: Calendar.calendar(),
  hour: Calendar.hour(),
  microsecond: Calendar.microsecond(),
  minute: Calendar.minute(),
  second: Calendar.second()
}

Functions

add(time, amount_to_add, unit \\ :second)

(since 1.6.0)
@spec add(Calendar.time(), integer(), :hour | :minute | System.time_unit()) :: t()

Adds the amount_to_add of units to the given time.

Prefer shift/2

Prefer shift/2 over add/3, as it offers a more ergonomic API.

add/3 always considers the unit to be computed according to the Calendar.ISO.

Accepts an amount_to_add in any unit. unit can be :hour, :minute, :second or any subsecond precision from System.time_unit/0 for convenience but ultimately they are all converted to microseconds. Negative values will move backwards in time and the default precision is :second.

Note the result value represents the time of day, meaning that it is cyclic, for instance, it will never go over 24 hours for the ISO calendar.

Examples

iex> Time.add(~T[10:00:00], 27000)
~T[17:30:00]
iex> Time.add(~T[11:00:00.005], 2400)
~T[11:40:00.005]
iex> Time.add(~T[00:00:00.000], 86_399_999, :millisecond)
~T[23:59:59.999]

Negative values are allowed:

iex> Time.add(~T[23:00:00], -60)
~T[22:59:00]

Note that the time is cyclic:

iex> Time.add(~T[17:10:05], 86400)
~T[17:10:05]

Hours and minutes are also supported:

iex> Time.add(~T[17:10:05], 2, :hour)
~T[19:10:05]
iex> Time.add(~T[17:10:05], 30, :minute)
~T[17:40:05]

This operation merges the precision of the time with the given unit:

iex> result = Time.add(~T[00:29:10], 21, :millisecond)
~T[00:29:10.021]
iex> result.microsecond
{21000, 3}

after?(time1, time2)

(since 1.15.0)
@spec after?(Calendar.time(), Calendar.time()) :: boolean()

Returns true if the first time is strictly later than the second.

Examples

iex> Time.after?(~T[16:04:28], ~T[16:04:16])
true
iex> Time.after?(~T[16:04:16], ~T[16:04:16])
false
iex> Time.after?(~T[16:04:16.001], ~T[16:04:16.01])
false

before?(time1, time2)

(since 1.15.0)
@spec before?(Calendar.time(), Calendar.time()) :: boolean()

Returns true if the first time is strictly earlier than the second.

Examples

iex> Time.before?(~T[16:04:16], ~T[16:04:28])
true
iex> Time.before?(~T[16:04:16], ~T[16:04:16])
false
iex> Time.before?(~T[16:04:16.01], ~T[16:04:16.001])
false

compare(time1, time2)

(since 1.4.0)
@spec compare(Calendar.time(), Calendar.time()) :: :lt | :eq | :gt

Compares two time structs.

Returns :gt if first time is later than the second and :lt for vice versa. If the two times are equal :eq is returned.

Examples

iex> Time.compare(~T[16:04:16], ~T[16:04:28])
:lt
iex> Time.compare