Skip to content

Commit

Permalink
Merge pull request #9685 from dimko/patch-2
Browse files Browse the repository at this point in the history
Added Date#all_week/month/quarter/year for generating date ranges
  • Loading branch information
carlosantoniodasilva committed Dec 3, 2013
2 parents 1441961 + 18546d4 commit c55200c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
4 changes: 4 additions & 0 deletions 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
Expand Down
Expand Up @@ -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)
Expand Down
21 changes: 0 additions & 21 deletions activesupport/lib/active_support/core_ext/time/calculations.rb
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions activesupport/test/core_ext/date_ext_test.rb
Expand Up @@ -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)
Expand Down

0 comments on commit c55200c

Please sign in to comment.