diff --git a/ext/psych/lib/psych/scalar_scanner.rb b/ext/psych/lib/psych/scalar_scanner.rb index e2aef65bda..e1e59d37e3 100644 --- a/ext/psych/lib/psych/scalar_scanner.rb +++ b/ext/psych/lib/psych/scalar_scanner.rb @@ -90,8 +90,15 @@ module Psych return time if 'Z' == md[3] return Time.at(time.to_i, us) unless md[3] - tz = md[3].split(':').map { |digit| Integer(digit.sub(/([-+])0/, '\1')) } - offset = tz.first * 3600 + ((tz[1] || 0) * 60) + tz = md[3].split(':').map { |digit| Integer(digit, 10) } + offset = tz.first * 3600 + + if offset < 0 + offset -= ((tz[1] || 0) * 60) + else + offset += ((tz[1] || 0) * 60) + end + Time.at((time - offset).to_i, us) end end diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb index 21a77e9982..3ecd9471e6 100644 --- a/ext/psych/lib/psych/visitors/yaml_tree.rb +++ b/ext/psych/lib/psych/visitors/yaml_tree.rb @@ -257,12 +257,12 @@ module Psych private def format_time time - formatted = time.strftime("%Y-%m-%d %H:%M:%S") + formatted = time.strftime("%Y-%m-%d %H:%M:%S.%9N") if time.utc? - formatted += ".%06dZ" % [time.nsec] + formatted += "Z" else - formatted += ".%06d %+.2d:%.2d" % [time.nsec, - time.gmt_offset / 3600, time.gmt_offset % 3600 / 60] + zone = time.strftime('%z') + formatted += " #{zone[0,3]}:#{zone[3,5]}" end formatted end diff --git a/test/psych/visitors/test_to_ruby.rb b/test/psych/visitors/test_to_ruby.rb index eb2841d9d0..b5b8e1443d 100644 --- a/test/psych/visitors/test_to_ruby.rb +++ b/test/psych/visitors/test_to_ruby.rb @@ -112,8 +112,10 @@ description: def test_time now = Time.now - formatted = now.strftime("%Y-%m-%d %H:%M:%S") + - ".%06d %+.2d:00" % [now.nsec, now.gmt_offset / 3600] + zone = now.strftime('%z') + zone = " #{zone[0,3]}:#{zone[3,5]}" + + formatted = now.strftime("%Y-%m-%d %H:%M:%S.%9N") + zone assert_equal now, Nodes::Scalar.new(formatted).to_ruby end @@ -121,7 +123,7 @@ description: def test_time_utc now = Time.now.utc formatted = now.strftime("%Y-%m-%d %H:%M:%S") + - ".%06dZ" % [now.nsec] + ".%09dZ" % [now.nsec] assert_equal now, Nodes::Scalar.new(formatted).to_ruby end @@ -129,7 +131,7 @@ description: def test_time_utc_no_z now = Time.now.utc formatted = now.strftime("%Y-%m-%d %H:%M:%S") + - ".%06d" % [now.nsec] + ".%09d" % [now.nsec] assert_equal now, Nodes::Scalar.new(formatted).to_ruby end