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



165
166
167
# File 'lib/timecop/time_extensions.rb', line 165

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
152
153
154
155
156
157
158
159
160
# 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_hash[:hour] && date_hash[:min]
    closest_date = Date.closest_wday(date_hash[:wday]).to_datetime

    DateTime.new(
      closest_date.year, closest_date.month, closest_date.day,
      date_hash[:hour], date_hash[:min]
    )
  when date_hash[:wday]
    Date.closest_wday(date_hash[:wday]).to_datetime
  when date_hash[:hour] && date_hash[:min] && date_hash[:sec]
    DateTime.new(mocked_time_stack_item.year, mocked_time_stack_item.month, mocked_time_stack_item.day, date_hash[:hour], date_hash[:min], date_hash[:sec])
  else
    parsed_date + mocked_time_stack_item.travel_offset_days
  end
end