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

ruby-changes:31785

From: tenderlove <ko1@a...>
Date: Wed, 27 Nov 2013 06:47:03 +0900 (JST)
Subject: [ruby-changes:31785] tenderlove:r43864 (trunk): * ext/psych/lib/psych/scalar_scanner.rb: fix support for negative

tenderlove	2013-11-27 06:41:48 +0900 (Wed, 27 Nov 2013)

  New Revision: 43864

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

  Log:
    * ext/psych/lib/psych/scalar_scanner.rb: fix support for negative
      years.
    * ext/psych/lib/psych/visitors/yaml_tree.rb: ditto
    * test/psych/test_date_time.rb: test for change.
      Fixes: https://github.com/tenderlove/psych/issues/168

  Modified files:
    trunk/ChangeLog
    trunk/ext/psych/lib/psych/scalar_scanner.rb
    trunk/ext/psych/lib/psych/visitors/yaml_tree.rb
    trunk/test/psych/test_date_time.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43863)
+++ ChangeLog	(revision 43864)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Nov 27 06:40:18 2013  Aaron Patterson <aaron@t...>
+
+	* ext/psych/lib/psych/scalar_scanner.rb: fix support for negative
+	  years.
+	* ext/psych/lib/psych/visitors/yaml_tree.rb: ditto
+	* test/psych/test_date_time.rb: test for change.
+	  Fixes: https://github.com/tenderlove/psych/issues/168
+
 Wed Nov 27 04:46:55 2013  Aaron Patterson <aaron@t...>
 
 	* ext/psych/lib/psych/scalar_scanner.rb: fix regexp for matching TIME
Index: ext/psych/lib/psych/scalar_scanner.rb
===================================================================
--- ext/psych/lib/psych/scalar_scanner.rb	(revision 43863)
+++ ext/psych/lib/psych/scalar_scanner.rb	(revision 43864)
@@ -5,7 +5,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/scalar_scanner.rb#L5
   # Scan scalars for built in types
   class ScalarScanner
     # Taken from http://yaml.org/type/timestamp.html
-    TIME = /^\d{4}-\d{1,2}-\d{1,2}(?:[Tt]|\s+)\d{1,2}:\d\d:\d\d(?:\.\d*)?(?:\s*(?:Z|[-+]\d{1,2}:?(?:\d\d)?))?$/
+    TIME = /^-?\d{4}-\d{1,2}-\d{1,2}(?:[Tt]|\s+)\d{1,2}:\d\d:\d\d(?:\.\d*)?(?:\s*(?:Z|[-+]\d{1,2}:?(?:\d\d)?))?$/
 
     # Taken from http://yaml.org/type/float.html
     FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9]*([eE][-+][0-9]+)?(?# base 10)
@@ -123,7 +123,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/scalar_scanner.rb#L123
       klass = class_loader.load 'Time'
 
       date, time = *(string.split(/[ tT]/, 2))
-      (yy, m, dd) = date.split('-').map { |x| x.to_i }
+      (yy, m, dd) = date.match(/^(-?\d{4})-(\d{1,2})-(\d{1,2})/).captures.map { |x| x.to_i }
       md = time.match(/(\d+:\d+:\d+)(?:\.(\d*))?\s*(Z|[-+]\d+(:\d\d)?)?/)
 
       (hh, mm, ss) = md[1].split(':').map { |x| x.to_i }
Index: ext/psych/lib/psych/visitors/yaml_tree.rb
===================================================================
--- ext/psych/lib/psych/visitors/yaml_tree.rb	(revision 43863)
+++ ext/psych/lib/psych/visitors/yaml_tree.rb	(revision 43864)
@@ -209,7 +209,11 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L209
       end
 
       def visit_DateTime o
-        formatted = format_time o.to_time
+        formatted = if o.offset.zero?
+                      o.strftime("%Y-%m-%d %H:%M:%S.%9N Z".freeze)
+                    else
+                      o.strftime("%Y-%m-%d %H:%M:%S.%9N %:z".freeze)
+                    end
         tag = '!ruby/object:DateTime'
         register o, @emitter.scalar(formatted, nil, tag, false, false, Nodes::Scalar::ANY)
       end
Index: test/psych/test_date_time.rb
===================================================================
--- test/psych/test_date_time.rb	(revision 43863)
+++ test/psych/test_date_time.rb	(revision 43864)
@@ -3,6 +3,15 @@ require 'date' https://github.com/ruby/ruby/blob/trunk/test/psych/test_date_time.rb#L3
 
 module Psych
   class TestDateTime < TestCase
+    def test_negative_year
+      time = Time.utc -1, 12, 16
+      assert_cycle time
+    end
+
+    def test_new_datetime
+      assert_cycle DateTime.new
+    end
+
     def test_invalid_date
       assert_cycle "2013-10-31T10:40:07-000000000000033"
     end

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

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