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

ruby-changes:52763

From: nobu <ko1@a...>
Date: Tue, 9 Oct 2018 14:55:34 +0900 (JST)
Subject: [ruby-changes:52763] nobu:r64975 (trunk): Time.parse based from non-Time object

nobu	2018-10-09 14:55:29 +0900 (Tue, 09 Oct 2018)

  New Revision: 64975

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

  Log:
    Time.parse based from non-Time object
    
    * lib/time.rb (Time.make_time): as the document states, the second
      argument of `Time.parse` may be a non-`Time` object which does not
      have `getlocal` method, assume it is in the local time in the case.
      based on the patch by nkmrya (Yasuhiro Nakamura) at
      [ruby-core:68775].  [ruby-core:68775] [Bug #11037]
    
    Co-authored-by: nkmrya (Yasuhiro Nakamura) <yasuhiro6194@g...>

  Modified files:
    trunk/lib/time.rb
    trunk/test/test_time.rb
Index: lib/time.rb
===================================================================
--- lib/time.rb	(revision 64974)
+++ lib/time.rb	(revision 64975)
@@ -267,7 +267,7 @@ class Time https://github.com/ruby/ruby/blob/trunk/lib/time.rb#L267
         return make_time(date, year, nil, mon, day, hour, min, sec, sec_fraction, zone, now)
       end
 
-      if now
+      if now and now.respond_to?(:getlocal)
         if off
           now = now.getlocal(off) if now.utc_offset != off
         else
Index: test/test_time.rb
===================================================================
--- test/test_time.rb	(revision 64974)
+++ test/test_time.rb	(revision 64975)
@@ -523,4 +523,23 @@ class TestTimeExtension < Test::Unit::Te https://github.com/ruby/ruby/blob/trunk/test/test_time.rb#L523
     define_method(test) {__send__(sub, :xmlschema)}
     define_method(test.sub(/xmlschema/, 'iso8601')) {__send__(sub, :iso8601)}
   end
+
+  def test_parse_with_various_object
+    d  = Date.new(2010, 10, 28)
+    dt = DateTime.new(2010, 10, 28)
+    md = MyDate.new(10, 28, 2010)
+
+    t = Time.local(2010, 10, 28, 21, 26, 00)
+    assert_equal(t, Time.parse("21:26",  d))
+    assert_equal(t, Time.parse("21:26", dt))
+    assert_equal(t, Time.parse("21:26", md))
+  end
+
+  class MyDate
+    attr_reader :mon, :day, :year
+
+    def initialize(mon, day, year)
+      @mon, @day, @year = mon, day, year
+    end
+  end
 end

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

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