Skip to content

Commit d695c6f

Browse files
authored
Require Ruby >= 2.1.0 (#423)
1 parent ce3e6bb commit d695c6f

4 files changed

Lines changed: 178 additions & 184 deletions

File tree

lib/timecop/time_extensions.rb

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -168,58 +168,56 @@ def mocked_time_stack_item
168168
end
169169
end
170170

171-
if RUBY_VERSION >= '2.1.0'
172-
module Process #:nodoc:
173-
class << self
174-
alias_method :clock_gettime_without_mock, :clock_gettime
175-
176-
def clock_gettime_mock_time(clock_id, unit = :float_second)
177-
mock_time = case clock_id
178-
when Process::CLOCK_MONOTONIC
179-
mock_time_monotonic
180-
when Process::CLOCK_REALTIME
181-
mock_time_realtime
182-
end
183-
184-
return clock_gettime_without_mock(clock_id, unit) unless Timecop.mock_process_clock? && mock_time
185-
186-
divisor = case unit
187-
when :float_second
188-
1_000_000_000.0
189-
when :second
190-
1_000_000_000
191-
when :float_millisecond
192-
1_000_000.0
193-
when :millisecond
194-
1_000_000
195-
when :float_microsecond
196-
1000.0
197-
when :microsecond
198-
1000
199-
when :nanosecond
200-
1
171+
module Process #:nodoc:
172+
class << self
173+
alias_method :clock_gettime_without_mock, :clock_gettime
174+
175+
def clock_gettime_mock_time(clock_id, unit = :float_second)
176+
mock_time = case clock_id
177+
when Process::CLOCK_MONOTONIC
178+
mock_time_monotonic
179+
when Process::CLOCK_REALTIME
180+
mock_time_realtime
201181
end
202182

203-
(mock_time / divisor)
204-
end
183+
return clock_gettime_without_mock(clock_id, unit) unless Timecop.mock_process_clock? && mock_time
184+
185+
divisor = case unit
186+
when :float_second
187+
1_000_000_000.0
188+
when :second
189+
1_000_000_000
190+
when :float_millisecond
191+
1_000_000.0
192+
when :millisecond
193+
1_000_000
194+
when :float_microsecond
195+
1000.0
196+
when :microsecond
197+
1000
198+
when :nanosecond
199+
1
200+
end
201+
202+
(mock_time / divisor)
203+
end
205204

206-
alias_method :clock_gettime, :clock_gettime_mock_time
205+
alias_method :clock_gettime, :clock_gettime_mock_time
207206

208-
private
207+
private
209208

210-
def mock_time_monotonic
211-
mocked_time_stack_item = Timecop.top_stack_item
212-
mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.monotonic
213-
end
209+
def mock_time_monotonic
210+
mocked_time_stack_item = Timecop.top_stack_item
211+
mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.monotonic
212+
end
214213

215-
def mock_time_realtime
216-
mocked_time_stack_item = Timecop.top_stack_item
214+
def mock_time_realtime
215+
mocked_time_stack_item = Timecop.top_stack_item
217216

218-
return nil if mocked_time_stack_item.nil?
217+
return nil if mocked_time_stack_item.nil?
219218

220-
t = mocked_time_stack_item.time
221-
t.to_i * 1_000_000_000 + t.nsec
222-
end
219+
t = mocked_time_stack_item.time
220+
t.to_i * 1_000_000_000 + t.nsec
223221
end
224222
end
225223
end

lib/timecop/time_stack_item.rb

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize(mock_type, *args)
99
@travel_offset = @scaling_factor = nil
1010
@scaling_factor = args.shift if mock_type == :scale
1111
@mock_type = mock_type
12-
@monotonic = parse_monotonic_time(*args) if RUBY_VERSION >= '2.1.0'
12+
@monotonic = parse_monotonic_time(*args)
1313
@time = parse_time(*args)
1414
@time_was = Time.now_without_mock_time
1515
@travel_offset = compute_travel_offset
@@ -55,24 +55,22 @@ def scaling_factor
5555
@scaling_factor
5656
end
5757

58-
if RUBY_VERSION >= '2.1.0'
59-
def monotonic
60-
if travel_offset.nil?
61-
@monotonic
62-
elsif scaling_factor.nil?
63-
current_monotonic + travel_offset * (10 ** 9)
64-
else
65-
(@monotonic + (current_monotonic - @monotonic) * scaling_factor).to_i
66-
end
58+
def monotonic
59+
if travel_offset.nil?
60+
@monotonic
61+
elsif scaling_factor.nil?
62+
current_monotonic + travel_offset * (10 ** 9)
63+
else
64+
(@monotonic + (current_monotonic - @monotonic) * scaling_factor).to_i
6765
end
66+
end
6867

69-
def current_monotonic
70-
Process.clock_gettime_without_mock(Process::CLOCK_MONOTONIC, :nanosecond)
71-
end
68+
def current_monotonic
69+
Process.clock_gettime_without_mock(Process::CLOCK_MONOTONIC, :nanosecond)
70+
end
7271

73-
def current_monotonic_with_mock
74-
Process.clock_gettime_mock_time(Process::CLOCK_MONOTONIC, :nanosecond)
75-
end
72+
def current_monotonic_with_mock
73+
Process.clock_gettime_mock_time(Process::CLOCK_MONOTONIC, :nanosecond)
7674
end
7775

7876
def time(time_klass = Time) #:nodoc:

0 commit comments

Comments
 (0)