Class: CiscoWebex::Toolbox::WebexTime

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

Class Method Summary collapse

Class Method Details

.consistent_timestamp(timestamp = "now") ⇒ Object

converts possible time type to `Time` format for easier parsing



37
38
39
40
41
42
43
44
# File 'lib/Toolbox/Toolbox.rb', line 37

def self.consistent_timestamp(timestamp="now")
    return timestamp if timestamp.class == Time
    return Time.now() if timestamp.class == String && timestamp.downcase == "now"
    return Time.parse(timestamp) if timestamp.class == String
    return Time.at(timestamp) if timestamp.class == Integer
    return Time.parse(timestamp.to_s) if timestamp.class == DateTime
    return self.error()
end

.errorObject



27
28
29
30
# File 'lib/Toolbox/Toolbox.rb', line 27

def self.error()
    STDERR.puts "Error CiscoWebex::Toolbox::WebexTime: Could not format/transform time from timestamp"
    return false
end

.format(timestamp, time_format = "control_hub") ⇒ Object

Format time based on default for Controlhub CC open for epoch



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/Toolbox/Toolbox.rb', line 47

def self.format(timestamp, time_format="control_hub")
    timestamp = self.consistent_timestamp(timestamp)

    if time_format.downcase() == "cc" || time_format.downcase() =~ /contact.*/
        return timestamp.strftime("%s").to_i rescue self.error()

    elsif time_format.downcase() == "epoch" || time_format.downcase() == "ms"  
        return timestamp.strftime("%s").to_i * 1000

    else
        return timestamp.strftime("%Y-%m-%dT%H:%M:%S.000Z") rescue self.error()

    end
end

.nowObject



32
33
34
# File 'lib/Toolbox/Toolbox.rb', line 32

def self.now()
    return Time.now()
end

.round(timestamp = "now", interval = 15, time_format = "control_hub") ⇒ Object

round time to nearest 15min default



71
72
73
74
75
# File 'lib/Toolbox/Toolbox.rb', line 71

def self.round(timestamp="now", interval=15, time_format="control_hub")
    timestamp = self.consistent_timestamp(timestamp)
    interval = interval.to_i
    return self.format(Time.at((timestamp.to_i / (interval*60)).round * interval*60).to_i, time_format) rescue self.error()
end

.shift(timestamp = "now", shift_seconds = 60, time_format = "control_hub", round = 0) ⇒ Object

shift curret time by X seconds round if required



63
64
65
66
67
68
# File 'lib/Toolbox/Toolbox.rb', line 63

def self.shift(timestamp="now", shift_seconds=60, time_format="control_hub", round=0)
    timestamp = self.consistent_timestamp(timestamp)
    timestamp = self.consistent_timestamp(timestamp.to_i + shift_seconds)
    timestamp = self.round(timestamp, round, time_format) if round.to_i > 0
    return self.format(timestamp, time_format) rescue self.error()
end