我有一个简单的:

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

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

由于两个原因,本测试在不应通过时通过:
在测试中,XCTestCase变为1442867934.379912
1442867934.379911函数(复制在下面)似乎也是这样认为的,sam_变量变为millisecond当稍后从millisecond double 0.37991200000000003 0.37991200000000003转换为NSDate时,似乎会失去微秒精度(调试器):
double
po NSDate.sam_dateFromISO8601String(date).timeIntervalSince1970 -> 1442867934.37991
知道为什么吗?微秒精度(小数点后6位)对我的应用程序非常重要,我需要使用po 1442867934.379912 -> 1442867934.37991格式无缝地转换为和从NSStringNSDate转换。
+ (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]会发生剧烈的变化…绝对时间API不会…

08-05 22:01