Timex.Interval
Summary
Functions
Return the interval duration, given a unit
Formats the interval as a human readable string
Same as format/3
, but raises a Timex.Interval.FormatError
on failure
Create a new Interval struct
Change the step value for the provided interval
Functions
Return the interval duration, given a unit.
When the unit is one of :secs
, :mins
, :hours
, :days
, :weeks
, :months
, :years
, the result is an integer
.
When the unit is :timestamp
, the result is a tuple representing a valid Timex.Time
.
Example
iex> use Timex …> Interval.new(from: Date.from({2014, 9, 22}), until: [months: 5]) …> |> Interval.duration(:months) 5
iex> use Timex …> Interval.new(from: Date.from({{2014, 9, 22}, {15, 30, 0}}), until: [mins: 20]) …> |> Interval.duration(:timestamp) {0, 1200, 0}
Formats the interval as a human readable string.
Examples
iex> use Timex
...> Interval.new(from: Date.from({2014, 9, 22}), until: [days: 3])
...> |> Interval.format!("%Y-%m-%d %H:%M", :strftime)
"[2014-09-22 00:00, 2014-09-25 00:00)"
iex> use Timex
...> Interval.new(from: Date.from({2014, 9, 22}), until: [days: 3])
...> |> Interval.format!("%Y-%m-%d", :strftime)
"[2014-09-22, 2014-09-25)"
Same as format/3
, but raises a Timex.Interval.FormatError
on failure.
Create a new Interval struct.
Note: By default intervals are right open.
Valid keywords:
from
: The date the interval starts at. Should be a DateTime.until
: Either a DateTime, or a time shift that will be applied to thefrom
date.left_open
: Whether the interval is left open. See explanation below.right_open
: Whether the interval is right open. See explanation below.step
: The step to use when iterating the interval, defaults to[days: 1]
The termsleft_open
and right_open
come from the mathematical concept of intervals, the following
excerpt from Wikipedia gives a good explanation of their meaning:
"An interval is said to be left-open if and only if it has no minimum
(an element that is smaller than all other elements); right-open if it has no maximum;
and open if it has both properties. The interval [0,1) = {x | 0 ≤ x < 1}, for example,
is left-closed and right-open. The empty set and the set of all reals are open intervals,
while the set of non-negative reals, for example, is a right-open but not left-open interval.
The open intervals coincide with the open sets of the real line in its standard topology."
Note: until
shifts delegate to Date.shift
, so the options provided should match its valid options.
Examples
iex> use Timex …> Interval.new(from: Date.from({2014, 9, 22}), until: Date.from({2014, 9, 29})) …> |> Interval.format!(“%Y-%m-%d”, :strftime) “[2014-09-22, 2014-09-29)”
iex> use Timex …> Interval.new(from: Date.from({2014, 9, 22}), until: [days: 7]) …> |> Interval.format!(“%Y-%m-%d”, :strftime) “[2014-09-22, 2014-09-29)”
iex> use Timex …> Interval.new(from: Date.from({2014, 9, 22}), until: [days: 7], left_open: true, right_open: false) …> |> Interval.format!(“%Y-%m-%d”, :strftime) “(2014-09-22, 2014-09-29]”
iex> use Timex …> Interval.new(from: Date.from({{2014, 9, 22}, {15, 30, 0}}), until: [mins: 20], right_open: false) …> |> Interval.format!(“%H:%M”, :strftime) “[15:30, 15:50]“
Change the step value for the provided interval.
The step should be a keyword list valid for use with Timex.Date.shift
.
Examples
iex> use Timex …> Interval.new(from: Date.from({2014, 9, 22}), until: [days: 3], right_open: false) …> |> Interval.with_step([days: 1]) |> Enum.map(&DateFormat.format!(&1, “%Y-%m-%d”, :strftime)) ["2014-09-22", "2014-09-23", "2014-09-24", "2014-09-25"]
iex> use Timex …> Interval.new(from: Date.from({2014, 9, 22}), until: [days: 3], right_open: false) …> |> Interval.with_step([days: 2]) |> Enum.map(&DateFormat.format!(&1, “%Y-%m-%d”, :strftime)) ["2014-09-22", "2014-09-24"]
iex> use Timex …> Interval.new(from: Date.from({2014, 9, 22}), until: [days: 3], right_open: false) …> |> Interval.with_step([days: 3]) |> Enum.map(&DateFormat.format!(&1, “%Y-%m-%d”, :strftime)) ["2014-09-22", "2014-09-25"]