Class: Timecop::TimeStackItem
- Inherits:
-
Object
- Object
- Timecop::TimeStackItem
- Defined in:
- lib/timecop/time_stack_item.rb
Overview
A data class for carrying around “time movement” objects. Makes it easy to keep track of the time movements on a simple stack.
Instance Attribute Summary collapse
-
#mock_type ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #current_monotonic ⇒ Object
- #current_monotonic_with_mock ⇒ Object
- #date(date_klass = Date) ⇒ Object
- #datetime(datetime_klass = DateTime) ⇒ Object
- #day ⇒ Object
- #hour ⇒ Object
-
#initialize(mock_type, *args) ⇒ TimeStackItem
constructor
A new instance of TimeStackItem.
- #min ⇒ Object
- #monotonic ⇒ Object
- #month ⇒ Object
- #scaled_time ⇒ Object
- #scaling_factor ⇒ Object
- #sec ⇒ Object
-
#time(time_klass = Time) ⇒ Object
:nodoc:.
- #travel_offset ⇒ Object
- #travel_offset_days ⇒ Object
- #utc_offset ⇒ Object
- #year ⇒ Object
Constructor Details
#initialize(mock_type, *args) ⇒ TimeStackItem
Returns a new instance of TimeStackItem.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/timecop/time_stack_item.rb', line 7 def initialize(mock_type, *args) raise "Unknown mock_type #{mock_type}" unless [:freeze, :travel, :scale].include?(mock_type) @travel_offset = @scaling_factor = nil @scaling_factor = args.shift if mock_type == :scale @mock_type = mock_type @monotonic = parse_monotonic_time(*args) if RUBY_VERSION >= '2.1.0' @time = parse_time(*args) @time_was = Time.now_without_mock_time @travel_offset = compute_travel_offset end |
Instance Attribute Details
#mock_type ⇒ Object (readonly)
:nodoc:
5 6 7 |
# File 'lib/timecop/time_stack_item.rb', line 5 def mock_type @mock_type end |
Instance Method Details
#current_monotonic ⇒ Object
69 70 71 |
# File 'lib/timecop/time_stack_item.rb', line 69 def current_monotonic Process.clock_gettime_without_mock(Process::CLOCK_MONOTONIC, :nanosecond) end |
#current_monotonic_with_mock ⇒ Object
73 74 75 |
# File 'lib/timecop/time_stack_item.rb', line 73 def current_monotonic_with_mock Process.clock_gettime_mock_time(Process::CLOCK_MONOTONIC, :nanosecond) end |
#date(date_klass = Date) ⇒ Object
98 99 100 |
# File 'lib/timecop/time_stack_item.rb', line 98 def date(date_klass = Date) date_klass.jd(time.__send__(:to_date).jd) end |
#datetime(datetime_klass = DateTime) ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/timecop/time_stack_item.rb', line 102 def datetime(datetime_klass = DateTime) if Float.method_defined?(:to_r) fractions_of_a_second = time.to_f % 1 datetime_klass.new(year, month, day, hour, min, (fractions_of_a_second + sec), utc_offset_to_rational(utc_offset)) else datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(utc_offset)) end end |
#day ⇒ Object
26 27 28 |
# File 'lib/timecop/time_stack_item.rb', line 26 def day time.day end |
#hour ⇒ Object
30 31 32 |
# File 'lib/timecop/time_stack_item.rb', line 30 def hour time.hour end |
#min ⇒ Object
34 35 36 |
# File 'lib/timecop/time_stack_item.rb', line 34 def min time.min end |
#monotonic ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/timecop/time_stack_item.rb', line 59 def monotonic if travel_offset.nil? @monotonic elsif scaling_factor.nil? current_monotonic + travel_offset * (10 ** 9) else (@monotonic + (current_monotonic - @monotonic) * scaling_factor).to_i end end |
#month ⇒ Object
22 23 24 |
# File 'lib/timecop/time_stack_item.rb', line 22 def month time.month end |
#scaled_time ⇒ Object
94 95 96 |
# File 'lib/timecop/time_stack_item.rb', line 94 def scaled_time (@time + (Time.now_without_mock_time - @time_was) * scaling_factor).to_f end |
#scaling_factor ⇒ Object
54 55 56 |
# File 'lib/timecop/time_stack_item.rb', line 54 def scaling_factor @scaling_factor end |
#sec ⇒ Object
38 39 40 |
# File 'lib/timecop/time_stack_item.rb', line 38 def sec time.sec end |
#time(time_klass = Time) ⇒ Object
:nodoc:
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/timecop/time_stack_item.rb', line 78 def time(time_klass = Time) #:nodoc: if @time.respond_to?(:in_time_zone) time = time_klass.at(@time.dup.localtime) else time = time_klass.at(@time) end if travel_offset.nil? time elsif scaling_factor.nil? time_klass.at(Time.now_without_mock_time + travel_offset) else time_klass.at(scaled_time) end end |
#travel_offset ⇒ Object
46 47 48 |
# File 'lib/timecop/time_stack_item.rb', line 46 def travel_offset @travel_offset unless mock_type == :freeze end |
#travel_offset_days ⇒ Object
50 51 52 |
# File 'lib/timecop/time_stack_item.rb', line 50 def travel_offset_days (@travel_offset / 60 / 60 / 24).round end |
#utc_offset ⇒ Object
42 43 44 |
# File 'lib/timecop/time_stack_item.rb', line 42 def utc_offset time.utc_offset end |
#year ⇒ Object
18 19 20 |
# File 'lib/timecop/time_stack_item.rb', line 18 def year time.year end |