Skip to content

Commit ebd700d

Browse files
committed
Parse ISO8601 datetimes without Time.parse
1 parent 4da2b2d commit ebd700d

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

lib/rubygems/yaml_serializer.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,19 @@ def coerce(val)
271271
return Sequence.new if inner.empty?
272272
items = inner.split(/\s*,\s*/).reject(&:empty?).map {|e| Scalar.new(value: coerce(e)) }
273273
Sequence.new(items: items)
274-
elsif /^\d{4}-\d{2}-\d{2}/.match?(val)
275-
require "time"
274+
elsif /\A(\d{4})-(\d{2})-(\d{2})(?: (\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?\s*(Z|[+-]\d{2}:\d{2}))?\z/.match(val)
276275
begin
277-
Time.parse(val)
276+
year, mon, mday = $1.to_i, $2.to_i, $3.to_i
277+
hour, min, sec = ($4 || 0).to_i, ($5 || 0).to_i, ($6 || 0).to_i
278+
nsec = $7 ? $7.ljust(9, "0")[0, 9].to_i : 0
279+
tz = $8
280+
t = Time.utc(year, mon, mday, hour, min, sec, Rational(nsec, 1000))
281+
if tz && tz != "Z"
282+
sign = tz[0] == "-" ? 1 : -1
283+
tz_hour, tz_min = tz[1..].split(":").map(&:to_i)
284+
t += sign * (tz_hour * 3600 + tz_min * 60)
285+
end
286+
t
278287
rescue ArgumentError
279288
val
280289
end

0 commit comments

Comments
 (0)