From 3582bce6fdb30730b34b91a17b8bb33066eed7b8 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 27 Feb 2008 22:53:54 +0000 Subject: Adding Time#end_of_day, _quarter, _week, and _year. Closes #9312. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8934 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 2 ++ .../active_support/core_ext/date/calculations.rb | 20 +++++++++++++ .../active_support/core_ext/time/calculations.rb | 19 ++++++++++++ activesupport/test/core_ext/date_ext_test.rb | 23 ++++++++++++++ activesupport/test/core_ext/time_ext_test.rb | 35 ++++++++++++++++++++++ 5 files changed, 99 insertions(+) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 78b6c47d30..c00ce1e0cb 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Adding Time#end_of_day, _quarter, _week, and _year. #9312 [BigTitus] + * Adding TimeWithZone#between? [Geoff Buesing] * Time.=== returns true for TimeWithZone instances [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index b82f7be496..ab3b5e38f0 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -141,6 +141,14 @@ module ActiveSupport #:nodoc: alias :monday :beginning_of_week alias :at_beginning_of_week :beginning_of_week + # Returns a new Date/DateTime representing the end of this week (Sunday, DateTime objects will have time set to 23:59:59) + def end_of_week + days_to_sunday = self.wday!=0 ? 7-self.wday : 0 + result = self + days_to_sunday.days + self.acts_like?(:time) ? result.end_of_day : result + end + alias :at_end_of_week :end_of_week + # Returns a new Date/DateTime representing the start of the given day in next week (default is Monday). def next_week(day = :monday) days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} @@ -167,12 +175,24 @@ module ActiveSupport #:nodoc: end alias :at_beginning_of_quarter :beginning_of_quarter + # Returns a new Date/DateTime representing the end of the quarter (last day of march, june, september, december; DateTime objects will have time set to 23:59:59) + def end_of_quarter + change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month + end + alias :at_end_of_quarter :end_of_quarter + # Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00) def beginning_of_year self.acts_like?(:time) ? change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0) : change(:month => 1, :day => 1) end alias :at_beginning_of_year :beginning_of_year + # Returns a new Time representing the end of the year (31st of december; DateTime objects will have time set to 23:59:59) + def end_of_year + self.acts_like?(:time) ? change(:month => 12,:day => 31,:hour => 23, :min => 59, :sec => 59) : change(:month => 12, :day => 31) + end + alias :at_end_of_year :end_of_year + # Convenience method which returns a new Date/DateTime representing the time 1 day ago def yesterday self - 1 diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index ad260f809b..867ac86617 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -154,6 +154,13 @@ module ActiveSupport #:nodoc: alias :monday :beginning_of_week alias :at_beginning_of_week :beginning_of_week + # Returns a new Time representing the end of this week (Sunday, 23:59:59) + def end_of_week + days_to_sunday = self.wday!=0 ? 7-self.wday : 0 + (self + days_to_sunday.days).end_of_day + end + alias :at_end_of_week :end_of_week + # Returns a new Time representing the start of the given day in next week (default is Monday). def next_week(day = :monday) days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} @@ -194,12 +201,24 @@ module ActiveSupport #:nodoc: end alias :at_beginning_of_quarter :beginning_of_quarter + # Returns a new Time representing the end of the quarter (last day of march, june, september, december, 23:59:59) + def end_of_quarter + change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month + end + alias :at_end_of_quarter :end_of_quarter + # 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, :min => 0, :sec => 0, :usec => 0) end alias :at_beginning_of_year :beginning_of_year + # Returns a new Time representing the end of the year (31st of december, 23:59:59) + def end_of_year + change(:month => 12,:day => 31,:hour => 23, :min => 59, :sec => 59) + end + alias :at_end_of_year :end_of_year + # Convenience method which returns a new Time representing the time 1 day ago def yesterday self.ago(1.day) diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index cd74be8321..2e363c439a 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -60,6 +60,29 @@ class DateExtCalculationsTest < Test::Unit::TestCase assert_equal Date.new(2005,4,1), Date.new(2005,6,30).beginning_of_quarter end + def test_end_of_week + assert_equal Date.new(2008,2,24), Date.new(2008,2,22).end_of_week + assert_equal Date.new(2008,3,2), Date.new(2008,2,25).end_of_week #monday + assert_equal Date.new(2008,3,2), Date.new(2008,2,26).end_of_week #tuesday + assert_equal Date.new(2008,3,2), Date.new(2008,2,27).end_of_week #wednesday + assert_equal Date.new(2008,3,2), Date.new(2008,2,28).end_of_week #thursday + assert_equal Date.new(2008,3,2), Date.new(2008,2,29).end_of_week #friday + assert_equal Date.new(2008,3,2), Date.new(2008,3,01).end_of_week #saturday + assert_equal Date.new(2008,3,2), Date.new(2008,3,02).end_of_week #sunday + end + + def test_end_of_quarter + assert_equal Date.new(2008,3,31), Date.new(2008,2,15).end_of_quarter + assert_equal Date.new(2008,3,31), Date.new(2008,3,31).end_of_quarter + assert_equal Date.new(2008,12,31), Date.new(2008,10,8).end_of_quarter + assert_equal Date.new(2008,6,30), Date.new(2008,4,14).end_of_quarter + assert_equal Date.new(2008,9,30), Date.new(2008,8,21).end_of_quarter + end + + def test_end_of_year + assert_equal Date.new(2008,12,31).to_s, Date.new(2008,2,22).end_of_year.to_s + end + def test_end_of_month assert_equal Date.new(2005,3,31), Date.new(2005,3,20).end_of_month assert_equal Date.new(2005,2,28), Date.new(2005,2,20).end_of_month diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index d4a555d55e..628f07d6c4 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -83,12 +83,47 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_equal Time.local(2005,4,1,0,0,0), Time.local(2005,6,30,23,59,59).beginning_of_quarter end + def test_end_of_day + assert_equal Time.local(2007,8,12,23,59,59), Time.local(2007,8,12,10,10,10).end_of_day + with_env_tz 'US/Eastern' do + assert_equal Time.local(2007,4,2,23,59,59), Time.local(2007,4,2,10,10,10).end_of_day, 'start DST' + assert_equal Time.local(2007,10,29,23,59,59), Time.local(2007,10,29,10,10,10).end_of_day, 'ends DST' + end + with_env_tz 'NZ' do + assert_equal Time.local(2006,3,19,23,59,59), Time.local(2006,3,19,10,10,10).end_of_day, 'ends DST' + assert_equal Time.local(2006,10,1,23,59,59), Time.local(2006,10,1,10,10,10).end_of_day, 'start DST' + end + end + + def test_end_of_week + assert_equal Time.local(2008,1,6,23,59,59), Time.local(2007,12,31,10,10,10).end_of_week + assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,27,0,0,0).end_of_week #monday + assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,28,0,0,0).end_of_week #tuesday + assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,29,0,0,0).end_of_week #wednesday + assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,30,0,0,0).end_of_week #thursday + assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,31,0,0,0).end_of_week #friday + assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,9,01,0,0,0).end_of_week #saturday + assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,9,02,0,0,0).end_of_week #sunday + end + def test_end_of_month assert_equal Time.local(2005,3,31,23,59,59), Time.local(2005,3,20,10,10,10).end_of_month assert_equal Time.local(2005,2,28,23,59,59), Time.local(2005,2,20,10,10,10).end_of_month assert_equal Time.local(2005,4,30,23,59,59), Time.local(2005,4,20,10,10,10).end_of_month end + def test_end_of_quarter + assert_equal Time.local(2007,3,31,23,59,59), Time.local(2007,2,15,10,10,10).end_of_quarter + assert_equal Time.local(2007,3,31,23,59,59), Time.local(2007,3,31,0,0,0).end_of_quarter + assert_equal Time.local(2007,12,31,23,59,59), Time.local(2007,12,21,10,10,10).end_of_quarter + assert_equal Time.local(2007,6,30,23,59,59), Time.local(2007,4,1,0,0,0).end_of_quarter + end + + def test_end_of_year + assert_equal Time.local(2007,12,31,23,59,59), Time.local(2007,2,22,10,10,10).end_of_year + assert_equal Time.local(2007,12,31,23,59,59), Time.local(2007,12,31,10,10,10).end_of_year + end + def test_beginning_of_year assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_year end -- cgit v1.2.3