NearestDate.find
You're seeing just the function
find
, go back to NearestDate module for more information.
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 tonil
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"}