@@ -15,7 +15,21 @@ class Utils
1515
1616 DSIG = "http://www.w3.org/2000/09/xmldsig#"
1717 XENC = "http://www.w3.org/2001/04/xmlenc#"
18- DURATION_FORMAT = %r(^(-?)P(?:(?:(?:(\d +)Y)?(?:(\d +)M)?(?:(\d +)D)?(?:T(?:(\d +)H)?(?:(\d +)M)?(?:(\d +)S)?)?)|(?:(\d +)W))$)
18+ DURATION_FORMAT = %r(^
19+ (-?)P # 1: Duration sign
20+ (?:
21+ (?:(\d +)Y)? # 2: Years
22+ (?:(\d +)M)? # 3: Months
23+ (?:(\d +)D)? # 4: Days
24+ (?:T
25+ (?:(\d +)H)? # 5: Hours
26+ (?:(\d +)M)? # 6: Minutes
27+ (?:(\d +(?:[.,]\d +)?)S)? # 7: Seconds
28+ )?
29+ |
30+ (\d +)W # 8: Weeks
31+ )
32+ $)x
1933
2034 # Checks if the x509 cert provided is expired
2135 #
@@ -37,31 +51,18 @@ def self.is_cert_expired(cert)
3751 # current time.
3852 #
3953 # @return [Integer] The new timestamp, after the duration is applied.
40- #
54+ #
4155 def self . parse_duration ( duration , timestamp = Time . now . utc )
4256 matches = duration . match ( DURATION_FORMAT )
43-
57+
4458 if matches . nil?
4559 raise Exception . new ( "Invalid ISO 8601 duration" )
4660 end
4761
48- durYears = matches [ 2 ] . to_i
49- durMonths = matches [ 3 ] . to_i
50- durDays = matches [ 4 ] . to_i
51- durHours = matches [ 5 ] . to_i
52- durMinutes = matches [ 6 ] . to_i
53- durSeconds = matches [ 7 ] . to_f
54- durWeeks = matches [ 8 ] . to_i
55-
56- if matches [ 1 ] == "-"
57- durYears = -durYears
58- durMonths = -durMonths
59- durDays = -durDays
60- durHours = -durHours
61- durMinutes = -durMinutes
62- durSeconds = -durSeconds
63- durWeeks = -durWeeks
64- end
62+ sign = matches [ 1 ] == '-' ? -1 : 1
63+
64+ durYears , durMonths , durDays , durHours , durMinutes , durSeconds , durWeeks =
65+ matches [ 2 ..8 ] . map { |match | match ? sign * match . tr ( ',' , '.' ) . to_f : 0.0 }
6566
6667 initial_datetime = Time . at ( timestamp ) . utc . to_datetime
6768 final_datetime = initial_datetime . next_year ( durYears )
0 commit comments