Khu*_*fie 3 datetime swift healthkit
我正在尝试根据 HealthKit 数据计算睡眠持续时间。对于睡眠,HealthKit 返回来自不同来源的所有样本,即使样本重叠。(与步骤之类的东西不同,您可以使用 HKStatisticsQuery 并为您删除重复数据)。
每个样本都包含开始日期和结束日期。我已将这些开始/结束日期放入 [DateInterval] 数组中,其结果如下(时间以秒为单位)。
start 2020-08-26 02:55:00 +0000 end 2020-08-26 03:00:00 +0000 time 300.0
start 2020-08-26 03:40:00 +0000 end 2020-08-26 05:00:00 +0000 time 4800.0
start 2020-08-26 05:15:00 +0000 end 2020-08-26 07:15:00 +0000 time 7200.0
start 2020-08-26 07:25:00 +0000 end 2020-08-26 08:00:00 +0000 time 2100.0
start 2020-08-26 08:10:00 +0000 end 2020-08-26 08:50:00 +0000 time 2400.0
start 2020-08-26 03:05:45 +0000 end 2020-08-26 11:51:47 +0000 time 31562.0
start 2020-08-26 11:51:50 +0000 end 2020-08-26 11:51:53 +0000 time 3.0
start 2020-08-26 12:10:39 +0000 end 2020-08-26 12:10:40 +0000 time 1.0
Run Code Online (Sandbox Code Playgroud)
我需要一种不会重复睡眠时间的算法或某种逻辑。在此示例中,将所有时间间隔的持续时间加在一起将返回 13 小时 26 分钟。对数据进行重复数据删除后,总计应该需要 8 小时 51 分钟(根据 Health 应用程序的显示)。
还没有正确测试过,但你明白了。对它们进行排序并跳过重叠的
func calculateSpentTime(for intervals: [DateInterval]) -> TimeInterval {
guard intervals.count > 1 else {
return intervals.first?.duration ?? 0
}
let sorted = intervals.sorted { $0.start < $1.start }
var total: TimeInterval = 0
var start = sorted[0].start
var end = sorted[0].end
for i in 1..<sorted.count {
if sorted[i].start > end {
total += end.timeIntervalSince(start)
start = sorted[i].start
end = sorted[i].end
} else if sorted[i].end > end {
end = sorted[i].end
}
}
total += end.timeIntervalSince(start)
return total
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
517 次 |
| 最近记录: |