class TimePeriod
Attributes
ends[R]
last[R]
Public Class Methods
new(spec, since: nil, format: nil, offset: nil, calendar: "standard", tz: nil, ends: "[]" )
click to toggle source
Calls superclass method
TimeStep::new
# File lib/timesteps/timeperiod.rb, line 5 def initialize (spec, since: nil, format: nil, offset: nil, calendar: "standard", tz: nil, ends: "[]" ) super(spec, since: since, format: format, offset: offset, calendar: calendar, tz: tz) raise "invalid ends specification" unless ends =~ /\A[\[\(][\]\)]\z/ @ends = ends @include_start = ( ends[0] == "[" ) ? true : false @include_last = ( ends[1] == "]" ) ? true : false @last = time_at(1) end
Public Instance Methods
clip(other)
click to toggle source
# File lib/timesteps/timeperiod.rb, line 142 def clip (other) raise "can't clip period without contacted period" unless contact?(other) if ( @origin < other.origin ) || ( @origin == other.origin && include_start? && ( not other.include_start? ) ) left = other.origin left_end = other.include_start? ? "[" : "(" else left = @origin left_end = include_start? ? "[" : "(" end if ( other.last < @last ) || ( other.last == @last && include_last? && ( not other.include_last? ) ) right = other.last right_end = other.include_last? ? "]" : ")" else right = @last right_end = include_last? ? ")" : "]" end ridx = index_at(right) lidx = index_at(left) numeric = (ridx - lidx) * @numeric origin = left interval_spec = format("%g %s", numeric, @symbol) ends = left_end + right_end return TimePeriod.new(interval_spec, since: origin, calendar: @calendar, ends: ends) end
contact?(other)
click to toggle source
# File lib/timesteps/timeperiod.rb, line 94 def contact? (other) case other when TimePeriod if @origin <= other.last left = true else left = false end if other.origin <= @last right = true else right = false end return left && right else left = @origin <= other right = other <= @last return left & right end end
include?(other)
click to toggle source
# File lib/timesteps/timeperiod.rb, line 42 def include? (other) case other when TimePeriod if ( other.origin < @origin ) || ( other.origin == @origin && ( not @include_start ) && other.include_start? ) left = false else left = true end if ( @last < other.last ) || ( @last == other.last && ( not @include_last ) && other.include_last? ) right = false else right = true end return left & right else if @include_start left = @origin <= other else left = @origin < other end if @include_last right = other <= @last else right = other < @last end return left & right end end
include_last?()
click to toggle source
# File lib/timesteps/timeperiod.rb, line 24 def include_last? return @include_last end
include_start?()
click to toggle source
# File lib/timesteps/timeperiod.rb, line 20 def include_start? return @include_start end
inspect()
click to toggle source
Returns the value as a string for inspection.
# File lib/timesteps/timeperiod.rb, line 30 def inspect options = "" case @calendar.name when "standard", "gregorian" else options << " calendar='#{calendar.name}'" end left_paren = @include_start ? "[" : "(" right_paren = @include_last ? "]" : ")" "#<TimePeriod '#{interval_spec}' #{left_paren}#{start.to_s}, #{last.to_s}#{right_paren} #{options}>" end
merge(other)
click to toggle source
# File lib/timesteps/timeperiod.rb, line 115 def merge (other) raise "can't merge period without contacted period" unless contact?(other) if ( other.origin < @origin ) || ( other.origin == @origin && ( not include_start? ) && other.include_start? ) left = other.origin left_end = other.include_start? ? "[" : "(" else left = @origin left_end = include_start? ? "[" : "(" end if ( @last < other.last ) || ( @last == other.last && ( not include_last? ) && other.include_last? ) right = other.last right_end = other.include_last? ? "]" : ")" else right = @last right_end = include_last? ? ")" : "]" end ridx = index_at(right) lidx = index_at(left) numeric = (ridx - lidx) * @numeric origin = left interval_spec = format("%g %s", numeric, @symbol) ends = left_end + right_end return TimePeriod.new(interval_spec, since: origin, calendar: @calendar, ends: ends) end
next()
click to toggle source
# File lib/timesteps/timeperiod.rb, line 184 def next return TimePeriod.new(interval_spec, since: @last, calendar: @calendar, ends: ends) end
overlap?(other)
click to toggle source
# File lib/timesteps/timeperiod.rb, line 73 def overlap? (other) case other when TimePeriod if ( @origin < other.last ) || ( @origin == other.last && @include_start && other.include_last? ) left = true else left = false end if ( other.origin < @last ) || ( other.origin == @last && @include_last && other.include_start? ) right = true else right = false end return left && right else return include?(other) end end
prev()
click to toggle source
# File lib/timesteps/timeperiod.rb, line 188 def prev origin = index_at(-1) return TimePeriod.new(interval_spec, since: origin, calendar: @calendar, ends: ends) end
shift_origin(index, with: "index")
click to toggle source
# File lib/timesteps/timeperiod.rb, line 193 def shift_origin (index, with: "index") case with when :index, "index" return TimePeriod.new(interval_spec, since: time_at(index), calendar: @calendar, ends: ends) when :duration, "duration", :days, "days" time = @origin + index return TimePeriod.new(interval_spec, since: time, calendar: @calendar, ends: ends) end end
split(time, ends: ")[")
click to toggle source
# File lib/timesteps/timeperiod.rb, line 169 def split (time, ends: ")[") raise "can't split period without contacted time" unless contact?(time) # left numeric = index_at(time) * @numeric interval_spec = format("%g %s", numeric, @symbol) ends = (@include_start ? "[" : "(") + ends[0] left_period = TimePeriod.new(interval_spec, since: @origin, calendar: @calendar, ends: ends) # right numeric = (1 - index_at(time)) * @numeric interval_spec = format("%g %s", numeric, @symbol) ends = ends[1] + (@include_last ? "]" : ")") right_period = TimePeriod.new(interval_spec, since: @origin, calendar: @calendar, ends: ends) return left_period, right_period end
start()
click to toggle source
# File lib/timesteps/timeperiod.rb, line 16 def start return @origin end