summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/psych/scalar_scanner.rb34
-rw-r--r--lib/psych/visitors/to_ruby.rb3
-rw-r--r--lib/psych/visitors/yaml_tree.rb25
3 files changed, 42 insertions, 20 deletions
diff --git a/lib/psych/scalar_scanner.rb b/lib/psych/scalar_scanner.rb
index bee88de..dc6f994 100644
--- a/lib/psych/scalar_scanner.rb
+++ b/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,25 @@ 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]
+ return Time.at(time.to_i, us) unless md[3]
+
+ tz = md[3].split(':').map { |digit| Integer(digit, 10) }
+ offset = tz.first * 3600 + ((tz[1] || 0) * 60)
+ Time.at((time - offset).to_i, us)
+ end
end
end
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index ffff636..745338a 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/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/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb
index 93ccc58..4df7b4d 100644
--- a/lib/psych/visitors/yaml_tree.rb
+++ b/lib/psych/visitors/yaml_tree.rb
@@ -135,14 +135,14 @@ module Psych
@emitter.scalar o.inspect, nil, '!ruby/regexp', false, false, Nodes::Scalar::ANY
end
- def visit_Time o
- formatted = o.strftime("%Y-%m-%d %H:%M:%S")
- if o.utc?
- formatted += ".%06dZ" % [o.usec]
- else
- formatted += ".%06d %+.2d:00" % [o.usec, o.gmt_offset / 3600]
- end
+ def visit_DateTime o
+ formatted = format_time o.to_time
+ tag = '!ruby/object:DateTime'
+ @emitter.scalar formatted, nil, tag, false, false, Nodes::Scalar::ANY
+ end
+ def visit_Time o
+ formatted = format_time o
@emitter.scalar formatted, nil, nil, true, false, Nodes::Scalar::ANY
end
@@ -268,6 +268,17 @@ module Psych
end
private
+ def format_time time
+ formatted = time.strftime("%Y-%m-%d %H:%M:%S")
+ if time.utc?
+ formatted += ".%09dZ" % [time.nsec]
+ else
+ formatted += ".%09d %+.2d:%.2d" % [time.nsec,
+ time.gmt_offset / 3600, time.gmt_offset % 3600 / 60]
+ end
+ formatted
+ end
+
# FIXME: remove this method once "to_yaml_properties" is removed
def find_ivars target
loc = target.method(:to_yaml_properties).source_location.first