objective c - Getting screwy results from NSDateComponentsFormatter -
i learned new-to-ios8 class nsdatecomponentsformatter. lets generate time strings time-spans rather dates. pretty cool. decided tinker it, , wrote test code creates various time-spans , logs output. here's code (english , 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]; (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. --------------"); (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 months value since it's fixed.)
here's output:
for 01 years 00 days, date components string = '1 year, 3 months' 02 years 05 days, date components string = '2 years, 3 months, 5 days' 03 years 01 days, date components string = '3 years, 3 months' 04 years 06 days, date components string = '4 years, 3 months, 6 days' 05 years 07 days, date components string = '5 years, 3 months, 7 days' 06 years 02 days, date components string = '6 years, 3 months, 1 day' 07 years 04 days, date components string = '7 years, 3 months, 4 days' 08 years 02 days, date components string = '8 years, 3 months, 2 days' 09 years 08 days, date components string = '9 years, 3 months, 8 days' 10 years 03 days, date components string = '10 years, 3 months, 2 days' --------------- french. -------------- 01 years 02 days, date components string = '1 année, 3 mois et 2 jours' 02 years 02 days, date components string = '2 années, 3 mois et 2 jours' 03 years 05 days, date components string = '3 années, 3 mois et 5 jours' 04 years 03 days, date components string = '4 années, 3 mois et 3 jours' 05 years 04 days, date components string = '5 années, 3 mois et 4 jours' 06 years 04 days, date components string = '6 années, 3 mois et 4 jours' 07 years 04 days, date components string = '7 années, 3 mois et 3 jours' 08 years 08 days, date components string = '8 années, 3 mois et 8 jours' 09 years 05 days, date components string = '9 années, 3 mois et 5 jours' 10 years 02 days, date components string = '10 années, 3 mois et 2 jours'
note in several cases output days value in output of formatter not match days value fed it.
does know what's going wrong here? looks awful lot os/framework bug me.
edit:
i ran test code in mac app, under os x version 10.10.5 else running 10.10.5 , can try it?
i got 1 wrong answer against 8.1 sim. (for input value 07 years, 3 months, 06 days, date components string = '7 years, 3 months, 5 days')
it appears ios 8.x , mac os 10.10 version of nsdatecomponentsformatter
buggy. (intermittent!) different, wrong results dates in os versions, correct results latest os versions.
sigh...
Comments
Post a Comment