Class: LogStash::Util::TimeValue

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/util/time_value.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(duration, time_unit) ⇒ TimeValue

Returns a new instance of TimeValue.



4
5
6
7
# File 'lib/logstash/util/time_value.rb', line 4

def initialize(duration, time_unit)
  @duration = duration
  @time_unit = time_unit
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



66
67
68
# File 'lib/logstash/util/time_value.rb', line 66

def duration
  @duration
end

#time_unitObject (readonly)

Returns the value of attribute time_unit.



67
68
69
# File 'lib/logstash/util/time_value.rb', line 67

def time_unit
  @time_unit
end

Class Method Details

.from_value(value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/logstash/util/time_value.rb', line 9

def self.from_value(value)
  if value.is_a?(TimeValue)
    TimeValue.new(value.duration, value.time_unit)
  elsif value.is_a?(::String)
    normalized = value.downcase.strip
    if normalized.end_with?("nanos")
      TimeValue.new(parse(normalized, 5), :nanosecond)
    elsif normalized.end_with?("micros")
      TimeValue.new(parse(normalized, 6), :microsecond)
    elsif normalized.end_with?("ms")
      TimeValue.new(parse(normalized, 2), :millisecond)
    elsif normalized.end_with?("s")
      TimeValue.new(parse(normalized, 1), :second)
    elsif normalized.end_with?("m")
      TimeValue.new(parse(normalized, 1), :minute)
    elsif normalized.end_with?("h")
      TimeValue.new(parse(normalized, 1), :hour)
    elsif normalized.end_with?("d")
      TimeValue.new(parse(normalized, 1), :day)
    elsif normalized =~ /^-0*1/
      TimeValue.new(-1, :nanosecond)
    else
      raise ArgumentError.new("invalid time unit: \"#{value}\"")
    end
  else
    raise ArgumentError.new("value is not a string: #{value} [#{value.class}]")
  end
end

Instance Method Details

#==(other) ⇒ Object



57
58
59
# File 'lib/logstash/util/time_value.rb', line 57

def ==(other)
  self.duration == other.duration and self.time_unit == other.time_unit
end

#to_nanosObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/logstash/util/time_value.rb', line 38

def to_nanos
  case @time_unit
  when :day
    86400000000000 * @duration
  when :hour
    3600000000000 * @duration
  when :minute
    60000000000 * @duration
  when :second
    1000000000 * @duration
  when :millisecond
    1000000 * @duration
  when :microsecond
    1000 * @duration
  when :nanosecond
    @duration
  end
end