summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-01-15 19:12:30 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2010-01-15 19:12:30 -0800
commit506f5883dfb3cfa0f3e399ef496f491d8ff3ab7a (patch)
tree96e24739bb9e69655d151d79aa82017b98717e48 /lib
parent6ff0b18681d8cbd4c9acb41401f13ef3cd431a51 (diff)
downloadpsych-506f5883dfb3cfa0f3e399ef496f491d8ff3ab7a.zip
moving time logic to the scalar scanner
Diffstat (limited to 'lib')
-rw-r--r--lib/psych/scalar_scanner.rb14
-rw-r--r--lib/psych/visitors/to_ruby.rb17
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