From 06024139b6564f7430a980ce508a5f2f83a9d491 Mon Sep 17 00:00:00 2001 From: Matt Darby Date: Fri, 20 Mar 2009 16:19:10 -0400 Subject: [PATCH 1/2] Added #weekday? and #weekdays_until --- .../active_support/core_ext/date/calculations.rb | 11 +++++++++++ activesupport/test/core_ext/date_ext_test.rb | 13 +++++++++++++ 2 files changed, 24 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 7f94da0..34a6fb8 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -47,6 +47,11 @@ module ActiveSupport #:nodoc: self > ::Date.current end + # Tells whether the Date object is a weekday + def weekday? + (1..5).include?(wday) + end + # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) # and then subtracts the specified number of seconds def ago(seconds) @@ -215,6 +220,12 @@ module ActiveSupport #:nodoc: end alias :at_end_of_year :end_of_year + # Returns the number of weekdays until a future Date + def weekdays_until(date) + return 0 if date <= self + (self...date).select{|day| day.weekday?}.size + end + # Convenience method which returns a new Date/DateTime representing the time 1 day ago def yesterday self - 1 diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 1001868..70fb96a 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -231,6 +231,19 @@ class DateExtCalculationsTest < Test::Unit::TestCase assert_equal true, Date.new(2000,1,2).future? end + def test_weekday + Date.stubs(:today).returns(Date.new(2000, 1, 1)) + assert_equal false, Date.today.weekday? + + Date.stubs(:today).returns(Date.new(2000, 1, 3)) + assert_equal true, Date.today.weekday? + end + + def test_weekdays_until + Date.stubs(:today).returns(Date.new(2000, 1, 1)) + assert_equal 5, Date.today.weekdays_until(Date.new(2000, 1, 8)) + end + def test_current_returns_date_today_when_zone_default_not_set with_env_tz 'US/Central' do Time.stubs(:now).returns Time.local(1999, 12, 31, 23) -- 1.6.2 From 09cafb78960f7afc022584a3176861b70c4ba62e Mon Sep 17 00:00:00 2001 From: Matt Darby Date: Fri, 20 Mar 2009 16:19:39 -0400 Subject: [PATCH 2/2] Added #weekdays_from and #weekdays_ago --- .../lib/active_support/core_ext/numeric/time.rb | 30 ++++++++++++++++++++ activesupport/test/core_ext/time_ext_test.rb | 16 ++++++++++ 2 files changed, 46 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/numeric/time.rb b/activesupport/lib/active_support/core_ext/numeric/time.rb index bc7f180..fd975b0 100644 --- a/activesupport/lib/active_support/core_ext/numeric/time.rb +++ b/activesupport/lib/active_support/core_ext/numeric/time.rb @@ -75,6 +75,36 @@ module ActiveSupport #:nodoc: # Reads best without arguments: 10.minutes.from_now alias :from_now :since + + # Returns a Date that is n number of weekdays in the future of a given Date + def weekdays_from(date = ::Time.now.to_date) + x = 0 + curr_date = nil + + until x == self + curr_date = date + 1.days + x += 1 if curr_date.weekday? + date = curr_date + end + + curr_date + end + alias :weekdays_from_now :weekdays_from + + # Returns a Date that is n number of weekdays in the past from a given Date + def weekdays_ago(date = ::Time.now.to_date) + x = 0 + curr_date = nil + + until x == self + curr_date = date - 1.days + x += 1 if curr_date.weekday? + date = curr_date + end + + curr_date + end + end end end diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index c085552..0009bf2 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -354,6 +354,22 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_equal Time.local(2005,3,2,10,10,10), Time.local(2005,2,28,10,10,10).tomorrow.tomorrow end + def test_weekdays_from + assert_equal 5.weekdays_from(Date.new(2000, 1, 1)), Date.new(2000, 1, 7) + end + + def test_weekdays_from_now + Time.stubs(:now).returns(Date.new(2000, 1, 1)) + assert_equal 5.weekdays_from_now, Date.new(2000, 1, 7) + end + + def test_weekdays_ago + Time.stubs(:now).returns(Date.new(2000, 1, 1)) + assert_equal 5.weekdays_ago, Date.new(1999, 12, 27) + + assert_equal 5.weekdays_ago(Date.new(1999, 12, 27)), Date.new(1999, 12, 20) + end + def test_change assert_equal Time.local(2006,2,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:year => 2006) assert_equal Time.local(2005,6,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:month => 6) -- 1.6.2