Class: RSMP::Clock

Inherits:
Object
  • Object
show all
Defined in:
lib/rsmp/helpers/clock.rb

Overview

Get the current time in UTC, with optional adjustment. Conversion to string uses the RSMP format 2015-06-08T12:01:39.654Z Use ‘Clock.to_s` to format times in RSMP format.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClock

Returns a new instance of Clock.



10
11
12
# File 'lib/rsmp/helpers/clock.rb', line 10

def initialize
  @adjustment = 0
end

Instance Attribute Details

#adjustmentObject (readonly)

Returns the value of attribute adjustment.



8
9
10
# File 'lib/rsmp/helpers/clock.rb', line 8

def adjustment
  @adjustment
end

Class Method Details

.nowObject



30
31
32
# File 'lib/rsmp/helpers/clock.rb', line 30

def self.now
  Time.now.utc
end

.parse(str) ⇒ Object



38
39
40
# File 'lib/rsmp/helpers/clock.rb', line 38

def self.parse(str)
  Time.parse(str)
end

.to_s(time = nil) ⇒ Object



34
35
36
# File 'lib/rsmp/helpers/clock.rb', line 34

def self.to_s(time = nil)
  (time || now).strftime('%FT%T.%3NZ')
end

Instance Method Details

#nowObject



22
23
24
# File 'lib/rsmp/helpers/clock.rb', line 22

def now
  Time.now.utc + @adjustment
end

#resetObject



18
19
20
# File 'lib/rsmp/helpers/clock.rb', line 18

def reset
  @adjustment = 0
end

#set(target) ⇒ Object



14
15
16
# File 'lib/rsmp/helpers/clock.rb', line 14

def set(target)
  @adjustment = target - Time.now
end

#to_sObject



26
27
28
# File 'lib/rsmp/helpers/clock.rb', line 26

def to_s
  Clock.to_s now
end