Last active
September 1, 2019 10:13
-
-
Save raunakp/dbb5d713834b7ac3b18e33e33cf1fa9f to your computer and use it in GitHub Desktop.
TimeInterval+playbackString.swift
This file contains hidden or 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
extension TimeInterval { | |
var playbackString: String { | |
if self.isNaN { | |
return "??" | |
} | |
var seconds = self | |
var mins = Int((seconds / 60).rounded(.down)) | |
seconds = seconds.truncatingRemainder(dividingBy: 60) | |
let hours = mins / 60 | |
mins = mins % 60 | |
var str = "" | |
if mins == 0 && hours == 0 { | |
str = String(format: "%.2fs", seconds) | |
} else { | |
str = String(format: "%02d:%02d", mins, Int(seconds)) | |
if hours > 0 { | |
let hoursPart = String(format: "%d:", hours) | |
str = hoursPart + str | |
} | |
} | |
return str | |
} | |
} |
print(TimeInterval(Double.nan).playbackString)
// ??
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
print(TimeInterval(2).playbackString)
// 2.00s
print(TimeInterval(62).playbackString)
// 01:02
print(TimeInterval(3762).playbackString)
// 1:02:42
print(TimeInterval(213762).playbackString)
// 1:02:42