我最近了解了新的iOS8类NSDateComponentsFormatter。它允许您为时间跨度而不是日期生成时间字符串。很酷。我决定修补它,并编写了一些测试代码,创建了各种时间跨度并记录输出。这是我的代码(英文和法文输出)
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponentsFormatter* formatter = [[NSDateComponentsFormatter alloc] init];
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
formatter.calendar = gregorian;
formatter.allowedUnits = NSCalendarUnitYear + NSCalendarUnitMonth + NSCalendarUnitDay;
NSDateComponents *components = [[NSDateComponents alloc] init];
for (int years = 1; years <= 10; years++)
{
int days = arc4random_uniform(10);
components.year = years;
components.month = 3;
components.day = days;
NSString* outputString = [formatter stringFromDateComponents: components];
NSLog(@"For %02d years %02d days, Date Components string = '%@'", years, days, outputString);
}
NSLocale *franceLocale = [NSLocale localeWithLocaleIdentifier: @"fr_FR"];
gregorian.locale = franceLocale;
formatter.calendar = gregorian;
NSLog(@"--------------- French. --------------");
for (int years = 1; years <= 10; years++)
{
int days = arc4random_uniform(10) ;
components.year = years;
components.day = days;
NSString* outputString = [formatter stringFromDateComponents: components];
NSLog(@"For %02d years %02d days, Date Components string = '%@'", years, days, outputString);
}
(我没有记录月份值,因为它已修复。)
这是输出:
For 01 years 00 days, Date Components string = '1 year, 3 months'
For 02 years 05 days, Date Components string = '2 years, 3 months, 5 days'
For 03 years 01 days, Date Components string = '3 years, 3 months'
For 04 years 06 days, Date Components string = '4 years, 3 months, 6 days'
For 05 years 07 days, Date Components string = '5 years, 3 months, 7 days'
For 06 years 02 days, Date Components string = '6 years, 3 months, 1 day'
For 07 years 04 days, Date Components string = '7 years, 3 months, 4 days'
For 08 years 02 days, Date Components string = '8 years, 3 months, 2 days'
For 09 years 08 days, Date Components string = '9 years, 3 months, 8 days'
For 10 years 03 days, Date Components string = '10 years, 3 months, 2 days'
--------------- French. --------------
For 01 years 02 days, Date Components string = '1 année, 3 mois et 2 jours'
For 02 years 02 days, Date Components string = '2 années, 3 mois et 2 jours'
For 03 years 05 days, Date Components string = '3 années, 3 mois et 5 jours'
For 04 years 03 days, Date Components string = '4 années, 3 mois et 3 jours'
For 05 years 04 days, Date Components string = '5 années, 3 mois et 4 jours'
For 06 years 04 days, Date Components string = '6 années, 3 mois et 4 jours'
For 07 years 04 days, Date Components string = '7 années, 3 mois et 3 jours'
For 08 years 08 days, Date Components string = '8 années, 3 mois et 8 jours'
For 09 years 05 days, Date Components string = '9 années, 3 mois et 5 jours'
For 10 years 02 days, Date Components string = '10 années, 3 mois et 2 jours'
请注意,在某些情况下,格式化程序输出中的输出天数值与输入的天数值不匹配。
有人知道这里出了什么问题吗?对我来说,这看起来很像OS /框架错误。
我在Mac应用程序中运行我的测试代码,在OS X版本10.10.5下是否有其他人运行10.10.5并且可以尝试吗?
对于8.1 sim,我也得到了一个错误的答案。 (输入值为07年,3个月,06天,日期组件字符串=&#39; 7年,3个月,5天&#39;)
答案 0 :(得分:0)
似乎NSDateComponentsFormatter
的iOS 8.x和Mac OS 10.10版本有问题。对于那些操作系统版本中的某些日期,我得到(间歇!)不同的错误结果,但是最新操作系统版本的结果是正确的。
唉...