Skip to content

Instantly share code, notes, and snippets.

@lancejpollard
Created August 23, 2011 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancejpollard/1165914 to your computer and use it in GitHub Desktop.
Save lancejpollard/1165914 to your computer and use it in GitHub Desktop.
Breaking down a timeframe into the optimal number of parts.
starts_on = Date.parse("March 19, 2009")
ends_on = Date.parse("July 15, 2011")
first_beginning_of_day_interval = starts_on.beginning_of_day
first_end_of_day_interval = nil
first_beginning_of_month_interval = starts_on.next_month.beginning_of_month.beginning_of_day
first_end_of_month_interval = nil
beginning_of_year_interval = starts_on.next_year.beginning_of_year.beginning_of_day
end_of_year_interval = nil
last_beginning_of_month_interval = ends_on.beginning_of_year.beginning_of_day
last_end_of_month_interval = ends_on.prev_month.end_of_month.end_of_day
last_beginning_of_day_interval = ends_on.beginning_of_month.beginning_of_day
last_end_of_day_interval = ends_on.end_of_day
same_year = starts_on.year == ends_on.year
same_month = same_year && starts_on.month == ends_on.month
if same_month
# interval == ("day", first_beginning_of_day_interval, last_end_of_day_interval)
elsif same_year
if first_beginning_of_day_interval < first_beginning_of_month_interval
first_end_of_day_interval = starts_on.end_of_month.end_of_day
else
first_beginning_of_day_interval = nil
end
if first_beginning_of_day_interval
# interval == ("day", first_beginning_of_day_interval, first_end_of_day_interval)
end
if first_beginning_of_month_interval
# interval == ("month", first_beginning_of_month_interval, last_end_of_month_interval)
end
if last_beginning_of_day_interval
# interval == ("day", last_beginning_of_day_interval, last_end_of_day_interval)
end
else
# if "oct 3, 2009" < "nov 1, 2009"
# first_end_of_day_interval = "oct 31, 2009"
# else
# first day is the first of the month, so there will be no day intervals
if first_beginning_of_day_interval < first_beginning_of_month_interval
first_end_of_day_interval = starts_on.end_of_month.end_of_day
else
first_beginning_of_day_interval = nil
end
# if "nov 1, 2009" < "jan 1, 2010"
# first_end_of_month_interval = "dec 31, 2009"
# else
# first day of the month is the new year, so there are no months
if first_beginning_of_month_interval < beginning_of_year_interval
first_end_of_month_interval = first_beginning_of_month_interval.end_of_year.end_of_day
else
first_beginning_of_month_interval = nil
end
# if "jan 1, 2011" > "jan 1, 2010"
# end_of_year_interval = "dec 31, 2010"
# else
# first day of the year is the first day of the starting year, so there are no years
if last_beginning_of_month_interval > beginning_of_year_interval
end_of_year_interval = ends_on.prev_year.end_of_year.end_of_day
else
beginning_of_year_interval = nil
end
# if "march 1, 2011" > "jan 1, 2011"
# last_end_of_month_interval = "feb 28, 2011"
# else
# the last day is within the first month of the year, so there are no months
if last_beginning_of_day_interval > last_beginning_of_month_interval
last_end_of_month_interval = ends_on.prev_month.end_of_month.end_of_day
else
last_beginning_of_month_interval = nil
end
if first_beginning_of_day_interval
# interval == ("day", first_beginning_of_day_interval, first_end_of_day_interval)
end
if first_beginning_of_month_interval
# interval == ("month", first_beginning_of_month_interval, first_end_of_month_interval)
end
if beginning_of_year_interval
# interval == ("year", beginning_of_year_interval, end_of_year_interval)
end
if last_beginning_of_month_interval
# interval == ("month", last_beginning_of_month_interval, last_end_of_month_interval)
end
if last_beginning_of_day_interval
# interval == ("day", last_beginning_of_day_interval, last_end_of_day_interval)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment