From 4544b3824c8ce74e0a92c58a3167f900904f506b Mon Sep 17 00:00:00 2001 From: tenderlove Date: Thu, 25 Aug 2011 23:24:33 +0000 Subject: * time.c (strftimev): Make Time#to_s default to US-ASCII encoding but respect Encoding.default_internal. [ruby-core:39092] * test/ruby/test_time.rb (class TestTime): Corresponding test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33073 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ NEWS | 5 +++++ test/ruby/test_time.rb | 16 ++++++++++++++++ time.c | 2 ++ 4 files changed, 29 insertions(+) diff --git a/ChangeLog b/ChangeLog index e058d861dd..e25b8ec650 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Fri Aug 26 08:21:10 2011 Aaron Patterson + + * time.c (strftimev): Make Time#to_s default to US-ASCII encoding but + respect Encoding.default_internal. [ruby-core:39092] + * test/ruby/test_time.rb (class TestTime): Corresponding test. + Thu Aug 25 09:43:16 2011 Eric Hodel * ext/openssl/lib/openssl/bn.rb: Hide copyright info from RDoc. diff --git a/NEWS b/NEWS index d16e73971c..56a25f31d8 100644 --- a/NEWS +++ b/NEWS @@ -24,6 +24,11 @@ with all sufficient information, see the ChangeLog file. * Signal.trap raises ArgumentError when :SEGV, :BUS, :ILL, :FPE, :VTALRM are specified. + * Time + * change return value: + * Time#to_s returned encoding defaults to US-ASCII but automatically + transcodes to Encoding.default_internal if it is set. + === Language changes === Compatibility issues (excluding feature bug fixes) diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb index 1f07a8249c..32d3b976d0 100644 --- a/test/ruby/test_time.rb +++ b/test/ruby/test_time.rb @@ -15,6 +15,22 @@ class TestTime < Test::Unit::TestCase $VERBOSE = @verbose end + def test_to_s_default_encoding + before = Encoding.default_internal + Encoding.default_internal = nil + assert_equal Encoding::US_ASCII, Time.now.to_s.encoding + ensure + Encoding.default_internal = before + end + + def test_to_s_transcoding + before = Encoding.default_internal + Encoding.default_internal = Encoding::UTF_8 + assert_equal Encoding::UTF_8, Time.now.to_s.encoding + ensure + Encoding.default_internal = before + end + def test_new assert_equal(Time.utc(2000,2,10), Time.new(2000,2,10, 11,0,0, 3600*11)) assert_equal(Time.utc(2000,2,10), Time.new(2000,2,9, 13,0,0, -3600*11)) diff --git a/time.c b/time.c index ebe0ab9bd9..fbd0ecf198 100644 --- a/time.c +++ b/time.c @@ -4347,6 +4347,8 @@ strftimev(const char *fmt, VALUE time) MAKE_TM(time, tobj); len = rb_strftime_alloc(&buf, fmt, &tobj->vtm, tobj->timew, TIME_UTC_P(tobj)); str = rb_str_new(buf, len); + rb_enc_associate_index(str, rb_usascii_encindex()); + str = rb_str_export_to_enc(str, rb_default_internal_encoding()); if (buf != buffer) xfree(buf); return str; } -- cgit v1.2.3