4

在将日期从客户端传递到服务器并再次返回时,我使用@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"带有@"en_US_POSIX"语言环境的格式将日期格式化为 JSON 的字符串。NSDate相同的格式化程序提供了从服务器返回到客户端的日期字符串的实例。

为了测试转换,我尝试使用NSDateComponentsNSCalendar生成一个独立的日期,用于验证格式化程序中的日期。但是,从和NSDate创建的实例与. 我不懂为什么。不同的日期会产生不同数量的差异。我为另一个/问题道歉,但希望你觉得它有点新奇。NSCalendarNSDateComponentsNSDateNSDateFormatterNSDateNSDateFormatter

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
dateFormatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSMutableArray *valuesWithError = [NSMutableArray arrayWithCapacity:750];
NSMutableArray *valuesThatMatch = [NSMutableArray arrayWithCapacity:250];

for (NSInteger milliseconds = 0; milliseconds < 1000; milliseconds++) {
    NSString *dateString = [NSString stringWithFormat:@"2001-01-01T00:00:00.%03dZ", milliseconds];
    NSLog(@"%@", dateString);

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    calendar.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

    NSDateComponents *dc = [[NSDateComponents alloc] init];
    dc.year = 2001;
    dc.month = 1;
    dc.day = 1;
    dc.hour = 0;
    dc.minute = 0;
    dc.second = 0;

    NSDate *expectedDate = [calendar dateFromComponents:dc];

    NSTimeInterval baseInterval = [expectedDate timeIntervalSinceReferenceDate];
    double mulitplier = 0.001;
    NSTimeInterval millisecondsToAdd = milliseconds * mulitplier;
    baseInterval += millisecondsToAdd;

    expectedDate = [NSDate dateWithTimeIntervalSinceReferenceDate:baseInterval];

    NSDate *dateFromFormat = [dateFormatter dateFromString:dateString];

    NSTimeInterval expectedDateFromReferenceDate = [expectedDate timeIntervalSinceReferenceDate];
    NSTimeInterval dateFromFormatFromReferenceDate = [dateFromFormat timeIntervalSinceReferenceDate];

    NSTimeInterval differenceByTimeIntervalSubtraction = ABS(expectedDateFromReferenceDate - dateFromFormatFromReferenceDate);

    NSTimeInterval differenceByTimeIntervalFromDate = [expectedDate timeIntervalSinceDate:dateFromFormat];

    if (![expectedDate isEqualToDate:dateFromFormat]) {
        NSLog(@"Difference = %e", differenceByTimeIntervalSubtraction);
        [valuesWithError addObject:@(milliseconds)];
    } else {
        [valuesThatMatch addObject:@(milliseconds)];
    }
}
4

0 回答 0