about summary refs log tree commit homepage
path: root/lib/yahns.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/yahns.rb')
-rw-r--r--lib/yahns.rb13
1 files changed, 13 insertions, 0 deletions
diff --git a/lib/yahns.rb b/lib/yahns.rb
index a55837f..fd84223 100644
--- a/lib/yahns.rb
+++ b/lib/yahns.rb
@@ -55,6 +55,19 @@ module Yahns # :nodoc:
 
   class ClientTimeout < RuntimeError # :nodoc:
   end
+
+  # try to use the monotonic clock in Ruby >= 2.1, it is immune to clock
+  # offset adjustments and generates less garbage (Float vs Time object)
+  begin
+    Process.clock_gettime(Process::CLOCK_MONOTONIC)
+    def self.now
+      Process.clock_gettime(Process::CLOCK_MONOTONIC)
+    end
+  rescue NameError, NoMethodError
+    def self.now # Ruby <= 2.0
+      Time.now.to_f
+    end
+  end
 end
 
 # FIXME: require lazily