Created
October 26, 2018 14:53
-
-
Save alexrepty/fcdc6ee5be87f5eae426047bb45b843a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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