Skip to content
This repository
Browse code

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

  • Loading branch information...
commit 18546d4e3551e21ea781dba8c69383c0538b4086 1 parent 1441961
DM dimko authored
4 activesupport/CHANGELOG.md
Source Rendered
... ... @@ -1,3 +1,7 @@
  1 +* Added Date#all_week/month/quarter/year for generating date ranges.
  2 +
  3 + *Dmitriy Meremyanin*
  4 +
1 5 * Add `Time.zone.yesterday` and `Time.zone.tomorrow`. These follow the
2 6 behavior of Ruby's `Date.yesterday` and `Date.tomorrow` but return localized
3 7 versions, similar to how `Time.zone.today` has returned a localized version
21 activesupport/lib/active_support/core_ext/date_and_time/calculations.rb
@@ -213,6 +213,27 @@ def end_of_year
213 213 end
214 214 alias :at_end_of_year :end_of_year
215 215
  216 + # Returns a Range representing the whole week of the current date/time.
  217 + # Week starts on start_day, default is <tt>Date.week_start</tt> or <tt>config.week_start</tt> when set.
  218 + def all_week(start_day = Date.beginning_of_week)
  219 + beginning_of_week(start_day)..end_of_week(start_day)
  220 + end
  221 +
  222 + # Returns a Range representing the whole month of the current date/time.
  223 + def all_month
  224 + beginning_of_month..end_of_month
  225 + end
  226 +
  227 + # Returns a Range representing the whole quarter of the current date/time.
  228 + def all_quarter
  229 + beginning_of_quarter..end_of_quarter
  230 + end
  231 +
  232 + # Returns a Range representing the whole year of the current date/time.
  233 + def all_year
  234 + beginning_of_year..end_of_year
  235 + end
  236 +
216 237 private
217 238
218 239 def first_hour(date_or_time)
21 activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -199,27 +199,6 @@ def all_day
199 199 beginning_of_day..end_of_day
200 200 end
201 201
202   - # Returns a Range representing the whole week of the current time.
203   - # Week starts on start_day, default is <tt>Date.week_start</tt> or <tt>config.week_start</tt> when set.
204   - def all_week(start_day = Date.beginning_of_week)
205   - beginning_of_week(start_day)..end_of_week(start_day)
206   - end
207   -
208   - # Returns a Range representing the whole month of the current time.
209   - def all_month
210   - beginning_of_month..end_of_month
211   - end
212   -
213   - # Returns a Range representing the whole quarter of the current time.
214   - def all_quarter
215   - beginning_of_quarter..end_of_quarter
216   - end
217   -
218   - # Returns a Range representing the whole year of the current time.
219   - def all_year
220   - beginning_of_year..end_of_year
221   - end
222   -
223 202 def plus_with_duration(other) #:nodoc:
224 203 if ActiveSupport::Duration === other
225 204 other.since(self)
17 activesupport/test/core_ext/date_ext_test.rb
@@ -276,6 +276,23 @@ def test_end_of_day_when_zone_is_set
276 276 end
277 277 end
278 278
  279 + def test_all_week
  280 + assert_equal Date.new(2011,6,6)..Date.new(2011,6,12), Date.new(2011,6,7).all_week
  281 + assert_equal Date.new(2011,6,5)..Date.new(2011,6,11), Date.new(2011,6,7).all_week(:sunday)
  282 + end
  283 +
  284 + def test_all_month
  285 + assert_equal Date.new(2011,6,1)..Date.new(2011,6,30), Date.new(2011,6,7).all_month
  286 + end
  287 +
  288 + def test_all_quarter
  289 + assert_equal Date.new(2011,4,1)..Date.new(2011,6,30), Date.new(2011,6,7).all_quarter
  290 + end
  291 +
  292 + def test_all_year
  293 + assert_equal Date.new(2011,1,1)..Date.new(2011,12,31), Date.new(2011,6,7).all_year
  294 + end
  295 +
279 296 def test_xmlschema
280 297 with_env_tz 'US/Eastern' do
281 298 assert_match(/^1980-02-28T00:00:00-05:?00$/, Date.new(1980, 2, 28).xmlschema)

0 comments on commit 18546d4

Please sign in to comment.
Something went wrong with that request. Please try again.