diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/psych/scalar_scanner.rb | 7 | ||||
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 13 |
2 files changed, 19 insertions, 1 deletions
diff --git a/lib/psych/scalar_scanner.rb b/lib/psych/scalar_scanner.rb index 5a038a5..a15096f 100644 --- a/lib/psych/scalar_scanner.rb +++ b/lib/psych/scalar_scanner.rb @@ -4,6 +4,9 @@ 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)?)?/ + def initialize string @string = string end @@ -12,7 +15,9 @@ module Psych return [:NULL, nil] if @string.empty? case @string - when /^\d{4}-\d{2}-\d{2}$/ + when TIME + [:TIME, @string] + when /^\d{4}-\d{1,2}-\d{1,2}$/ [:DATE, @string] when /^\.inf$/i [:POSITIVE_INFINITY, 1 / 0.0] diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index abdfd4c..b4e33ed 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -35,6 +35,19 @@ module Psych when :DATE require 'date' Date.strptime token.last, '%Y-%m-%d' + when :TIME + lexeme = token.last + + 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 } + + time = Time.utc(yy, m, dd, hh, mm, ss) + + tz = (!md[3] || md[3] == 'Z') ? 0 : Integer(md[3].split(':').first) + Time.at((time - (tz * 3600)).to_i, md[2].sub(/^\./, '').to_i) else token.last end |