gpt4 book ai didi

objective-c - 处理时间戳时丢失 double

转载 作者:可可西里 更新时间:2023-10-31 23:44:32 29 4
gpt4 key购买 nike

我有一个简单的XCTestCase:

func testExample() {
let date = "2015-09-21T20:38:54.379912Z";// as NSString;
let date1 = 1442867934.379912;

XCTAssertEqual(date1, NSDate.sam_dateFromISO8601String(date).timeIntervalSince1970);
}

这个测试在不应该通过的时候通过了,原因有二:

  • 1442867934.379912 在测试中变成了 1442867934.379911,尽管它实际上只是一个重新打印的变量
  • sam_ 函数(在下面复制)似乎以同样的方式思考,millisecond 变量变为 millisecond double 0.37991200000000003 0.37991200000000003 稍后转换从 NSDatedouble,似乎失去了微秒精度(调试器):po NSDate.sam_dateFromISO8601String(date).timeIntervalSince1970 -> 1442867934.37991

po 1442867934.379912 -> 1442867934.37991

知道为什么吗?微秒精度(小数点后 6 位)对我的应用程序非常重要,我需要使用 iso8601NSStringNSDate 之间无缝转换格式。


+ (NSDate *)sam_dateFromISO8601String:(NSString *)iso8601 {
// Return nil if nil is given
if (!iso8601 || [iso8601 isEqual:[NSNull null]]) {
return nil;
}

// Parse number
if ([iso8601 isKindOfClass:[NSNumber class]]) {
return [NSDate dateWithTimeIntervalSince1970:[(NSNumber *)iso8601 doubleValue]];
}

// Parse string
else if ([iso8601 isKindOfClass:[NSString class]]) {
const char *str = [iso8601 cStringUsingEncoding:NSUTF8StringEncoding];
size_t len = strlen(str);
if (len == 0) {
return nil;
}

struct tm tm;
char newStr[25] = "";
BOOL hasTimezone = NO;

// 2014-03-30T09:13:00Z
if (len == 20 && str[len - 1] == 'Z') {
strncpy(newStr, str, len - 1);
}

// 2014-03-30T09:13:00-07:00
else if (len == 25 && str[22] == ':') {
strncpy(newStr, str, 19);
hasTimezone = YES;
}

// 2014-03-30T09:13:00.000Z
else if (len == 24 && str[len - 1] == 'Z') {
strncpy(newStr, str, 19);
}
// 2014-03-30T09:13:00.000000Z
else if (len == 27 && str[len - 1] == 'Z') {
strncpy(newStr, str, 19);
}


// 2014-03-30T09:13:00.000-07:00
else if (len == 29 && str[26] == ':') {
strncpy(newStr, str, 19);
hasTimezone = YES;
}

// Poorly formatted timezone
else {
strncpy(newStr, str, len > 24 ? 24 : len);
}

// Timezone
size_t l = strlen(newStr);
if (hasTimezone) {
strncpy(newStr + l, str + len - 6, 3);
strncpy(newStr + l + 3, str + len - 2, 2);
} else {
strncpy(newStr + l, "+0000", 5);
}

// Add null terminator
newStr[sizeof(newStr) - 1] = 0;

if (strptime(newStr, "%FT%T%z", &tm) == NULL) {
return nil;
}

double millisecond = 0.0f;

NSString *subStr = [[iso8601 componentsSeparatedByString:@"."].lastObject substringToIndex:6];

millisecond = subStr.doubleValue/1000000.f;

time_t t;
t = mktime(&tm);

return [NSDate dateWithTimeIntervalSince1970:t + millisecond];
}

NSAssert1(NO, @"Failed to parse date: %@", iso8601);
return nil;
}

最佳答案

1442867934.3799121442867934.379911 很可能等于相同的数字,只是打印方式不同(一个四舍五入,另一个截断)...如果微秒时间对您很重要,也许你应该看看https://developer.apple.com/library/mac/qa/qa1398/_index.html

特别是因为 [NSDate date] 可以在时钟变化时发生剧烈变化......而绝对时间 api 不会......

关于objective-c - 处理时间戳时丢失 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706327/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com