SOK
SOK

Reputation: 595

RestKit with NSDateFormatter

this is my first time using RestKit, and I've trouble with the date formatting. I'm giving it a date like: 2014-07-18T09:00:54

I've have created a NSDateFormatter and tried testing it by using NSLog:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSLog(@"%@",     [dateFormatter dateFromString:@"2014-07-18T09:00:54"]);

It then prints out the following answer which is correct: 2014-07-18 09:00:54 +0000. I've looked at https://github.com/RestKit/RestKit/wiki/Object-Mapping#transforming-date-and-time-representations, and tried to add the formatter like this with no lock.

[[RKValueTransformer defaultValueTransformer] insertValueTransformer:dateFormatter atIndex:0];

It prints out: 2014-07-18 11:00:54 CEST, so i guess it uses a default formatter. Can any explain the problem for me?

Upvotes: 1

Views: 555

Answers (1)

SOK
SOK

Reputation: 595

The following code solved my

/
*
* Before we set up mappings, add a String <--> Date transformer that interprets string dates
*  lacking timezone info to be in the user's local time zone
*/
[RKObjectMapping class];    // Message the RKObjectMapping class (+ subclasses) so +initialize is
[RKEntityMapping class];    //  called to work around RK bug, see GH issue #1631
NSDateFormatter *localOffsetDateFormatter = [[NSDateFormatter alloc] init];
[localOffsetDateFormatter setLocale:[NSLocale currentLocale]];
[localOffsetDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[localOffsetDateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[[RKValueTransformer defaultValueTransformer] insertValueTransformer:localOffsetDateFormatter atIndex:0];problem.

Can also be found on: www.github.com/RestKit/RestKit/issues/1715

Upvotes: 2

Related Questions