Module: MonthDate

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

Constant Summary collapse

VERSION =
"0.5.0"

Class Method Summary collapse

Class Method Details

.date_in_month(year, month, format = "%Y%m%d", day_type = false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/month_date.rb', line 94

def MonthDate.date_in_month(year, month, format="%Y%m%d", day_type=false)
    days = self.days_in_month(year, month)
    ary = []
    result = {:weekend=>[], :weekday=>[]}
    if day_type
        1.upto(days) do |day|
            date = Date.new(year, month, day)
            if self.weekend?(date)
                result[:weekend] << date.strftime(format)
            else
                result[:weekday] << date.strftime(format)
            end
        end
        return result
    else
        1.upto(days) do |day|
            date = Date.new(year, month, day)
            ary << date.strftime(format)
        end
        return ary
    end
end

.date_in_month_weekday(year, month, format = "%Y%m%d") ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/month_date.rb', line 127

def MonthDate.date_in_month_weekday(year, month, format="%Y%m%d")
    days = self.days_in_month(year, month)
    result = []
    1.upto(days) do |day|
        date = Date.new(year, month, day)
        result << date.strftime(format) unless self.weekend?(date)
    end
    return result
end

.date_in_month_weekend(year, month, format = "%Y%m%d") ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/month_date.rb', line 117

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

.dates_in_range(start_date, end_date, format = "%Y%m%d", day_type = false) ⇒ 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
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
92
# File 'lib/month_date.rb', line 22

def MonthDate.dates_in_range(start_date, end_date, format="%Y%m%d", day_type=false)
    start_date = Time.parse(start_date.to_s)
    end_date = Time.parse(end_date.to_s)
    weekend = []
    weekday = []
    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)
            if self.weekend?(date)
                weekend << date.strftime(format)
            else
                weekday << date.strftime(format)
            end
        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)
            if self.weekend?(date)
                weekend << date.strftime(format)
            else
                weekday << date.strftime(format)
            end
        end
        (start_date.mon+1..end_date.mon-1).to_a.each do |month|
            tmp = self.date_in_month(start_date.year, month, true)
            weekend += tmp[:weekend]
            weekday += tmp[:weekday]
        end
        (1..end_date.day).each do |day|
            date = Date.new(end_date.year, end_date.mon, day)
            if self.weekend?(date)
                weekend << date.strftime(format)
            else
                weekday << date.strftime(format)
            end
        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)
            if self.weekend?(date)
                weekend << date.strftime(format)
            else
                weekday << date.strftime(format)
            end
        end
        (start_date.mon+1..12).to_a.each do |month|
            tmp = self.date_in_month(start_date.year, month, format, true)
            weekend += tmp[:weekend]
            weekday += tmp[:weekday]
        end
        (1..end_date.mon-1).to_a.each do |month|
            tmp = self.date_in_month(end_date.year, month, format, true)
            weekend += tmp[:weekend]
            weekday += tmp[:weekday]
        end
        (1..end_date.day).each do |day|
            date = Date.new(end_date.year, end_date.mon, day)
            if self.weekend?(date)
                weekend << date.strftime(format)
            else
                weekday << date.strftime(format)
            end
        end
    end
    if day_type
        return {:weekday=>weekday, :weekend=>weekend}
    else
        return (weekday + weekend).sort
    end
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, format = "%Y%m%d") ⇒ 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, format="%Y%m%d")
    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(format)
        end
    end
    return result
end

.weekend?(date) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
140
141
142
143
144
# File 'lib/month_date.rb', line 137

def self.weekend?(date)
    wday = date.wday
    if wday == 0 || wday == 6
        return true
    else
        return false
    end
end