Class: Wavefront::ParseTime

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront-sdk/support/parse_time.rb

Overview

Parse various times into integers. This class is not for direct consumption: it’s used by the mixins parse_time method, which does all the type sanity stuff.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time, in_ms = false) ⇒ ParseTime

param t [Numeric] a timestamp param ms [Bool] whether the timestamp is in milliseconds



17
18
19
20
# File 'lib/wavefront-sdk/support/parse_time.rb', line 17

def initialize(time, in_ms = false)
  @t = time
  @ms = in_ms
end

Instance Attribute Details

#msObject (readonly)

Returns the value of attribute ms.



12
13
14
# File 'lib/wavefront-sdk/support/parse_time.rb', line 12

def ms
  @ms
end

#tObject (readonly)

Returns the value of attribute t.



12
13
14
# File 'lib/wavefront-sdk/support/parse_time.rb', line 12

def t
  @t
end

Instance Method Details

#parse!Object



57
58
59
60
61
62
# File 'lib/wavefront-sdk/support/parse_time.rb', line 57

def parse!
  method = :"parse_time_#{t.class.name.downcase}"
  send(method)
rescue StandardError
  raise Wavefront::Exception::InvalidTimestamp, t
end

#parse_time_datetimeObject



53
54
55
# File 'lib/wavefront-sdk/support/parse_time.rb', line 53

def parse_time_datetime
  parse_time_time
end

#parse_time_fixnumFixnum

Returns timestamp.

Returns:

  • (Fixnum)

    timestamp



24
25
26
# File 'lib/wavefront-sdk/support/parse_time.rb', line 24

def parse_time_fixnum
  t
end

#parse_time_integerInteger

Returns timestamp.

Returns:

  • (Integer)

    timestamp



30
31
32
# File 'lib/wavefront-sdk/support/parse_time.rb', line 30

def parse_time_integer
  t
end

#parse_time_stringFixnum

Returns timestamp.

Returns:

  • (Fixnum)

    timestamp



36
37
38
39
40
41
# File 'lib/wavefront-sdk/support/parse_time.rb', line 36

def parse_time_string
  return t.to_i if t.match?(/^\d+$/)

  @t = Time.parse("#{t} #{Time.now.getlocal.zone}")
  parse_time_time
end

#parse_time_timeInteger

Returns timestamp.

Returns:

  • (Integer)

    timestamp



45
46
47
48
49
50
51
# File 'lib/wavefront-sdk/support/parse_time.rb', line 45

def parse_time_time
  if ms
    t.to_datetime.strftime('%Q').to_i
  else
    t.strftime('%s').to_i
  end
end