The Interval class provides objects that model a time interval. The start end end time are represented as seconds after Jan 1, 1970. The start is part of the interval, the end is not.
Create a new Interval. args can be three different kind of arguments.
a and b should be TjTime objects.
Interval.new(a, b) | -> Interval(a, b) Interval.new(a) | -> Interval(a, a) Interval.new(iv) | -> Interval(iv.start, iv.end)
# File lib/Interval.rb, line 32 32: def initialize(*args) 33: if args.length == 1 34: if args[0].is_a?(TjTime) 35: # Just one argument, a date 36: @start = @end = args[0] 37: elsif args[0].is_a?(Interval) 38: # Just one argument, an Interval 39: @start = args[0].start 40: @end = args[0].end 41: else 42: raise "Illegal argument" 43: end 44: elsif args.length == 2 45: # Two arguments, a start and end date 46: @start = args[0] 47: @end = args[1] 48: raise "Interval start must be a date" unless @start.is_a?(TjTime) 49: raise "Interval end must be a date" unless @end.is_a?(TjTime) 50: if @end < @start 51: raise "Invalid interval" 52: end 53: else 54: raise "Illegal arguments" 55: end 56: end
Return true if the Interval iv describes an identical time period.
# File lib/Interval.rb, line 117 117: def ==(iv) 118: @start == iv.start && @end == iv.end 119: end
Append or prepend the Interval iv to self. If iv does not directly attach to self, just return self.
# File lib/Interval.rb, line 93 93: def combine(iv) 94: if iv.end == @start 95: # Prepend iv 96: Array.new Interval(iv.start, @end) 97: elsif @end == iv.start 98: # Append iv 99: Array.new Interval(@start, iv.end) 100: else 101: self 102: end 103: end
Return true if arg is contained within the Interval. It can either be a single TjTime or another Interval.
# File lib/Interval.rb, line 65 65: def contains?(arg) 66: if arg.is_a?(TjTime) 67: return @start <= arg && arg < @end 68: else 69: return @start <= arg.start && arg.end <= @end 70: end 71: end
Return the duration of the Interval.
# File lib/Interval.rb, line 59 59: def duration 60: @end - @start 61: end
Return a new Interval that contains the overlap of self and the Interval iv. In case there is no overlap, nil is returned.
# File lib/Interval.rb, line 85 85: def intersection(iv) 86: newStart = @start > iv.start ? @start : iv.start 87: newEnd = @end < iv.end ? @end : iv.end 88: newStart < newEnd ? Interval.new(newStart, newEnd) : nil 89: end
Check whether the Interval arg overlaps with this interval.
# File lib/Interval.rb, line 74 74: def overlaps?(arg) 75: if arg.is_a?(TjTime) 76: return @start <= arg && arg < @end 77: else 78: return (@start <= arg.start && arg.start < @end) || 79: (arg.start <= @start && @start < arg.end) 80: end 81: end
Turn the Interval into a human readable form.
# File lib/Interval.rb, line 122 122: def to_s 123: @start.to_s + ' - ' + @end.to_s 124: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.