Module: EnterpriseTimeExtensions::ClassMethods

Defined in:
lib/enterprise_time_extensions.rb

Instance Method Summary collapse

Instance Method Details

#next_sundayObject



3
4
5
6
7
# File 'lib/enterprise_time_extensions.rb', line 3

def next_sunday
  timeobj = Time.local(Time.now.year, Time.now.month, Time.now.day, 0) # Today at midnight
  timeobj += 60*60*24 until timeobj.wday == 0
  timeobj # Should now be next Sunday at midnight
end

#nth_wday(n, wday, month, year) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/enterprise_time_extensions.rb', line 9

def nth_wday(n, wday, month, year)
  if (!n.between? 1,5) ||
     (!wday.between? 0,6) ||
     (!month.between? 1,12)
    raise ArgumentError
  end
  t = Time.local year, month, 1
  first = t.wday
  if first == wday
    fwd = 1
  elsif first < wday
    fwd = wday - first + 1
  elsif first > wday
    fwd = (wday+7) - first + 1
  end
  target = fwd + (n-1)*7
  begin
    t2 = Time.local year, month, target
  rescue ArgumentError
    return nil
  end
  t2
end