从 NSDateComponentsFormatter 获取古怪的结果

Getting screwy results from NSDateComponentsFormatter

我最近了解了 iOS8 class 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/framework 错误。

编辑:

我 运行 我在 Mac 应用程序中的测试代码,在 OS X 版本 10.10.5 下还有其他人 运行 10.10.5 他们可以试试吗?

我在 8.1 sim 上也得到了一个错误的答案。 (对于输入值 07 年 3 月 06 日,Date Components string = '7 years, 3 months, 5 days')

iOS 8.x 和 Mac OS 10.10 版本的 NSDateComponentsFormatter 似乎有问题。对于那些 OS 版本中的某些日期,我得到(间歇性的!)不同的错误结果,但最新的 OS 版本的结果是正确的。

叹气...