Module: Wavefront::Mixins

Included in:
Base
Defined in:
lib/wavefront-sdk/mixins.rb

Instance Method Summary collapse

Instance Method Details

#parse_time(t, ms = false) ⇒ Integer

Return a time as an integer, however it might come in.

Parameters:

  • t (Integer, String, Time)

    timestamp

  • ms (Boolean) (defaults to: false)

    whether to return epoch milliseconds

Returns:

  • (Integer)

    epoch time in seconds

Raises:

  • Wavefront::InvalidTimestamp



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/wavefront-sdk/mixins.rb', line 12

def parse_time(t, ms = false)
  #
  # Numbers, or things that look like numbers, pass straight
  # through. No validation, because maybe the user means one
  # second past the epoch, or the year 2525.
  #
  return t if t.is_a?(Integer)

  if t.is_a?(String)
    return t.to_i if t.match(/^\d+$/)
    begin
      t = DateTime.parse("#{t} #{Time.now.getlocal.zone}")
    rescue
      raise Wavefront::Exception::InvalidTimestamp
    end
  end

  ms ? t.to_datetime.strftime('%Q').to_i : t.strftime('%s').to_i
end