leap year wacked out months_ago

      0.upto(11) do |n|

?> puts Time.now.beginning_of_month.ago(n.months)

      end

Sat Mar 01 00:00:00 -0600 2008 Thu Jan 31 00:00:00 -0600 2008 Tue Jan 01 00:00:00 -0600 2008 Sun Dec 02 00:00:00 -0600 2007 Fri Nov 02 00:00:00 -0500 2007 Wed Oct 03 00:00:00 -0500 2007 Mon Sep 03 00:00:00 -0500 2007 Sat Aug 04 00:00:00 -0500 2007 Thu Jul 05 00:00:00 -0500 2007 Tue Jun 05 00:00:00 -0500 2007 Sun May 06 00:00:00 -0500 2007 Fri Apr 06 00:00:00 -0500 2007

Uhhh....where is Feb in this list...

Here it is, could of sworn I was doing it this way already, sorry about that internet...

    0.upto(11) do |n|

?> puts Time.now.beginning_of_month.months_ago(n)

    end

Sat Mar 01 00:00:00 -0600 2008 Fri Feb 01 00:00:00 -0600 2008 Tue Jan 01 00:00:00 -0600 2008 Sat Dec 01 00:00:00 -0600 2007 Thu Nov 01 00:00:00 -0500 2007 Mon Oct 01 00:00:00 -0500 2007 Sat Sep 01 00:00:00 -0500 2007 Wed Aug 01 00:00:00 -0500 2007 Sun Jul 01 00:00:00 -0500 2007 Fri Jun 01 00:00:00 -0500 2007 Tue May 01 00:00:00 -0500 2007 Sun Apr 01 00:00:00 -0500 2007

     0.upto(11) do |n|

?> puts Time.now.beginning_of_month.ago(n.months)

     end

Sounds like you're using rails 1.x. n.months (or n.days etc...) used
to be dumb: it just converted an integer into a corresponding number
of seconds, an approach with obvious limitations. In rails 2, it's smarter and leverages Date to di it's computations,
so when I run your code I get

Sat Mar 01 00:00:00 UTC 2008 Fri Feb 01 00:00:00 UTC 2008 Tue Jan 01 00:00:00 UTC 2008 Sat Dec 01 00:00:00 UTC 2007 Thu Nov 01 00:00:00 UTC 2007 Mon Oct 01 00:00:00 UTC 2007 Sat Sep 01 00:00:00 UTC 2007 Wed Aug 01 00:00:00 UTC 2007 Sun Jul 01 00:00:00 UTC 2007 Fri Jun 01 00:00:00 UTC 2007 Tue May 01 00:00:00 UTC 2007 Sun Apr 01 00:00:00 UTC 2007

(Note also how your code drifts, because it's effectively just
subtracting a fixed number of days and not all months have the same
length).

If you're stuck with 1.x this probably does the trick too

0.upto(11) do |n|    puts Time.now.months_ago(n).beginning_of_month end

Fred

uncle wrote:

Here it is, could of sworn I was doing it this way already, sorry about that internet...

On behalf of the internet, I forgive you!