Skip to content

Commit 138bbdd

Browse files
committed
Switch the default date formatter to ISO-8601. closes RestKit#1069, refs RestKit#1010
1 parent ae6b58d commit 138bbdd

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

Code/ObjectMapping/RKObjectMapping.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,9 @@
375375
/**
376376
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.
377377
378-
Defaults to a date formatter configured for the UTC Time Zone with a format string of "yyyy-MM-dd HH:mm:ss Z"
378+
Defaults to an instance of the `RKISO8601DateFormatter` configured with the UTC time-zone. The format string is equal to "YYYY-MM-DDThh:mm:ssTZD"
379+
380+
For details about the ISO-8601 format, see http://www.w3.org/TR/NOTE-datetime
379381
380382
@return The preferred NSFormatter object to use when serializing dates into strings
381383
*/

Code/ObjectMapping/RKObjectMapping.m

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ - (BOOL)isEqualToMapping:(RKObjectMapping *)otherMapping
383383
/////////////////////////////////////////////////////////////////////////////
384384

385385
static NSMutableArray *defaultDateFormatters = nil;
386-
static NSDateFormatter *preferredDateFormatter = nil;
386+
static NSFormatter *preferredDateFormatter = nil;
387387

388388
@implementation RKObjectMapping (DateAndTimeFormatting)
389389

@@ -440,11 +440,10 @@ + (void)addDefaultDateFormatterForString:(NSString *)dateFormatString inTimeZone
440440
+ (NSFormatter *)preferredDateFormatter
441441
{
442442
if (!preferredDateFormatter) {
443-
// A date formatter that matches the output of [NSDate description]
444-
preferredDateFormatter = [NSDateFormatter new];
445-
[preferredDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
446-
preferredDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
447-
preferredDateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
443+
RKISO8601DateFormatter *iso8601Formatter = [[RKISO8601DateFormatter alloc] init];
444+
iso8601Formatter.defaultTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
445+
iso8601Formatter.includeTime = YES;
446+
preferredDateFormatter = iso8601Formatter;
448447
}
449448

450449
return preferredDateFormatter;

Tests/Logic/ObjectMapping/RKObjectParameterizationTest.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ - (void)testShouldSerializeADate
7979
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping objectClass:[NSDictionary class] rootKeyPath:nil];
8080
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:object requestDescriptor:requestDescriptor error:&error];
8181
expect(error).to.beNil();
82-
expect(parameters[@"date-form-name"]).to.equal(@"1970-01-01 00:00:00 +0000");
82+
expect(parameters[@"date-form-name"]).to.equal(@"1970-01-01T00:00:00Z");
8383
}
8484

8585
- (void)testShouldSerializeADateToAStringUsingThePreferredDateFormatter
@@ -118,7 +118,7 @@ - (void)testShouldSerializeADateToJSON
118118
NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
119119

120120
expect(error).to.beNil();
121-
expect(string).to.equal(@"{\"key1-form-name\":\"value1\",\"date-form-name\":\"1970-01-01 00:00:00 +0000\"}");
121+
expect(string).to.equal(@"{\"key1-form-name\":\"value1\",\"date-form-name\":\"1970-01-01T00:00:00Z\"}");
122122
}
123123

124124
- (void)testShouldSerializeNSDecimalNumberAttributesToJSON
@@ -237,9 +237,9 @@ - (void)testShouldSerializeNestedObjectsContainingDatesToJSON
237237

238238
// Encodes differently on iOS / OS X
239239
#if TARGET_OS_IPHONE
240-
expect(string).to.equal(@"{\"stringTest\":\"The string\",\"hasOne\":{\"date\":\"1970-01-01 00:00:00 +0000\"}}");
240+
expect(string).to.equal(@"{\"stringTest\":\"The string\",\"hasOne\":{\"date\":\"1970-01-01T00:00:00Z\"}}");
241241
#else
242-
expect(string).to.equal(@"{\"hasOne\":{\"date\":\"1970-01-01 00:00:00 +0000\"},\"stringTest\":\"The string\"}");
242+
expect(string).to.equal(@"{\"hasOne\":{\"date\":\"1970-01-01T00:00:00Z\"},\"stringTest\":\"The string\"}");
243243
#endif
244244
}
245245

@@ -280,7 +280,7 @@ - (void)testShouldSerializeToManyRelationships
280280

281281
NSData *data = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];
282282
NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
283-
expect(string).to.equal(@"{\"hasMany\":[{\"date\":\"1970-01-01 00:00:00 +0000\"}],\"stringTest\":\"The string\"}");
283+
expect(string).to.equal(@"{\"hasMany\":[{\"date\":\"1970-01-01T00:00:00Z\"}],\"stringTest\":\"The string\"}");
284284
}
285285

286286
- (void)testShouldSerializeAnNSNumberContainingABooleanToTrueFalseIfRequested
@@ -542,7 +542,7 @@ - (void)testDynamicParameterizationIncludingADate
542542

543543
// Test generation of Flight Number parameters
544544
parameters = [RKObjectParameterization parametersWithObject:flightSearch requestDescriptor:requestDescriptor error:&error];
545-
NSDictionary *expectedParameters = @{ @"flight_search": @{ @"departure_date": @"1970-01-01 00:00:00 +0000" }};
545+
NSDictionary *expectedParameters = @{ @"flight_search": @{ @"departure_date": @"1970-01-01T00:00:00Z" }};
546546
expect(parameters).to.equal(expectedParameters);
547547
}
548548

0 commit comments

Comments
 (0)