Skip to content

Instantly share code, notes, and snippets.

@jasdev
Created January 25, 2021 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdev/638aae403661a5fee8afa725f4770052 to your computer and use it in GitHub Desktop.
Save jasdev/638aae403661a5fee8afa725f4770052 to your computer and use it in GitHub Desktop.
Timecode line parsing with time interval outputs.
let timecodeParser = timecodeHours
.skip(StartsWith(":"))
.take(timecodeMinutes)
.skip(StartsWith(":"))
.take(timecodeSeconds)
.skip(StartsWith(","))
.take(timecodeMilliseconds)
.map { hours, minutes, seconds, milliseconds -> TimeInterval in
let hoursInSeconds = Double(hours) * 60 * 60
let minutesInSeconds = Double(minutes) * 60
let millisecondsInSeconds = Double(milliseconds) * 1 / 1_000
return hoursInSeconds + minutesInSeconds + Double(seconds) + millisecondsInSeconds
}
let timecodeLineParser = timecodeParser
.skip(StartsWith(" --> "))
.take(timecodeParser) // ⇒ Parser<Substring, (TimeInterval, TimeInterval)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment