68 lines
1.9 KiB
Swift
68 lines
1.9 KiB
Swift
//
|
|
// Int+Extension.swift
|
|
// dinoGo
|
|
//
|
|
// Created by Lin on 2022/9/23.
|
|
// Copyright © 2022 dino. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
//import
|
|
|
|
public extension Int {
|
|
func timeStampToString(_ timeValue: (_ day: Int,
|
|
_ hour: Int,
|
|
_ minute: Int,
|
|
_ second: Int) -> String) -> String {
|
|
///天
|
|
let days = Int(self / (3600*24))
|
|
///时
|
|
let hours = Int((self - days*24*3600)/3600)
|
|
///分
|
|
let minute = Int((self - days*24*3600-hours*3600)/60)
|
|
///秒
|
|
let second = Int((self - days*24*3600-hours*3600) - 60*minute)
|
|
return timeValue(days, hours, minute, second)
|
|
}
|
|
|
|
/// 时分秒
|
|
func timeStampToHHSS() -> String {
|
|
///时
|
|
let hours = Int(self/3600)
|
|
///分
|
|
let minute = Int(self%3600/60)
|
|
///秒
|
|
let second = Int(self%60)
|
|
let timeString = String.init(format: "%02d:%02d:%02d", hours, minute, second)
|
|
return timeString
|
|
}
|
|
|
|
/// 分秒
|
|
func timeStampToMMSS() -> String {
|
|
///分
|
|
let minute = Int(self%3600/60)
|
|
///秒
|
|
let second = Int(self%60)
|
|
let timeString = String.init(format: "%02d:%02d", minute, second)
|
|
return timeString
|
|
}
|
|
}
|
|
|
|
// MARK: - File Size
|
|
public extension Int64 {
|
|
/// 文件大小size, 是以kb单位为最低数值
|
|
var sizeValue: String {
|
|
if self < 0 { return "0kb" }
|
|
switch self {
|
|
case 0..<1024:
|
|
return String(format: "%dkb", self)
|
|
case 1024..<(1024 << 10):
|
|
return String(format: "%.2fM", CGFloat(self) / CGFloat(1024))
|
|
case (1024 << 10)..<(1024 << 20):
|
|
return String(format: "%.2fG", CGFloat(self) / CGFloat(1024 << 10))
|
|
default:
|
|
return String(format: "%.2fT", CGFloat(self) / CGFloat(1024 << 20))
|
|
}
|
|
}
|
|
}
|