Skip to content

Commit 3ff0b69

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 ce12b30 commit 3ff0b69

1 file changed

Lines changed: 3 additions & 11 deletions

File tree

lib/onelogin/ruby-saml/utils.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,17 @@ 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 =
6365
matches[2..8].map do |match|
6466
if match.nil?
6567
0.0
6668
else
67-
match.tr(',', '.').to_f
69+
sign * match.tr(',', '.').to_f
6870
end
6971
end
7072

71-
if matches[1] == "-"
72-
durYears = -durYears
73-
durMonths = -durMonths
74-
durDays = -durDays
75-
durHours = -durHours
76-
durMinutes = -durMinutes
77-
durSeconds = -durSeconds
78-
durWeeks = -durWeeks
79-
end
80-
8173
initial_datetime = Time.at(timestamp).utc.to_datetime
8274
final_datetime = initial_datetime.next_year(durYears)
8375
final_datetime = final_datetime.next_month(durMonths)

0 commit comments

Comments
 (0)