Module: Wavefront::Mixins
- Included in:
- Base
- Defined in:
- lib/wavefront-sdk/mixins.rb
Instance Method Summary collapse
- #parse_relative_time(t, ms = false) ⇒ Object
-
#parse_time(t, ms = false) ⇒ Integer
Return a time as an integer, however it might come in.
-
#relative_time(t, ms = false, ref = DateTime.now) ⇒ Integer
Return a timetamp described by the given string.
-
#time_multiplier(suffix) ⇒ Integer
naively return the number of seconds from the given multiplier.
Instance Method Details
#parse_relative_time(t, ms = false) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/wavefront-sdk/mixins.rb', line 57 def parse_relative_time(t, ms = false) unless t.start_with?('+') || t.start_with?('-') raise Wavefront::Exception::InvalidRelativeTime end m = ms ? 1000 : 1 t = t[1..-1] if t.start_with?('+') match = t.match(/^(-?\d*\.?\d*)([smhdwy])$/) (match[1].to_f * time_multiplier(match[2]) * m).to_i rescue NoMethodError raise Wavefront::Exception::InvalidRelativeTime end |
#parse_time(t, ms = false) ⇒ Integer
Return a time as an integer, however it might come in.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/wavefront-sdk/mixins.rb', line 15 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?(Numeric) if t.is_a?(String) return t.to_i if t.match(/^\d+$/) if t.start_with?('-') || t.start_with?('+') return relative_time(t, ms) end 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 |
#relative_time(t, ms = false, ref = DateTime.now) ⇒ Integer
Return a timetamp described by the given string. That is, '+5m' is five minutes in the future, and '-.1h' is half an hour ago.
52 53 54 55 |
# File 'lib/wavefront-sdk/mixins.rb', line 52 def relative_time(t, ms = false, ref = DateTime.now) ref = ms ? ref.to_date.strftime('%Q') : ref.to_time ref.to_i + parse_relative_time(t, ms) end |
#time_multiplier(suffix) ⇒ Integer
naively return the number of seconds from the given multiplier. This makes absolutely no attempt to compensate for any kind of daylight savings or calendar adjustment. A day is always going to 60 seconds x 60 minutes x 24 hours, and a year will always have 365 days.
82 83 84 85 86 87 |
# File 'lib/wavefront-sdk/mixins.rb', line 82 def time_multiplier(suffix) u = { s: 1, m: 60, h: 3600, d: 86400, w: 604800, y: 31536000 } return u[suffix.to_sym] if u.key?(suffix.to_sym) raise Wavefront::Exception::InvalidTimeUnit end |