Module: PgVerify::TimeUtil

Defined in:
lib/pg-verify/core/util.rb

Constant Summary collapse

SECONDS_IN_SECOND =
1
SECONDS_IN_MINUTE =
SECONDS_IN_SECOND * 60
SECONDS_IN_HOUR =
SECONDS_IN_MINUTE * 60
SECONDS_IN_DAY =
SECONDS_IN_HOUR   * 24
SECONDS_IN_WEEK =
SECONDS_IN_DAY    * 7
SECONDS_IN_MONTH =
SECONDS_IN_WEEK   * 4
SECONDS_IN_YEAR =
SECONDS_IN_MONTH  * 12

Class Method Summary collapse

Class Method Details

.ago_h(start_time, short: false) ⇒ Object



38
39
40
# File 'lib/pg-verify/core/util.rb', line 38

def self.ago_h(start_time, short: false)
    duration_to_h((Time.now - start_time).to_i, short: short) + " ago"
end

.duration_string(seconds, short: false) ⇒ Object



14
15
16
17
18
# File 'lib/pg-verify/core/util.rb', line 14

def self.duration_string(seconds, short: false)
    i_seconds = seconds.to_i
    return "~#{'%.2f' % (seconds*1000)}#{short ? "ms" : " milliseconds"}" if i_seconds == 0
    return duration_to_h(i_seconds, short: short)
end

.duration_to_h(seconds, short: false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pg-verify/core/util.rb', line 20

def self.duration_to_h(seconds, short: false)
    seconds = seconds.to_i
    {  
        SECONDS_IN_YEAR   => ( short ? "y" : " year"   ),
        SECONDS_IN_MONTH  => ( short ? "M" : " month"  ),
        SECONDS_IN_WEEK   => ( short ? "w" : " week"   ),
        SECONDS_IN_DAY    => ( short ? "d" : " day"    ),
        SECONDS_IN_HOUR   => ( short ? "h" : " hour"   ),
        SECONDS_IN_MINUTE => ( short ? "m" : " minute" ),
        SECONDS_IN_SECOND => ( short ? "s" : " second" )
    }.each do |unit_seconds, unit_string|
        units = seconds / unit_seconds
        unit_string = unit_string + "s" if !short && units != 1
        return "#{units}#{unit_string} #{duration_to_h(seconds % unit_seconds, short: short)}".strip unless units == 0
    end
    nil
end

.from_timestamp(ts) ⇒ Object



46
47
48
49
# File 'lib/pg-verify/core/util.rb', line 46

def self.from_timestamp(ts)
    time = Time.at(ts.to_i).utc
    return time
end

.timestamp(time = Time.new) ⇒ Object



42
43
44
# File 'lib/pg-verify/core/util.rb', line 42

def self.timestamp(time = Time.new)
    return time.utc.to_i
end