Class: Date

Inherits:
Object
  • Object
show all
Defined in:
lib/nemo/util/date.rb

Instance Method Summary collapse

Instance Method Details

#calendarObject

Calendar representation of a month. Consists of an array of weeks, each week an array of 7 days, each day a Date object. Padded with days showing for previous and next month.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nemo/util/date.rb', line 7

def calendar
  # months
  curr_month = Date.new(self.year, self.month, 1)
  prev_month = (curr_month << 1)
  next_month = (curr_month >> 1)

  # previous month days
  prev_days = Array.new
  prev_in_curr = curr_month.wday
  ((curr_month-1)-(prev_in_curr-1)).upto(curr_month-1) { |d| prev_days << d }

  # current month days
  curr_days = Array.new
  curr_month.upto(next_month-1) { |d| curr_days << d }
  
  # next month days
  next_days = Array.new
  days = prev_days+curr_days
  weeks = (days.size.to_f/7).ceil
  cdays_size = weeks*7
  next_in_curr = (cdays_size-days.size)
  next_month.upto(next_month+(next_in_curr-1)) { |d| next_days << d }
  days += next_days
  
  # split into weeks
  table = Array.new
  days.each do |day|
    table << Array.new if table.size == 0 or table.last.size == 7
    table.last << day
  end

  table
end