View Source Tempus.Guards (Tempus v0.10.0)

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 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.

Link to this section Functions

Link to this macro

is_coming_before(o1, o2)

View Source (since 0.9.0) (macro)
@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
Link to this macro

is_covered(o, s)

View Source (since 0.9.0) (macro)
@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
Link to this macro

is_joint(s1, s2)

View Source (since 0.9.0) (macro)
@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
Link to this macro

is_locator(origin)

View Source (since 0.9.0) (macro)
@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

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
Link to this macro

is_origin(term)

View Source (since 0.9.0) (macro)
@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
Link to this macro

is_slot_border(dt, s)

View Source (since 0.9.0) (macro)
@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.

Link to this macro

is_slot_equal(s1, s2)

View Source (since 0.9.0) (macro)
@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
Link to this macro

is_slot_nil(s)

View Source (since 0.9.0) (macro)
@spec is_slot_nil(Tempus.Slot.t()) :: boolean()

Guard to validate whether the slot is nil (has neither end set.)

Link to this macro

is_slot_open(s)

View Source (since 0.9.0) (macro)
@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.

Link to this function

joint_in_delta?(s1, s2, delta)

View Source (since 0.9.0)
@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

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, microsecond: 500)
false