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
Returns the current monotonic clock time in seconds.
-
#offset(value) ⇒ Float
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.
-
#remaining(deadline) ⇒ Float, ...
Returns the seconds left until a monotonic clock deadline.
-
#time(value) ⇒ Float
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
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
Converts the given value to a monotonic clock time in seconds. The value is assumed to be a relative offset from the current time.
113 114 115 116 |
# File 'lib/farce/clock.rb', line 113 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.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/farce/clock.rb', line 67 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 |
#remaining(deadline) ⇒ Float, ...
Returns the seconds left until a monotonic clock deadline. Expired deadlines return zero. Nil means there is no deadline and returns nil.
40 41 42 43 44 |
# File 'lib/farce/clock.rb', line 40 def remaining(deadline) return if deadline.nil? remaining = (deadline - now) remaining.positive? ? remaining : 0 end |
#time(value) ⇒ Float
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.
97 98 99 100 101 102 103 104 105 |
# File 'lib/farce/clock.rb', line 97 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 |