简体   繁体   English

从 BLE 设备获取 UTC 时间并转换其显示的错误时间

[英]Getting UTC time from BLE device and while converting its showing wrong time

I am using a Bluetooth device data capture project, in that, I have noticed that I am receiving data from Bluetooth devices in the below format我正在使用蓝牙设备数据捕获项目,因为我注意到我正在以以下格式从蓝牙设备接收数据

[11, 6, 0, 231, 7, 1, 26, 11, 46, 45, 79, 1, 133, 176, 248, 0, 0] [11, 6, 0, 231, 7, 1, 26, 11, 46, 45, 79, 1, 133, 176, 248, 0, 0]

This have more reference to the field details 更多地参考了字段详细信息

Field1: Flags (8bits)
Field2: Sequence number (16bits)
Field3: Date Time (54 bits)
Field4: Time offset (16 bits)
Field5: Units kg/L: (16 bits SFLOAT)
Field6: Units mol/L: (16 bits SFLOAT): NOT PRESENT
Field7: Type: (4bits): 
Field8: Location: (4bit)
Field9: Status: (16bit)

By using the below code I am creating the UTC time通过使用下面的代码,我正在创建 UTC 时间

        // First construct the UTC date from the  base time
        let calendar = Calendar.current
        var components = DateComponents()
        
        components.day = newMeasurement[6]
        components.month = newMeasurement[5]
        components.year = ((newMeasurement[4] << 8) | newMeasurement[3])
        
        components.hour = newMeasurement[7]
        components.minute = newMeasurement[8]
        components.second = newMeasurement[9]
        components.timeZone = TimeZone(identifier: "UTC")
        
        let UTCRecord = calendar.date(from: components)

UTC time value looks like 2023-01-26 11:46:45 +0000 and the time offset value in seconds: 20100 and when I convert that into string format I will get: 2023-01-26 17:16:45 UTC 时间值看起来像 2023-01-26 11:46:45 +0000 和以秒为单位的时间偏移值:20100 当我将其转换为字符串格式时,我将得到:2023-01-26 17:16:45

Here time shows 17:16:45, but the actual time on the device is 17:21:45此处时间显示为 17:16:45,但设备上的实际时间为 17:21:45

This is how i convert UTCRecord in date format to string format这就是我如何将日期格式的 UTCRecord 转换为字符串格式

    let dateString = DateFormatter.localizedString(
            from: UTCRecord,
            dateStyle: .long,
          timeStyle: .medium)`

I really don't have any idea about how this time got wrong.我真的不知道这次是怎么弄错的。

The calculations are correct.计算是正确的。 You just need to make use of offset value.您只需要使用偏移值。 Offset is telling us the timeZone of current user.偏移量告诉我们当前用户的时区。 Here is the code sample:这是代码示例:

func convertDate() {
    let gmtTimeString = "2023-01-24 13:40:00" // date string you get from device

    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    formatter.timeZone = TimeZone(identifier: "UTC")
    guard let date = formatter.date(from: gmtTimeString) else {
        print("can't convert time string")
        return
    }
    let userTimeZone = NSTimeZone.init(forSecondsFromGMT: 20100) //20100 is offset
    formatter.timeZone = userTimeZone as TimeZone            
    let localTimeString = formatter.string(from: date)
    print("date Now:  \(localTimeString)")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM