我有一个(有点?)关于Swift中的时间转换的基本问题。
我有一个整数,我想转换成小时/分钟/秒。
示例:Int = 27005会给我:
7 Hours 30 Minutes 5 Seconds
我知道如何在PHP中做到这一点,但是,唉,swift不是PHP。
我有一个(有点?)关于Swift中的时间转换的基本问题。
我有一个整数,我想转换成小时/分钟/秒。
示例:Int = 27005会给我:
7 Hours 30 Minutes 5 Seconds
我知道如何在PHP中做到这一点,但是,唉,swift不是PHP。
当前回答
定义
func secondsToHoursMinutesSeconds(_ seconds: Int) -> (Int, Int, Int) {
return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}
Use
> secondsToHoursMinutesSeconds(27005)
(7,30,5)
or
let (h,m,s) = secondsToHoursMinutesSeconds(27005)
上面的函数使用Swift元组一次返回三个值。使用let (var,…)语法解构元组,如果需要,也可以访问单个元组成员。
如果你真的需要用Hours等文字打印出来,那么可以使用这样的东西:
func printSecondsToHoursMinutesSeconds(_ seconds: Int) {
let (h, m, s) = secondsToHoursMinutesSeconds(seconds)
print ("\(h) Hours, \(m) Minutes, \(s) Seconds")
}
注意,上面的secondstohoursminutessecseconds()实现适用于Int参数。如果你想要一个Double版本,你需要决定返回值是什么-可能是(Int, Int, Double)或可能是(Double, Double, Double)。你可以尝试这样做:
func secondsToHoursMinutesSeconds(seconds: Double) -> (Double, Double, Double) {
let (hr, minf) = modf(seconds / 3600)
let (min, secf) = modf(60 * minf)
return (hr, min, 60 * secf)
}
其他回答
基于Vadian的回答,我写了一个扩展,它接受Double(其中TimeInterval是类型别名),并输出一个格式化为时间的字符串。
extension Double {
func asString(style: DateComponentsFormatter.UnitsStyle) -> String {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute, .second, .nanosecond]
formatter.unitsStyle = style
return formatter.string(from: self) ?? ""
}
}
这里是各种DateComponentsFormatter。UnitsStyle选项看起来像这样:
10000.asString(style: .positional) // 2:46:40
10000.asString(style: .abbreviated) // 2h 46m 40s
10000.asString(style: .short) // 2 hr, 46 min, 40 sec
10000.asString(style: .full) // 2 hours, 46 minutes, 40 seconds
10000.asString(style: .spellOut) // two hours, forty-six minutes, forty seconds
10000.asString(style: .brief) // 2hr 46min 40sec
以下是我在Swift 4+中使用的音乐播放器。我将秒Int转换为可读的字符串格式
extension Int {
var toAudioString: String {
let h = self / 3600
let m = (self % 3600) / 60
let s = (self % 3600) % 60
return h > 0 ? String(format: "%1d:%02d:%02d", h, m, s) : String(format: "%1d:%02d", m, s)
}
}
像这样使用:
print(7903.toAudioString)
输出:2:11:43
以下是一个更结构化/灵活的方法:(Swift 3)
struct StopWatch {
var totalSeconds: Int
var years: Int {
return totalSeconds / 31536000
}
var days: Int {
return (totalSeconds % 31536000) / 86400
}
var hours: Int {
return (totalSeconds % 86400) / 3600
}
var minutes: Int {
return (totalSeconds % 3600) / 60
}
var seconds: Int {
return totalSeconds % 60
}
//simplified to what OP wanted
var hoursMinutesAndSeconds: (hours: Int, minutes: Int, seconds: Int) {
return (hours, minutes, seconds)
}
}
let watch = StopWatch(totalSeconds: 27005 + 31536000 + 86400)
print(watch.years) // Prints 1
print(watch.days) // Prints 1
print(watch.hours) // Prints 7
print(watch.minutes) // Prints 30
print(watch.seconds) // Prints 5
print(watch.hoursMinutesAndSeconds) // Prints (7, 30, 5)
使用这样的方法可以添加方便的解析,如下所示:
extension StopWatch {
var simpleTimeString: String {
let hoursText = timeText(from: hours)
let minutesText = timeText(from: minutes)
let secondsText = timeText(from: seconds)
return "\(hoursText):\(minutesText):\(secondsText)"
}
private func timeText(from number: Int) -> String {
return number < 10 ? "0\(number)" : "\(number)"
}
}
print(watch.simpleTimeString) // Prints 07:30:05
值得注意的是,纯基于整数的方法不考虑闰日/秒。如果用例处理的是真实的日期/时间,则应该使用Date和Calendar。
我已经构建了一个现有答案的mashup,以简化一切并减少Swift 3所需的代码量。
func hmsFrom(seconds: Int, completion: @escaping (_ hours: Int, _ minutes: Int, _ seconds: Int)->()) {
completion(seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}
func getStringFrom(seconds: Int) -> String {
return seconds < 10 ? "0\(seconds)" : "\(seconds)"
}
用法:
var seconds: Int = 100
hmsFrom(seconds: seconds) { hours, minutes, seconds in
let hours = getStringFrom(seconds: hours)
let minutes = getStringFrom(seconds: minutes)
let seconds = getStringFrom(seconds: seconds)
print("\(hours):\(minutes):\(seconds)")
}
打印:
00:01:40
我正在使用这个扩展
extension Double {
func stringFromInterval() -> String {
let timeInterval = Int(self)
let millisecondsInt = Int((self.truncatingRemainder(dividingBy: 1)) * 1000)
let secondsInt = timeInterval % 60
let minutesInt = (timeInterval / 60) % 60
let hoursInt = (timeInterval / 3600) % 24
let daysInt = timeInterval / 86400
let milliseconds = "\(millisecondsInt)ms"
let seconds = "\(secondsInt)s" + " " + milliseconds
let minutes = "\(minutesInt)m" + " " + seconds
let hours = "\(hoursInt)h" + " " + minutes
let days = "\(daysInt)d" + " " + hours
if daysInt > 0 { return days }
if hoursInt > 0 { return hours }
if minutesInt > 0 { return minutes }
if secondsInt > 0 { return seconds }
if millisecondsInt > 0 { return milliseconds }
return ""
}
}
用途不同
// assume myTimeInterval = 96460.397
myTimeInteval.stringFromInterval() // 1d 2h 47m 40s 397ms