2021-10-23 02:19:31 +05:30
|
|
|
import Foundation
|
|
|
|
|
|
|
|
extension Double {
|
2022-08-27 06:13:47 +05:30
|
|
|
func formattedAsPlaybackTime(allowZero: Bool = false, forceHours: Bool = false) -> String? {
|
2022-06-18 18:09:49 +05:30
|
|
|
guard allowZero || !isZero, isFinite else {
|
2021-10-23 02:19:31 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let formatter = DateComponentsFormatter()
|
|
|
|
|
|
|
|
formatter.unitsStyle = .positional
|
2022-08-27 06:13:47 +05:30
|
|
|
formatter.allowedUnits = self >= (60 * 60) || forceHours ? [.hour, .minute, .second] : [.minute, .second]
|
2021-10-23 02:19:31 +05:30
|
|
|
formatter.zeroFormattingBehavior = [.pad]
|
|
|
|
|
|
|
|
return formatter.string(from: self)
|
|
|
|
}
|
2021-11-15 04:36:01 +05:30
|
|
|
|
|
|
|
func formattedAsRelativeTime() -> String? {
|
|
|
|
let date = Date(timeIntervalSince1970: self)
|
|
|
|
|
|
|
|
let formatter = RelativeDateTimeFormatter()
|
2022-12-14 02:25:03 +05:30
|
|
|
formatter.dateTimeStyle = .named
|
|
|
|
formatter.unitsStyle = .short
|
|
|
|
formatter.formattingContext = .standalone
|
2021-11-15 04:36:01 +05:30
|
|
|
|
|
|
|
return formatter.localizedString(for: date, relativeTo: Date())
|
|
|
|
}
|
2021-10-23 02:19:31 +05:30
|
|
|
}
|