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

ruby-changes:14704

From: nobu <ko1@a...>
Date: Thu, 4 Feb 2010 00:38:00 +0900 (JST)
Subject: [ruby-changes:14704] Ruby:r26560 (trunk): * time.c (time_mdump, time_mload): dump/load utc_offset.

nobu	2010-02-04 00:37:45 +0900 (Thu, 04 Feb 2010)

  New Revision: 26560

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=26560

  Log:
    * time.c (time_mdump, time_mload): dump/load utc_offset.
      [ruby-dev:40063]

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_time.rb
    trunk/time.c

Index: time.c
===================================================================
--- time.c	(revision 26559)
+++ time.c	(revision 26560)
@@ -103,7 +103,7 @@
 }
 #endif
 
-static ID id_divmod, id_mul, id_submicro, id_nano_num, id_nano_den;
+static ID id_divmod, id_mul, id_submicro, id_nano_num, id_nano_den, id_offset;
 static ID id_eq, id_ne, id_quo, id_div, id_cmp, id_lshift;
 
 #define eq(x,y) (RTEST(rb_funcall((x), id_eq, 1, (y))))
@@ -3675,6 +3675,13 @@
             len = 1;
         rb_ivar_set(str, id_submicro, rb_str_new(buf, len));
     }
+    if (!TIME_UTC_P(tobj)) {
+	VALUE off = time_utc_offset(time), div, mod;
+	divmodv(off, INT2FIX(1), &div, &mod);
+	if (rb_equal(mod, INT2FIX(0)))
+	    off = rb_Integer(div);
+	rb_ivar_set(str, id_offset, off);
+    }
     return str;
 }
 
@@ -3711,7 +3718,7 @@
     struct vtm vtm;
     int i, gmt;
     long nsec;
-    VALUE timexv, submicro, nano_num, nano_den;
+    VALUE timexv, submicro, nano_num, nano_den, offset;
 
     time_modify(time);
 
@@ -3727,6 +3734,11 @@
     if (submicro != Qnil) {
         st_delete(rb_generic_ivar_table(str), (st_data_t*)&id_submicro, 0);
     }
+    offset = rb_attr_get(str, id_offset);
+    if (offset != Qnil) {
+        validate_utc_offset(offset);
+        st_delete(rb_generic_ivar_table(str), (st_data_t*)&id_offset, 0);
+    }
     rb_copy_generic_ivar(time, str);
 
     StringValue(str);
@@ -3745,6 +3757,7 @@
 
     if ((p & (1UL<<31)) == 0) {
         gmt = 0;
+	offset = Qnil;
 	sec = p;
 	usec = s;
         nsec = usec * 1000;
@@ -3799,8 +3812,14 @@
 
     GetTimeval(time, tobj);
     tobj->tm_got = 0;
-    if (gmt) TIME_SET_UTC(tobj);
     tobj->timexv = timexv;
+    if (gmt) {
+	TIME_SET_UTC(tobj);
+    }
+    else if (!NIL_P(offset)) {
+	time_set_utc_offset(time, offset);
+	time_fixoff(time);
+    }
 
     return time;
 }
@@ -3855,6 +3874,7 @@
     id_submicro = rb_intern("submicro");
     id_nano_num = rb_intern("nano_num");
     id_nano_den = rb_intern("nano_den");
+    id_offset = rb_intern("offset");
 
     rb_cTime = rb_define_class("Time", rb_cObject);
     rb_include_module(rb_cTime, rb_mComparable);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 26559)
+++ ChangeLog	(revision 26560)
@@ -1,3 +1,8 @@
+Thu Feb  4 00:37:43 2010  Nobuyoshi Nakada  <nobu@r...>
+
+	* time.c (time_mdump, time_mload): dump/load utc_offset.
+	  [ruby-dev:40063]
+
 Wed Feb  3 22:22:30 2010  Nobuyoshi Nakada  <nobu@r...>
 
 	* configure.in: check for non-portable stack attribute functions.
Index: test/ruby/test_time.rb
===================================================================
--- test/ruby/test_time.rb	(revision 26559)
+++ test/ruby/test_time.rb	(revision 26560)
@@ -210,6 +210,20 @@
     assert_marshal_roundtrip(Marshal.load(Marshal.dump(t)))
   end
 
+  def test_marshal_timezone
+    bug = '[ruby-dev:40063]'
+
+    t1 = Time.gm(2000)
+    m = Marshal.dump(t1.getlocal("-02:00"))
+    t2 = Marshal.load(m)
+    assert_equal(t1, t2)
+    assert_equal(-7200, t2.utc_offset, bug)
+    m = Marshal.dump(t1.getlocal("+08:15"))
+    t2 = Marshal.load(m)
+    assert_equal(t1, t2)
+    assert_equal(29700, t2.utc_offset, bug)
+  end
+
   # Sat Jan 01 00:00:00 UTC 2000
   T2000 = Time.at(946684800).gmtime
 

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

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