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.
293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/fluent/time.rb', line 293 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
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/fluent/time.rb', line 336 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
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/fluent/time.rb', line 305 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 |