Switch the default date formatter to ISO-8601. closes #1069, refs #1010

This commit is contained in:
Blake Watters
2012-12-11 21:51:53 -05:00
parent ae6b58d7bc
commit 138bbddfcd
3 changed files with 14 additions and 13 deletions

View File

@@ -375,7 +375,9 @@
/**
Returns the preferred date formatter to use when generating NSString representations from NSDate attributes. This type of transformation occurs when RestKit is mapping local objects into JSON or form encoded serializations that do not have a native time construct.
Defaults to a date formatter configured for the UTC Time Zone with a format string of "yyyy-MM-dd HH:mm:ss Z"
Defaults to an instance of the `RKISO8601DateFormatter` configured with the UTC time-zone. The format string is equal to "YYYY-MM-DDThh:mm:ssTZD"
For details about the ISO-8601 format, see http://www.w3.org/TR/NOTE-datetime
@return The preferred NSFormatter object to use when serializing dates into strings
*/

View File

@@ -383,7 +383,7 @@ static RKSourceToDesinationKeyTransformationBlock defaultSourceToDestinationKeyT
/////////////////////////////////////////////////////////////////////////////
static NSMutableArray *defaultDateFormatters = nil;
static NSDateFormatter *preferredDateFormatter = nil;
static NSFormatter *preferredDateFormatter = nil;
@implementation RKObjectMapping (DateAndTimeFormatting)
@@ -440,11 +440,10 @@ static NSDateFormatter *preferredDateFormatter = nil;
+ (NSFormatter *)preferredDateFormatter
{
if (!preferredDateFormatter) {
// A date formatter that matches the output of [NSDate description]
preferredDateFormatter = [NSDateFormatter new];
[preferredDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
preferredDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
preferredDateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
RKISO8601DateFormatter *iso8601Formatter = [[RKISO8601DateFormatter alloc] init];
iso8601Formatter.defaultTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
iso8601Formatter.includeTime = YES;
preferredDateFormatter = iso8601Formatter;
}
return preferredDateFormatter;

View File

@@ -79,7 +79,7 @@
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping objectClass:[NSDictionary class] rootKeyPath:nil];
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:object requestDescriptor:requestDescriptor error:&error];
expect(error).to.beNil();
expect(parameters[@"date-form-name"]).to.equal(@"1970-01-01 00:00:00 +0000");
expect(parameters[@"date-form-name"]).to.equal(@"1970-01-01T00:00:00Z");
}
- (void)testShouldSerializeADateToAStringUsingThePreferredDateFormatter
@@ -118,7 +118,7 @@
NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
expect(error).to.beNil();
expect(string).to.equal(@"{\"key1-form-name\":\"value1\",\"date-form-name\":\"1970-01-01 00:00:00 +0000\"}");
expect(string).to.equal(@"{\"key1-form-name\":\"value1\",\"date-form-name\":\"1970-01-01T00:00:00Z\"}");
}
- (void)testShouldSerializeNSDecimalNumberAttributesToJSON
@@ -237,9 +237,9 @@
// Encodes differently on iOS / OS X
#if TARGET_OS_IPHONE
expect(string).to.equal(@"{\"stringTest\":\"The string\",\"hasOne\":{\"date\":\"1970-01-01 00:00:00 +0000\"}}");
expect(string).to.equal(@"{\"stringTest\":\"The string\",\"hasOne\":{\"date\":\"1970-01-01T00:00:00Z\"}}");
#else
expect(string).to.equal(@"{\"hasOne\":{\"date\":\"1970-01-01 00:00:00 +0000\"},\"stringTest\":\"The string\"}");
expect(string).to.equal(@"{\"hasOne\":{\"date\":\"1970-01-01T00:00:00Z\"},\"stringTest\":\"The string\"}");
#endif
}
@@ -280,7 +280,7 @@
NSData *data = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];
NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
expect(string).to.equal(@"{\"hasMany\":[{\"date\":\"1970-01-01 00:00:00 +0000\"}],\"stringTest\":\"The string\"}");
expect(string).to.equal(@"{\"hasMany\":[{\"date\":\"1970-01-01T00:00:00Z\"}],\"stringTest\":\"The string\"}");
}
- (void)testShouldSerializeAnNSNumberContainingABooleanToTrueFalseIfRequested
@@ -542,7 +542,7 @@ typedef enum {
// Test generation of Flight Number parameters
parameters = [RKObjectParameterization parametersWithObject:flightSearch requestDescriptor:requestDescriptor error:&error];
NSDictionary *expectedParameters = @{ @"flight_search": @{ @"departure_date": @"1970-01-01 00:00:00 +0000" }};
NSDictionary *expectedParameters = @{ @"flight_search": @{ @"departure_date": @"1970-01-01T00:00:00Z" }};
expect(parameters).to.equal(expectedParameters);
}