Class: Fluent::NumericTimeParser
- Inherits:
-
TimeParser
- Object
- TimeParser
- Fluent::NumericTimeParser
- Defined in:
- lib/fluent/time.rb
Overview
to include TimeParseError
Instance Method Summary collapse
-
#initialize(type, localtime = nil, timezone = nil) ⇒ NumericTimeParser
constructor
A new instance of NumericTimeParser.
-
#parse_float(value) ⇒ Object
rough benchmark result to compare handmade parser vs Fluent::EventTime.from_time(Time.at(value.to_r)) full: with 9-digits of nsec after dot msec: with 3-digits of msec after dot 10_000_000 times loop on MacBookAir parse_by_myself(full): 12.162475 sec parse_by_myself(msec): 15.050435 sec parse_by_to_r (full): 28.722362 sec parse_by_to_r (msec): 28.232856 sec.
- #parse_unixtime(value) ⇒ Object
Methods inherited from TimeParser
Constructor Details
#initialize(type, localtime = nil, timezone = nil) ⇒ NumericTimeParser
Returns a new instance of NumericTimeParser.
245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/fluent/time.rb', line 245 def initialize(type, localtime = nil, timezone = nil) @cache1_key = @cache1_time = @cache2_key = @cache2_time = nil if type == :unixtime define_singleton_method(:parse, method(:parse_unixtime)) define_singleton_method(:call, method(:parse_unixtime)) else # :float define_singleton_method(:parse, method(:parse_float)) define_singleton_method(:call, method(:parse_float)) end end |
Instance Method Details
#parse_float(value) ⇒ Object
rough benchmark result to compare handmade parser vs Fluent::EventTime.from_time(Time.at(value.to_r)) full: with 9-digits of nsec after dot msec: with 3-digits of msec after dot 10_000_000 times loop on MacBookAir parse_by_myself(full): 12.162475 sec parse_by_myself(msec): 15.050435 sec parse_by_to_r (full): 28.722362 sec parse_by_to_r (msec): 28.232856 sec
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/fluent/time.rb', line 288 def parse_float(value) unless value.is_a?(String) || value.is_a?(Numeric) raise TimeParseError, "value must be a string or a number: #{value}(value.class)" end if @cache1_key == value return @cache1_time elsif @cache2_key == value return @cache2_time end begin sec_s, nsec_s, _ = value.to_s.split('.', 3) # throw away second-dot and later nsec_s = nsec_s && nsec_s[0..9] || '0' nsec_s += '0' * (9 - nsec_s.size) if nsec_s.size < 9 time = Fluent::EventTime.new(sec_s.to_i, nsec_s.to_i) rescue => e raise TimeParseError, "invalid time format: value = #{value}, error_class = #{e.class.name}, error = #{e.}" end @cache1_key = @cache2_key @cache1_time = @cache2_time @cache2_key = value @cache2_time = time time end |
#parse_unixtime(value) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/fluent/time.rb', line 257 def parse_unixtime(value) unless value.is_a?(String) || value.is_a?(Numeric) raise TimeParseError, "value must be a string or a number: #{value}(value.class)" end if @cache1_key == value return @cache1_time elsif @cache2_key == value return @cache2_time end begin time = Fluent::EventTime.new(value.to_i) rescue => e raise TimeParseError, "invalid time format: value = #{value}, error_class = #{e.class.name}, error = #{e.}" end @cache1_key = @cache2_key @cache1_time = @cache2_time @cache2_key = value @cache2_time = time time end |