aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2011-06-13 18:54:15 +0200
committerDavid Heinemeier Hansson <david@loudthinking.com>2011-06-13 18:56:06 +0200
commitd6cc0e56bdf1d657f16e5c4026f53d0a4e2dd37e (patch)
tree167db89b84e1f443c24a3a73961b5f004104c9e0
parentb12c2e4ebb85170467ac250219557d631c842d8d (diff)
downloadrails-d6cc0e56bdf1d657f16e5c4026f53d0a4e2dd37e.tar.gz
rails-d6cc0e56bdf1d657f16e5c4026f53d0a4e2dd37e.tar.bz2
rails-d6cc0e56bdf1d657f16e5c4026f53d0a4e2dd37e.zip
Added Time#whole_day/week/quarter/year as a way of generating ranges (example: Event.where(created_at: Time.now.whole_week)) [DHH]
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb27
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb20
3 files changed, 48 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 6c1e6185af..043b55077a 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*Rails 3.2.0 (unreleased)*
+* Added Time#whole_day/week/quarter/year as a way of generating ranges (example: Event.where(created_at: Time.now.whole_week)) [DHH]
+
* Added instance_accessor: false as an option to Class#cattr_accessor and friends [DHH]
* Removed ActiveSupport::SecureRandom in favour of SecureRandom from the standard library [Jon Leighton]
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index 00fda2b370..54e16a9fb7 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -226,7 +226,7 @@ class Time
end
alias :at_end_of_quarter :end_of_quarter
- # Returns a new Time representing the start of the year (1st of january, 0:00)
+ # Returns a new Time representing the start of the year (1st of january, 0:00)
def beginning_of_year
change(:month => 1, :day => 1, :hour => 0)
end
@@ -248,6 +248,31 @@ class Time
advance(:days => 1)
end
+ # Returns a Range representing the whole day of the current time.
+ def whole_day
+ beginning_of_day..end_of_day
+ end
+
+ # Returns a Range representing the whole week of the current time.
+ def whole_week
+ beginning_of_week..end_of_week
+ end
+
+ # Returns a Range representing the whole month of the current time.
+ def whole_month
+ beginning_of_month..end_of_month
+ end
+
+ # Returns a Range representing the whole quarter of the current time.
+ def whole_quarter
+ beginning_of_quarter..end_of_quarter
+ end
+
+ # Returns a Range representing the whole year of the current time.
+ def whole_year
+ beginning_of_year..end_of_year
+ end
+
def plus_with_duration(other) #:nodoc:
if ActiveSupport::Duration === other
other.since(self)
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index 44e02109b1..989a598ae0 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -767,6 +767,26 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
assert_equal false, Time === DateTime.civil(2000)
end
+ def test_whole_day
+ assert_equal Time.local(2011,6,7,0,0,0)..Time.local(2011,6,7,23,59,59,999999.999), Time.local(2011,6,7,10,10,10).whole_day
+ end
+
+ def test_whole_week
+ assert_equal Time.local(2011,6,6,0,0,0)..Time.local(2011,6,12,23,59,59,999999.999), Time.local(2011,6,7,10,10,10).whole_week
+ end
+
+ def test_whole_month
+ assert_equal Time.local(2011,6,1,0,0,0)..Time.local(2011,6,30,23,59,59,999999.999), Time.local(2011,6,7,10,10,10).whole_month
+ end
+
+ def test_whole_quarter
+ assert_equal Time.local(2011,4,1,0,0,0)..Time.local(2011,6,30,23,59,59,999999.999), Time.local(2011,6,7,10,10,10).whole_quarter
+ end
+
+ def test_whole_year
+ assert_equal Time.local(2011,1,1,0,0,0)..Time.local(2011,12,31,23,59,59,999999.999), Time.local(2011,6,7,10,10,10).whole_year
+ end
+
protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz