aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/CHANGELOG5
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb8
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb7
3 files changed, 20 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index b48837b2f3..b1c1f12d29 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,3 +1,8 @@
+*SVN*
+
+* Added Time#end_of_month to accompany Time#beginning_of_month #2514 [Jens-Christian Fischer]
+
+
*1.2.2* (October 26th, 2005)
* Set Logger.silencer = false to disable Logger#silence. Useful for debugging fixtures.
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index 85cd318a22..43424df8f4 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -145,6 +145,14 @@ module ActiveSupport #:nodoc:
end
alias :at_beginning_of_month :beginning_of_month
+ # Returns a new Time representing the end of the month (last day of the month, 0:00)
+ def end_of_month
+ #self - ((self.mday-1).days + self.seconds_since_midnight)
+ last_day = ::Time.days_in_month( self.month, self.year )
+ change(:mday => last_day,:hour => 0, :min => 0, :sec => 0, :usec => 0)
+ end
+ alias :at_end_of_month :end_of_month
+
# Returns a new Time representing the start of the year (1st of january, 0:00)
def beginning_of_year
change(:month => 1,:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index 0119c92c93..5dce75b6f4 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -30,6 +30,13 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
assert_equal Time.local(2005,2,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_month
end
+ def test_end_of_month
+ assert_equal Time.local(2005,3,31,0,0,0), Time.local(2005,3,20,10,10,10).end_of_month
+ assert_equal Time.local(2005,2,28,0,0,0), Time.local(2005,2,20,10,10,10).end_of_month
+ assert_equal Time.local(2005,4,30,0,0,0), Time.local(2005,4,20,10,10,10).end_of_month
+
+ end
+
def test_beginning_of_year
assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_year
end