Module: ActiveSupport::CoreExtensions::Numeric::Time

Included in:
Numeric
Defined in:
lib/weekdays.rb

Instance Method Summary collapse

Instance Method Details

#weekdays_ago(time = ::Time.now) ⇒ Object

Returns a Time object that is n number of weekdays in the past from a given Date



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/weekdays.rb', line 87

def weekdays_ago(time = ::Time.now)
  # -5.weekdays_ago(time) == 5.weekdays_from(time)
  return self.abs.weekdays_from(time) if self < 0
  
  x = 0
  curr_date = time

  until x == self
    curr_date -= 1.days
    x += 1 if curr_date.weekday?
  end

  curr_date
end

#weekdays_from(time = ::Time.now) ⇒ Object Also known as: weekdays_from_now

Returns a Time object that is n number of weekdays in the future of a given Date



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/weekdays.rb', line 70

def weekdays_from(time = ::Time.now)
  # -5.weekdays_from(time) == 5.weekdays_ago(time)
  return self.abs.weekdays_ago(time) if self < 0
  
  x = 0
  curr_date = time

  until x == self
    curr_date += 1.days
    x += 1 if curr_date.weekday?
  end

  curr_date
end