class StringConversionsTest

Public Instance Methods

test_daylight_savings_string_to_time_when_current_time_is_daylight_savings() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 554
def test_daylight_savings_string_to_time_when_current_time_is_daylight_savings
  with_env_tz "US/Eastern" do
    Time.stub(:now, Time.local(2012, 7, 1)) do
      assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time
      assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 -0700".to_time
      assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 -0700".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 -0400".to_time
      assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 -0400".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 6, 0), "2012-07-01 10:00 UTC".to_time
      assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00 UTC".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 PDT".to_time
      assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 PDT".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 EDT".to_time
      assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 EDT".to_time(:utc)
    end
  end
end
test_daylight_savings_string_to_time_when_current_time_is_standard_time() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 535
def test_daylight_savings_string_to_time_when_current_time_is_standard_time
  with_env_tz "US/Eastern" do
    Time.stub(:now, Time.local(2012, 1, 1)) do
      assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time
      assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 -0700".to_time
      assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 -0700".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 -0400".to_time
      assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 -0400".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 6, 0), "2012-07-01 10:00 UTC".to_time
      assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00 UTC".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 PDT".to_time
      assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 PDT".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 EDT".to_time
      assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 EDT".to_time(:utc)
    end
  end
end
test_partial_string_to_datetime() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 627
def test_partial_string_to_datetime
  now = DateTime.now
  assert_equal DateTime.civil(now.year, now.month, now.day, 23, 50), "23:50".to_datetime
  assert_equal DateTime.civil(now.year, now.month, now.day, 23, 50, 0, "-04:00"), "23:50 -0400".to_datetime
end
test_partial_string_to_time() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 487
def test_partial_string_to_time
  with_env_tz "Europe/Moscow" do # use timezone which does not observe DST.
    now = Time.now
    assert_equal Time.local(now.year, now.month, now.day, 23, 50), "23:50".to_time
    assert_equal Time.utc(now.year, now.month, now.day, 23, 50), "23:50".to_time(:utc)
    assert_equal Time.local(now.year, now.month, now.day, 17, 50), "13:50 -0100".to_time
    assert_equal Time.utc(now.year, now.month, now.day, 23, 50), "22:50 -0100".to_time(:utc)
  end
end
test_partial_string_to_time_when_current_time_is_daylight_savings() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 596
def test_partial_string_to_time_when_current_time_is_daylight_savings
  with_env_tz "US/Eastern" do
    Time.stub(:now, Time.local(2012, 7, 1)) do
      assert_equal Time.local(2012, 7, 1, 10, 0), "10:00".to_time
      assert_equal Time.utc(2012, 7, 1, 10, 0), "10:00".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 7, 0), "10:00 -0100".to_time
      assert_equal Time.utc(2012, 7, 1, 11, 0), "10:00 -0100".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 11, 0), "10:00 -0500".to_time
      assert_equal Time.utc(2012, 7, 1, 15, 0), "10:00 -0500".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 6, 0), "10:00 UTC".to_time
      assert_equal Time.utc(2012, 7, 1, 10, 0), "10:00 UTC".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 14, 0), "10:00 PST".to_time
      assert_equal Time.utc(2012, 7, 1, 18, 0), "10:00 PST".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 13, 0), "10:00 PDT".to_time
      assert_equal Time.utc(2012, 7, 1, 17, 0), "10:00 PDT".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 11, 0), "10:00 EST".to_time
      assert_equal Time.utc(2012, 7, 1, 15, 0), "10:00 EST".to_time(:utc)
      assert_equal Time.local(2012, 7, 1, 10, 0), "10:00 EDT".to_time
      assert_equal Time.utc(2012, 7, 1, 14, 0), "10:00 EDT".to_time(:utc)
    end
  end
