-
-
Save jasdev/f35d690b67eb122ff7066efe014c0ba8 to your computer and use it in GitHub Desktop.
subRipTimingLine
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
func subRipTimingLine(start: TimeInterval, duration: TimeInterval) -> String { | |
"\(timeIntervalToSubRipFormattedTimestamp(start)) --> \(timeIntervalToSubRipFormattedTimestamp(start + duration))" | |
} | |
private let subRipTimeIntervalFormatter: DateComponentsFormatter = { | |
let formatter = DateComponentsFormatter() | |
formatter.zeroFormattingBehavior = .pad | |
formatter.allowedUnits = [.hour, .minute, .second] // (1) | |
return formatter | |
}() | |
func timeIntervalToSubRipFormattedTimestamp(_ time: TimeInterval) -> String { | |
let millisecondsDigits = 3 | |
let milliseconds = "\(Int((time.truncatingRemainder(dividingBy: 1) * 1_000).rounded()))" // (2) | |
.prefix(millisecondsDigits) | |
let millisecondsZeroPadding = Array( | |
repeating: "0", | |
count: millisecondsDigits - milliseconds.count | |
) | |
.joined() | |
return subRipTimeIntervalFormatter.string(from: time)! + | |
",\(millisecondsZeroPadding + milliseconds)" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment