View Source Tempus.Guards (Tempus v0.11.0)
Handy guards to simplify pattern matching slots
Summary
Functions
Guard to compare two instances of Tempus.Slot.origin/0
Guard to validate the slot covers the origin passed as the first argument
Guard to validate one slot ovelaps another
Guard to validate that the term given is actually a Tempus.Slot.origin/0
or
a function which might be used as a slot locator.
Guard to validate that the term given is actually a Tempus.Slot.origin/0
Guard to validate whether the DateTime.t/0
given as the first argument
is the border of the slot.
Guard to validate whether two slots are equal
Guard to validate whether the slot is nil
(has neither end set.)
Guard to validate whether the slot is open (has either end not set)
Helper to validate one slot overlaps another in delta. Unlike guards,
this function does not expect arguments in the correct order, and would return
true
if the slots overlap even if s2
comes before s1
.
Functions
@spec is_coming_before(Date.t() | DateTime.t(), Date.t() | DateTime.t()) :: boolean()
@spec is_coming_before(Tempus.Slot.t(), Tempus.Slot.t()) :: boolean()
Guard to compare two instances of Tempus.Slot.origin/0
Examples
iex> import Tempus.Guards, only: [is_coming_before: 2]
...> is_coming_before(~D[2023-04-10], ~U[2023-04-11T00:00:00.000000Z])
true
iex> is_coming_before(~D[2023-04-10], ~D[2023-04-10])
false
@spec is_covered(Tempus.Slot.origin(), Tempus.Slot.t()) :: boolean()
Guard to validate the slot covers the origin passed as the first argument
Examples
iex> import Tempus.Guards, only: [is_covered: 2]
...> import Tempus.Sigils
...> {from, to} = {~U[2023-04-10 00:00:00Z], ~U[2023-04-10 00:59:59Z]}
...> s = %Tempus.Slot{from: from, to: to}
...> is_covered(from, s) and is_covered(to, s)
true
iex> s1 = ~I[2023-04-10 00:00:00Z|2023-04-11 00:00:00Z]
...> s2 = Tempus.Slot.wrap(~D|2023-04-10|)
...> is_covered(s1, s2)
false
iex> s1 = ~I[2023-04-10 00:00:00Z|2023-04-11 00:00:00Z]
...> s2 = Tempus.Slot.wrap(~D|2023-04-10|)
...> is_covered(s1, s2)
false
@spec is_joint(Tempus.Slot.t(), Tempus.Slot.t()) :: boolean()
Guard to validate one slot ovelaps another
Examples
iex> import Tempus.Guards, only: [is_joint: 2]
...> import Tempus.Sigils
...> s1 = ~I[2023-04-09 23:00:00Z|2023-04-10 00:59:59Z]
...> s2 = Tempus.Slot.wrap(~D|2023-04-10|)
...> is_joint(s1, s2)
true
iex> s1 = ~I[2023-04-09 23:00:00Z|2023-04-10 00:00:00Z]
...> s2 = Tempus.Slot.wrap(~D|2023-04-10|)
...> is_joint(s1, s2)
true
@spec is_locator(Tempus.Slot.origin() | (Tempus.Slot.t() -> boolean()) | any()) :: boolean()
Guard to validate that the term given is actually a Tempus.Slot.origin/0
or
a function which might be used as a slot locator.
Examples
iex> import Tempus.Guards, only: [is_locator: 1, is_coming_before: 2]
...> is_locator(Date.utc_today())
true
...> is_locator(& Date.utc_today() |> Tempus.Slot.wrap() |> is_coming_before(&1))
true
...> is_locator(true)
false
@spec is_origin(Tempus.Slot.origin() | any()) :: boolean()
Guard to validate that the term given is actually a Tempus.Slot.origin/0
Examples
iex> import Tempus.Guards, only: [is_origin: 1]
...> is_origin(Date.utc_today())
true
...> is_origin(nil)
true
...> is_origin(:ok)
false
@spec is_slot_border(DateTime.t(), Tempus.Slot.t()) :: boolean()
Guard to validate whether the DateTime.t/0
given as the first argument
is the border of the slot.
Examples
iex> import Tempus.Guards, only: [is_slot_border: 2]
iex> dt = DateTime.utc_now()
...> is_slot_border(dt, %Tempus.Slot{from: dt, to: nil})
true
iex> is_slot_border(dt, Tempus.Slot.wrap(Date.utc_today()))
false
@spec is_slot_equal(Tempus.Slot.t(), Tempus.Slot.t()) :: boolean()
Guard to validate whether two slots are equal
Examples
iex> import Tempus.Guards, only: [is_slot_equal: 2]
...> import Tempus.Sigils
...> s1 = ~I[2023-04-09 00:00:00Z|2023-04-09 23:59:59.999999Z]
...> s2 = Tempus.Slot.wrap(~D|2023-04-09|)
...> s3 = ~I[2023-04-09 00:00:00Z|2023-04-09 23:59:59Z]
...> is_slot_equal(s1, s1)
true
iex> is_slot_equal(s1, s2)
true
iex> is_slot_equal(s1, s3)
false
@spec is_slot_nil(Tempus.Slot.t()) :: boolean()
Guard to validate whether the slot is nil
(has neither end set.)
Examples
iex> import Tempus.Guards, only: [is_slot_nil: 1]
iex> is_slot_nil(Tempus.Slot.id())
true
iex> is_slot_nil(Tempus.Slot.wrap(Date.utc_today()))
false
iex> is_slot_nil(:ok)
false
@spec is_slot_open(Tempus.Slot.t()) :: boolean()
Guard to validate whether the slot is open (has either end not set)
Please note, that the slot having both ends set to nil
is considered
a special case and is not reported as open.
Examples
iex> import Tempus.Guards, only: [is_slot_open: 1]
iex> is_slot_open(%Tempus.Slot{from: nil, to: DateTime.utc_now()})
true
iex> is_slot_open(Tempus.Slot.wrap(Date.utc_today()))
false
iex> is_slot_open(:ok)
false
@spec joint_in_delta?( Tempus.Slot.t(), Tempus.Slot.t(), non_neg_integer() | [{System.time_unit(), non_neg_integer()}] ) :: boolean()
Helper to validate one slot overlaps another in delta. Unlike guards,
this function does not expect arguments in the correct order, and would return
true
if the slots overlap even if s2
comes before s1
.
Examples
iex> import Tempus.Guards, only: [joint_in_delta?: 3]
...> import Tempus.Sigils
...> s1 = ~I[2023-04-09 23:00:00Z|2023-04-09 23:59:59Z]
...> joint_in_delta?(s1, s1, 1)
true
iex> s2 = Tempus.Slot.wrap(~D|2023-04-10|)
...> joint_in_delta?(s1, s2, 1)
true
iex> joint_in_delta?(s2, s1, 1)
true
iex> joint_in_delta?(s1, s2, microsecond: 500)
false