NSDateFormatter fails for some input strings

Originator:drodrigueztroitino
Number:rdar://13024643 Date Originated:16-Jan-2013 06:10 PM
Status:Duplicate/#11357077 Resolved:
Product:Mac OS X Product Version:10.7.5
Classification:Serious bug Reproducible:Always
 
NSDateFormatter fails to provide a non-nil result parsing some strings with more that three fraction digits.

Steps to Reproduce:
1. Setup a NSDateFormatter with a dateFormat that has more than three (3) ā€œSā€ symbols.
2. Parse a string with a fractional part like ".9994".
3. Parse a string with a fractional part like ".9995".

Expected Results:
Both 2 and 3 should result in a valid NSDate (even if we lose precision).

Actual Results:
Case 2 works without problem, while case 3 returns nil.

Regression:
I have been able to reproduce this in both 10.7 and 10.8 machines, and also in iOS 5.0 and iOS 6.0 devices.

Notes:
I attach a little test program that shows the problem. The program does the parsing of 10 test strings for a formatter that works as expected and other 10 test strings with a formatter that fails for the last 5 strings.

Attachments:

--- test-date-formatter.m ---
// clang -Wall -framework Foundation -o test-date-formatter test-date-formatter.m
#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
  @autoreleasepool {
    NSString *problem0 = @"2013-01-15T17:34:26.9990Z";
    NSString *problem1 = @"2013-01-15T17:34:26.9991Z";
    NSString *problem2 = @"2013-01-15T17:34:26.9992Z";
    NSString *problem3 = @"2013-01-15T17:34:26.9993Z";
    NSString *problem4 = @"2013-01-15T17:34:26.9994Z";
    NSString *problem5 = @"2013-01-15T17:34:26.9995Z";
    NSString *problem6 = @"2013-01-15T17:34:26.9996Z";
    NSString *problem7 = @"2013-01-15T17:34:26.9997Z";
    NSString *problem8 = @"2013-01-15T17:34:26.9998Z";
    NSString *problem9 = @"2013-01-15T17:34:26.9999Z";

    NSString *noProblem0 = @"2013-01-15T17:34:26.990Z";
    NSString *noProblem1 = @"2013-01-15T17:34:26.991Z";
    NSString *noProblem2 = @"2013-01-15T17:34:26.992Z";
    NSString *noProblem3 = @"2013-01-15T17:34:26.993Z";
    NSString *noProblem4 = @"2013-01-15T17:34:26.994Z";
    NSString *noProblem5 = @"2013-01-15T17:34:26.995Z";
    NSString *noProblem6 = @"2013-01-15T17:34:26.996Z";
    NSString *noProblem7 = @"2013-01-15T17:34:26.997Z";
    NSString *noProblem8 = @"2013-01-15T17:34:26.998Z";
    NSString *noProblem9 = @"2013-01-15T17:34:26.999Z";

    NSDateFormatter *problemFormatter = [[NSDateFormatter alloc] init];
    problemFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSS'Z";

    NSDateFormatter *noProblemFormatter = [[NSDateFormatter alloc] init];
    noProblemFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z";

    NSLog(@"%@ -> %@", problem0, [problemFormatter dateFromString:problem0]);
    NSLog(@"%@ -> %@", problem1, [problemFormatter dateFromString:problem1]);
    NSLog(@"%@ -> %@", problem2, [problemFormatter dateFromString:problem2]);
    NSLog(@"%@ -> %@", problem3, [problemFormatter dateFromString:problem3]);
    NSLog(@"%@ -> %@", problem4, [problemFormatter dateFromString:problem4]);
    NSLog(@"%@ -> %@", problem5, [problemFormatter dateFromString:problem5]);
    NSLog(@"%@ -> %@", problem6, [problemFormatter dateFromString:problem6]);
    NSLog(@"%@ -> %@", problem7, [problemFormatter dateFromString:problem7]);
    NSLog(@"%@ -> %@", problem8, [problemFormatter dateFromString:problem8]);
    NSLog(@"%@ -> %@", problem9, [problemFormatter dateFromString:problem9]);

    NSLog(@"%@ -> %@", noProblem0, [noProblemFormatter dateFromString:noProblem0]);
    NSLog(@"%@ -> %@", noProblem1, [noProblemFormatter dateFromString:noProblem1]);
    NSLog(@"%@ -> %@", noProblem2, [noProblemFormatter dateFromString:noProblem2]);
    NSLog(@"%@ -> %@", noProblem3, [noProblemFormatter dateFromString:noProblem3]);
    NSLog(@"%@ -> %@", noProblem4, [noProblemFormatter dateFromString:noProblem4]);
    NSLog(@"%@ -> %@", noProblem5, [noProblemFormatter dateFromString:noProblem5]);
    NSLog(@"%@ -> %@", noProblem6, [noProblemFormatter dateFromString:noProblem6]);
    NSLog(@"%@ -> %@", noProblem7, [noProblemFormatter dateFromString:noProblem7]);
    NSLog(@"%@ -> %@", noProblem8, [noProblemFormatter dateFromString:noProblem8]);
    NSLog(@"%@ -> %@", noProblem9, [noProblemFormatter dateFromString:noProblem9]);
  }
}
--- EOF ---

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!