Class: DateTime

Inherits:
Object
  • Object
show all
Defined in:
lib/timecop/time_extensions.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.mock_timeObject



122
123
124
# File 'lib/timecop/time_extensions.rb', line 122

def mock_time
  mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.datetime(self)
end

.mocked_time_stack_itemObject



156
157
158
# File 'lib/timecop/time_extensions.rb', line 156

def mocked_time_stack_item
  Timecop.top_stack_item
end

.now_with_mock_timeObject Also known as: now



126
127
128
# File 'lib/timecop/time_extensions.rb', line 126

def now_with_mock_time
  mock_time || now_without_mock_time
end

.parse_with_mock_date(*args) ⇒ Object Also known as: parse



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/timecop/time_extensions.rb', line 134

def parse_with_mock_date(*args)
  parsed_date = parse_without_mock_date(*args)
  return parsed_date unless mocked_time_stack_item
  date_hash = DateTime._parse(*args)

  case
  when date_hash[:year] && date_hash[:mon]
    parsed_date
  when date_hash[:mon] && date_hash[:mday]
    DateTime.new(mocked_time_stack_item.year, date_hash[:mon], date_hash[:mday])
  when date_hash[:mday]
    DateTime.new(mocked_time_stack_item.year, mocked_time_stack_item.month, date_hash[:mday])
  when date_hash[:wday]
    Date.closest_wday(date_hash[:wday]).to_datetime
  else
    parsed_date + mocked_time_stack_item.travel_offset_days
  end
end