A time interval is the intervening time between two time points. Source: Wikipedia ISO8601
This library implements the Time Interval type via ISO8601::TimeInterval
with the following constructors:
TimeInterval.new(String)(orTimeInterval.parse(String))TimeInterval.from_duration(Duration, Hash<DateTime>)TimeInterval.from_datetime(DateTime, DateTime)
<start>/<end>
<start>/<duration>
<duration>/<end>
Where <start> and <end> are points in time and <duration> is an amount of
time.
The pattern <duration> is not implemented; instead, you can use
TimeInterval.from_duration.
The resulting time interval will have a starting point based on the provided
<start> pattern and an ending point based on the provided <end> pattern.
ti = ISO8601::TimeInterval.parse('2015-12-12T19:53:00Z/2015-12-13T19:53:00Z')
ti.start_time.to_s # => '2015-12-12T19:53:00Z'
ti.end_time.to_s # => '2015-12-13T19:53:00Z'
ti.size # => 86_400.0The resulting time interval will have a starting point based on the provided
<start> pattern and an ending point result of <start> + <duration>.
ti = ISO8601::TimeInterval.parse('2015-12-12T19:53:00Z/P1D')
ti.start_time.to_s # => '2015-12-12T19:53:00Z'
ti.end_time.to_s # => '2015-12-13T19:53:00Z'
ti.size # => 86_400.0The resulting time interval will have a starting point result of
<end> - <duration> and an ending point based on the provided <end> pattern.
ti = ISO8601::TimeInterval.parse('P1D/2015-12-13T19:53:00Z')
ti.start_time.to_s # => '2015-12-12T19:53:00Z'
ti.end_time.to_s # => '2015-12-13T19:53:00Z'
ti.size # => 86_400.0duration = ISO8601::Duration.new('P1D`)
start_time = ISO8601::DateTime.new('2015-12-12T19:53:00Z')
ti = ISO8601::TimeInterval.from_duration(start_time, duration)
ti.start_time.to_s # => '2015-12-12T19:53:00Z'
ti.end_time.to_s # => '2015-12-13T19:53:00Z'
ti.size # => 86_400.0
end_time = ISO8601::DateTime.new('2015-12-13T19:53:00Z')
ti2 = ISO8601::TimeInterval.from_duration(duration, end_time)
ti2.start_time.to_s # => '2015-12-12T19:53:00Z'
ti2.end_time.to_s # => '2015-12-13T19:53:00Z'
ti2.size # => 86_400.0This constructor is an alternative way to <start>/<end> via Ruby objects.
start_time = ISO8601::DateTime.new('2015-12-12T19:53:00Z')
end_time = ISO8601::DateTime.new('2015-12-13T19:53:00Z')
ti2 = ISO8601::TimeInterval.from_duration(start_time, end_time)
ti2.start_time.to_s # => '2015-12-12T19:53:00Z'
ti2.end_time.to_s # => '2015-12-13T19:53:00Z'
ti2.size # => 86_400.0TimeInterval is Comparable, so you can use the usual <, >, <=, >=,
== to compare against another TimeInterval. Is equivalent to get the
amount of seconds via #to_f and compare the resulting numbers.
A time interval can be viewed as an ordered set of datetime elements:
#empty?checks if an interval is empty.#include?checks if aDateTimeis part of the time interval.#intersect?checks if two time intervals overlap.#intersectionreturns the intersected time interval if the two intervals intersect.#disjoint?checks if two time intervals don't intersect.#subset?checks if a time interval is included in another one.#superset?checks if a time interval is included in another one.#firstis the lower bound.#lastis the upper bound.#sizeis the total amount of seconds.
You can convert a time interval into a string with #to_s or into a float via
#to_f.