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

natural_date_range_tokens()

@type natural_date_range_tokens() ::
  :month
  | :day
  | :year
  | :hour
  | :minute
  | :second
  | :meridian
  | :one_word
  | :month_name
  | :year_for_month
  | :relative
  | :quantity
  | :units
  | :range

t()

@type t() :: %Pageantry.NaturalDateRange{
  first: Timex.Types.valid_datetime(),
  last: Timex.Types.valid_datetime()
}

Functions

calc_natural_date_end(tokens, anchor, prefs)

@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]

calc_natural_date_start(tokens, anchor, prefs)

@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]

calc_relative_date_start(units, anchor, prefs)

@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]

last_day_of_month(year, month)

@spec last_day_of_month(integer(), integer()) :: integer()

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

new(first, last)

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]}

normalize_relative(value)

@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

normalize_token_key(key)

@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

normalize_units(value)

@spec normalize_units(String.t()) :: atom()

Converts the specified string units to an atom.

Examples

iex> import Pageantry.NaturalDateRange
iex> normalize_units("year")
:years
iex> normalize_units("mo")
:months

parse(value, default \\ nil, prefs)

@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]}

parse_hour_or_default(map, key, default)

@spec parse_hour_or_default(map(), atom(), integer()) :: integer()

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

parse_map_number_or_default(map, key, default)

@spec parse_map_number_or_default(map(), atom(), integer()) :: integer()

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

parse_month_or_default(map, key, default)

@spec parse_month_or_default(map(), atom(), integer()) :: integer()

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

parse_year_or_default(map, key, default)

@spec parse_year_or_default(map(), atom(), integer()) :: integer()

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