Class: Date

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

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.closest_wday(wday) ⇒ Object



102
103
104
105
106
107
# File 'lib/timecop/time_extensions.rb', line 102

def closest_wday(wday)
  today = Date.today
  result = today - today.wday
  result += 1 until wday == result.wday
  result
end

.mock_dateObject



33
34
35
# File 'lib/timecop/time_extensions.rb', line 33

def mock_date
  mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.date(self)
end

.mocked_time_stack_itemObject



98
99
100
# File 'lib/timecop/time_extensions.rb', line 98

def mocked_time_stack_item
  Timecop.top_stack_item
end

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/timecop/time_extensions.rb', line 78

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

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

.strptime_with_mock_date(str = '-4712-01-01', fmt = '%F', start = Date::ITALY) ⇒ Object Also known as: strptime



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/timecop/time_extensions.rb', line 47

def strptime_with_mock_date(str = '-4712-01-01', fmt = '%F', start = Date::ITALY)
  unless start == Date::ITALY
    raise ArgumentError, "Timecop's #{self}::#{__method__} only " +
      "supports Date::ITALY for the start argument."
  end

  d = Date._strptime(str, fmt) || Date.strptime_without_mock_date(str, fmt)
  now = Time.now.to_date
  year = d[:year] || now.year
  mon = d[:mon] || now.mon
  if d[:mday]
    Date.new(year, mon, d[:mday])
  elsif d[:wday]
    Date.new(year, mon, now.mday) + (d[:wday] - now.wday)
  elsif d[:yday]
    Date.new(year).next_day(d[:yday] - 1)
  elsif d[:cwyear] && d[:cweek]
    if d[:cwday]
      Date.commercial(d[:cwyear], d[:cweek], d[:cwday])
    else
      Date.commercial(d[:cwyear], d[:cweek])
    end
  elsif d[:seconds]
    Time.at(d[:seconds]).to_date
  else
    Date.new(year, mon)
  end
end

.today_with_mock_dateObject Also known as: today



39
40
41
# File 'lib/timecop/time_extensions.rb', line 39

def today_with_mock_date
  mock_date || today_without_mock_date
end