Locale-sensitive relative time formatting, modelled on
Intl.RelativeTimeFormat.
Produces strings such as "3 days ago", "in 2 hours", "yesterday", or "tomorrow" depending on the value, unit, and locale.
Delegates to Localize.DateTime.Relative for the underlying
formatting.
Summary
Functions
Formats a relative time value.
Formats a relative time value, raising on error.
Formats a relative time value into typed parts.
Formats a relative time value into typed parts, raising on error.
Functions
Formats a relative time value.
Arguments
valueis an integer representing the offset. Negative values indicate the past (for example, -1 for "1 day ago"), positive values indicate the future (for example, 2 for "in 2 hours").unitis the time unit atom::second,:minute,:hour,:day,:week,:month,:year, or:quarter.optionsis a keyword list of options.
Options
:localeis a locale identifier string or atom. The default is the current process locale.:styleis:long,:short, or:narrow. The default is:long.:numericis:alwaysor:auto. When:auto, special forms like "yesterday" and "tomorrow" are used where the locale defines them. When:always, numeric forms like "1 day ago" and "in 1 day" are always used. The default is:auto.
Returns
{:ok, formatted_string}on success.{:error, reason}if the value, unit, or options are invalid.
Examples
iex> Intl.RelativeTimeFormat.format(-1, :day, locale: :en)
{:ok, "yesterday"}
iex> Intl.RelativeTimeFormat.format(2, :hour, locale: :en)
{:ok, "in 2 hours"}
iex> Intl.RelativeTimeFormat.format(-3, :day, locale: :en)
{:ok, "3 days ago"}
iex> Intl.RelativeTimeFormat.format(-1, :day, locale: :en, numeric: :always)
{:ok, "1 day ago"}
Formats a relative time value, raising on error.
Same as format/3 but returns the string directly or raises.
Arguments
valueis an integer offset.unitis the time unit atom.optionsis a keyword list of options.
Returns
- A formatted string.
Examples
iex> Intl.RelativeTimeFormat.format!(-1, :day, locale: :en)
"yesterday"
@spec format_to_parts(integer(), atom(), Keyword.t()) :: {:ok, [%{type: atom(), value: String.t()}]} | {:error, term()}
Formats a relative time value into typed parts.
Modelled on the JS Intl.RelativeTimeFormat.formatToParts().
Named forms ("yesterday") are a single :literal part; numeric
forms tag the number as an :integer part carrying a :unit
key, with the surrounding pattern text as :literal parts.
Arguments
valueis an integer offset.unitis the time unit atom.optionsis a keyword list of options. Accepts the same options asformat/3.
Returns
{:ok, parts}wherepartsis a list of%{type: atom, value: String.t()}maps;:integerparts also carry a:unitkey.{:error, reason}if the value, unit, or options are invalid.
Examples
iex> Intl.RelativeTimeFormat.format_to_parts(-1, :day, locale: :en)
{:ok, [%{type: :literal, value: "yesterday"}]}
iex> Intl.RelativeTimeFormat.format_to_parts(-3, :day, locale: :en)
{:ok, [
%{type: :integer, value: "3", unit: :day},
%{type: :literal, value: " days ago"}
]}
@spec format_to_parts!(integer(), atom(), Keyword.t()) :: [%{type: atom(), value: String.t()}] | no_return()
Formats a relative time value into typed parts, raising on error.
Same as format_to_parts/3 but returns the parts directly or raises.
Arguments
valueis an integer offset.unitis the time unit atom.optionsis a keyword list of options.
Returns
- A list of
%{type: atom, value: String.t()}maps.
Examples
iex> Intl.RelativeTimeFormat.format_to_parts!(-1, :day, locale: :en)
[%{type: :literal, value: "yesterday"}]