Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added Date#all_week/month/quarter/year for generating date ranges #9685

Merged
merged 1 commit into from

3 participants

@dimko

Similar to Time#all_* methods, but with dates.

@carlosantoniodasilva

I think it makes sense, but it needs a changelog entry. Thanks.

@dimko

Added missing changelog entry.

@dimko dimko closed this
@dimko dimko reopened this
@dimko

Sorry, closed accidentally.
@carlosantoniodasilva can you review this again, please?

@JonRowe

Hey @carlosantoniodasilva did you ever get a chance to re-review this?

@carlosantoniodasilva

I'll get back to it soon, sorry.

@carlosantoniodasilva

@dimko can you rebase it? Might have a changelog conflict. Thanks.

@carlosantoniodasilva carlosantoniodasilva merged commit c55200c into from
@carlosantoniodasilva

There we go, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
4 activesupport/CHANGELOG.md
@@ -1,3 +1,7 @@
+* Added Date#all_week/month/quarter/year for generating date ranges.
+
+ *Dmitriy Meremyanin*
+
* Add `Time.zone.yesterday` and `Time.zone.tomorrow`. These follow the
behavior of Ruby's `Date.yesterday` and `Date.tomorrow` but return localized
versions, similar to how `Time.zone.today` has returned a localized version
View
21 activesupport/lib/active_support/core_ext/date_and_time/calculations.rb
@@ -213,6 +213,27 @@ def end_of_year
end
alias :at_end_of_year :end_of_year
+ # Returns a Range representing the whole week of the current date/time.
+ # Week starts on start_day, default is <tt>Date.week_start</tt> or <tt>config.week_start</tt> when set.
+ def all_week(start_day = Date.beginning_of_week)
+ beginning_of_week(start_day)..end_of_week(start_day)
+ end
+
+ # Returns a Range representing the whole month of the current date/time.
+ def all_month
+ beginning_of_month..end_of_month
+ end
+
+ # Returns a Range representing the whole quarter of the current date/time.
+ def all_quarter
+ beginning_of_quarter..end_of_quarter
+ end
+
+ # Returns a Range representing the whole year of the current date/time.
+ def all_year
+ beginning_of_year..end_of_year
+ end
+
private
def first_hour(date_or_time)
View
21 activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -199,27 +199,6 @@ def all_day
beginning_of_day..end_of_day
end
- # Returns a Range representing the whole week of the current time.
- # Week starts on start_day, default is <tt>Date.week_start</tt> or <tt>config.week_start</tt> when set.
- def all_week(start_day = Date.beginning_of_week)
- beginning_of_week(start_day)..end_of_week(start_day)
- end
-
- # Returns a Range representing the whole month of the current time.
- def all_month
- beginning_of_month..end_of_month
- end
-
- # Returns a Range representing the whole quarter of the current time.
- def all_quarter
- beginning_of_quarter..end_of_quarter
- end
-
- # Returns a Range representing the whole year of the current time.
- def all_year
- beginning_of_year..end_of_year
- end
-
def plus_with_duration(other) #:nodoc:
if ActiveSupport::Duration === other
other.since(self)
View
17 activesupport/test/core_ext/date_ext_test.rb
@@ -276,6 +276,23 @@ def test_end_of_day_when_zone_is_set
end
end
+ def test_all_week
+ assert_equal Date.new(2011,6,6)..Date.new(2011,6,12), Date.new(2011,6,7).all_week
+ assert_equal Date.new(2011,6,5)..Date.new(2011,6,11), Date.new(2011,6,7).all_week(:sunday)
+ end
+
+ def test_all_month
+ assert_equal Date.new(2011,6,1)..Date.new(2011,6,30), Date.new(2011,6,7).all_month
+ end
+
+ def test_all_quarter
+ assert_equal Date.new(2011,4,1)..Date.new(2011,6,30), Date.new(2011,6,7).all_quarter
+ end
+
+ def test_all_year
+ assert_equal Date.new(2011,1,1)..Date.new(2011,12,31), Date.new(2011,6,7).all_year
+ end
+
def test_xmlschema
with_env_tz 'US/Eastern' do
assert_match(/^1980-02-28T00:00:00-05:?00$/, Date.new(1980, 2, 28).xmlschema)
Something went wrong with that request. Please try again.