Class: Timerage::TimeInterval
- Inherits:
-
Range
- Object
- Range
- Timerage::TimeInterval
- Defined in:
- lib/timerage/time_interval.rb
Overview
A range of time. The exposes the Range like interface.
Class Method Summary collapse
Instance Method Summary collapse
-
#&(other) ⇒ Object
Returns a new TimeInterval that is the intersection of ‘self` and `other`.
-
#+(other) ⇒ Object
Return new TimeInterval that is the concatenation of self and other (if possible).
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #adjacent_to?(other) ⇒ Boolean
- #cover?(time_or_interval) ⇒ Boolean
-
#duration ⇒ Object
Returns number of seconds in this interval.
- #getutc ⇒ Object
-
#iso8601(*args) ⇒ Object
Returns an ISO8601 interval representation of self Takes same args as Time#iso8601.
- #overlap?(other) ⇒ Boolean
- #slice(seconds) ⇒ Object
- #step(n, &blk) ⇒ Object
- #to_time_interval ⇒ Object
Class Method Details
.new(*args) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/timerage/time_interval.rb', line 8 def new(*args) args = [args.first.begin, args.first.end, args.first.exclude_end?] if args.first.respond_to?(:exclude_end?) new_obj = allocate new_obj.send(:initialize, *args) new_obj end |
Instance Method Details
#&(other) ⇒ Object
Returns a new TimeInterval that is the intersection of ‘self` and `other`.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/timerage/time_interval.rb', line 113 def &(other) fail ArgumentError, "#{other} does not overlap #{self}" unless self.overlap? other new_begin = [self.begin, other.begin].max new_end,ex_end = if self.end > other.end [other.end, other.exclude_end?] elsif self.end < other.end [self.end, self.exclude_end?] elsif self.exclude_end? || other.exclude_end? [self.end, true] else [self.end, false] end self.class.new(new_begin, new_end, ex_end) end |
#+(other) ⇒ Object
Return new TimeInterval that is the concatenation of self and other (if possible).
Raises ArgumentError if other is not adjacent to self chronologically.
48 49 50 51 52 |
# File 'lib/timerage/time_interval.rb', line 48 def +(other) fail ArgumentError, "other must be adjacent to self" unless adjacent_to?(other) self.class.new([self.begin, other.begin].min, [self.end, other.end].max) end |
#<=>(other) ⇒ Object
97 98 99 100 101 |
# File 'lib/timerage/time_interval.rb', line 97 def <=>(other) return super unless rangeish?(other) self.begin <=> other.begin end |
#==(other) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/timerage/time_interval.rb', line 103 def ==(other) self.begin == other.begin && self.end == other.end && self.exclude_end? == other.exclude_end? rescue NoMethodError false end |
#adjacent_to?(other) ⇒ Boolean
65 66 67 |
# File 'lib/timerage/time_interval.rb', line 65 def adjacent_to?(other) other.begin == self.end || other.end == self.begin end |
#cover?(time_or_interval) ⇒ Boolean
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/timerage/time_interval.rb', line 69 def cover?(time_or_interval) other = time_or_interval return super unless rangeish?(other) return false unless overlap?(other) self_end, other_end = self.end, other.end other.begin >= self.begin && if !self.exclude_end? || other.exclude_end? other_end <= self_end else other_end < self_end end end |
#duration ⇒ Object
Returns number of seconds in this interval
23 24 25 |
# File 'lib/timerage/time_interval.rb', line 23 def duration self.end - self.begin end |
#getutc ⇒ Object
60 61 62 63 |
# File 'lib/timerage/time_interval.rb', line 60 def getutc return self if self.begin.utc? && self.end.utc? self.class.new(self.begin.getutc, self.end.getutc, self.exclude_end?) end |
#iso8601(*args) ⇒ Object
Returns an ISO8601 interval representation of self Takes same args as Time#iso8601
56 57 58 |
# File 'lib/timerage/time_interval.rb', line 56 def iso8601(*args) "#{self.begin.iso8601(*args)}/#{self.end.iso8601(*args)}" end |
#overlap?(other) ⇒ Boolean
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/timerage/time_interval.rb', line 83 def overlap?(other) if self.begin <= other.begin earliest, latest = self, other else earliest, latest = 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 |
#slice(seconds) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/timerage/time_interval.rb', line 35 def slice(seconds) time_enumerator(seconds) .map{|t| end_time = [t+seconds, self.end].min inclusive = (t == end_time || t+seconds > self.end) && !exclude_end? TimeInterval.new(t, end_time, !inclusive) } end |
#step(n, &blk) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/timerage/time_interval.rb', line 27 def step(n, &blk) if block_given? time_enumerator(n).each(&blk) else time_enumerator(n) end end |
#to_time_interval ⇒ Object
16 17 18 |
# File 'lib/timerage/time_interval.rb', line 16 def to_time_interval self end |