Class: Wavefront::ParseTime

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront-sdk/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(t, ms = false) ⇒ ParseTime

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



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

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

Instance Attribute Details

#msObject (readonly)

Returns the value of attribute ms.



7
8
9
# File 'lib/wavefront-sdk/parse_time.rb', line 7

def ms
  @ms
end

#tObject (readonly)

Returns the value of attribute t.



7
8
9
# File 'lib/wavefront-sdk/parse_time.rb', line 7

def t
  @t
end

Instance Method Details

#parse!Object



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

def parse!
  method = ('parse_time_' + t.class.name).to_sym
  send(method)
rescue
  raise Wavefront::Exception::InvalidTimestamp
end

#parse_time_DateTimeObject



47
48
49
# File 'lib/wavefront-sdk/parse_time.rb', line 47

def parse_time_DateTime
  parse_time_Time
end

#parse_time_FixnumFixnum

Returns timestamp.

Returns:

  • (Fixnum)

    timestamp



19
20
21
# File 'lib/wavefront-sdk/parse_time.rb', line 19

def parse_time_Fixnum
  t
end

#parse_time_IntegerInteger

Returns timestamp.

Returns:

  • (Integer)

    timestamp



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

def parse_time_Integer
  t
end

#parse_time_StringFixnum

Returns timestamp.

Returns:

  • (Fixnum)

    timestamp



31
32
33
34
35
# File 'lib/wavefront-sdk/parse_time.rb', line 31

def parse_time_String
  return t.to_i if t =~ /^\d+$/
  @t = DateTime.parse("#{t} #{Time.now.getlocal.zone}")
  parse_time_Time
end

#parse_time_TimeInteger

Returns timestamp.

Returns:

  • (Integer)

    timestamp



39
40
41
42
43
44
45
# File 'lib/wavefront-sdk/parse_time.rb', line 39

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