Timex.Format.DateTime.Formatters.Relative (timex v4.0.0)

Copy Markdown View Source

Localized relative date and time formatting.

The format string must contain {relative}. Date targets compare calendar days; datetime targets compare elapsed time after conversion to UTC.

Elapsed values truncate to whole seconds toward zero, then to whole units within the following ranges. The same thresholds apply in both directions. A month is an approximate 30 days and a year is 360 days in this formatter.

Absolute elapsed timePast outputFuture output
0 whole secondsnownow
1–45 secondsN seconds agoin N seconds
46 seconds to <2 minutes1 minute agoin 1 minute
2 minutes to <1 hourN minutes agoin N minutes
1 hour to <2 hours1 hour agoin 1 hour
2 hours to <24 hoursN hours agoin N hours
24 hours to <48 hoursyesterdaytomorrow
48 hours to <30 daysN days agoin N days
30 days to <60 days1 month agoin 1 month
60 days to <360 daysN months agoin N months
360 days to <720 days1 year agoin 1 year
720 days or moreN years agoin N years

Timex.from_now/1 samples the clock when formatting. A target computed as two days after an earlier clock sample can therefore be less than 48 hours away and render as "tomorrow". Pass the same reference to Timex.from_now/2 or relative_to/3 for deterministic boundaries. Use Date targets when the intended comparison is calendar days rather than elapsed hours.

Summary

Types

relative_date()

Functions

format(date, format_string)

@spec format(relative_date(), String.t()) :: {:ok, String.t()} | {:error, term()}

Formats a date/time as a relative time formatted string.

Date targets (including Erlang date tuples) compare calendar days with today's UTC date. Datetime targets compare elapsed time. Time-only targets are unsupported.

Examples

iex> Elixir.Timex.Format.DateTime.Formatters.Relative.format(Timex.shift(Timex.now, minutes: -1), "{relative}")
{:ok, "1 minute ago"}

format!(date, format_string)

@spec format!(relative_date(), String.t()) :: String.t() | no_return()

Callback implementation for Timex.Format.DateTime.Formatter.format!/2.

lformat(date, format_string, locale)

@spec lformat(relative_date(), String.t(), String.t()) ::
  {:ok, String.t()} | {:error, term()}

Callback implementation for Timex.Format.DateTime.Formatter.lformat/3.

lformat!(date, format_string, locale)

@spec lformat!(relative_date(), String.t(), String.t()) :: String.t() | no_return()

Callback implementation for Timex.Format.DateTime.Formatter.lformat!/3.

relative_to(date, relative_to, format_string)

Formats a target relative to an explicit reference.

A Date target compares calendar dates, ignoring the reference's time of day. An aware reference is first converted to UTC, so its UTC date is used. Datetime targets compare elapsed time, treating naive values as UTC and date references as midnight. Use Date targets for calendar-day labels such as "tomorrow".

relative_to(date, relative_to, format_string, locale)

tokenize(format_string)

@spec tokenize(String.t()) ::
  {:ok, [Timex.Parse.DateTime.Tokenizers.Directive.t()]} | {:error, term()}

Callback implementation for Timex.Format.DateTime.Formatter.tokenize/1.