gpt4 book ai didi

iOS DateComponentsFormatter - 获取使用的单位

转载 作者:行者123 更新时间:2023-11-28 21:01:48 24 4
gpt4 key购买 nike

我想将时间从秒转换为位置格式,例如 2:05 min1:23 h19 s。我在检索本地化时间缩写单位时遇到问题。这是我的代码。

let secs: Double = 3801
let stopWatchFormatter = DateComponentsFormatter()
stopWatchFormatter.unitsStyle = .positional
stopWatchFormatter.maximumUnitCount = 2
stopWatchFormatter.allowedUnits = [.hour, .minute, .second]
print(stopWatchFormatter.string(from: secs)) // 1:03
stopWatchFormatter.unitsStyle = .short
print(stopWatchFormatter.string(from: secs)) // 1 hr, 3 min

如您所见,3801 秒的格式设置为 1:03,这很好,但我不知道 DateComponentsFormatter 使用的是小时还是分钟等。我可能会使用简单的 MOD 逻辑来检查它,但接下来是本地化的困难部分。另请注意,如果我将 collapsesLargestUnit 设置为 false,MOD 解决方案将毫无值(value)。

最佳答案

DateComponentsFormatter 不直接支持您想要的格式,它本质上是位置格式,但短格式的第一个单元显示在末尾。

以下辅助函数将这两个单独的结果组合成您想要的结果。这应该适用于任何语言环境,但需要进行彻底的测试以确认这一点。

func formatStopWatchTime(seconds: Double) -> String {
let stopWatchFormatter = DateComponentsFormatter()
stopWatchFormatter.unitsStyle = .positional
stopWatchFormatter.maximumUnitCount = 2
stopWatchFormatter.allowedUnits = [.hour, .minute, .second]
var pos = stopWatchFormatter.string(from: seconds)!

// Work around a bug where some values return 3 units despite only requesting 2 units
let parts = pos.components(separatedBy: CharacterSet.decimalDigits.inverted)
if parts.count > 2 {
let seps = pos.components(separatedBy: .decimalDigits).filter { !$0.isEmpty }
pos = parts[0..<2].joined(separator: seps[0])
}

stopWatchFormatter.maximumUnitCount = 1
stopWatchFormatter.unitsStyle = .short
let unit = stopWatchFormatter.string(from: seconds)!

// Replace the digits in the unit result with the pos result
let res = unit.replacingOccurrences(of: "[\\d]+", with: pos, options: [.regularExpression])

return res
}

print(formatStopWatchTime(seconds: 3801))

输出:

1:03 hr

关于iOS DateComponentsFormatter - 获取使用的单位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48230176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com