Я получаю данные о движении устройства из Apple Watch Series 4 вместе с временным интервалом, но когда я пытаюсь преобразовать их в dateTime, я не получаю правильное время, всегда есть разница в несколько минут.
![enter image description here](https://i.stack.imgur.com/ixfTh.jpg)
код для получения LoggingTime
func loggingTime() -> String{
let dateFormatter = ISO8601DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.formatOptions = .withInternetDateTime
var time = dateFormatter.string(from: Date())
let splitTime = time.split(separator: "+")
time = String(splitTime[0])
return time
}
код для преобразования метки времени в дату
func timespanToDate(timespan: Double) -> String{
let date = Date(timeIntervalSinceNow: timespan)
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
//dateFormatter.locale = NSLocale.current
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let strDate = dateFormatter.string(from: date)
return strDate
}
вот как я получаю движение устройства
func processMotionData(_ motionData: CMDeviceMotion) {
self.sessionId = self.loggingTime()
numActionsRecorded+=1
let sample = """
\(self.sessionId_fNAME.dropLast(3).replacingOccurrences(of: "T", with: "_").replacingOccurrences(of: ":", with: "").replacingOccurrences(of: "-", with: "")),\
\(self.sessionId.replacingOccurrences(of: "T", with: " ")),\
\(DefUid),\
\(numActionsRecorded),\
\(Int(self.defHz)),\
\(self.DefActivity),\
\(self.defMoveType),\
\(self.wrist),\
\(self.side),\
\(self.timespanToDate(timespan: motionData.timestamp)),\
\(motionData.attitude.roll),\
\(motionData.attitude.pitch),\
\(motionData.attitude.yaw),\
\(motionData.rotationRate.x),\
\(motionData.rotationRate.y),\
\(motionData.rotationRate.z),\
\(motionData.gravity.x),\
\(motionData.gravity.y),\
\(motionData.gravity.z),\
\(motionData.userAcceleration.x),\
\(motionData.userAcceleration.y),\
\(motionData.userAcceleration.z),\
}
я также пробовал время
timeIntervalSinceReferenceDate
, а также
timeIntervalSince1970
но оба приведенные выше дают мне совершенно неправильные дату и время