Skip to content

Instantly share code, notes, and snippets.

@alexrepty
Created October 26, 2018 14:53
Show Gist options
  • Save alexrepty/fcdc6ee5be87f5eae426047bb45b843a to your computer and use it in GitHub Desktop.
Save alexrepty/fcdc6ee5be87f5eae426047bb45b843a to your computer and use it in GitHub Desktop.
import Foundation
func extractTimeInterval(from string: String) -> TimeInterval? {
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm:ss,SSS"
guard let date = formatter.date(from: string) else { return nil }
guard let referenceDate = formatter.date(from: "00:00:00,000") else { return nil }
let interval = date.timeIntervalSince(referenceDate)
return interval
}
let timingStr = "00:00:53,266 --> 00:00:56,266"
let components = timingStr.components(separatedBy: " --> ")
let intervals = components.map({ return extractTimeInterval(from: $0) })
print("intervals: \(intervals)")
// prints: intervals: [Optional(53.26600003242493), Optional(56.26600003242493)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment