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



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

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

.mock_dateObject



37
38
39
# File 'lib/timecop/time_extensions.rb', line 37

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

.mocked_time_stack_itemObject



117
118
119
# File 'lib/timecop/time_extensions.rb', line 117

def mocked_time_stack_item
  Timecop.top_stack_item
end

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/timecop/time_extensions.rb', line 95

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[:mday]
    Date.new(mocked_time_stack_item.year, mocked_time_stack_item.month, 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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/timecop/time_extensions.rb', line 51

def strptime_with_mock_date(str = '-4712-01-01', fmt = '%F', start = Date::ITALY)
  #If date is not valid the following line raises
  Date.strptime_without_mock_date(str, fmt, start)

  d = Date._strptime(str, fmt)
  now = Time.now.to_date

  # If "current" time falls near a year boundary, with a year-ambiguous str, we need to explicitly handle it
  cwday = d[:cwday] && (now + (d[:cwday] - now.wday))
  wday = d[:wday] && (now + (d[:wday] - now.wday))

  year = d[:year] || d[:cwyear] || (cwday || wday || now).year
  mon = d[:mon] || now.mon
  if d.keys == [:year]
    Date.new(year, 1, 1, start)
  elsif d[:mday]
    Date.new(year, mon, d[:mday], start)
  elsif d[:yday]
    Date.new(year, 1, 1, start).next_day(d[:yday] - 1)
  elsif d[:cwyear] || d[:cweek] || d[:wnum0] || d[:wnum1] || d[:wday] || d[:cwday]
    # When only a day (wday/cwday) is present, derive the week from the resolved date; otherwise fall back to now's week
    week = d[:cweek] || d[:wnum1] || d[:wnum0] || (cwday || wday || now).strftime('%W').to_i
    if d[:wnum0] #Week of year where week starts on sunday
      if d[:cwday] #monday based day of week
        Date.strptime_without_mock_date("#{year} #{week} #{d[:cwday]}", '%Y %U %u', start)
      else
        Date.strptime_without_mock_date("#{year} #{week} #{d[:wday] || 0}", '%Y %U %w', start)
      end
    else #Week of year where week starts on monday
      if d[:wday] #sunday based day of week
        Date.strptime_without_mock_date("#{year} #{week} #{d[:wday]}", '%Y %W %w', start)
      else
        Date.strptime_without_mock_date("#{year} #{week} #{d[:cwday] || 1}", '%Y %W %u', start)
      end
    end
  elsif d[:seconds]
    Time.at(d[:seconds]).to_date
  else
    Date.new(year, mon, 1, start)
  end
end

.today_with_mock_dateObject Also known as: today



43
44
45
# File 'lib/timecop/time_extensions.rb', line 43

def today_with_mock_date
  mock_date || today_without_mock_date
end