title: Algorithm to find optimal day/month/year intervals to in an arbitrary timeframe?
body: <p>If you have a timeframe, say:</p>
  
  <p><code>March 19, 2009 - July 15, 2011</code></p>
  
  <p>Is there an algorithm that will break that timeframe down into this:</p>
  
  <pre><code>March 19, 2009  - March 31, 2009    # complete days
  April 1, 2009   - December 31, 2009 # complete months
  January 1, 2010 - December 31, 2010 # complete years
  January 1, 2011 - June 30, 2011     # complete months
  July 1, 2011    - July 15, 2011     # complete days
  </code></pre>
  
  <p>More specifically, given any arbitrary timeframe, even down to the second, is there an algorithm that can divide that up into the optimal number of <em>arbitrarily sized</em> intervals?</p>
  
  <p>So, maybe instead of dividing the above date range into days/months/years you wanted to divide it up into 5 day and 18 month chunks, something random like that.  Is there a formal name for this type of algorithm?  A ruby example would be awesome, but any language works.</p>
  
  <p>I've been able to hack together some hardcoded Ruby thing to handle the day/month/year example:</p>
  
  <ul>
  <li><a href="https://gist.github.com/1165914" rel="nofollow">https://gist.github.com/1165914</a></li>
  </ul>
  
  <p>...but it seems like there should be an algorithm to abstract this to handle any interval breakdown.  Maybe it just boils down to simple math.</p>