0
私は手順は私が各時間と、このカウント取得しようとしている:HealthKitが不正な時間を返すのはなぜですか?
func retrieveSteps(completion: @escaping (_ stepsCount: Double) -> Void) {
let stepsCount = HKQuantityType.quantityType(forIdentifier: .stepCount)
let date = Date()
let calendar = Calendar(identifier: .gregorian)
let newDate = calendar.startOfDay(for: date)
let predicate = HKQuery.predicateForSamples(withStart: newDate, end: date, options: [.strictStartDate])
var interval = DateComponents()
interval.hour = 1
let query = HKStatisticsCollectionQuery(quantityType: stepsCount!, quantitySamplePredicate: predicate, options: [.cumulativeSum], anchorDate: newDate, intervalComponents: interval)
query.initialResultsHandler = { query, result, error in
if let stats = result {
stats.enumerateStatistics(from: newDate, to: date) { statistics, _ in
if let quantity = statistics.sumQuantity() {
let steps = quantity.doubleValue(for: HKUnit.count())
print("Steps: \(steps) for: \(statistics.endDate)")
completion(steps)
}
}
}
}
HKHealthStore().execute(query)
}
そして、私はそれを実行したとき、私は間違った日付の値を取得します。例:
Steps: 28.3782023430627 for: 2017-10-22 10:00:00 +0000
ただし、健康アプリでは、時刻は11:58
です。なぜ私は10:00
を得ているのですか?そしてそれをどうすれば改善できますか?
時、分、秒をフォーマットして出力してください。 – Lumialxk
@ Lumialxkしかし、それは私を助ける方法? '10:00'と' 11:58'の間 - 2時間=/ –
あなたのタイムゾーンは10:00と思っています。 – Lumialxk