end
test_partial_string_to_time_when_current_time_is_standard_time() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 573
def test_partial_string_to_time_when_current_time_is_standard_time
  with_env_tz "US/Eastern" do
    Time.stub(:now, Time.local(2012, 1, 1)) do
      assert_equal Time.local(2012, 1, 1, 10, 0), "10:00".to_time
      assert_equal Time.utc(2012, 1, 1, 10, 0),  "10:00".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 6, 0), "10:00 -0100".to_time
      assert_equal Time.utc(2012, 1, 1, 11, 0), "10:00 -0100".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 10, 0), "10:00 -0500".to_time
      assert_equal Time.utc(2012, 1, 1, 15, 0), "10:00 -0500".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 5, 0), "10:00 UTC".to_time
      assert_equal Time.utc(2012, 1, 1, 10, 0), "10:00 UTC".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 13, 0), "10:00 PST".to_time
      assert_equal Time.utc(2012, 1, 1, 18, 0), "10:00 PST".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 12, 0), "10:00 PDT".to_time
      assert_equal Time.utc(2012, 1, 1, 17, 0), "10:00 PDT".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 10, 0), "10:00 EST".to_time
      assert_equal Time.utc(2012, 1, 1, 15, 0), "10:00 EST".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 9, 0), "10:00 EDT".to_time
      assert_equal Time.utc(2012, 1, 1, 14, 0), "10:00 EDT".to_time(:utc)
    end
  end
end
test_standard_time_string_to_time_when_current_time_is_daylight_savings() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 516
def test_standard_time_string_to_time_when_current_time_is_daylight_savings
  with_env_tz "US/Eastern" do
    Time.stub(:now, Time.local(2012, 7, 1)) do
      assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time
      assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 -0800".to_time
      assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 -0800".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 -0500".to_time
      assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 -0500".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 5, 0), "2012-01-01 10:00 UTC".to_time
      assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00 UTC".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 PST".to_time
      assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 PST".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 EST".to_time
      assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 EST".to_time(:utc)
    end
  end
end
test_standard_time_string_to_time_when_current_time_is_standard_time() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 497
def test_standard_time_string_to_time_when_current_time_is_standard_time
  with_env_tz "US/Eastern" do
    Time.stub(:now, Time.local(2012, 1, 1)) do
      assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time
      assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 -0800".to_time
      assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 -0800".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 -0500".to_time
      assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 -0500".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 5, 0), "2012-01-01 10:00 UTC".to_time
      assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00 UTC".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 PST".to_time
      assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 PST".to_time(:utc)
      assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 EST".to_time
      assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 EST".to_time(:utc)
    end
  end
end
test_string_to_date() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 633
def test_string_to_date
  assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date
  assert_nil "".to_date
  assert_equal Date.new(Date.today.year, 2, 3), "Feb 3rd".to_date
end
test_string_to_datetime() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 619
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
test_string_to_time() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 455
def test_string_to_time
  with_env_tz "Europe/Moscow" do
    assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:utc)
    assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time
    assert_equal Time.utc(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:utc)
    assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time
    assert_equal Time.utc(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:utc)
    assert_equal Time.local(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time
    assert_equal Time.local(2011, 2, 27, 17, 50), "2011-02-27 13:50 -0100".to_time
    assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time(:utc)
    assert_equal Time.local(2005, 2, 27, 22, 50), "2005-02-27 14:50 -0500".to_time
    assert_nil "010".to_time
    assert_nil "".to_time
  end
end
test_string_to_time_utc_offset() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 471
def test_string_to_time_utc_offset
  with_env_tz "US/Eastern" do
    if ActiveSupport.to_time_preserves_timezone
      assert_equal 0, "2005-02-27 23:50".to_time(:utc).utc_offset
      assert_equal(-18000, "2005-02-27 23:50".to_time.utc_offset)
      assert_equal 0, "2005-02-27 22:50 -0100".to_time(:utc).utc_offset
      assert_equal(-3600, "2005-02-27 22:50 -0100".to_time.utc_offset)
    else
      assert_equal 0, "2005-02-27 23:50".to_time(:utc).utc_offset
      assert_equal(-18000, "2005-02-27 23:50".to_time.utc_offset)
      assert_equal 0, "2005-02-27 22:50 -0100".to_time(:utc).utc_offset
      assert_equal(-18000, "2005-02-27 22:50 -0100".to_time.utc_offset)
    end
  end
end