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

ruby-changes:52746

From: nobu <ko1@a...>
Date: Mon, 8 Oct 2018 13:03:37 +0900 (JST)
Subject: [ruby-changes:52746] nobu:r64958 (trunk): Timezone at Time#+ and Time#-

nobu	2018-10-08 13:03:32 +0900 (Mon, 08 Oct 2018)

  New Revision: 64958

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

  Log:
    Timezone at Time#+ and Time#-
    
    * time.c (time_add): support for Timezone.

  Modified files:
    trunk/NEWS
    trunk/spec/ruby/core/time/plus_spec.rb
    trunk/test/ruby/test_time_tz.rb
    trunk/time.c
Index: NEWS
===================================================================
--- NEWS	(revision 64957)
+++ NEWS	(revision 64958)
@@ -263,7 +263,8 @@ sufficient information, see the ChangeLo https://github.com/ruby/ruby/blob/trunk/NEWS#L263
     * New features:
 
         * `Time.new` and `Time#getlocal` accept a timezone object as
-	  well as UTC offset string.  [Feature #14850]
+          well as UTC offset string. `Time#+`, `Time#-` and `Time#succ`
+          also preserve the timezone.  [Feature #14850]
 
 * `TracePoint`
 
Index: test/ruby/test_time_tz.rb
===================================================================
--- test/ruby/test_time_tz.rb	(revision 64957)
+++ test/ruby/test_time_tz.rb	(revision 64958)
@@ -536,4 +536,10 @@ class TestTimeTZ::WithTZ < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time_tz.rb#L536
     t = Time.new(2018, 9, 1, 12, 0, 0, tz)
     assert_equal("+0900 #{abbr}", t.strftime("%z %Z"))
   end
+
+  def test_plus_with_timezone
+    t = Time.new(2018, 9, 1, 12, 0, 0, tz) + 4000
+    assert_equal([2018, 9, 1, 13, 6, 40, tz], [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.zone])
+    assert_equal(Time.utc(2018, 9, 1, 4, 6, 40), t)
+  end
 end
Index: time.c
===================================================================
--- time.c	(revision 64957)
+++ time.c	(revision 64958)
@@ -3899,6 +3899,11 @@ time_add(struct time_object *tobj, VALUE https://github.com/ruby/ruby/blob/trunk/time.c#L3899
         GetTimeval(result, tobj);
         TZMODE_SET_FIXOFF(tobj, off);
     }
+    else if (TZMODE_LOCALTIME_P(tobj)) {
+        VALUE zone = tobj->vtm.zone;
+        GetTimeval(result, tobj);
+        tobj->vtm.zone = zone;
+    }
 
     return result;
 }
@@ -3982,6 +3987,9 @@ rb_time_succ(VALUE time) https://github.com/ruby/ruby/blob/trunk/time.c#L3987
     time = time_new_timew(rb_cTime, wadd(tobj->timew, WINT2FIXWV(TIME_SCALE)));
     GetTimeval(time, tobj2);
     TZMODE_COPY(tobj2, tobj);
+    if (TZMODE_LOCALTIME_P(tobj2) && maybe_tzobj_p(tobj2->vtm.zone)) {
+        zone_localtime(tobj2->vtm.zone, time);
+    }
     return time;
 }
 
Index: spec/ruby/core/time/plus_spec.rb
===================================================================
--- spec/ruby/core/time/plus_spec.rb	(revision 64957)
+++ spec/ruby/core/time/plus_spec.rb	(revision 64958)
@@ -47,6 +47,19 @@ describe "Time#+" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/time/plus_spec.rb#L47
     (Time.new(2012, 1, 1, 0, 0, 0, 3600) + 10).utc_offset.should == 3600
   end
 
+  ruby_version_is "2.6" do
+    it "returns a time with the same timezone as self" do
+      zone = mock("timezone")
+      zone.should_receive(:local_to_utc).and_return(Time.utc(2012, 1, 1, 6, 30, 0))
+      zone.should_receive(:utc_to_local).and_return(Time.utc(2012, 1, 1, 12, 0, 10))
+      t = Time.new(2012, 1, 1, 12, 0, 0, zone) + 10
+      t.zone.should == zone
+      t.utc_offset.should == 19800
+      t.to_a[0, 6].should == [10, 0, 12, 1, 1, 2012]
+      t.should == Time.utc(2012, 1, 1, 6, 30, 10)
+    end
+  end
+
   it "does not return a subclass instance" do
     c = Class.new(Time)
     x = c.now + 1

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

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