Class: Currency::Exchange::TimeQuantitizer
- Defined in:
- lib/currency/exchange/time_quantitizer.rb
Overview
Currency::Exchange::TimeQuantitizer
The Currency::Exchange::TimeQuantitizer quantitizes time values such that money values and rates at a given time can be turned into a hash key, depending on the rate source’s temporal accuracy.
Instance Attribute Summary collapse
-
#time_quant_offset ⇒ Object
Time quantization offset in seconds.
-
#time_quant_size ⇒ Object
Time quantitization size.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(*opt) ⇒ TimeQuantitizer
constructor
A new instance of TimeQuantitizer.
-
#quantitize_time(time) ⇒ Object
Normalizes time to a quantitized value.
-
#quantitize_time_range(time) ⇒ Object
Returns a Range of Time such that:.
-
#to_s ⇒ Object
Returns a simple string rep.
Constructor Details
#initialize(*opt) ⇒ TimeQuantitizer
Returns a new instance of TimeQuantitizer.
26 27 28 29 30 31 |
# File 'lib/currency/exchange/time_quantitizer.rb', line 26 def initialize(*opt) @time_quant_size ||= 60 * 60 * 24 @time_quant_offset ||= nil opt = Hash[*opt] opt.each_pair{|k,v| self.send("#{k}=", v)} end |
Instance Attribute Details
#time_quant_offset ⇒ Object
Time quantization offset in seconds. This is applied to epoch time before quantization. If nil, uses Time#utc_offset. Defaults to nil.
24 25 26 |
# File 'lib/currency/exchange/time_quantitizer.rb', line 24 def time_quant_offset @time_quant_offset end |
#time_quant_size ⇒ Object
Time quantitization size. Defaults to 1 day.
18 19 20 |
# File 'lib/currency/exchange/time_quantitizer.rb', line 18 def time_quant_size @time_quant_size end |
Class Method Details
.current ⇒ Object
13 |
# File 'lib/currency/exchange/time_quantitizer.rb', line 13 def self.current; @current ||= self.new; end |
.current=(x) ⇒ Object
14 |
# File 'lib/currency/exchange/time_quantitizer.rb', line 14 def self.current=(x); @current = x; end |
Instance Method Details
#quantitize_time(time) ⇒ Object
Normalizes time to a quantitized value. For example: a time_quant_size of 60 * 60 * 24 will truncate a rate time to a particular day.
Subclasses can override this method.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/currency/exchange/time_quantitizer.rb', line 39 def quantitize_time(time) # If nil, then nil. return time unless time # Get bucket parameters. was_utc = time.utc? quant_offset = time_quant_offset quant_offset ||= time.utc_offset # $stderr.puts "quant_offset = #{quant_offset}" quant_size = time_quant_size.to_i # Get offset from epoch. time = time.tv_sec # Remove offset (timezone) time += quant_offset # Truncate to quantitize size. time = (time.to_i / quant_size) * quant_size # Add offset (timezone) time -= quant_offset # Convert back to Time object. time = Time.at(time) # Quant to day? # NOTE: is this due to a Ruby bug, or # some wierd UTC time-flow issue, like leap-seconds. if quant_size == 60 * 60 * 24 time = time + 60 * 60 if was_utc time = time.getutc time = Time.utc(time.year, time.month, time.day, 0, 0, 0, 0) else time = Time.local(time.year, time.month, time.day, 0, 0, 0, 0) end end # Convert back to UTC? time = time.getutc if was_utc time end |
#quantitize_time_range(time) ⇒ Object
Returns a Range of Time such that:
range.include?(time)
! range.include?(time + time_quant_size)
! range.include?(time - time_quant_size)
range.exclude_end?
The range.max is end-exclusive to avoid precision issues:
t = Time.now
=> Thu Feb 15 15:32:34 EST 2007
x.quantitize_time_range(t)
=> Thu Feb 15 00:00:00 EST 2007...Fri Feb 16 00:00:00 EST 2007
98 99 100 101 102 |
# File 'lib/currency/exchange/time_quantitizer.rb', line 98 def quantitize_time_range(time) time_0 = quantitize_time(time) time_1 = time_0 + time_quant_size.to_i time_0 ... time_1 end |
#to_s ⇒ Object
Returns a simple string rep.
105 106 107 |
# File 'lib/currency/exchange/time_quantitizer.rb', line 105 def to_s "#<#{self.class.name} #{quant_offset} #{quant_size}>" end |