class TimeStep::Pair
Attributes
Public Class Methods
Constructs the object.
The `from` and `to` arguments are either TimeStep
or a string representing the time step definition (“<INTERVAL> since <TIME>”). If a string representing the time step definition is given as an argument `from` or `to`, the options `calendar` are used to construct TimeStep
. The option `calendar` specifies the calendar for datetime calculation.
@param from [TimeStep, String] @param to [TimeStep, String] @option calendar [String, TimeStep::Calendar]
# File lib/timesteps/timestep_pair.rb, line 19 def initialize (from, to, calendar: "standard") case from when String @from = TimeStep.new(from, calendar: calendar) when TimeStep @from = from else raise ArgumentError, "from argument '#{from}' should be a time-step or a string" end case to when String @to = TimeStep.new(to, calendar: calendar) when TimeStep @to = to else raise ArgumentError, "to argument '#{to}' should be a time-step or a string" end @from_origin = @from.origin @to_origin = @to.origin @from_interval = @from.interval @to_interval = @to.interval @from_symbol = @from.symbol @to_symbol = @to.symbol if @from_symbol == :years or @from_symbol == :months or @to_symbol == :years or @to_symbol == :months @numeric_conversion = false else @numeric_conversion = true end @difference = ( (@from_origin.jd + @from_origin.fraction - @from_origin.offset) - (@to_origin.jd + @to_origin.fraction - @to_origin.offset) ) * 86400 end
Public Instance Methods
Returns true if other has same contents of `from` and `to` as self has.
@return [Boolean]
# File lib/timesteps/timestep_pair.rb, line 74 def == (other) return @from == other.from && @to == other.to end
Converts index refering `from` timestep to index refering `to`timestep.
# File lib/timesteps/timestep_pair.rb, line 89 def forward (*indices, &block) if indices.empty? || indices.size == 1 && indices.first.is_a?(Range) return forward(*@from.range(*indices), &block) elsif indices.size == 1 index = indices.first.to_r if @numeric_conversion if @to_interval == 0 value = 0 else value = (index*@from_interval + @difference).quo(@to_interval) end else case @from_symbol when :years, :months target = @from.time_at(index) else target = @from_origin + index*(@from_interval.quo(86400)) end case @to_symbol when :years diff = target.difference_in_years(@to_origin) value = diff.quo(@to.numeric) when :months diff = target.difference_in_months(@to_origin) value = diff.quo(@to.numeric) else value = (target.ajd - @to_origin.ajd).quo(@to_interval)*86400 end end value = value.to_i if value.denominator == 1 if block return block[value] else return value end else if block return indices.map { |index| block[forward(index)] } else return indices.map { |index| forward(index) } end end end
# File lib/timesteps/timestep_pair.rb, line 135 def forward_time (*indices) return forward(*indices) {|index| @to.time_at(index) } end
Returns the value as a string for inspection.
# File lib/timesteps/timestep_pair.rb, line 63 def inspect "#<TimeStep::Pair from='#{from.inspect}' to='#{to.inspect}'>" end
Converts index refering `to` timestep to index refering `from` timestep.
# File lib/timesteps/timestep_pair.rb, line 141 def inverse (*indices, &block) if indices.empty? || indices.size == 1 && indices.first.is_a?(Range) return inverse(*@to.range(*indices), &block) elsif indices.size == 1 index = indices.first.to_r if @numeric_conversion if @from_interval == 0 value = 0 else value = (index*@to_interval - @difference).quo(@from_interval) end else case @to_symbol when :years, :months target = @to.time_at(index) else target = @to_origin + index*(@to_interval.quo(86400)) end case @from_symbol when :years diff = target.difference_in_years(@from_origin) value = diff.quo(@from.numeric) when :months diff = target.difference_in_months(@from_origin) value = diff.quo(@from.numeric) else value = (target.ajd - @from_origin.ajd).quo(@from_interval)*86400 end end value = value.to_i if value.denominator == 1 if block return block[value] else return value end else if block return indices.map { |index| block[inverse(index)] } else return indices.map { |index| inverse(index) } end end end
# File lib/timesteps/timestep_pair.rb, line 185 def inverse_time (*indices) return inverse(*indices) {|index| @from.time_at(index) } end
# File lib/timesteps/timestep_pair.rb, line 67 def invert return TimeStep::Pair.new(@to, @from) end
Returns the time represented by the given index as DateTime
object
@param indices [Numeric,Array<Numeric>]
@return [DateTime]
# File lib/timesteps/timestep_pair.rb, line 83 def time_at (*indices) @from.time_at(*indices) end