8000 time.c: rescue find_timezone when loading · github/ruby@4471d4a · GitHub
[go: up one dir, main page]

Skip to content

Commit 4471d4a

Browse files
committed
time.c: rescue find_timezone when loading
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66080 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 2d0833e commit 4471d4a

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

test/ruby/test_time_tz.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,26 @@ def subtest_marshal(time_class, tz, tzarg, tzname, abbr, utc_offset)
565565
assert_instance_of(t.zone.class, t2.zone)
566566
end
567567

568+
def test_invalid_zone
569+
make_timezone("INVALID", "INV", 0)
570+
rescue => e
571+
assert_kind_of(StandardError, e)
572+
else
573+
assert false, "ArgumentError expected but nothing was raised."
574+
end
575+
576+
def nametest_marshal_compatibility(time_class, tzname, abbr, utc_offset)
577+
data = [
578+
"\x04\x08Iu:".b, Marshal.dump(time_class)[3..-1],
579+
"\x0d""\xEF\xA7\x1D\x80\x00\x00\x00\x00".b,
580+
Marshal.dump({offset: utc_offset, zone: abbr})[3..-1],
581+
].join('')
582+
t = Marshal.load(data)
583+
assert_equal(utc_offset, t.utc_offset)
584+
assert_equal(utc_offset, (t+1).utc_offset)
585+
# t.zone may be a mere String or timezone object.
586+
end
587+
568588
ZONES = {
569589
"Asia/Tokyo" => ["JST", +9*3600],
570590
"America/Los_Angeles" => ["PDT", -7*3600],
@@ -586,6 +606,16 @@ def make_timezone(tzname, abbr, utc_offset)
586606
end
587607
end
588608
end
609+
610+
instance_methods(false).grep(/\Aname(?=test_)/) do |subtest|
611+
test = $'
612+
ZONES.each_pair do |tzname, (abbr, utc_offset)|
613+
define_method("#{test}@#{tzname}") do
614+
time_class = self.class::TIME_CLASS
615+
__send__(subtest, time_class, tzname, abbr, utc_offset)
616+
end
617+
end
618+
end
589619
end
590620

591621
class TestTimeTZ::DummyTZ < Test::Unit::TestCase
@@ -632,7 +662,7 @@ class TestTimeTZ::GemTimezone < Test::Unit::TestCase
632662

633663
class TIME_CLASS < ::Time
634664
def self.find_timezone(name)
635-
Timezone[name]
665+
Timezone.fetch(name)
636666
end
637667
end
638668

time.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4965,6 +4965,26 @@ time_dump(int argc, VALUE *argv, VALUE time)
49654965
return str;
49664966
}
49674967

4968+
static VALUE
4969+
mload_findzone(VALUE arg)
4970+
{
4971+
VALUE *argp = (VALUE *)arg;
4972+
VALUE time = argp[0], zone = argp[1];
4973+
return find_timezone(time, zone);
4974+
}
4975+
4976+
static VALUE
4977+
mload_zone(VALUE time, VALUE zone)
4978+
{
4979+
VALUE z, args[2];
4980+
args[0] = time;
4981+
args[1] = zone;
4982+
z = rb_rescue(mload_findzone, (VALUE)args, (VALUE (*)(ANYARGS))NULL, Qnil);
4983+
if (NIL_P(z)) return rb_fstring(zone);
4984+
if (RB_TYPE_P(z, T_STRING)) return rb_fstring(z);
4985+
return z;
4986+
}
4987+
49684988
/* :nodoc: */
49694989
static VALUE
49704990
time_mload(VALUE time, VALUE str)
@@ -5079,8 +5099,7 @@ end_submicro: ;
50795099
time_fixoff(time);
50805100
}
50815101
if (!NIL_P(zone)) {
5082-
VALUE z = find_timezone(time, zone);
5083-
zone = NIL_P(z) ? rb_fstring(zone) : RB_TYPE_P(z, T_STRING) ? rb_fstring(z) : z;
5102+
zone = mload_zone(time, zone);
50845103
tobj->vtm.zone = zone;
50855104
}
50865105

0 commit comments

Comments
 (0)
0