Module: MonthDate

Defined in:
lib/month_date.rb,
lib/month_date/version.rb

Constant Summary collapse

VERSION =
"0.0.4.2"

Class Method Summary collapse

Class Method Details

.date_in_month(year, month) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/month_date.rb', line 63

def MonthDate.date_in_month(year, month)
    days = self.days_in_month(year, month)
    ary = []
    1.upto(days) do |day|
        date = Date.new(year, month, day)
        ary << date.strftime("%Y%m%d")
    end
    return ary
end

.dates_in_range(start_date, end_date) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/month_date.rb', line 22

def MonthDate.dates_in_range(start_date, end_date)
    start_date = Time.parse(start_date.to_s)
    end_date = Time.parse(end_date.to_s)
    result = []
    if start_date.mon == end_date.mon && start_date.year == end_date.year
        (start_date.day..end_date.day).to_a.each do |day|
            date = Date.new(start_date.year, start_date.mon, day)
            result << date.strftime("%Y%m%d")
        end
    elsif start_date.year == end_date.year
        # first count the start date month date.
        (start_date.day..self.days_in_month(start_date.year, start_date.mon)).to_a.each do |day|
            date = Date.new(start_date.year, start_date.mon, day)
            result << date.strftime("%Y%m%d")
        end
        (start_date.mon+1..end_date.mon-1).to_a.each do |month|
            result += self.date_in_month(start_date.year, month)
        end
        (1..end_date.day).each do |day|
            date = Date.new(end_date.year, end_date.mon, day)
            result << date.strftime("%Y%m%d")
        end
    else
        (start_date.day..self.days_in_month(start_date.year, start_date.mon)).to_a.each do |day|
            date = Date.new(start_date.year, start_date.mon, day)
            result << date.strftime("%Y%m%d")
        end
        (start_date.mon+1..12).to_a.each do |month|
            result += self.date_in_month(start_date.year, month)
        end
        (1..end_date.mon-1).to_a.each do |month|
            result += self.date_in_month(end_date.year, month)
        end
        (1..end_date.day).each do |day|
            date = Date.new(end_date.year, end_date.mon, day)
            result << date.strftime("%Y%m%d")
        end
    end
    return result
end

.days_in_month(year, month) ⇒ Object



18
19
20
# File 'lib/month_date.rb', line 18

def MonthDate.days_in_month(year, month)
    Date.new(year, month, -1).day
end

.week_date_in_month(year, month, week) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/month_date.rb', line 6

def MonthDate.week_date_in_month(year, month, week)
    days = self.days_in_month(year, month)
    result = []
    1.upto(days) do |day|
        date = Date.new(year, month, day)
        if date.wday == week
            result << date.strftime("%Y%m%d")
        end
    end
    return result
end