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.
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" may be used instead of "1 day ago" and "in 1 day". 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"}
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"