Skip to content

Commit 69dffef

Browse files
committed
Fix exception message for Time.rfc3339 failures
The previous message used xmlschema, which was inaccurate for an rfc3339 parse. While we could have _xmlschema take an extra argument for the callee, it seems simpler to have _xmlschema return nil for an unmatched parse, and do the raising in xmlschema/rfc3339. With this approach, we can reduce the nesting by using an early return.
1 parent b126827 commit 69dffef

1 file changed

Lines changed: 28 additions & 32 deletions

File tree

‎lib/time.rb‎

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,8 @@ def xmlschema(time)
634634
(\.\d+)?
635635
(Z|[+-]\d\d(?::?\d\d)?)?
636636
\s*\z/ix
637-
_xmlschema(pattern, time)
637+
_xmlschema(pattern, time) or
638+
raise ArgumentError.new("invalid xmlschema format: #{time.inspect}")
638639
end
639640
alias iso8601 xmlschema
640641

@@ -662,47 +663,42 @@ def rfc3339(time)
662663
(\.\d+)?
663664
(Z|[+-]\d\d:\d\d)
664665
\s*\z/ix
665-
_xmlschema(pattern, time)
666+
_xmlschema(pattern, time) or
667+
raise ArgumentError.new("invalid rfc3339 format: #{time.inspect}")
666668
end
667669

668670
private
669671

670672
if RUBY_VERSION >= "3.2"
671673
def _xmlschema(pattern, time) # :nodoc:
672-
if pattern.match?(time)
673-
time = time.strip
674-
time.upcase!
675-
new(time)
676-
else
677-
raise ArgumentError.new("invalid xmlschema format: #{time.inspect}")
678-
end
674+
return unless pattern.match?(time)
675+
time = time.strip
676+
time.upcase!
677+
new(time)
679678
end
680679
else
681680
def _xmlschema(pattern, time) # :nodoc:
682-
if pattern =~ time
683-
year = $1.to_i
684-
mon = $2.to_i
685-
day = $3.to_i
686-
hour = $4.to_i
687-
min = $5.to_i
688-
sec = $6.to_i
689-
usec = 0
690-
if $7
691-
usec = Rational($7) * 1000000
692-
end
693-
if $8
694-
zone = $8
695-
off = zone_offset(zone)
696-
year, mon, day, hour, min, sec =
697-
apply_offset(year, mon, day, hour, min, sec, off)
698-
t = self.utc(year, mon, day, hour, min, sec, usec)
699-
force_zone!(t, zone, off)
700-
t
701-
else
702-
self.local(year, mon, day, hour, min, sec, usec)
703-
end
681+
return unless pattern =~ time
682+
year = $1.to_i
683+
mon = $2.to_i
684+
day = $3.to_i
685+
hour = $4.to_i
686+
min = $5.to_i
687+
sec = $6.to_i
688+
usec = 0
689+
if $7
690+
usec = Rational($7) * 1000000
691+
end
692+
if $8
693+
zone = $8
694+
off = zone_offset(zone)
695+
year, mon, day, hour, min, sec =
696+
apply_offset(year, mon, day, hour, min, sec, off)
697+
t = self.utc(year, mon, day, hour, min, sec, usec)
698+
force_zone!(t, zone, off)
699+
t
704700
else
705-
raise ArgumentError.new("invalid xmlschema format: #{time.inspect}")
701+
self.local(year, mon, day, hour, min, sec, usec)
706702
end
707703
end
708704
end

0 commit comments

Comments
 (0)