1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Extensions/Double+Format.swift

29 lines
895 B
Swift
Raw Normal View History

import Foundation
extension Double {
2022-08-27 06:13:47 +05:30
func formattedAsPlaybackTime(allowZero: Bool = false, forceHours: Bool = false) -> String? {
guard allowZero || !isZero, isFinite else {
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]
formatter.zeroFormattingBehavior = [.pad]
return formatter.string(from: self)
}
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
return formatter.localizedString(for: date, relativeTo: Date())
}
}