Class: Grafana::Timestamp

Inherits:
Object
  • Object
show all
Defined in:
lib/grafana/time_window.rb

Overview

Offers a consistent API for timestamps originating from Grafana or other sources, allowing for formatting of timestamps as consumed by Grafana-related utilities

Constant Summary collapse

Error =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time) ⇒ Timestamp

Returns a new instance of Timestamp.

Parameters:

  • timestamp (Time)

91
92
93
# File 'lib/grafana/time_window.rb', line 91

def initialize(time)
  @time = time
end

Instance Attribute Details

#timeObject

Returns the value of attribute time.


88
89
90
# File 'lib/grafana/time_window.rb', line 88

def time
  @time
end

Class Method Details

.from_ms_since_epoch(time) ⇒ Object

Parameters:

  • time (String)

    Representing milliseconds since epoch. This is what JS “decided” unix is.

Raises:


109
110
111
112
113
114
115
# File 'lib/grafana/time_window.rb', line 109

def from_ms_since_epoch(time)
  return if time.nil?

  raise Error, 'Expected milliseconds since epoch' unless ms_since_epoch?(time)

  new(cast_ms_to_time(time))
end

Instance Method Details

#formattedObject

Formats a timestamp from Grafana for compatibility with parsing in JS via `new Date(timestamp)`


97
98
99
# File 'lib/grafana/time_window.rb', line 97

def formatted
  time.utc.strftime('%FT%TZ')
end

#to_msObject

Converts to milliseconds since epoch


102
103
104
# File 'lib/grafana/time_window.rb', line 102

def to_ms
  time.to_i * 1000
end