Skip to content

Commit

Permalink
Simplify handling of negative durations
Browse files Browse the repository at this point in the history
We can condense our code a bit by handling the sign at the point where
we coerce the duration parts from strings to numbers.
  • Loading branch information
Simon Coffey committed Aug 8, 2021
1 parent 36f1f36 commit f65c207
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions lib/onelogin/ruby-saml/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,10 @@ def self.parse_duration(duration, timestamp=Time.now.utc)
raise Exception.new("Invalid ISO 8601 duration")
end

sign = matches[1] == '-' ? -1 : 1

durYears, durMonths, durDays, durHours, durMinutes, durSeconds, durWeeks =
matches[2..8].map { |match| match ? match.tr(',', '.').to_f : 0.0 }

if matches[1] == "-"
durYears = -durYears
durMonths = -durMonths
durDays = -durDays
durHours = -durHours
durMinutes = -durMinutes
durSeconds = -durSeconds
durWeeks = -durWeeks
end
matches[2..8].map { |match| match ? sign * match.tr(',', '.').to_f : 0.0 }

initial_datetime = Time.at(timestamp).utc.to_datetime
final_datetime = initial_datetime.next_year(durYears)
Expand Down

0 comments on commit f65c207

Please sign in to comment.