从 NSDateComponentsFormatter 获得诡异的结果 [英] Getting screwy results from NSDateComponentsFormatter
问题描述
我最近了解了 iOS8 的新类 NSDateComponentsFormatter.它允许您为时间跨度而不是日期生成时间字符串.很酷.我决定修改它,并编写了一些测试代码来创建各种时间跨度并记录输出.这是我的代码(英文和法文输出)
I recently learned about the new-to-iOS8 class NSDateComponentsFormatter. It lets you generate time strings for time-spans rather than dates. Pretty cool. I decided to tinker with it, and wrote some test code that creates various time-spans and logs the output. Here's my code (English and French output)
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);
}
(我不记录月份值,因为它是固定的.)
(I don't log the months value since it's fixed.)
输出如下:
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'
请注意,在某些情况下,格式化程序输出中的输出天数值与输入其中的天数值不匹配.
Note that in several cases the output days value in the output of the formatter does not match the days value fed into it.
有人知道这里出了什么问题吗?对我来说,这看起来很像操作系统/框架错误.
Does anybody know what's going wrong here? This looks an awful lot like an OS/framework bug to me.
我在 OS X 版本 10.10.5 下在 Mac 应用程序中运行我的测试代码 还有其他人在运行 10.10.5 吗?他们可以试试吗?
I ran my test code in a Mac app, under OS X version 10.10.5 Is anybody else running 10.10.5 and can they try it?
我对 8.1 sim 也有一个错误的答案.(对于输入值 07 年、3 个月、06 天,日期组件字符串 = '7 年、3 个月、5 天')
I also got one wrong answer against the 8.1 sim. (For input value 07 years, 3 months, 06 days, Date Components string = '7 years, 3 months, 5 days')
推荐答案
NSDateComponentsFormatter
的 iOS 8.x 和 Mac OS 10.10 版本似乎有问题.我在这些操作系统版本中得到(间歇性!)不同的、错误的结果,但在最新的操作系统版本中得到了正确的结果.
It appears that the iOS 8.x and Mac OS 10.10 version of the NSDateComponentsFormatter
is buggy. I get (intermittent!) different, wrong results for some dates in those OS versions, but correct results from the latest OS versions.
叹气...
这篇关于从 NSDateComponentsFormatter 获得诡异的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!