From d6cc0e56bdf1d657f16e5c4026f53d0a4e2dd37e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 13 Jun 2011 18:54:15 +0200 Subject: Added Time#whole_day/week/quarter/year as a way of generating ranges (example: Event.where(created_at: Time.now.whole_week)) [DHH] --- .../active_support/core_ext/time/calculations.rb | 27 +++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'activesupport/lib/active_support/core_ext') 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) -- cgit v1.2.3