dchohfi / KeyValueObjectMapping

Automatic KeyValue Object Mapping for Objective-C, parse JSON/plist/Dictionary automatically
http://dchohfi.com/
MIT License
600 stars 90 forks source link

add RFC3339 date support #2

Open PongPong opened 12 years ago

PongPong commented 12 years ago
        // Process date
        NSString *RFC3339String = [[NSString stringWithString:dateString] uppercaseString];
        RFC3339String = [RFC3339String stringByReplacingOccurrencesOfString:@"Z" withString:@"-0000"];
        // Remove colon in timezone as it breaks NSDateFormatter in iOS 4+.
        // - see https://devforums.apple.com/thread/45837
        if (RFC3339String.length > 20) {
            RFC3339String = [RFC3339String stringByReplacingOccurrencesOfString:@":" 
                                                                     withString:@"" 
                                                                        options:0
                                                                          range:NSMakeRange(20, RFC3339String.length-20)];
        }
        if (!date) { // 1996-12-19T16:39:57-0800
            [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"]; 
            date = [dateFormatter dateFromString:RFC3339String];
        }
        if (!date) { // 1937-01-01T12:00:27.87+0020
            [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZZZ"]; 
            date = [dateFormatter dateFromString:RFC3339String];
        }
        if (!date) { // 1937-01-01T12:00:27
            [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss"]; 
            date = [dateFormatter dateFromString:RFC3339String];
        }
        if (!date) NSLog(@"Could not parse RFC3339 date: \"%@\" Possible invalid format.", dateString);
dchohfi commented 12 years ago

Could you send a pull request? Adding tests might be nice :P