View Source Pageantry.NaturalDateRange (Pageantry v0.5.2)

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

Link to this type

natural_date_range_tokens()

View Source
@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

Link to this function

calc_natural_date_end(tokens, anchor, prefs)

View Source
@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]
Link to this function

calc_natural_date_start(tokens, anchor, prefs)

View Source
@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]
Link to this function

calc_relative_date_start(units, anchor, prefs)

View Source
@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]
Link to this function

last_day_of_month(year, month)

View Source
@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

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]}
Link to this function

normalize_relative(value)

View Source
@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
Link to this function

normalize_token_key(key)

View Source
@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
@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
Link to this function

parse(value, default \\ nil, prefs)

View Source
@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]}
Link to this function

parse_hour_or_default(map, key, default)

View Source
@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
Link to this function

parse_map_number_or_default(map, key, default)

View Source
@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
Link to this function

parse_month_or_default(map, key, default)

View Source
@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
Link to this function

parse_year_or_default(map, key, default)

View Source
@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