diff options
5 files changed, 16 insertions, 11 deletions
diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index 3491dec25a..511c69544e 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -17,6 +17,9 @@ module ActiveSupport #:nodoc: alias_method :to_s, :to_formatted_s alias_method :default_inspect, :inspect alias_method :inspect, :readable_inspect + + # Ruby 1.9 has Date#to_time which converts to localtime only. + remove_method :to_time if base.instance_methods.include?(:to_time) end end @@ -25,13 +28,13 @@ module ActiveSupport #:nodoc: if formatter.respond_to?(:call) formatter.call(self).to_s else - strftime(formatter).strip + strftime(formatter) end else to_default_s end end - + # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005" def readable_inspect strftime("%a, %d %b %Y") @@ -40,18 +43,18 @@ module ActiveSupport #:nodoc: # To be able to keep Times, Dates and DateTimes interchangeable on conversions def to_date self - end + end if RUBY_VERSION < '1.9' # Converts self to a Ruby Time object; time is set to beginning of day # Timezone can either be :local or :utc (default :local) def to_time(form = :local) ::Time.send("#{form}_time", year, month, day) end - + # Converts self to a Ruby DateTime object; time is set to beginning of day def to_datetime ::DateTime.civil(year, month, day, 0, 0, 0, 0, 0) - end + end if RUBY_VERSION < '1.9' def xmlschema to_time.xmlschema diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb index 91ed0e94e9..4739e3fb6d 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -17,13 +17,13 @@ module ActiveSupport #:nodoc: if formatter.respond_to?(:call) formatter.call(self).to_s else - strftime(formatter).strip + strftime(formatter) end else to_datetime_default_s end end - + # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005 14:30:00 +0000" def readable_inspect to_s(:rfc822) diff --git a/activesupport/lib/active_support/core_ext/time/conversions.rb b/activesupport/lib/active_support/core_ext/time/conversions.rb index 742f71e0a6..c0bc8b4313 100644 --- a/activesupport/lib/active_support/core_ext/time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/time/conversions.rb @@ -24,7 +24,7 @@ module ActiveSupport #:nodoc: if formatter.respond_to?(:call) formatter.call(self).to_s else - strftime(formatter).strip + strftime(formatter) end else to_default_s diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 3c79fccdd1..b7bdbc5deb 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -32,6 +32,7 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_equal DateTime.new(2005, 2, 21), DateTime.new(2005, 2, 21).to_datetime end + # FIXME: ruby 1.9 compat def test_to_time assert_equal Time.utc(2005, 2, 21, 10, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12, 0, 0).to_time assert_equal Time.local(2005, 2, 21, 10, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12, Rational(-5, 24), 0).to_time diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 88fd16531e..4a64ebd584 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -279,7 +279,8 @@ class TimeExtCalculationsTest < Test::Unit::TestCase def test_to_s time = Time.utc(2005, 2, 21, 17, 44, 30) - assert_equal "Mon Feb 21 17:44:30 UTC 2005", time.to_s + assert_equal time.to_default_s, time.to_s + assert_equal time.to_default_s, time.to_s(:doesnt_exist) assert_equal "2005-02-21 17:44:30", time.to_s(:db) assert_equal "21 Feb 17:44", time.to_s(:short) assert_equal "17:44", time.to_s(:time) @@ -381,10 +382,10 @@ class TimeExtCalculationsTest < Test::Unit::TestCase end protected - def with_timezone new_tz='US/Eastern' + def with_timezone(new_tz = 'US/Eastern') old_tz, ENV['TZ'] = ENV['TZ'], new_tz yield ensure - ENV['TZ'] = old_tz + old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ') end end |