问题
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.)
Here's the output:
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.
EDIT:
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?
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')
回答1:
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.
Sigh...
来源:https://stackoverflow.com/questions/35676641/getting-screwy-results-from-nsdatecomponentsformatter