8000 Try Time.find_timezone to load · github/ruby@fe08f99 · GitHub
[go: up one dir, main page]

Skip to content

Commit fe08f99

Browse files
committed
Try Time.find_timezone to load
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66075 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent c80aeb5 commit fe08f99

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

test/ruby/test_time_tz.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ def subtest_marshal(time_class, tz, tzname, abbr, utc_offset)
542542
assert_equal(t, t2)
543543
assert_equal(t.utc_offset, t2.utc_offset)
544544
assert_equal(t.utc_offset, (t2+1).utc_offset)
545+
assert_instance_of(t.zone.class, t2.zone)
545546
end
546547

547548
ZONES = {

time.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
static ID id_divmod, id_submicro, id_nano_num, id_nano_den, id_offset, id_zone;
3939
static ID id_quo, id_div;
8000
4040
static ID id_nanosecond, id_microsecond, id_millisecond, id_nsec, id_usec;
41-
static ID id_local_to_utc, id_utc_to_local;
41+
static ID id_local_to_utc, id_utc_to_local, id_find_timezone;
4242
static ID id_year, id_mon, id_mday, id_hour, id_min, id_sec, id_isdst, id_name;
4343

4444
#ifndef TM_IS_TIME
@@ -2210,6 +2210,14 @@ zone_localtime(VALUE zone, VALUE time)
22102210
return 1;
22112211
}
22122212

2213+
static VALUE
2214+
find_timezone(VALUE time, VALUE zone)
2215+
{
2216+
VALUE klass = CLASS_OF(time);
2217+
2218+
return rb_check_funcall_default(klass, id_find_timezone, 1, &zone, Qnil);
2219+
}
2220+
22132221
static VALUE
22142222
time_init_1(int argc, VALUE *argv, VALUE time)
22152223
{
@@ -5016,7 +5024,8 @@ end_submicro: ;
50165024
time_fixoff(time);
50175025
}
50185026
if (!NIL_P(zone)) {
5019-
zone = rb_fstring(zone);
5027+
VALUE z = find_timezone(time, zone);
5028+
zone = NIL_P(z) ? rb_fstring(zone) : RB_TYPE_P(z, T_STRING) ? rb_fstring(z) : z;
50205029
tobj->vtm.zone = zone;
50215030
}
50225031

@@ -5403,6 +5412,11 @@ rb_time_zone_abbreviation(VALUE zone, VALUE time)
54035412
* object are not able to dump by Marshal.
54045413
*
54055414
* The +abbr+ method is used by '%Z' in #strftime.
5415+
*
5416+
* === Auto conversion to Timezone
5417+
*
5418+
* At loading marshaled data, a timezone name will be converted to a timezone
5419+
* object by +find_timezone+ class method, if the method is defined.
54065420
*/
54075421

54085422
void
@@ -5434,6 +5448,7 @@ Init_Time(void)
54345448
id_sec = rb_intern("sec");
54355449
id_isdst = rb_intern("isdst");
54365450
id_name = rb_intern("name");
5451+
id_find_timezone = rb_intern("find_timezone");
54375452

54385453
rb_cTime = rb_define_class("Time", rb_cObject);
54395454
rb_include_module(rb_cTime, rb_mComparable);

0 commit comments

Comments
 (0)
0