Getting screwy results from NSDateComponentsFormatter

Duncan C

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')

Duncan C

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...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting results from a database

From Dev

Getting results from thread

From Dev

Getting Success Results from $.post

From Dev

Getting readable results from Wikidata

From Dev

Not getting expected results from for loop

From Dev

Not getting results from SpeechRecognizer class?

From Dev

Getting no results from simple Query

From Dev

Not getting the expected results from for loop

From Dev

Getting attendee from Exchange's GetUserAvailability() results

From Dev

Getting list of results from etags.el

From Dev

Python getting meaningful results from cProfile

From Dev

getting 17 low results from 70 in php

From Dev

Getting test results from nosetests in Python module

From Dev

Getting all search results from Github API

From Dev

Getting specific results from multiple tables in mysql

From Dev

Not getting any results back from this xslt

From Dev

Getting the results of a Haskell script from Java

From Dev

Getting results from multiprocessing.process for a loop

From Dev

Getting Task results from synchronous execution

From Dev

Getting typed results from ActiveRecord raw SQL

From Dev

Not getting expected results from pyparsing on a particular grammar

From Dev

Reflection - Getting different results from HashMap - LinkedHashMap

From Dev

Getting all results from Func call

From Dev

Getting results from multiple types of data

From Dev

Getting multiple results from WHERE clause in PHP

From Dev

Getting results from multiple types of data

From Dev

Getting Results from External Intent on Android

From Dev

Getting results from query with alchimia library

From Dev

Getting results from each method within a delegate

Related Related

HotTag

Archive