aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorgbuesing <gbuesing@gmail.com>2008-05-08 22:48:47 -0500
committergbuesing <gbuesing@gmail.com>2008-05-08 22:48:47 -0500
commit66728087d0eb99a524498e8f24725dae6073edd6 (patch)
treee8675936450de56cc4603d9057668cc365ae7113 /activesupport
parent79e44a5ee484f977c2cad8a4cffe882b816340a1 (diff)
downloadrails-66728087d0eb99a524498e8f24725dae6073edd6.tar.gz
rails-66728087d0eb99a524498e8f24725dae6073edd6.tar.bz2
rails-66728087d0eb99a524498e8f24725dae6073edd6.zip
Adding Date.current, which returns Time.zone.today if config.time_zone is set; otherwise returns Date.today. ActionView date_helper uses Date.current to determine locale-appropriate default
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/date/calculations.rb5
-rw-r--r--activesupport/test/core_ext/date_ext_test.rb23
3 files changed, 30 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index ea5490eb8f..f72825731e 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Adding Date.current, which returns Time.zone.today if config.time_zone is set; otherwise returns Date.today [Geoff Buesing]
+
* TimeWithZone: date part getter methods (#year #mon #day etc) are defined on class; no longer relying on method_missing [Geoff Buesing]
* Time.zone.parse return nil for strings with no date information [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 183471706b..1e2dbf118e 100644
--- a/activesupport/lib/active_support/core_ext/date/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -25,6 +25,11 @@ module ActiveSupport #:nodoc:
def tomorrow
::Date.today.tomorrow
end
+
+ # Returns Time.zone.today when config.time_zone is set, otherwise just returns Date.today.
+ def current
+ ::Time.zone_default ? ::Time.zone.today : ::Date.today
+ end
end
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb
index 2e363c439a..5925ae3a61 100644
--- a/activesupport/test/core_ext/date_ext_test.rb
+++ b/activesupport/test/core_ext/date_ext_test.rb
@@ -208,6 +208,29 @@ class DateExtCalculationsTest < Test::Unit::TestCase
end
end
end
+
+ uses_mocha 'TestDateCurrent' do
+ 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)
+ assert_equal Date.new(1999, 12, 31), Date.today
+ assert_equal Date.new(1999, 12, 31), Date.current
+ end
+ end
+
+ def test_current_returns_time_zone_today_when_zone_default_set
+ silence_warnings do # silence warnings raised by tzinfo gem
+ Time.zone_default = TimeZone['Eastern Time (US & Canada)']
+ with_env_tz 'US/Central' do
+ Time.stubs(:now).returns Time.local(1999, 12, 31, 23)
+ assert_equal Date.new(1999, 12, 31), Date.today
+ assert_equal Date.new(2000, 1, 1), Date.current
+ end
+ end
+ ensure
+ Time.zone_default = nil
+ end
+ end
protected
def with_env_tz(new_tz = 'US/Eastern')