[前][次][番号順一覧][スレッド一覧]

ruby-changes:47143

From: nobu <ko1@a...>
Date: Tue, 4 Jul 2017 13:23:11 +0900 (JST)
Subject: [ruby-changes:47143] nobu:r59258 (trunk): time.c: preserve marshalled timezone

nobu	2017-07-04 13:23:06 +0900 (Tue, 04 Jul 2017)

  New Revision: 59258

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59258

  Log:
    time.c: preserve marshalled timezone
    
    * time.c (time_add): preserve timezone name restored by Marshal.
      [ruby-core:81892] [Bug #13710]
    
    * time.c (time_mload): reset localtime if having timezone.

  Modified files:
    trunk/test/ruby/test_time.rb
    trunk/time.c
Index: test/ruby/test_time.rb
===================================================================
--- test/ruby/test_time.rb	(revision 59257)
+++ test/ruby/test_time.rb	(revision 59258)
@@ -308,7 +308,9 @@ class TestTime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time.rb#L308
     in_timezone('JST-9') do
       t = Time.local(2013, 2, 24)
       assert_equal('JST', Time.local(2013, 2, 24).zone)
-      assert_equal('JST', Marshal.load(Marshal.dump(t)).zone)
+      t = Marshal.load(Marshal.dump(t))
+      assert_equal('JST', t.zone)
+      assert_equal('JST', (t+1).zone, '[ruby-core:81892] [Bug #13710]')
     end
   end
 
Index: time.c
===================================================================
--- time.c	(revision 59257)
+++ time.c	(revision 59258)
@@ -3597,9 +3597,9 @@ time_to_s(VALUE time) https://github.com/ruby/ruby/blob/trunk/time.c#L3597
 }
 
 static VALUE
-time_add(struct time_object *tobj, VALUE offset, int sign)
+time_add(struct time_object *tobj, VALUE torig, VALUE offset, int sign)
 {
-    VALUE result;
+    VALUE result, zone;
     offset = num_exact(offset);
     if (sign < 0)
         result = time_new_timew(rb_cTime, wsub(tobj->timew, rb_time_magnify(v2w(offset))));
@@ -3614,6 +3614,11 @@ time_add(struct time_object *tobj, VALUE https://github.com/ruby/ruby/blob/trunk/time.c#L3614
         GetTimeval(result, tobj);
         TIME_SET_FIXOFF(tobj, off);
     }
+    if (!tobj->vtm.zone && !NIL_P(zone = rb_attr_get(torig, id_zone))) {
+	tobj->vtm.zone = StringValueCStr(zone);
+	rb_ivar_set(result, id_zone, zone);
+    }
+
     return result;
 }
 
@@ -3637,7 +3642,7 @@ time_plus(VALUE time1, VALUE time2) https://github.com/ruby/ruby/blob/trunk/time.c#L3642
     if (IsTimeval(time2)) {
 	rb_raise(rb_eTypeError, "time + time?");
     }
-    return time_add(tobj, time2, 1);
+    return time_add(tobj, time1, time2, 1);
 }
 
 /*
@@ -3667,7 +3672,7 @@ time_minus(VALUE time1, VALUE time2) https://github.com/ruby/ruby/blob/trunk/time.c#L3672
 	GetTimeval(time2, tobj2);
         return rb_Float(rb_time_unmagnify_to_float(wsub(tobj->timew, tobj2->timew)));
     }
-    return time_add(tobj, time2, -1);
+    return time_add(tobj, time1, time2, -1);
 }
 
 /*
@@ -3770,9 +3775,9 @@ time_round(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/time.c#L3775
     den = quov(INT2FIX(1), a);
     v = modv(v, den);
     if (lt(v, quov(den, INT2FIX(2))))
-        return time_add(tobj, v, -1);
+        return time_add(tobj, time, v, -1);
     else
-        return time_add(tobj, subv(den, v), 1);
+        return time_add(tobj, time, subv(den, v), 1);
 }
 
 /*
@@ -4693,7 +4698,8 @@ end_submicro: ; https://github.com/ruby/ruby/blob/trunk/time.c#L4698
 	time_fixoff(time);
     }
     if (!NIL_P(zone)) {
-	zone = rb_str_new_frozen(zone);
+	if (TIME_FIXOFF_P(tobj)) TIME_SET_LOCALTIME(tobj);
+	zone = rb_fstring(zone);
 	tobj->vtm.zone = StringValueCStr(zone);
 	rb_ivar_set(time, id_zone, zone);
     }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]