diff --git a/submodules/UrlHandling/Sources/UrlHandling.swift b/submodules/UrlHandling/Sources/UrlHandling.swift index a0b251be7a..6f294faddc 100644 --- a/submodules/UrlHandling/Sources/UrlHandling.swift +++ b/submodules/UrlHandling/Sources/UrlHandling.swift @@ -546,8 +546,9 @@ public func parseInternalUrl(sharedContext: SharedAccountContext, context: Accou threadId = intValue } } else if queryItem.name == "t" { - if let doubleValue = Double(value) { - timecode = doubleValue + let timestampValue = hTmeParseDuration(value) + if timestampValue != 0 { + timecode = Double(timestampValue) } } } @@ -570,8 +571,9 @@ public func parseInternalUrl(sharedContext: SharedAccountContext, context: Accou for queryItem in queryItems { if let value = queryItem.value { if queryItem.name == "t" { - if let doubleValue = Double(value) { - timecode = doubleValue + let timestampValue = hTmeParseDuration(value) + if timestampValue != 0 { + timecode = Double(timestampValue) } } } @@ -628,8 +630,9 @@ public func parseInternalUrl(sharedContext: SharedAccountContext, context: Accou commentId = intValue } } else if queryItem.name == "t" { - if let doubleValue = Double(value) { - timecode = doubleValue + let timestampValue = hTmeParseDuration(value) + if timestampValue != 0 { + timecode = Double(timestampValue) } } } @@ -1400,3 +1403,41 @@ public func cleanDomain(url: String) -> (domain: String, fullUrl: String) { return (url, url) } } + +private func hTmeParseDuration(_ durationStr: String) -> Int { + // Optional hours, optional minutes, optional seconds + let pattern = "^(?:(\\d+)h)?(?:(\\d+)m)?(?:(\\d+)s)?$" + + // Attempt to create the regex + guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { + // If regex creation fails, fallback to integer parsing + return Int(durationStr) ?? 0 + } + + // Search for a match + let range = NSRange(durationStr.startIndex..., in: durationStr) + if let match = regex.firstMatch(in: durationStr, options: [], range: range) { + // Extract capture groups for hours, minutes, and seconds + let hoursRange = match.range(at: 1) + let minutesRange = match.range(at: 2) + let secondsRange = match.range(at: 3) + + // Helper to safely extract integer from a matched range + func intValue(_ nsRange: NSRange) -> Int { + guard nsRange.location != NSNotFound, + let substringRange = Range(nsRange, in: durationStr) else { + return 0 + } + return Int(durationStr[substringRange]) ?? 0 + } + + let hours = intValue(hoursRange) + let minutes = intValue(minutesRange) + let seconds = intValue(secondsRange) + + return hours * 3600 + minutes * 60 + seconds + } + + // If the string didn't match the pattern, parse it as a positive integer + return Int(durationStr) ?? 0 +}