@@ -10,25 +10,54 @@ def initialize
1010
1111 if defined? ( Process ::CLOCK_MONOTONIC )
1212 # @!visibility private
13- def get_time
14- Process . clock_gettime ( Process ::CLOCK_MONOTONIC )
13+ def get_time ( unit )
14+ Process . clock_gettime ( Process ::CLOCK_MONOTONIC , unit )
1515 end
1616 elsif Concurrent . on_jruby?
1717 # @!visibility private
18- def get_time
19- java . lang . System . nanoTime ( ) / 1_000_000_000.0
18+ self ::TIME_UNITS = Hash . new { |_hash , key | raise ArgumentError , "unexpected unit: #{ key } " } . compare_by_identity
19+ private_constant :TIME_UNITS
20+ TIME_UNITS . merge! (
21+ second : 1_000_000_000 ,
22+ millisecond : 1_000_000 ,
23+ microsecond : 1_000 ,
24+ nanosecond : 1 ,
25+ float_second : 1_000_000_000.0 ,
26+ float_millisecond : 1_000_000.0 ,
27+ float_microsecond : 1_000.0 ,
28+ )
29+
30+ # @!visibility private
31+ def get_time ( unit )
32+ java . lang . System . nanoTime ( ) / TIME_UNITS [ unit ]
2033 end
2134 else
35+ # @!visibility private
36+ self ::TIME_UNITS = Hash . new { |_hash , key | raise ArgumentError , "unexpected unit: #{ key } " } . compare_by_identity
37+ private_constant :TIME_UNITS
38+ TIME_UNITS . merge! (
39+ second : [ nil , true ] ,
40+ millisecond : [ 1_000 , true ] ,
41+ microsecond : [ 1_000_000 , true ] ,
42+ nanosecond : [ 1_000_000_000 , true ] ,
43+ float_second : [ nil , false ] ,
44+ float_millisecond : [ 1_000.0 , false ] ,
45+ float_microsecond : [ 1_000_000.0 , false ] ,
46+ )
2247
2348 # @!visibility private
24- def get_time
49+ def get_time ( unit )
2550 synchronize do
2651 now = Time . now . to_f
2752 if @last_time < now
2853 @last_time = now
2954 else # clock has moved back in time
3055 @last_time += 0.000_001
3156 end
57+ scale , to_int = TIME_UNITS [ unit ]
58+ now *= scale if scale
59+ now = now . to_i if to_int
60+ now
3261 end
3362 end
3463
@@ -46,12 +75,16 @@ def get_time
4675 #
4776 # Returns the current time a tracked by the application monotonic clock.
4877 #
78+ # @param [Symbol] unit the time unit to be returned, can be either
79+ # :float_second, :float_millisecond, :float_microsecond, :second,
80+ # :millisecond, :microsecond, or :nanosecond default to :float_second.
81+ #
4982 # @return [Float] The current monotonic time since some unspecified
5083 # starting point
5184 #
5285 # @!macro monotonic_clock_warning
53- def monotonic_time
54- GLOBAL_MONOTONIC_CLOCK . get_time
86+ def monotonic_time ( unit = :float_second )
87+ GLOBAL_MONOTONIC_CLOCK . get_time ( unit )
5588 end
5689
5790 module_function :monotonic_time
0 commit comments