Skip to content

Commit f6366d4

Browse files
author
Simon Coffey
committed
Simplify handling of negative durations
We can condense our code a bit by handling the sign at the point where we coerce the duration parts from strings to numbers.
1 parent ab0b88e commit f6366d4

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

lib/onelogin/ruby-saml/utils.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,10 @@ def self.parse_duration(duration, timestamp=Time.now.utc)
5959
raise Exception.new("Invalid ISO 8601 duration")
6060
end
6161

62+
sign = matches[1] == '-' ? -1 : 1
63+
6264
durYears, durMonths, durDays, durHours, durMinutes, durSeconds, durWeeks =
63-
matches[2..8].map { |match| match ? match.tr(',', '.').to_f : 0.0 }
64-
65-
if matches[1] == "-"
66-
durYears = -durYears
67-
durMonths = -durMonths
68-
durDays = -durDays
69-
durHours = -durHours
70-
durMinutes = -durMinutes
71-
durSeconds = -durSeconds
72-
durWeeks = -durWeeks
73-
end
65+
matches[2..8].map { |match| match ? sign * match.tr(',', '.').to_f : 0.0 }
7466

7567
initial_datetime = Time.at(timestamp).utc.to_datetime
7668
final_datetime = initial_datetime.next_year(durYears)

0 commit comments

Comments
 (0)