mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
Update timestamp parsing
This commit is contained in:
parent
46225ca99b
commit
a3562eb133
@ -546,8 +546,9 @@ public func parseInternalUrl(sharedContext: SharedAccountContext, context: Accou
|
|||||||
threadId = intValue
|
threadId = intValue
|
||||||
}
|
}
|
||||||
} else if queryItem.name == "t" {
|
} else if queryItem.name == "t" {
|
||||||
if let doubleValue = Double(value) {
|
let timestampValue = hTmeParseDuration(value)
|
||||||
timecode = doubleValue
|
if timestampValue != 0 {
|
||||||
|
timecode = Double(timestampValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -570,8 +571,9 @@ public func parseInternalUrl(sharedContext: SharedAccountContext, context: Accou
|
|||||||
for queryItem in queryItems {
|
for queryItem in queryItems {
|
||||||
if let value = queryItem.value {
|
if let value = queryItem.value {
|
||||||
if queryItem.name == "t" {
|
if queryItem.name == "t" {
|
||||||
if let doubleValue = Double(value) {
|
let timestampValue = hTmeParseDuration(value)
|
||||||
timecode = doubleValue
|
if timestampValue != 0 {
|
||||||
|
timecode = Double(timestampValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -628,8 +630,9 @@ public func parseInternalUrl(sharedContext: SharedAccountContext, context: Accou
|
|||||||
commentId = intValue
|
commentId = intValue
|
||||||
}
|
}
|
||||||
} else if queryItem.name == "t" {
|
} else if queryItem.name == "t" {
|
||||||
if let doubleValue = Double(value) {
|
let timestampValue = hTmeParseDuration(value)
|
||||||
timecode = doubleValue
|
if timestampValue != 0 {
|
||||||
|
timecode = Double(timestampValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1400,3 +1403,41 @@ public func cleanDomain(url: String) -> (domain: String, fullUrl: String) {
|
|||||||
return (url, url)
|
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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user