View Source Tempus (Tempus v0.8.0)
Tempus
is a library to deal with timeslots.
It aims to be a fast yet easy to use implementation of a schedule of any type, including but not limited to free/busy time schedules.
The example of it might be a calendar software, where slots might be marked as free, or busy. It also allows simple arithmetics with schedules, like adding five days or subtracting 7 hours 30 minutes from now, considering busy slots.
Link to this section Summary
Types
The type defining how slicing is to be applied.
When :greedy
, overlapping boundary slots would be included,
:reluctant
would take only those fully contained in the interval.
Functions
Adds an amount of units to the origin, considering slots given.
Returns the reversed list of free days after origin.
Returns the reversed list of free days after origin.
Returns the reversed list of free days after origin.
Drops slots at the beginning of the %Slots{}
struct while fun
returns a truthy value.
Checks whether the slot is disjoined against slots.
Returns the next busy slot from the slots passed as a first argument,
that immediately follows origin
. IOf slots are overlapped, the overlapped
one gets returned.
Returns the next free slot from the slots passed as a first argument,
that immediately follows origin
. If slots are overlapped, the overlapped
one gets returned.
Slices the %Slots{}
based on origins from
and to
and an optional type
(default: :reluctant
.) Returns sliced %Slots{}
back.
Takes slots at the beginning of the %Slots{}
struct while fun
returns a truthy value.
Link to this section Types
@type count() :: :infinity | :stream | non_neg_integer()
@type direction() :: :fwd | :bwd
@type option() :: {:origin, Tempus.Slot.origin()} | {:count, count() | neg_integer()} | {:direction, direction()}
@type options() :: [option()]
@type slice_type() :: :greedy | :reluctant
The type defining how slicing is to be applied.
When :greedy
, overlapping boundary slots would be included,
:reluctant
would take only those fully contained in the interval.
Link to this section Functions
add(slots, origin \\ DateTime.utc_now(), amount_to_add, unit \\ :second)
View Source@spec add( slots :: Tempus.Slots.t(), origin :: DateTime.t(), amount_to_add :: integer(), unit :: System.time_unit() ) :: DateTime.t()
Adds an amount of units to the origin, considering slots given.
@spec days_add(slots :: Tempus.Slots.t(), opts :: options()) :: [Date.t()]
Returns the reversed list of free days after origin.
examples
Examples
iex> slots = [
...> Tempus.Slot.wrap(~D|2020-08-07|),
...> Tempus.Slot.wrap(~D|2020-08-10|)
...> ] |> Enum.into(%Tempus.Slots{})
iex> Tempus.days_add(slots, origin: ~D|2020-08-07|, count: 3) |> hd()
~D|2020-08-12|
iex> Tempus.days_add(slots, origin: ~D|2020-08-07|, count: 3, direction: :fwd) |> hd()
~D|2020-08-12|
iex> Tempus.days_add(slots, origin: ~D|2020-08-07|, count: -3, direction: :bwd) |> hd()
~D|2020-08-12|
iex> Tempus.days_add(slots, origin: ~D|2020-08-12|, count: -4) |> hd()
~D|2020-08-06|
iex> Tempus.days_add(slots, origin: ~D|2020-08-12|, count: 4, direction: :bwd) |> hd()
~D|2020-08-06|
iex> Tempus.days_add(slots, origin: ~D|2020-08-12|, count: -4, direction: :fwd) |> hd()
~D|2020-08-06|
@spec days_ago(slots :: Tempus.Slots.t(), origin :: Date.t(), count :: integer()) :: [ Date.t() ]
Returns the reversed list of free days after origin.
examples
Examples
iex> slots = [
...> Tempus.Slot.wrap(~D|2020-08-07|),
...> Tempus.Slot.wrap(~D|2020-08-10|)
...> ] |> Enum.into(%Tempus.Slots{})
iex> Tempus.days_ago(slots, ~D|2020-08-07|, 0)
[~D|2020-08-06|]
iex> Tempus.days_ago(slots, ~D|2020-08-12|, 4) |> hd()
~D|2020-08-06|
@spec days_ahead(slots :: Tempus.Slots.t(), origin :: Date.t(), count :: integer()) :: [Date.t()]
Returns the reversed list of free days after origin.
examples
Examples
iex> slots = [
...> Tempus.Slot.wrap(~D|2020-08-07|),
...> Tempus.Slot.wrap(~D|2020-08-10|)
...> ] |> Enum.into(%Tempus.Slots{})
iex> Tempus.days_ahead(slots, ~D|2020-08-07|, 0)
[~D|2020-08-08|]
iex> Tempus.days_ahead(slots, ~D|2020-08-07|, 3) |> hd()
~D|2020-08-12|
@spec drop_while( slots :: Tempus.Slots.t(), fun :: (Tempus.Slot.t() -> as_boolean(term())) ) :: Tempus.Slots.t()
Drops slots at the beginning of the %Slots{}
struct while fun
returns a truthy value.
@spec free?( slots :: Tempus.Slots.t(), slot :: Tempus.Slot.origin(), method :: :smart | :size ) :: boolean() | no_return()
Checks whether the slot is disjoined against slots.
examples
Examples
iex> slots = [
...> Tempus.Slot.wrap(~D|2020-08-07|),
...> Tempus.Slot.wrap(~D|2020-08-10|)
...> ] |> Enum.into(%Tempus.Slots{})
iex> Tempus.free?(slots, ~D|2020-08-07|)
false
iex> Tempus.free?(slots, ~D|2020-08-08|)
true
@spec next_busy(Tempus.Slots.t(), options()) :: [Tempus.Slot.t()] | Tempus.Slot.t() | nil | no_return()
Returns the next busy slot from the slots passed as a first argument,
that immediately follows origin
. IOf slots are overlapped, the overlapped
one gets returned.
examples
Examples
iex> slots = [
...> Tempus.Slot.wrap(~D|2020-08-07|),
...> Tempus.Slot.wrap(~D|2020-08-10|)
...> ] |> Enum.into(%Tempus.Slots{})
iex> Tempus.next_busy(slots, origin: %Tempus.Slot{from: ~U|2020-08-08 23:00:00Z|, to: ~U|2020-08-09 12:00:00Z|})
#Slot<[from: ~U[2020-08-10 00:00:00.000000Z], to: ~U[2020-08-10 23:59:59.999999Z]]>
iex> Tempus.next_busy(slots, origin: %Tempus.Slot{from: ~U|2020-08-07 11:00:00Z|, to: ~U|2020-08-07 12:00:00Z|}, count: 2) |> hd()
#Slot<[from: ~U[2020-08-07 00:00:00.000000Z], to: ~U[2020-08-07 23:59:59.999999Z]]>
iex> Tempus.next_busy(slots, origin: %Tempus.Slot{from: ~U|2020-08-07 11:00:00Z|, to: ~U|2020-08-08 12:00:00Z|})
#Slot<[from: ~U[2020-08-07 00:00:00.000000Z], to: ~U[2020-08-07 23:59:59.999999Z]]>
iex> Tempus.next_busy(slots, origin: %Tempus.Slot{from: ~U|2020-08-07 11:00:00Z|, to: ~U|2020-08-10 12:00:00Z|})
#Slot<[from: ~U[2020-08-07 00:00:00.000000Z], to: ~U[2020-08-07 23:59:59.999999Z]]>
iex> Tempus.next_busy(slots, origin: ~D|2020-08-07|)
#Slot<[from: ~U[2020-08-07 00:00:00.000000Z], to: ~U[2020-08-07 23:59:59.999999Z]]>
iex> Tempus.next_busy(slots, origin: ~D|2020-08-08|)
#Slot<[from: ~U[2020-08-10 00:00:00.000000Z], to: ~U[2020-08-10 23:59:59.999999Z]]>
iex> Tempus.next_busy(slots, origin: ~D|2020-08-08|, direction: :bwd)
#Slot<[from: ~U[2020-08-07 00:00:00.000000Z], to: ~U[2020-08-07 23:59:59.999999Z]]>
iex> Tempus.next_busy(slots, origin: ~D|2020-08-10|, direction: :bwd)
#Slot<[from: ~U[2020-08-10 00:00:00.000000Z], to: ~U[2020-08-10 23:59:59.999999Z]]>
iex> Tempus.next_busy(slots, origin: %Tempus.Slot{from: ~U|2020-08-11 11:00:00Z|, to: ~U|2020-08-11 12:00:00Z|})
nil
iex> Tempus.next_busy(slots, origin: %Tempus.Slot{from: ~U|2020-08-06 11:00:00Z|, to: ~U|2020-08-06 12:00:00Z|}, direction: :bwd)
nil
iex> Tempus.next_busy(%Tempus.Slots{})
nil
@spec next_free(Tempus.Slots.t(), options()) :: [Tempus.Slot.t()] | Tempus.Slot.t() | no_return()
Returns the next free slot from the slots passed as a first argument,
that immediately follows origin
. If slots are overlapped, the overlapped
one gets returned.
examples
Examples
iex> slots = [
...> Tempus.Slot.wrap(~D|2020-08-07|),
...> Tempus.Slot.wrap(~D|2020-08-10|),
...> Tempus.Slot.wrap(~D|2020-08-12|),
...> Tempus.Slot.wrap(~D|2020-08-14|)
...> ] |> Enum.into(%Tempus.Slots{})
iex> Tempus.next_free(slots, origin: %Tempus.Slot{from: ~U|2020-08-08 23:00:00Z|, to: ~U|2020-08-09 12:00:00Z|})
#Slot<[from: ~U[2020-08-08 00:00:00.000000Z], to: ~U[2020-08-09 23:59:59.999999Z]]>
iex> Tempus.next_free(slots, origin: %Tempus.Slot{from: ~U|2020-08-06 11:00:00Z|, to: ~U|2020-08-06 12:00:00Z|})
#Slot<[from: ~U[2020-08-06 11:00:00.000000Z], to: ~U[2020-08-06 23:59:59.999999Z]]>
iex> Tempus.next_free(slots, origin: ~U|2020-08-13 01:00:00.000000Z|)
#Slot<[from: ~U[2020-08-13 00:00:00.000000Z], to: ~U[2020-08-13 23:59:59.999999Z]]>
iex> Tempus.next_free(slots, origin: ~D|2020-08-13|)
#Slot<[from: ~U[2020-08-13 00:00:00.000000Z], to: ~U[2020-08-13 23:59:59.999999Z]]>
iex> Tempus.next_free(slots, origin: ~D|2020-08-14|)
#Slot<[from: ~U[2020-08-15 00:00:00.000000Z], to: nil]>
iex> Tempus.next_free(slots, origin: ~D|2020-08-07|, count: 5)
[
%Tempus.Slot{from: ~U[2020-08-08 00:00:00.000000Z], to: ~U[2020-08-09 23:59:59.999999Z]},
%Tempus.Slot{from: ~U[2020-08-11 00:00:00.000000Z], to: ~U[2020-08-11 23:59:59.999999Z]},
%Tempus.Slot{from: ~U[2020-08-13 00:00:00.000000Z], to: ~U[2020-08-13 23:59:59.999999Z]},
%Tempus.Slot{from: ~U[2020-08-15 00:00:00.000000Z], to: nil}
]
iex> Tempus.next_free(slots, origin: ~D|2020-08-15|, count: -5)
[
%Tempus.Slot{from: ~U[2020-08-15 00:00:00.000000Z], to: ~U[2020-08-15 23:59:59.999999Z]},
%Tempus.Slot{from: ~U[2020-08-13 00:00:00.000000Z], to: ~U[2020-08-13 23:59:59.999999Z]},
%Tempus.Slot{from: ~U[2020-08-11 00:00:00.000000Z], to: ~U[2020-08-11 23:59:59.999999Z]},
%Tempus.Slot{from: ~U[2020-08-08 00:00:00.000000Z], to: ~U[2020-08-09 23:59:59.999999Z]},
%Tempus.Slot{from: nil, to: ~U[2020-08-06 23:59:59.999999Z]},
]
iex> Tempus.next_free(slots, origin: ~D|2020-08-12|, count: :infinity, direction: :bwd)
[
%Tempus.Slot{from: ~U[2020-08-11 00:00:00.000000Z], to: ~U[2020-08-11 23:59:59.999999Z]},
%Tempus.Slot{from: ~U[2020-08-08 00:00:00.000000Z], to: ~U[2020-08-09 23:59:59.999999Z]},
%Tempus.Slot{from: nil, to: ~U[2020-08-06 23:59:59.999999Z]}
]
@spec slice( slots :: Tempus.Slots.t(), from :: Tempus.Slot.origin(), to :: Tempus.Slot.origin(), type :: slice_type() ) :: Tempus.Slots.t()
Slices the %Slots{}
based on origins from
and to
and an optional type
(default: :reluctant
.) Returns sliced %Slots{}
back.
@spec take_while( slots :: Tempus.Slots.t(), fun :: (Tempus.Slot.t() -> as_boolean(term())) ) :: Tempus.Slots.t()
Takes slots at the beginning of the %Slots{}
struct while fun
returns a truthy value.