lib/timerage/time_interval.rb in timerage-1.8.0 vs lib/timerage/time_interval.rb in timerage-2.0.0
- old
+ new
@@ -11,10 +11,14 @@
end
super rng
end
+ def to_time_interval
+ self
+ end
+
alias_method :to_time, :begin
# Returns number of seconds in this interval
def duration
self.end - self.begin
@@ -51,41 +55,45 @@
# Takes same args as Time#iso8601
def iso8601(*args)
"#{self.begin.iso8601(*args)}/#{self.end.iso8601(*args)}"
end
+ def getutc
+ return self if self.begin.utc? && self.end.utc?
+ self.class.new(self.begin.getutc, self.end.getutc, self.exclude_end?)
+ end
+
def adjacent_to?(other)
other.begin == self.end || other.end == self.begin
end
def cover?(time_or_interval)
- return super unless rangeish?(time_or_interval)
-
other = time_or_interval
+ return super unless rangeish?(other)
+ return false unless overlap?(other)
- self.begin <= other.begin &&
- if self.exclude_end? && other.exclude_end?
- self.end > other.begin && self.begin < other.end && other.end <= self.end
-
- elsif self.exclude_end?
- self.end > other.begin && self.begin <= other.end && other.end < self.end
-
- elsif other.exclude_end?
- self.end >= other.begin && self.begin < other.end && other.end <= self.end
-
+ self_end, other_end = self.end, other.end
+ other.begin >= self.begin &&
+ if !self.exclude_end? || other.exclude_end?
+ other_end <= self_end
else
- self.end >= other.begin && self.begin <= other.end && other.end <= self.end
+ other_end < self_end
end
end
def overlap?(other)
- cover?(other) ||
- other.cover?(self) ||
- cover?(other.begin) ||
- other.cover?(self.begin) ||
- cover?(other.end) && (!other.exclude_end? || other.end != self.begin) ||
- other.cover?(self.end) && (!self.exclude_end? || other.begin != self.end)
+ earliest, latest = if self.begin <= other.begin
+ [self, other]
+ else
+ [other, self]
+ end
+
+ latest_begin, earliest_end = latest.begin, earliest.end
+ return true if latest_begin < earliest_end
+ return false if earliest_end < latest_begin
+
+ !earliest.exclude_end?
end
def <=>(other)
return super unless rangeish?(other)
@@ -157,13 +165,14 @@
# represented by the specified string.
#
# --
#
# Currently this only supports `<begin>/<end>` style time intervals.
- def self.iso8601(str)
- new *str.split("/").map{|s| Time.iso8601(s)}
+ def self.iso8601(str, exclusive_end: true)
+ new *str.split("/", 2).map{|s| Time.iso8601(s)}, exclusive_end
rescue ArgumentError
raise ArgumentError, "Invalid iso8601 interval: #{str.inspect}"
end
end
+
end