View Source Tempus.Guards (Tempus v0.9.1)
Handy guards to simplify pattern matching slots
Link to this section 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
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
.
Link to this section 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
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
...> 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
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
...> 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
...> 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
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
...> 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_origin(Tempus.Slot.origin() | any()) :: boolean()
Guard to validate that the term given is actually a Tempus.Slot.origin/0
examples
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.
@spec is_slot_equal(Tempus.Slot.t(), Tempus.Slot.t()) :: boolean()
Guard to validate whether two slots are equal
examples
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
...> is_slot_equal(s1, s2)
true
...> 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.)
@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.
@spec joint_in_delta?( Tempus.Slot.t(), Tempus.Slot.t(), non_neg_integer() | {non_neg_integer(), 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
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]
...> s2 = Tempus.Slot.wrap(~D|2023-04-10|)
...> joint_in_delta?(s1, s2, 1)
true
...> joint_in_delta?(s2, s1, 1)
true
...> joint_in_delta?(s1, s2, {0, 500})
false