Module: Farce::Clock
- Extended by:
- Clock
- Included in:
- Clock
- Defined in:
- lib/farce/clock.rb,
lib/farce/integrations/active_support/clock.rb
Overview
Wrapper module for clock-related functionality.
Converts everything to a monotonic clock time in seconds, from when farce/clock was loaded.
ActiveSupport Integration collapse
-
.parse(value) ⇒ BasicObject
Treat ActiveSupport durations as relative offsets.
Instance Method Summary collapse
-
#clock(value) ⇒ Float
Converts the given value to a monotonic clock time in seconds, assuming that it already represents a clock time.
-
#current ⇒ Float
(also: #now)
Returns the current monotonic clock time in seconds.
-
#offset(value) ⇒ Float
(also: #delay, #in, #timeout, #wait)
Converts the given value to a monotonic clock time in seconds.
-
#parse(value = nil) ⇒ Float
Converts the given value to a monotonic clock time in seconds.
-
#time(value) ⇒ Float
(also: #at, #timeout_at)
Converts the given value to a monotonic clock time in seconds.
Class Method Details
.parse(value) ⇒ BasicObject
This methods is only available if ActiveSupport has been loaded.
Treat ActiveSupport durations as relative offsets.
12 13 14 15 |
# File 'lib/farce/integrations/active_support/clock.rb', line 12 def self.parse(value) return offset(value) if ActiveSupport::Duration === value super end |
Instance Method Details
#clock(value) ⇒ Float
Converts the given value to a monotonic clock time in seconds, assuming that it already represents a clock time. Returns the current clock time if no value is given.
Same as calling parse(clock: value)
28 |
# File 'lib/farce/clock.rb', line 28 def clock(value) = (value ? value.to_f : current) |
#current ⇒ Float Also known as: now
Returns the current monotonic clock time in seconds.
32 |
# File 'lib/farce/clock.rb', line 32 def current = Process.clock_gettime(CLOCK_MONOTONIC) - MONOTONIC |
#offset(value) ⇒ Float Also known as: delay, in, timeout, wait
Converts the given value to a monotonic clock time in seconds. The value is assumed to be a relative offset from the current time.
101 102 103 104 |
# File 'lib/farce/clock.rb', line 101 def offset(value) return (current + value.to_f) if value.is_a?(Numeric) raise TypeError, "Cannot convert #{value.class} to clock time" end |
#parse(value = nil) ⇒ Float
Converts the given value to a monotonic clock time in seconds.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/farce/clock.rb', line 55 def parse(value = nil) case value when nil, UNDEFINED then return current when Float, Integer then return value > CUTOFF_CLOCK ? time(value) : offset(value) when Time then return time(value) when Hash case value.size when 0 then return current when 1 return time(value[:at]) if value.key?(:at) method = argument = nil value.each_pair { |key, option| method, argument = key, option } return public_send(method, argument) end else return at(value.to_time) if value.respond_to?(:to_time) return parse(value.to_f) if value.is_a?(Numeric) end raise TypeError, "Cannot convert #{value.inspect} to clock time" end |
#time(value) ⇒ Float Also known as: at, timeout_at
Converts the given value to a monotonic clock time in seconds. The value is assumed to be a fixed point in time (independent of the current time).
If the value is numeric and greater than one billion, it is treated as a UNIX timestamp. Meaning that you can't pass a clock time greater than 31 years, or a timestamp before September 9, 2001.
85 86 87 88 89 90 91 92 93 |
# File 'lib/farce/clock.rb', line 85 def time(value) case value when Float, Integer then = value > CUTOFF_TIME ? value - REAL_TIME : Float(value) when Time then = value.to_f - REAL_TIME when Numeric then return time(value.to_f) else raise TypeError, "Cannot convert #{value.class} to clock time" end () end |