我相信NSDate / NSCalendar在iOS 4.2上有一两个bug。
[[NSDate date] description]
始终使用GMT打印,即忽略本地时区设置。之前已有报道,但包括在内有希望更好地解释下面的输出。
[calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:dateA]
并不总是返回正确的值,而这样做:
[calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:dateA]
根据之前的帖子,我创建了一些测试代码:
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate* dateA = [formatter dateFromString:@"2010-12-29 11:11:17 +1100"];
NSDate* dateB = [formatter dateFromString:@"2010-12-29 10:11:17 +1100"];
NSDate* dateC = [formatter dateFromString:@"2010-12-27 11:11:17 +1100"];
NSDate* dateD = [formatter dateFromString:@"2010-12-27 10:11:17 +1100"];
NSLog(@"dateA=%@, %@", dateA, [formatter stringFromDate:dateA]);
NSLog(@"dateB=%@, %@", dateB, [formatter stringFromDate:dateB]);
NSLog(@"dateC=%@, %@", dateC, [formatter stringFromDate:dateC]);
NSLog(@"dateD=%@, %@", dateD, [formatter stringFromDate:dateD]);
NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSLog(@"timezone=%@", [calendar timeZone]);
NSLog(@"A day inUnit:Year=%d", [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:dateA]);
NSLog(@"B day inUnit:Year=%d", [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:dateB]);
NSLog(@"C day inUnit:Year=%d", [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:dateC]);
NSLog(@"D day inUnit:Year=%d", [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:dateD]);
NSLog(@"A day inUnit:Era =%d", [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:dateA]);
NSLog(@"B day inUnit:Era =%d", [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:dateB]);
NSLog(@"C day inUnit:Era =%d", [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:dateC]);
NSLog(@"D day inUnit:Era =%d", [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:dateD]);
得到了这个输出:
[33920:207] dateA=2010-12-29 00:11:17 +0000, 2010-12-29 11:11:17 +1100
[33920:207] dateB=2010-12-28 23:11:17 +0000, 2010-12-29 10:11:17 +1100
[33920:207] dateC=2010-12-27 00:11:17 +0000, 2010-12-27 11:11:17 +1100
[33920:207] dateD=2010-12-26 23:11:17 +0000, 2010-12-27 10:11:17 +1100
[33920:207] timezone=Australia/Hobart (GMT+11:00) offset 39600 (Daylight)
[33920:207] A day inUnit:Year=363
[33920:207] B day inUnit:Year=363
[33920:207] C day inUnit:Year=361
[33920:207] D day inUnit:Year=361
[33920:207] A day inUnit:Era =734135
[33920:207] B day inUnit:Era =734134
[33920:207] C day inUnit:Era =734133
[33920:207] D day inUnit:Era =734132
答案 0 :(得分:1)
我发现通过将日期偏移时区偏移,我得到了一致的常规。
从代码中获取dateC和dateD:
NSTimeZone *tz = [[NSCalendar autoupdatingCurrentCalendar] timeZone];
NSTimeInterval offset = [tz secondsFromGMT];
NSDate* dateC = [formatter dateFromString:@"2010-12-27 11:11:17 +1100"];
NSDate* dateD = [formatter dateFromString:@"2010-12-27 10:11:17 +1100"];
NSLog(@"dateC=%@, %@", dateC, [formatter stringFromDate:dateC]);
NSLog(@"dateD=%@, %@", dateD, [formatter stringFromDate:dateD]);
dateC = [dateC dateByAddingTimeInterval: offset];
dateD = [dateD dateByAddingTimeInterval: offset];
NSLog(@"C day inUnit:Era =%d", [calendar ordinalityOfUnit:NSDayCalendarUnitinUnit:NSEraCalendarUnit forDate: dateC]);
NSLog(@"D day inUnit:Era =%d", [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate: dateD]);
为两者产生相同的正常性。
答案 1 :(得分:0)
我看到了同样的问题。看起来不像the ordinalityOfUnit尊重时区