5
# Scan scalars for built in types
7
# Taken from http://yaml.org/type/timestamp.html
8
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)?)?/
10
# Create a new scanner
15
# Tokenize +string+ returning the ruby object
17
return nil if string.empty?
18
return string if @string_cache.key?(string)
23
@string_cache[string] = true
29
@string_cache[string] = true
33
when /^(yes|true|on)$/i
35
when /^(no|false|off)$/i
38
@string_cache[string] = true
43
when /^\d{4}-\d{1,2}-\d{1,2}$/
45
Date.strptime(string, '%Y-%m-%d')
53
if string =~ /^:(["'])(.*)\1/
54
$2.sub(/^:/, '').to_sym
56
string.sub(/^:/, '').to_sym
58
when /^[-+]?[1-9][0-9_]*(:[0-5]?[0-9])+$/
60
string.split(':').each_with_index do |n,e|
61
i += (n.to_i * 60 ** (e - 2).abs)
64
when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+\.[0-9_]*$/
66
string.split(':').each_with_index do |n,e|
67
i += (n.to_f * 60 ** (e - 2).abs)
71
return Integer(string.gsub(/[,_]/, '')) rescue ArgumentError
72
return Float(string.gsub(/[,_]/, '')) rescue ArgumentError
73
@string_cache[string] = true
79
# Parse and return a Time from +string+
81
date, time = *(string.split(/[ tT]/, 2))
82
(yy, m, dd) = date.split('-').map { |x| x.to_i }
83
md = time.match(/(\d+:\d+:\d+)(\.\d*)?\s*(Z|[-+]\d+(:\d\d)?)?/)
85
(hh, mm, ss) = md[1].split(':').map { |x| x.to_i }
86
us = (md[2] ? Rational(md[2].sub(/^\./, '0.')) : 0) * 1000000
88
time = Time.utc(yy, m, dd, hh, mm, ss, us)
90
return time if 'Z' == md[3]
91
return Time.at(time.to_i, us) unless md[3]
93
tz = md[3].split(':').map { |digit| Integer(digit, 10) }
94
offset = tz.first * 3600
97
offset -= ((tz[1] || 0) * 60)
99
offset += ((tz[1] || 0) * 60)
102
Time.at((time - offset).to_i, us)