NearestDate (nearest_date v0.2.0) View Source

NearestDate is a micro library to help finding the date with the smallest delta to a given date.

Link to this section Summary

Functions

Returns the datetime from the given list with the smallest delta to the given target datetime.

Link to this section Types

Specs

direction() :: :future | :past | :both

Link to this section Functions

Link to this function

find(timestamps, target, opts \\ [])

View Source

Specs

find(list_of_timestamps(), timestamp_or_date(), Keyword.t()) ::
  {:ok, timestamp_or_date()} | {:error, NearestDate.Error.t() | Exception.t()}

Returns the datetime from the given list with the smallest delta to the given target datetime.

Options

  • :direction - (:future, :past) constrains the search space in one direction. Defaults to nil which will search both future and past.

Examples

iex> list_of_dates = ["2019-12-31T01:00:00+02:00", "2020-03-01T02:00:00+02:00"]
iex> NearestDate.find(list_of_dates, "2020-01-01T00:00:00+02:00")
{:ok, "2019-12-31T01:00:00+02:00"}

iex> list_of_dates = ["2020-02-01T01:00:00+02:00", "2020-03-01T02:00:00+02:00"]
iex> NearestDate.find(list_of_dates, "2020-01-01T00:00:00+02:00", direction: :future)
{:ok, "2020-02-01T01:00:00+02:00"}

iex> list_of_dates = ["2019-02-01T01:00:00+02:00", "2018-03-01T02:00:00+02:00"]
iex> NearestDate.find(list_of_dates, "2020-01-01T00:00:00+02:00", direction: :future)
{:error, %NearestDate.Error{reason: :not_found}}

iex> list_of_dates = ["2019-02-01T01:00:00+02:00", "2016-03-01T02:00:00+02:00"]
iex> NearestDate.find(list_of_dates, "2020-01-01T00:00:00+02:00", direction: :past)
{:ok, "2019-02-01T01:00:00+02:00"}
Link to this function

find!(timestamps, target, opts \\ [])

View Source

Specs

find!(list_of_timestamps(), timestamp_or_date(), Keyword.t()) ::
  timestamp_or_date() | no_return()