View Source Pageantry.NaturalDateRange (Pageantry v0.6.0)
Represents a datetime (or date or time) range.
Fields
first
: First datetime in range (inclusive).last
: Last datetime in range (inclusive).
Summary
Functions
Calculates the date end for the specified natural-date tokens and anchor.
Calculates the date start for the specified natural-date tokens and anchor.
Calculates the date start for the relative unit at the given anchor.
Calculates last day of the specified month.
Creates a new range from the specified first and last dates.
Converts the specified string relative value to an atom.
Converts the specified string token key to an atom.
Converts the specified string units to an atom.
Parses the specified natural-date string into a first date-time to last date-time range.
Parses hour number at map key and applies meridian, or returns default.
Parses number at map key, or returns default.
Parses month number at map key, or returns default.
Parses year number at map key, or returns default.
Types
@type natural_date_range_tokens() ::
:month
| :day
| :year
| :hour
| :minute
| :second
| :meridian
| :one_word
| :month_name
| :year_for_month
| :relative
| :quantity
| :units
| :range
@type t() :: %Pageantry.NaturalDateRange{ first: Timex.Types.valid_datetime(), last: Timex.Types.valid_datetime() }
Functions
@spec calc_natural_date_end(map(), DateTime.t(), Pageantry.Prefs.t()) :: DateTime.t()
Calculates the date end for the specified natural-date tokens and anchor.
Examples
iex> import Pageantry.NaturalDateRange
iex> prefs = %Pageantry.Prefs{}
iex> calc_natural_date_end(%{one_word: "today"}, ~U[2001-02-03 04:05:06Z], prefs)
~U[2001-02-03 23:59:59Z]
@spec calc_natural_date_start(map(), DateTime.t(), Pageantry.Prefs.t()) :: DateTime.t()
Calculates the date start for the specified natural-date tokens and anchor.
Examples
iex> import Pageantry.NaturalDateRange
iex> prefs = %Pageantry.Prefs{}
iex> calc_natural_date_start(%{one_word: "today"}, ~U[2001-02-03 04:05:06Z], prefs)
~U[2001-02-03 00:00:00Z]
@spec calc_relative_date_start(atom(), DateTime.t(), Pageantry.Prefs.t()) :: DateTime.t()
Calculates the date start for the relative unit at the given anchor.
Examples
iex> import Pageantry.NaturalDateRange
iex> calc_relative_date_start(:months, ~U[2001-02-03 04:05:06Z], %{})
~U[2001-02-01 00:00:00Z]
Calculates last day of the specified month.
Examples
iex> import Pageantry.NaturalDateRange
iex> last_day_of_month(2000, 1)
31
iex> last_day_of_month(2000, 2)
29
iex> last_day_of_month(2001, 2)
28
@spec new(Timex.Comparable.comparable(), Timex.Comparable.comparable()) :: t()
Creates a new range from the specified first and last dates.
Examples
iex> import Pageantry.NaturalDateRange
iex> new(~D[2001-02-03], ~D[2001-02-13])
%{first: ~D[2001-02-03], last: ~D[2001-02-13]}
@spec normalize_relative(String.t()) :: :last | :past | :prev | :this | :that | :next
Converts the specified string relative value to an atom.
Examples
iex> import Pageantry.NaturalDateRange
iex> normalize_relative("next")
:next
iex> normalize_relative("past")
:prev
@spec normalize_token_key(String.t()) :: natural_date_range_tokens()
Converts the specified string token key to an atom.
Examples
iex> import Pageantry.NaturalDateRange
iex> normalize_token_key("month_name_lo")
:month
Converts the specified string units to an atom.
Examples
iex> import Pageantry.NaturalDateRange
iex> normalize_units("year")
:years
iex> normalize_units("mo")
:months
@spec parse(String.t(), t(), Pageantry.Prefs.t()) :: t()
Parses the specified natural-date string into a first date-time to last date-time range.
Examples
iex> import Pageantry.NaturalDateRange
iex> prefs = %Pageantry.Prefs{now: fn _ -> {:ok, ~U[2001-02-03 04:05:06Z]} end}
iex> parse("foobar", nil, prefs)
nil
iex> parse("today", nil, prefs)
%{first: ~U[2001-02-03 00:00:00Z], last: ~U[2001-02-03 23:59:59Z]}
Parses hour number at map key and applies meridian, or returns default.
Examples
iex> import Pageantry.NaturalDateRange
iex> parse_hour_or_default(%{meridian: "pm"}, :hour, 1)
1
iex> parse_hour_or_default(%{hour: "2"}, :hour, 1)
2
iex> parse_hour_or_default(%{hour: "2", meridian: "pm"}, :hour, 1)
14
Parses number at map key, or returns default.
Examples
iex> import Pageantry.NaturalDateRange
iex> parse_map_number_or_default(%{}, :day, 1)
1
iex> parse_map_number_or_default(%{day: "2"}, :day, 1)
2
Parses month number at map key, or returns default.
Examples
iex> import Pageantry.NaturalDateRange
iex> parse_month_or_default(%{}, :month, 1)
1
iex> parse_month_or_default(%{month: "2"}, :month, 1)
2
iex> parse_month_or_default(%{month: "feb"}, :month, 1)
2
Parses year number at map key, or returns default.
Examples
iex> import Pageantry.NaturalDateRange
iex> parse_year_or_default(%{}, :year, 1)
1
iex> parse_year_or_default(%{year: "00"}, :year, 1)
2000
iex> parse_year_or_default(%{year: "70"}, :year, 1)
1970