mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-23 12:27:52 +08:00
RKObjectMappingOperation#parseDateFromString now transforms string representations of integers into dates as if they were unix timestamps
This commit is contained in:
@@ -113,14 +113,27 @@ BOOL RKObjectIsValueEqualToValue(id sourceValue, id destinationValue) {
|
||||
- (NSDate*)parseDateFromString:(NSString*)string {
|
||||
RKLogTrace(@"Transforming string value '%@' to NSDate...", string);
|
||||
|
||||
NSDate* date = nil;
|
||||
for (NSDateFormatter *dateFormatter in self.objectMapping.dateFormatters) {
|
||||
@synchronized(dateFormatter) {
|
||||
NSDate* date = nil;
|
||||
|
||||
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
|
||||
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
|
||||
|
||||
NSNumber *numeric = [numberFormatter numberFromString:string];
|
||||
|
||||
[numberFormatter release];
|
||||
|
||||
if (numeric) {
|
||||
date = [NSDate dateWithTimeIntervalSince1970:[numeric doubleValue]];
|
||||
} else {
|
||||
for (NSDateFormatter *dateFormatter in self.objectMapping.dateFormatters) {
|
||||
@synchronized(dateFormatter) {
|
||||
date = [dateFormatter dateFromString:string];
|
||||
}
|
||||
|
||||
if (date) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (date) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return date;
|
||||
|
||||
@@ -222,6 +222,20 @@
|
||||
[operation release];
|
||||
}
|
||||
|
||||
- (void)testShouldMapAUnixTimestampStringAppropriately {
|
||||
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[TestMappable class]];
|
||||
[mapping mapAttributes:@"date", nil];
|
||||
TestMappable* object = [[[TestMappable alloc] init] autorelease];
|
||||
NSDictionary* dictionary = [NSDictionary dictionaryWithObject:@"457574400" forKey:@"date"];
|
||||
RKObjectMappingOperation* operation = [[RKObjectMappingOperation alloc] initWithSourceObject:dictionary destinationObject:object mapping:mapping];
|
||||
NSError* error = nil;
|
||||
BOOL success = [operation performMapping:&error];
|
||||
assertThatBool(success, is(equalToBool(YES)));
|
||||
assertThat(object.date, isNot(nilValue()));
|
||||
assertThat([object.date description], is(equalTo(@"1984-07-02 00:00:00 +0000")));
|
||||
[operation release];
|
||||
}
|
||||
|
||||
- (void)testShouldMapASimpleDateStringAppropriately {
|
||||
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[TestMappable class]];
|
||||
[mapping mapAttributes:@"date", nil];
|
||||
|
||||
Reference in New Issue
Block a user