Class: TimeStep::Pair

Inherits:
Object
  • Object
show all
Defined in:
lib/timesteps/timesteppair.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to, calendar: "standard", bc: false) ⇒ Pair

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` and `bc` are used to construct TimeStep. The option `calendar` specifies the calendar for datetime calculation. The option `bc` is a flag whether AD/BC or EC when parsing timestamp for negative year.

Parameters:

  • from (TimeStep, String)
  • to (TimeStep, String)
  • calendar (Hash) (defaults to: "standard")

    a customizable set of options

  • bc (Hash) (defaults to: false)

    a customizable set of options

Options Hash (calendar:):

Options Hash (bc:):

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/timesteps/timesteppair.rb', line 22

def initialize (from, to, calendar: "standard", bc: false)

  case from
  when String
    @from = TimeStep.new(from, calendar: calendar, bc: bc)
  when TimeStep
    @from = from
  else
    raise "first argument should be TimeStep or string"
  end
          
  case to
  when String
    @to = TimeStep.new(to, calendar: calendar, bc: bc)
  when TimeStep
    @to = to
  else
    raise "second argument should be TimeStep or 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

Instance Attribute Details

#differenceObject (readonly)

Returns the value of attribute difference.



62
63
64
# File 'lib/timesteps/timesteppair.rb', line 62

def difference
  @difference
end

#fromObject (readonly)

Returns the value of attribute from.



62
63
64
# File 'lib/timesteps/timesteppair.rb', line 62

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



62
63
64
# File 'lib/timesteps/timesteppair.rb', line 62

def to
  @to
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if other has same contents of ‘from` and `to` as self has.

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/timesteps/timesteppair.rb', line 71

def == (other)
  return @from == other.from && 
         @to == other.to 
end

#forward(*indices, &block) ⇒ Object Also known as: []



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/timesteps/timesteppair.rb', line 80

def forward (*indices, &block)
  if indices.size == 1    
    index = indices.first.to_r
    if @numeric_conversion
      value = (index*@from_interval + @difference).quo(@to_interval)
    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        
        value = target.difference_in_years(@to_origin).quo(@to.numeric)
      when :months
        value = target.difference_in_months(@to_origin).quo(@to.numeric)
      else
        value = (target.ajd - @to_origin.ajd).quo(@to_interval)
      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

#forward_time(index) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/timesteps/timesteppair.rb', line 118

def forward_time (index)
  if @numeric_conversion
    return @to.time_at((index*@from_interval + @difference).quo(@to_interval))
  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
      index = (target.year - @from_origin.year).quo(@to.numeric)
    when :months
      ms = 12*(target.year - @from_origin.year)
      ms += target.month - @from_origin.month
      index = ms.quo(@to.numeric)
    else
      index = (target.ajd - @from_origin.ajd).quo(@to_interval)
    end
    return @to.time_at(index)
  end      
end

#inspectObject



64
65
66
# File 'lib/timesteps/timesteppair.rb', line 64

def inspect
  "#<TimeStep::Pair from='#{from.inspect}' to='#{to.inspect}'>"
end

#inverse(*indices, &block) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/timesteps/timesteppair.rb', line 142

def inverse (*indices, &block)
  if indices.size == 1    
    index = indices.first.to_r
    if @numeric_conversion
      value = (index*@to_interval - @difference).quo(@from_interval)
    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        
        value = target.difference_in_years(@from_origin).quo(@from.numeric)
      when :months
        value = target.difference_in_months(@from_origin).quo(@from.numeric)
      else
        value = (target.ajd - @from_origin.ajd).quo(@from_interval)
      end
      if block
        return block[value]
      else
        return value
      end
    end
  else
    if block
      return indices.map { |index| block[inverse(index)] }
    else
      return indices.map { |index| inverse(index) }
    end
  end
end

#inverse_time(index) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/timesteps/timesteppair.rb', line 178

def inverse_time (index)
  if @numeric_conversion
    return @from.time_at((index*@to_interval - @difference).quo(@from_interval))
  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
      index = (target.year - @to_origin.year).quo(@from.numeric)
    when :months
      ms = 12*(target.year - @to_origin.year)
      ms += target.month - @to_origin.month
      index = ms.quo(@from.numeric)
    else
      index = (target.ajd - @to_origin.ajd).quo(@from_interval)
    end
    return @from.time_at(index)
  end
end

#time_at(*indices) ⇒ Object



76
77
78
# File 'lib/timesteps/timesteppair.rb', line 76

def time_at (*indices)
  @from.time_at(*indices)
end