简体   繁体   English

如何将unix时间戳转换为NSData对象?

[英]How to convert unix timestamp to NSData object?

I'm using Core Bluetooth to write to a peripheral. 我正在使用Core Bluetooth写入外围设备。 I would like to send the current unix timestamp to the sensor, and I have attempted to do that like so: 我想将当前的unix时间戳发送给传感器,并且我尝试这样做:

// Write timestamp to paired peripheral
NSDate*           measureTime = [NSDate date];
NSDateFormatter*  usDateFormatter = [NSDateFormatter new];
NSLocale*         enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

[usDateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.000'Z'"];
[usDateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[usDateFormatter setLocale:enUSPOSIXLocale];  // Should force 24hr time regardless of settings value

NSString *dateString = [usDateFormatter stringFromDate:measureTime];
NSDate* startTime = [usDateFormatter dateFromString:dateString];

uint32_t timestamp = [startTime timeIntervalSince1970];
NSData *timestampData = [NSData dataWithBytes:&timestamp length:sizeof(timestamp)]; // <- Troublemaker
[pairedPeripheral writeValue:timestampData forCharacteristic:currentCharacteristic type:CBCharacteristicWriteWithResponse];

Here's the issue: 这是问题所在:

My 32 bit timestamp returns the correct value, however when I convert it into NSData, the peripheral reads it as a 24 hour clock value like this: "16:42:96" 我的32位时间戳返回正确的值,但是当我将其转换为NSData时,外设将其读取为24小时制的时钟值,如下所示:“ 16:42:96”

Where am I making the error? 我在哪里出错?

EDIT 编辑

I have modified the code to get rid of NSDateFormatter, as someone has mentioned that it is unnecessary. 我修改了代码以摆脱NSDateFormatter,因为有人提到这是不必要的。 I still seem to be getting the same result: 我似乎仍然得到相同的结果:

// Write timestamp to paired peripheral
NSDate*           measureTime = [NSDate date];
uint64_t timestamp = [measureTime timeIntervalSince1970];
NSData *timestampData = [NSData dataWithBytes:&timestamp length:sizeof(timestamp)]; // <- Troublemaker
[pairedPeripheral writeValue:timestampData forCharacteristic:currentCharacteristic type:CBCharacteristicWriteWithResponse]; 

You are confused. 你很困惑。 What you're sending to the peripheral is an integer number of seconds since 1970. That's a reasonable way to send a Unix timestamp, but it is not a time in 24 hour format, it is an integer. 您要发送到外围设备的时间是从1970年以来的整数秒。这是发送Unix时间戳的合理方式,但是它不是24小时格式的时间,而是整数。

You will need to change your code to use a uint64_t or uint32_t since Unix timestamps are much larger numbers than will fit in a 32 bit integer. 您将需要更改代码以使用uint64_t或uint32_t,因为Unix时间戳的数字远大于32位整数所能容纳的数字。 (I suggest using uint64_t.) (我建议使用uint64_t。)

(See @DonMag's comment for sample timestamp values, like 1491580283) (请参阅@DonMag的注释以获取示例时间戳值,例如1491580283)

How you display that time once it is received by the peripheral is a separate question, and the question that you should really be asking. 外设收到时间后如何显示时间是一个单独的问题,也是您真正应该问的问题。

Note that you may run into problems sending an int as binary data if the peripheral has different "endian-ness" than your iOS device. 请注意,如果外围设备的“字节序”与您的iOS设备不同,则可能会以二进制数据形式发送int。 you might want to convert the timestamp integer to a string and send that in order to avoid the endian-ness problem. 您可能希望将时间戳整数转换为字符串,然后发送该字符串以避免字节序问题。

There's no need to use NSDateFormatter , unless you're planning on sending a string representation to the peripheral. 除非您打算将字符串表示形式发送到外围设备,否则无需使用NSDateFormatter

From the Apple Developer docs : 来自Apple Developer文档

NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. NSDate对象封装单个时间点,与任何特定的日历系统或时区无关。 Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001). 日期对象是不可变的,表示相对于绝对参考日期(2001年1月1日UTC:00:00:00)的不变时间间隔。

With that in mind, you can use measureTime as-is, and get the same result: 考虑到这一点,您可以measureTime原样使用measureTime ,并获得相同的结果:

uint32_t timestamp = [measureTime timeIntervalSince1970];

Without knowing specifics about the peripheral, it's impossible to say why it's showing a 24-hour value. 在不了解外围设备细节的情况下,无法说出为什么它显示24小时价值。

If I were to hazard a guess, I would expect there to be another characteristic / value you need to modify first, in order to switch it to a different format (if that's even possible). 如果要冒个险,我希望您需要首先修改另一个特征/值,以便将其切换为其他格式(如果可能的话)。

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

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