diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-02-24 00:07:01 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-02-24 00:07:01 +0000 |
commit | 5d94fb33c6b862c6b206c55f0d19e3f307fa4056 (patch) | |
tree | 60c9f6521d5e8fb718b7c8489a88dfe314c1ff8c /activesupport/lib | |
parent | 32dbf0b72554a9c32074a3f86ac1eabdbe6fbb5b (diff) | |
download | rails-5d94fb33c6b862c6b206c55f0d19e3f307fa4056.tar.gz rails-5d94fb33c6b862c6b206c55f0d19e3f307fa4056.tar.bz2 rails-5d94fb33c6b862c6b206c55f0d19e3f307fa4056.zip |
Added year-based time calculations
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@779 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/calculations.rb | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 6744930756..acc84017ab 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -54,6 +54,26 @@ module ActiveSupport #:nodoc: end end + # Returns a new Time representing the time a number of specified years ago + def years_ago(years) + change(:year => self.year - years) + end + + def years_since(years) + change(:year => self.year + years) + end + + # Short-hand for months_ago(1) + def last_year + years_since(1) + end + + # Short-hand for months_since(1) + def next_year + years_since(1) + end + + # Short-hand for months_ago(1) def last_month months_ago(1) @@ -87,10 +107,17 @@ module ActiveSupport #:nodoc: # Returns a new Time representing the start of the month (1st of the month, 0:00) def beginning_of_month - self - ((self.mday-1).days + self.seconds_since_midnight) + #self - ((self.mday-1).days + self.seconds_since_midnight) + change(:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0) end alias :at_beginning_of_month :beginning_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) + end + alias :at_beginning_of_year :beginning_of_year + # Convenience method which returns a new Time representing the time 1 day ago def yesterday self.ago(1.day) |