本文介绍了iOS 4.2 上的 NSDate 等问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信 NSDate/NSCalendar 在 iOS 4.2 上有一两个错误.

I believe that NSDate/NSCalendar has a bug or two on iOS 4.2.

[[NSDate date] description]

总是使用 GMT 打印,即忽略本地时区设置.之前已经报道过,但包括在内是为了更好地解释下面的输出.

always prints using GMT, i.e. the local time zone setting is ignored. Has been reported previously, but is included to hopefully better explain the output below.

[calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:dateA]

并不总是返回正确的值,而这样做:

Doesn't always return the correct value, while this does:

[calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:dateA]

基于之前的帖子,我创建了一些测试代码:

Based on a previous post, I created some test code:

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

推荐答案

我发现通过将日期偏移时区偏移,我得到了一致的序数.

I found that by offsetting the date by the time zone offset I get a consistent ordinality.

从您的代码中获取 dateC 和 dateD:

Taking dateC and dateD from your code:

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]);

对两者产生相同的序数.

yields the same ordinality for both.

这篇关于iOS 4.2 上的 NSDate 等问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:37