summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authortenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-11-26 21:41:48 +0000
committertenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-11-26 21:41:48 +0000
commit079ff69ca0bb5fc71e2de009829ad7410f374c26 (patch)
tree8015e361386fb00d2257e325b8d0745c9c663b1f /ext
parent725d6f4970423c6a28d1b0e1864c3651528d5670 (diff)
* 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 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43864 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/psych/lib/psych/scalar_scanner.rb4
-rw-r--r--ext/psych/lib/psych/visitors/yaml_tree.rb6
2 files changed, 7 insertions, 3 deletions
diff --git a/ext/psych/lib/psych/scalar_scanner.rb b/ext/psych/lib/psych/scalar_scanner.rb
index 5a805981b1..3fc9edd25d 100644
--- a/ext/psych/lib/psych/scalar_scanner.rb
+++ b/ext/psych/lib/psych/scalar_scanner.rb
@@ -5,7 +5,7 @@ module Psych
# 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
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 }
diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb
index b469e2ddc3..1cb2137693 100644
--- a/ext/psych/lib/psych/visitors/yaml_tree.rb
+++ b/ext/psych/lib/psych/visitors/yaml_tree.rb
@@ -209,7 +209,11 @@ module Psych
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