diff options
| -rw-r--r-- | activesupport/CHANGELOG.md | 2 | ||||
| -rw-r--r-- | activesupport/lib/active_support/core_ext/string.rb | 1 | ||||
| -rw-r--r-- | activesupport/lib/active_support/core_ext/string/zones.rb | 13 | ||||
| -rw-r--r-- | activesupport/test/core_ext/string_ext_test.rb | 68 | 
4 files changed, 60 insertions, 24 deletions
| diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index f5f2aa85ef..2d4864f6fa 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,5 +1,7 @@  ## Rails 4.0.0 (unreleased) ## +*   Add `String#in_time_zone` method to convert a string to an ActiveSupport::TimeWithZone. *Andrew White* +  *   Deprecate `ActiveSupport::BasicObject` in favor of `ActiveSupport::ProxyObject`.      This class is used for proxy classes. It avoids confusion with Ruby's BasicObject      class. diff --git a/activesupport/lib/active_support/core_ext/string.rb b/activesupport/lib/active_support/core_ext/string.rb index ad864765a3..5d7cb81e38 100644 --- a/activesupport/lib/active_support/core_ext/string.rb +++ b/activesupport/lib/active_support/core_ext/string.rb @@ -11,3 +11,4 @@ require 'active_support/core_ext/string/exclude'  require 'active_support/core_ext/string/strip'  require 'active_support/core_ext/string/inquiry'  require 'active_support/core_ext/string/indent' +require 'active_support/core_ext/string/zones' diff --git a/activesupport/lib/active_support/core_ext/string/zones.rb b/activesupport/lib/active_support/core_ext/string/zones.rb new file mode 100644 index 0000000000..e3f20eee29 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/string/zones.rb @@ -0,0 +1,13 @@ +require 'active_support/core_ext/time/zones' + +class String +  # Converts String to a TimeWithZone in the current zone if Time.zone or Time.zone_default +  # is set, otherwise converts String to a Time via String#to_time +  def in_time_zone(zone = ::Time.zone) +    if zone +      ::Time.find_zone!(zone).parse(self) +    else +      to_time +    end +  end +end diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 6720ed42f0..2a187385eb 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -161,30 +161,6 @@ class StringInflectionsTest < ActiveSupport::TestCase      assert_equal 97, 'abc'.ord    end -  def test_string_to_time -    assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time -    assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local) -    assert_equal Time.utc(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time -    assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:local) -    assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time -    assert_equal Time.local_time(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local) -    assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time -    assert_nil "".to_time -  end - -  def test_string_to_datetime -    assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_datetime -    assert_equal 0, "2039-02-27 23:50".to_datetime.offset # use UTC offset -    assert_equal ::Date::ITALY, "2039-02-27 23:50".to_datetime.start # use Ruby's default start value -    assert_equal DateTime.civil(2039, 2, 27, 23, 50, 19 + Rational(275038, 1000000), "-04:00"), "2039-02-27T23:50:19.275038-04:00".to_datetime -    assert_nil "".to_datetime -  end - -  def test_string_to_date -    assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date -    assert_nil "".to_date -  end -    def test_access      s = "hello"      assert_equal "h", s.at(0) @@ -308,6 +284,50 @@ class StringInflectionsTest < ActiveSupport::TestCase    end  end +class StringConversionsTest < ActiveSupport::TestCase +  def test_string_to_time +    assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time +    assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local) +    assert_equal Time.utc(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time +    assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:local) +    assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time +    assert_equal Time.local_time(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local) +    assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time +    assert_nil "".to_time +  end + +  def test_string_to_datetime +    assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_datetime +    assert_equal 0, "2039-02-27 23:50".to_datetime.offset # use UTC offset +    assert_equal ::Date::ITALY, "2039-02-27 23:50".to_datetime.start # use Ruby's default start value +    assert_equal DateTime.civil(2039, 2, 27, 23, 50, 19 + Rational(275038, 1000000), "-04:00"), "2039-02-27T23:50:19.275038-04:00".to_datetime +    assert_nil "".to_datetime +  end + +  def test_string_to_date +    assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date +    assert_nil "".to_date +  end + +  def test_string_in_time_zone +    with_tz_default "US/Eastern" do +      assert_equal Time.utc(2012,6,7,16,30).in_time_zone, "2012-06-07 12:30:00".in_time_zone +      assert_equal Time.utc(2012,6,7,11,30).in_time_zone, "2012-06-07 12:30:00 +0100".in_time_zone +      assert_equal Time.utc(2012,6,7,19,30).in_time_zone("US/Pacific"), "2012-06-07 12:30:00".in_time_zone("US/Pacific") +      assert_nil "".in_time_zone +    end +  end + +  private +    def with_tz_default(tz = nil) +      old_tz = Time.zone +      Time.zone = tz +      yield +    ensure +      Time.zone = old_tz +    end +end +  class StringBehaviourTest < ActiveSupport::TestCase    def test_acts_like_string      assert 'Bambi'.acts_like_string? | 
