aboutsummaryrefslogtreecommitdiffstats
path: root/ext
diff options
context:
space:
mode:
authortenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-07-03 00:52:43 +0000
committertenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-07-03 00:52:43 +0000
commit59cad45f9963b359b16c1b9a9324b78e5e0873d5 (patch)
treeb4647cc9b049e7ddcc4df946db04c4ed48ec23d1 /ext
parent6fe92a3cd6b4116580ff7663180407bc0c38030f (diff)
downloadruby-59cad45f9963b359b16c1b9a9324b78e5e0873d5.tar.gz
* ext/psych/lib/psych/visitors/to_ruby.rb(visit_Psych_Nodes_Scalar):
teaching Psych to deserialize DateTime objects. [Bug #1390] * ext/psych/lib/psych/visitors/yaml_tree.rb(visit_DateTime): added a method for serializing DateTime objects. * ext/psych/lib/psych/scalar_scanner.rb(parse_time): add method for parsing times objects from a string. * test/psych/test_date_time.rb: tests for dumping DateTime objects. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28532 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/psych/lib/psych/scalar_scanner.rb32
-rw-r--r--ext/psych/lib/psych/visitors/to_ruby.rb3
-rw-r--r--ext/psych/lib/psych/visitors/yaml_tree.rb13
3 files changed, 35 insertions, 13 deletions
diff --git a/ext/psych/lib/psych/scalar_scanner.rb b/ext/psych/lib/psych/scalar_scanner.rb
index bee88de419..e65651a740 100644
--- a/ext/psych/lib/psych/scalar_scanner.rb
+++ b/ext/psych/lib/psych/scalar_scanner.rb
@@ -39,19 +39,7 @@ module Psych
string
end
when TIME
- date, time = *(string.split(/[ tT]/, 2))
- (yy, m, dd) = date.split('-').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 }
- us = (md[2] ? Rational(md[2].sub(/^\./, '0.')) : 0) * 1000000
-
- time = Time.utc(yy, m, dd, hh, mm, ss, us)
-
- return time if 'Z' == md[3]
-
- tz = md[3] ? Integer(md[3].split(':').first.sub(/([-+])0/, '\1')) : 0
- Time.at((time - (tz * 3600)).to_i, us)
+ parse_time string
when /^\d{4}-\d{1,2}-\d{1,2}$/
require 'date'
Date.strptime(string, '%Y-%m-%d')
@@ -86,5 +74,23 @@ module Psych
string
end
end
+
+ ###
+ # Parse and return a Time from +string+
+ def parse_time string
+ date, time = *(string.split(/[ tT]/, 2))
+ (yy, m, dd) = date.split('-').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 }
+ us = (md[2] ? Rational(md[2].sub(/^\./, '0.')) : 0) * 1000000
+
+ time = Time.utc(yy, m, dd, hh, mm, ss, us)
+
+ return time if 'Z' == md[3]
+
+ tz = md[3] ? Integer(md[3].split(':').first.sub(/([-+])0/, '\1')) : 0
+ Time.at((time - (tz * 3600)).to_i, us)
+ end
end
end
diff --git a/ext/psych/lib/psych/visitors/to_ruby.rb b/ext/psych/lib/psych/visitors/to_ruby.rb
index ffff636d8e..745338ad99 100644
--- a/ext/psych/lib/psych/visitors/to_ruby.rb
+++ b/ext/psych/lib/psych/visitors/to_ruby.rb
@@ -50,6 +50,9 @@ module Psych
o.value.unpack('m').first
when '!str', 'tag:yaml.org,2002:str'
o.value
+ when "!ruby/object:DateTime"
+ require 'date'
+ @ss.parse_time(o.value).to_datetime
when "!ruby/object:Complex"
Complex(o.value)
when "!ruby/object:Rational"
diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb
index 4282e0bb0a..412acdb750 100644
--- a/ext/psych/lib/psych/visitors/yaml_tree.rb
+++ b/ext/psych/lib/psych/visitors/yaml_tree.rb
@@ -135,6 +135,19 @@ module Psych
@emitter.scalar o.inspect, nil, '!ruby/regexp', false, false, Nodes::Scalar::ANY
end
+ def visit_DateTime o
+ o = o.to_time
+ formatted = o.strftime("%Y-%m-%d %H:%M:%S")
+ if o.utc?
+ formatted += ".%06dZ" % [o.nsec]
+ else
+ formatted += ".%06d %+.2d:00" % [o.nsec, o.gmt_offset / 3600]
+ end
+
+ tag = '!ruby/object:DateTime'
+ @emitter.scalar formatted, nil, tag, false, false, Nodes::Scalar::ANY
+ end
+
def visit_Time o
formatted = o.strftime("%Y-%m-%d %H:%M:%S")
if o.utc?