View Source Bonfire.Common.Localise.Cldr.DateTime.Relative (Bonfire v0.9.10-classic-beta.169)

Summary

Functions

Returns a {:ok, string} representing a relative time (ago, in) for a given number, Date.t/0 or t:Datetime.t/0. Returns {:error, reason} when errors are detected.

Returns a {:ok, string} representing a relative time (ago, in) for a given number, Date or Datetime or raises an exception on error.

Functions

Link to this function

to_string(time, options \\ [])

View Source
@spec to_string(number() | Date.t() | DateTime.t(), Keyword.t()) ::
  {:ok, String.t()} | {:error, {module(), String.t()}}

Returns a {:ok, string} representing a relative time (ago, in) for a given number, Date.t/0 or t:Datetime.t/0. Returns {:error, reason} when errors are detected.

  • relative is a number or Date/Datetime representing the time distance from now or from options[:relative_to]

  • options is a Keyword list of options which are:

Options

  • :locale is the locale in which the binary is formatted. The default is Cldr.get_locale/0

  • :format is the format of the binary. Style may be :default, :narrow or :short

  • :unit is the time unit for the formatting. The allowable units are :second, :minute, :hour, :day, :week, :month, :year, :mon, :tue, :wed, :thu, :fri, :sat, :sun, :quarter

  • :relative_to is the baseline Date or Datetime from which the difference from relative is calculated when relative is a Date or a DateTime. The default for a Date is Date.utc_today, for a DateTime it is DateTime.utc_now

Notes

When options[:unit] is not specified, MyApp.Cldr.DateTime.Relative.to_string/2 attempts to identify the appropriate unit based upon the magnitude of relative. For example, given a parameter of less than 60, then to_string/2 will assume :seconds as the unit. See unit_from_relative_time/1.

Examples

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string(-1)
{:ok, "1 second ago"}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string(1)
{:ok, "in 1 second"}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string(1, unit: :day)
{:ok, "tomorrow"}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string(1, unit: :day, locale: "fr")
{:ok, "demain"}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string(1, unit: :day, format: :narrow)
{:ok, "tomorrow"}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string(1234, unit: :year)
{:ok, "in 1,234 years"}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string(1234, unit: :year, locale: "fr")
{:ok, "dans 1 234 ans"}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string(31)
{:ok, "in 31 seconds"}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string(~D[2017-04-29], relative_to: ~D[2017-04-26])
{:ok, "in 3 days"}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string(310, format: :short, locale: "fr")
{:ok, "dans 5 min"}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string(310, format: :narrow, locale: "fr")
{:ok, "+5 min"}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string 2, unit: :wed, format: :short, locale: "en"
{:ok, "in 2 Wed."}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string 1, unit: :wed, format: :short
{:ok, "next Wed."}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string -1, unit: :wed, format: :short
{:ok, "last Wed."}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string -1, unit: :wed
{:ok, "last Wednesday"}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string -1, unit: :quarter
{:ok, "last quarter"}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string -1, unit: :mon, locale: "fr"
{:ok, "lundi dernier"}

iex> Bonfire.Common.Localise.Cldr.DateTime.Relative.to_string(~D[2017-04-29], unit: :ziggeraut)
{:error, {Cldr.DateTime.UnknownTimeUnit,
 "Unknown time unit :ziggeraut.  Valid time units are [:day, :fri, :hour, :minute, :mon, :month, :quarter, :sat, :second, :sun, :thu, :tue, :wed, :week, :year]"}}
Link to this function

to_string!(time, options \\ [])

View Source
@spec to_string!(number() | Date.t() | DateTime.t(), Keyword.t()) :: String.t()

Returns a {:ok, string} representing a relative time (ago, in) for a given number, Date or Datetime or raises an exception on error.

Arguments

  • relative is a number or Date/Datetime representing the time distance from now or from options[:relative_to].

  • options is a Keyword list of options.

Options

  • :locale is the locale in which the binary is formatted. The default is Cldr.get_locale/0

  • :format is the format of the binary. Style may be :default, :narrow or :short. The default is :default

  • :unit is the time unit for the formatting. The allowable units are :second, :minute, :hour, :day, :week, :month, :year, :mon, :tue, :wed, :thu, :fri, :sat, :sun, :quarter

  • :relative_to is the baseline Date or Datetime from which the difference from relative is calculated when relative is a Date or a DateTime. The default for a Date is Date.utc_today, for a DateTime it is DateTime.utc_now

See to_string/2