diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/psych/scalar_scanner.rb | 14 | ||||
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 17 |
2 files changed, 14 insertions, 17 deletions
diff --git a/lib/psych/scalar_scanner.rb b/lib/psych/scalar_scanner.rb index 3afb7c6..f852418 100644 --- a/lib/psych/scalar_scanner.rb +++ b/lib/psych/scalar_scanner.rb @@ -30,7 +30,19 @@ module Psych [:SCALAR, @string] end when TIME - [: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, time] if 'Z' == md[3] + + tz = md[3] ? Integer(md[3].split(':').first) : 0 + [:TIME, Time.at((time - (tz * 3600)).to_i, us)] when /^\d{4}-\d{1,2}-\d{1,2}$/ require 'date' [:DATE, Date.strptime(@string, '%Y-%m-%d')] diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index fa13d2e..5c2d2ae 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -201,22 +201,7 @@ module Psych end def resolve_unknown o - type, lexeme = ScalarScanner.new(o.value).tokenize - return lexeme unless :TIME == type - - date, time = *(lexeme.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) : 0 - Time.at((time - (tz * 3600)).to_i, us) + ScalarScanner.new(o.value).tokenize.last end end end |