1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

time.c: unnecessary encoding

* time.c (time_zone_name): remove unnecessary encoding and
  conversion if it is 7bit-ascii only.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46907 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-07-23 07:20:39 +00:00
parent 87c8901d69
commit 2e8ce8899a
2 changed files with 15 additions and 3 deletions

View file

@ -520,6 +520,7 @@ class TestTime < Test::Unit::TestCase
def assert_zone_encoding(time) def assert_zone_encoding(time)
zone = time.zone zone = time.zone
assert_predicate(zone, :valid_encoding?) assert_predicate(zone, :valid_encoding?)
return if zone.ascii_only?
enc = Encoding.default_internal || Encoding.find('locale') enc = Encoding.default_internal || Encoding.find('locale')
assert_equal(enc, zone.encoding) assert_equal(enc, zone.encoding)
end end

17
time.c
View file

@ -4198,6 +4198,16 @@ time_isdst(VALUE time)
return tobj->vtm.isdst ? Qtrue : Qfalse; return tobj->vtm.isdst ? Qtrue : Qfalse;
} }
static VALUE
time_zone_name(const char *zone)
{
VALUE name = rb_str_new_cstr(zone);
if (!rb_enc_str_asciionly_p(name)) {
name = rb_external_str_with_enc(name, rb_locale_encoding());
}
return name;
}
/* /*
* call-seq: * call-seq:
* time.zone -> string * time.zone -> string
@ -4220,11 +4230,12 @@ time_zone(VALUE time)
MAKE_TM(time, tobj); MAKE_TM(time, tobj);
if (TIME_UTC_P(tobj)) { if (TIME_UTC_P(tobj)) {
return rb_obj_untaint(rb_locale_str_new_cstr("UTC")); return rb_usascii_str_new_cstr("UTC");
} }
if (tobj->vtm.zone == NULL) if (tobj->vtm.zone == NULL)
return Qnil; return Qnil;
return rb_obj_untaint(rb_locale_str_new_cstr(tobj->vtm.zone));
return time_zone_name(tobj->vtm.zone);
} }
/* /*
@ -4693,7 +4704,7 @@ time_mdump(VALUE time)
rb_ivar_set(str, id_offset, off); rb_ivar_set(str, id_offset, off);
} }
if (tobj->vtm.zone) { if (tobj->vtm.zone) {
rb_ivar_set(str, id_zone, rb_locale_str_new_cstr(tobj->vtm.zone)); rb_ivar_set(str, id_zone, time_zone_name(tobj->vtm.zone));
} }
return str; return str;
} }