Module: SeasonsOfLove
- Defined in:
- lib/seasons_of_love.rb,
lib/seasons_of_love/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
-
.split_dates_into_ranges(start_date, end_date, opts = {}) ⇒ Object
date trick taken from stackoverflow.com/questions/925905/is-it-possible-to-create-a-list-of-months-between-two-dates-in-rails -Dylan.
Class Method Details
.split_dates_into_ranges(start_date, end_date, opts = {}) ⇒ Object
date trick taken from stackoverflow.com/questions/925905/is-it-possible-to-create-a-list-of-months-between-two-dates-in-rails -Dylan
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/seasons_of_love.rb', line 7 def self.split_dates_into_ranges(start_date, end_date, opts={}) months = [] if start_date.blank? || end_date.blank? if opts[:allow_nil] [] else raise "Range of #{start_date} - #{end_date} is missing one endpoint." end else if opts[:format] == 'weeks' weeks = [] beginning_of_week = opts[:beginning_of_week] || :sunday week_start = start_date while week_start <= end_date weeks << { :start_date => week_start, :end_date => [week_start.end_of_week(beginning_of_week), end_date].min} week_start = week_start.end_of_week(beginning_of_week) + 1.day end weeks else (start_date.year..end_date.year).each do |y| mo_start = (start_date.year == y) ? start_date.month : 1 mo_end = (end_date.year == y) ? end_date.month : 12 (mo_start..mo_end).each do |m| if (start_date.year == y && start_date.month == m) #first month in range start_of_month = start_date else start_of_month = Date.strptime("#{sprintf '%02d', m}/01/#{y}", "%m/%d/%Y") end if (end_date.year == y && end_date.month == m) #last month in range end_of_month = end_date else end_of_month = start_of_month.end_of_month end month_entry = {} month_entry[:start_date] = start_of_month month_entry[:end_date] = end_of_month month_entry[:month] = Date::MONTHNAMES[m] months << month_entry end end months end end end |