Skip to content

Commit

Permalink
Fix error parsing duration for offline YouTube live streams ("P0D" is…
Browse files Browse the repository at this point in the history
… used for duration in this case).
  • Loading branch information
stefansundin committed Nov 25, 2022
1 parent 1d38960 commit eca3286
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion config/initializers/05-string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ def parse_duration
end

def parse_pt
if /^PT(?:(?<h>\d+)H)?(?:(?<m>\d+)M)?(?:(?<s>\d+)S)?$/ =~ self
# https://en.wikipedia.org/wiki/ISO_8601#Durations
# Converting year, month and day to seconds is not done properly here because it depends on the start time (see wikipedia), but it is ok because I've only see "P0D" being used by YouTube for offline live streams
# P[n]Y[n]M[n]DT[n]H[n]M[n]S
if /^P(?:(?<y>\d+)Y)?(?:(?<m>\d+)M)?(?:(?<d>\d+)D)?(?:T(?:(?<h>\d+)H)?(?:(?<m>\d+)M)?(?:(?<s>\d+)S)?)?$/ =~ self
result = 0
result += 31536000 * y.to_i if y # 365 days
result += 2592000 * m.to_i if m # 30 days
result += 86400 * d.to_i if d # 24 hours
result += 3600 * h.to_i if h
result += 60 * m.to_i if m
result += s.to_i if s
Expand Down

0 comments on commit eca3286

Please sign in to comment.