From 66728087d0eb99a524498e8f24725dae6073edd6 Mon Sep 17 00:00:00 2001 From: gbuesing Date: Thu, 8 May 2008 22:48:47 -0500 Subject: 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 --- activesupport/CHANGELOG | 2 ++ .../active_support/core_ext/date/calculations.rb | 5 +++++ activesupport/test/core_ext/date_ext_test.rb | 23 ++++++++++++++++++++++ 3 files changed, 30 insertions(+) (limited to 'activesupport') 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') -- cgit v1.2.3