添加时间间隔.例如小时,分钟和秒 [英] Adding Time Interval. e.g hours, minutes and seconds
问题描述
我从响应中获取时间,以小时,分钟和秒为单位,就像多个对象的"01:32:34"一样.我已将其保存在自定义日期对象中,其中我保存了日期值和字符串值,有大量记录,因此我将其保存在本地数据库中,并在检索到时以格式 1999-12-31 19:01:04 +0000
,而我也从响应中得到的字符串值是 19:01:04
.现在我想将所有这些值相加以返回一个字符串,例如 1:15:16
, 00:15:02
, 00:45:27
应该返回2小时15分45秒.我已经探索了 Calendar.Components.byAdding
,但是那里的方法只允许我添加一个组件,它不接收数组并返回 Date
.是否有任何快速的方法来实现这一目标,我想通过一种优雅而适当的方法来实现这一目标,我可以想到一些修复方法,但它们似乎不合适.
I am getting time from the response in hours, minutes and seconds like "01:32:34" of multiple objects. I have saved it in a custom date object, where i am saving the date value and the string value, there are a large number of records so i am saving it in my local db and upon retrieving i get the date value in the format 1999-12-31 19:01:04 +0000
whereas my string value which i am getting from response as well is 19:01:04
. now i would like to add all of these values to return a string e.g 1:15:16
, 00:15:02
, 00:45:27
should return 2 hours 15 minutes 45 seconds. I have explored Calendar.Components.byAdding
but the methods there only let me add one single component, it doesn't receive an array and return Date
. Is there any swifty way to achieve this, I want to achieve this via an elegant and proper method, i could think of a few fixes but they don't seem appropriate.
推荐答案
我将假设"01:32:34"表示经过时间为5,554秒,而不是上午1:32.因此,我将其转换为 TimeInterval
,而不是 Date
:
I’m going to assume that "01:32:34" represents an elapsed time of 5,554 seconds, not 1:32am. So, I’d convert it to a TimeInterval
, not a Date
:
func timeInterval(from string: String) -> TimeInterval? {
let components = string.components(separatedBy: ":").map { Double($0) }
guard
components.count == 3,
let hours = components[0],
let minutes = components[1],
let seconds = components[2]
else { return nil }
return ((hours * 60) + minutes) * 60 + seconds
}
您可以选择存储原始的"01:32:34"字符串或此值5,554.0.
You can chose to store either the original "01:32:34" string or this value, 5,554.0.
无论如何,然后将这些数字时间间隔相加是微不足道的.为了显示结果 TimeInterval
,您可以使用 DateComponentsFormatter
,例如
Anyway, then adding these numeric time intervals is trivial addition. And to display a resulting TimeInterval
, you’d use a DateComponentsFormatter
, e.g.
let timeIntervalFormatter: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
formatter.allowedUnits = [.hour, .minute, .second]
return formatter
}()
func totalElapsed(_ strings: [String]) -> String? {
let total = strings.reduce(TimeInterval.zero) { sum, string in
sum + (timeInterval(from: string) ?? 0)
}
return timeIntervalFormatter.string(from: total)
}
let strings = ["1:15:16", "00:15:02", "00:45:27"]
let result = totalElapsed(strings)
02:15:45
02:15:45
或者如果您想要更多(本地化)自然语言表示形式,请使用 .full
中的 unitsStyle
:
Or if you want more of a (localized) natural language representation, use unitsStyle
of .full
:
2小时15分钟45秒
2 hours, 15 minutes, 45 seconds
此方法(使用 TimeInterval
,而不是 Date
)的优点在于,它还可以表示超过24小时的间隔.
This approach (using TimeInterval
, not Date
) has the virtue that it also can represent intervals that exceed 24 hours.
这篇关于添加时间间隔.例如小时,分钟和秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!