View Source Tempus.Slot (Tempus v0.9.0)
Declares a timeslot and exports functions to check whether the given date and/or datetime is covered by this slot or not.
This module probably should not be called directly.
Link to this section Summary
Functions
Compares two slot structs.
Checks whether to Slot covers the data/datetime passed as a second argument.
Returns true if two slots are disjoined, false otherwise.
Calculates the duration of a slot in units given as a second parameter
(default: :second.)
Intersects slots to the minimal covered timeslice.
Joins slots to the maximal covered timeslice.
Joins two slots to the maximal covered timeslice.
Returns true if two slots are neighbours, false otherwise.
Creates new slot using arg[:from] as a starting origin and arg[:to] and an ending origin.
Creates new slot using from as a starting origin and to and an ending origin.
See new/1 for more readable implementation.
Creates new slot using from as a starting origin and to and an ending origin.
Unlike new/1, this function raises on malformed input.
Shifts both from and to values to UTC zone.
Compares two slot structs. The same as compare/2, but returns :joint if
the slots are overlapped.
Checks whether the Slot is valid (to > from) or not.
Link to this section Types
@type origin() :: t() | Date.t() | DateTime.t() | Time.t() | nil
The origin used in comparisons and calculations
@type t() :: %Tempus.Slot{from: nil | DateTime.t(), to: nil | DateTime.t()}
A timeslot to be used in Tempus
Link to this section Functions
Compares two slot structs.
Returns :gt if first slot is strictly later than the second and :lt for vice versa.
NB :eq is returned not only if slots are equal, but also when they are overlapped.
Might be used in Enum.sort/2.
Checks whether to Slot covers the data/datetime passed as a second argument.
examples
Examples
iex> dt_between = ~U|2015-09-30 01:00:00Z|
...> dt_from = ~U|2015-09-30 00:00:00Z|
...> dt_to = ~U|2015-10-01 01:00:00Z|
...> d_from = Date.from_iso8601!("2015-09-30")
...> d_to = Date.from_iso8601!("2015-10-01")
iex> slot = %Tempus.Slot{from: dt_from, to: dt_to}
iex> Tempus.Slot.cover?(slot, dt_between)
true
iex> Tempus.Slot.cover?(slot, dt_to)
true
iex> Tempus.Slot.cover?(slot, dt_to, true)
false
iex> Tempus.Slot.cover?(slot, d_from)
true
iex> Tempus.Slot.cover?(slot, d_from, true)
false
iex> Tempus.Slot.cover?(slot, d_to)
false
Returns true if two slots are disjoined, false otherwise.
examples
Examples
iex> slot = %Tempus.Slot{from: ~U|2015-09-01 00:00:00Z|, to: ~U|2015-10-01 00:00:00Z|}
iex> inner = %Tempus.Slot{from: ~U|2015-09-01 00:00:00Z|, to: ~U|2015-09-01 01:00:00Z|}
iex> Tempus.Slot.disjoint?(slot, inner)
false
iex> inner = %Tempus.Slot{from: ~U|2015-09-01 00:00:00Z|, to: ~U|2015-10-01 01:00:00Z|}
iex> Tempus.Slot.disjoint?(slot, inner)
false
iex> outer = %Tempus.Slot{from: ~U|2015-10-01 00:00:01Z|, to: ~U|2015-10-01 01:00:00Z|}
iex> Tempus.Slot.disjoint?(slot, outer)
true
@spec duration(slot :: t(), unit :: System.time_unit()) :: non_neg_integer() | :infinity
Calculates the duration of a slot in units given as a second parameter
(default: :second.)
example
Example
iex> ~D|2020-09-03| |> Tempus.Slot.wrap() |> Tempus.Slot.duration()
86400
Intersects slots to the minimal covered timeslice.
example
Example
iex> Tempus.Slot.intersect([Tempus.Slot.wrap(~D|2020-09-30|),
...> %Tempus.Slot{from: ~U|2020-09-30 23:00:00Z|, to: ~U|2020-10-02 00:00:00Z|}])
#Slot<[from: ~U[2020-09-30 23:00:00Z], to: ~U[2020-09-30 23:59:59.999999Z]]>
Joins slots to the maximal covered timeslice.
example
Example
iex> Tempus.Slot.join([Tempus.Slot.wrap(~D|2020-09-30|), Tempus.Slot.wrap(~D|2020-10-02|)])
#Slot<[from: ~U[2020-09-30 00:00:00.000000Z], to: ~U[2020-10-02 23:59:59.999999Z]]>
iex> Tempus.Slot.join([~D|2020-09-30|, ~D|2020-10-02|])
#Slot<[from: ~U[2020-09-30 00:00:00.000000Z], to: ~U[2020-10-02 23:59:59.999999Z]]>
Joins two slots to the maximal covered timeslice.
example
Example
iex> Tempus.Slot.join(Tempus.Slot.wrap(~D|2020-09-30|), Tempus.Slot.wrap(~D|2020-10-02|))
#Slot<[from: ~U[2020-09-30 00:00:00.000000Z], to: ~U[2020-10-02 23:59:59.999999Z]]>
iex> Tempus.Slot.join(~D|2020-09-30|, ~D|2020-10-02|)
#Slot<[from: ~U[2020-09-30 00:00:00.000000Z], to: ~U[2020-10-02 23:59:59.999999Z]]>
Returns true if two slots are neighbours, false otherwise.
examples
Examples
iex> slot = %Tempus.Slot{from: ~U|2015-09-01 00:00:00Z|, to: ~U|2015-10-01 23:59:59Z|}
iex> Tempus.Slot.neighbour?(slot, Tempus.Slot.wrap(~D|2015-10-02|))
true
iex> Tempus.Slot.neighbour?(slot, Tempus.Slot.wrap(~D|2015-08-31|))
true
iex> Tempus.Slot.neighbour?(slot, Tempus.Slot.wrap(~D|2015-10-01|))
false
iex> Tempus.Slot.neighbour?(slot, Tempus.Slot.wrap(~D|2015-10-03|))
false
Creates new slot using arg[:from] as a starting origin and arg[:to] and an ending origin.
examples
Examples
iex> Tempus.Slot.new(from: ~U|2015-09-30 00:00:00Z|, to: ~U|2015-10-01 01:00:00Z|)
{:ok, %Tempus.Slot{from: ~U|2015-09-30 00:00:00Z|, to: ~U|2015-10-01 01:00:00Z|}}
iex> Tempus.Slot.new(%{from: ~D|2015-09-30|, to: ~U|2015-10-01T12:00:00Z|})
{:ok, %Tempus.Slot{from: ~U|2015-09-30 00:00:00.000000Z|, to: ~U|2015-10-01 12:00:00Z|}}
Creates new slot using from as a starting origin and to and an ending origin.
See new/1 for more readable implementation.
examples
Examples
iex> Tempus.Slot.new(~U|2015-09-30 00:00:00Z|, ~U|2015-10-01 01:00:00Z|)
{:ok, %Tempus.Slot{from: ~U|2015-09-30 00:00:00Z|, to: ~U|2015-10-01 01:00:00Z|}}
iex> Tempus.Slot.new(~D|2015-09-30|, ~U|2015-10-01T12:00:00Z|)
{:ok, %Tempus.Slot{from: ~U|2015-09-30 00:00:00.000000Z|, to: ~U|2015-10-01 12:00:00Z|}}
iex> Tempus.Slot.new(:ok, :ok)
{:error, :invalid_input}
Creates new slot using from as a starting origin and to and an ending origin.
Unlike new/1, this function raises on malformed input.
examples
Examples
iex> Tempus.Slot.new!(~U|2015-09-30 00:00:00Z|, ~U|2015-10-01 01:00:00Z|)
%Tempus.Slot{from: ~U|2015-09-30 00:00:00Z|, to: ~U|2015-10-01 01:00:00Z|}
iex> Tempus.Slot.new!(~D|2015-09-30|, ~U|2015-10-01T12:00:00Z|)
%Tempus.Slot{from: ~U|2015-09-30 00:00:00.000000Z|, to: ~U|2015-10-01 12:00:00Z|}
iex> Tempus.Slot.new!(:ok, :ok)
** (ArgumentError) malformed from/to argument, expected `origin`
shift_tz(slot, tz \\ "Etc/UTC", tz_db \\ Calendar.get_time_zone_database())
View Source@spec shift_tz( slot :: t(), tz :: Calendar.time_zone(), tz_db :: Calendar.time_zone_database() ) :: t()
Shifts both from and to values to UTC zone.
examples
Examples
slot = %Tempus.Slot{
from: DateTime.from_naive!(~N|2018-01-05 21:00:00|, "America/New_York"),
to: DateTime.from_naive!(~N|2018-01-08 08:59:59|, "Australia/Sydney")
}
#⇒ #Slot<[from: ~U[2018-01-06 02:00:00Z], to: ~U[2018-01-07 21:59:59Z]]>
Compares two slot structs. The same as compare/2, but returns :joint if
the slots are overlapped.
Checks whether the Slot is valid (to > from) or not.
examples
Examples
iex> slot = %Tempus.Slot{from: ~U|2015-09-30 00:00:00Z|, to: ~U|2015-10-01 01:00:00Z|}
iex> Tempus.Slot.valid?(slot)
true
iex> Tempus.Slot.valid?(%Tempus.Slot{from: slot.to, to: slot.from})
false
@spec wrap(origin(), DateTime.t()) :: t()
Wraps the argument into a slot. For DateTime it’d be a single microsecond.
For a Date, it would be the whole day, starting at 00:00:00.000000 and
ending at `23:59:59:999999`.
examples
Examples
iex> Tempus.Slot.wrap(~D|2020-08-06|)
#Slot<[from: ~U[2020-08-06 00:00:00.000000Z], to: ~U[2020-08-06 23:59:59.999999Z]]>